TRIGGERS AND VIEWS
- What is Trigger? What is its use? What are the types of Triggers? What are the new kinds of triggers in sql 2000?
Triggers are a special class of stored procedure defined to execute automatically when an UPDATE, INSERT, or DELETE statement is issued against a table or view. Triggers are powerful tools that sites can use to enforce their business rules automatically when data is modified.
The CREATE TRIGGER statement can be defined with the FOR UPDATE, FOR INSERT, or FOR DELETE clauses to target a trigger to a specific class of data modification actions. When FOR UPDATE is specified, the IF UPDATE (column_name) clause can be used to target a trigger to updates affecting a particular column.
You can use the FOR clause to specify when a trigger is executed:
- AFTER (default) – The trigger executes after the statement that triggered it completes. If the statement fails with an error, such as a constraint violation or syntax error, the trigger is not executed. AFTER triggers cannot be specified for views.
- INSTEAD OF -The trigger executes in place of the triggering action. INSTEAD OF triggers can be specified on both tables and views. You can define only one INSTEAD OF trigger for each triggering action (INSERT, UPDATE, and DELETE). INSTEAD OF triggers can be used to perform enhance integrity checks on the data values supplied in INSERT and UPDATE statements. INSTEAD OF triggers also let you specify actions that allow views, which would normally not support updates, to be updatable.
An INSTEAD OF trigger can take actions such as:
- Ignoring parts of a batch.
- Not processing a part of a batch and logging the problem rows.
- Taking an alternative action if an error condition is encountered.
In SQL Server 6.5 you could define only 3 triggers per table, one for INSERT, one for UPDATE and one for DELETE. From SQL Server 7.0 onwards, this restriction is gone, and you could create multiple triggers per each action. But in 7.0 there's no way to control the order in which the triggers fire. In SQL Server 2000 you could specify which trigger fires first or fires last using sp_settriggerorder.
Till SQL Server 7.0, triggers fire only after the data modification operation happens. So in a way, they are called post triggers. But in SQL Server 2000 you could create pre triggers also.
- When should one use "instead of Trigger"? Example
CREATE TABLE BaseTable
PrimaryKey int IDENTITY(1,1),
Color nvarchar(10) NOT NULL,
Material nvarchar(10) NOT NULL,
ComputedCol AS (Color + Material)
–Create a view that contains all columns from the base table.
CREATE VIEW InsteadView
AS SELECT PrimaryKey, Color, Material, ComputedCol
–Create an INSTEAD OF INSERT trigger on tthe view.
CREATE TRIGGER InsteadTrigger on InsteadView
INSTEAD OF INSERT
–Build an INSERT statement ignoring inserrted.PrimaryKey and
INSERT INTO BaseTable
SELECT Color, Material
— can insert value to basetable by this insert into basetable(color,material) values ('red','abc')
— insert into InsteadView(color,material)) values ('red','abc') can't do this.
— It will give error "'PrimaryKey' iin table 'InsteadView' cannot be null."
— can insert value through table by this<
insert into InsteadView values (1,'red','abc',1) –PrimaryKey, ComputedCol wont take values from here
- Difference between trigger and stored procedure?
Trigger will get execute automatically when an UPDATE, INSERT, or DELETE statement is issued against a table or view.
We have to call stored procedure manually, or it can execute automatic when the SQL Server starts (You can use the sp_procoption system stored procedure to mark the stored procedure to automatic execution when the SQL Server will start.
- The following trigger generates an e-mail whenever a new title is added.
CREATE TRIGGER reminder
EXEC master..xp_sendmail 'MaryM', 'New title, mention in the next report to distributors.'
- Drawback of trigger? Its alternative solution?
Triggers are generally used to implement business rules, auditing. Triggers can also be used to extend the referential integrity checks, but wherever possible, use constraints for this purpose, instead of triggers, as constraints are much faster.
- What is View? Use? Syntax of View?
A view is a virtual table made up of data from base tables and other views, but not stored separately.
- Views simplify users perception of the database (can be used to present only the necessary information while hiding details in underlying relations)
- Views improve data security preventing undesired accesses
- Views facilite the provision of additional data independence
- Does the View occupy memory space?
- Can u drop a table if it has a view?
Views or tables participating in a view created with the SCHEMABINDING clause cannot be dropped. If the view is not created using SCHEMABINDING, then we can drop the table.
- Why doesn't SQL Server permit an ORDER BY clause in the definition of a view?
SQL Server excludes an ORDER BY clause from a view to comply with the ANSI SQL-92 standard. Because analyzing the rationale for this standard requires a discussion of the underlying structure of the structured query language (SQL) and the mathematics upon which it is based, we can't fully explain the restriction here. However, if you need to be able to specify an ORDER BY clause in a view, consider using the following workaround:
CREATE VIEW AuthorsByName
SELECT TOP 100 PERCENT *
ORDER BY au_lname, au_fname
The TOP construct, which Microsoft introduced in SQL Server 7.0, is most useful when you combine it with the ORDER BY clause. The only time that SQL Server supports an ORDER BY clause in a view is when it is used in conjunction with the TOP keyword. (Note that the TOP keyword is a SQL Server extension to the ANSI SQL-92 standard.)