Академический Документы
Профессиональный Документы
Культура Документы
com
TRIGGERS
A trigger is a special type of stored procedure that automatically takes effect when
the data in a specified table is modified. Triggers are invoked in response to an
INSERT, UPDATE, or DELETE statement. A trigger can query other tables and can
include complex Transact-SQL statements. The trigger and the statement that fires it
are treated as a single transaction, which can be rolled back from within the trigger.
If a severe error is detected (for example, insufficient disk space), the entire
transaction automatically rolls back.
• Triggers can enforce restrictions that are more complex than those defined
with CHECK constraints.
Unlike CHECK constraints, triggers can reference columns in other tables. For
example, a trigger can use a SELECT from another table to compare to the
inserted or updated data and to perform additional actions, such as modify
the data or display a user-defined error message.
• Triggers can also evaluate the state of a table before and after a data
modification and take action(s) based on that difference.
Creating a Trigger:
www.gcreddy.com 1
www.gcreddy.com
You can use the FOR clause to specify when a trigger is executed:
• AFTER
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, they
can only be specified for tables. You can specify multiple AFTER triggers for
each triggering action (INSERT, UPDATE, or DELETE). If you have multiple
AFTER triggers for a table, you can use sp_settriggerorder to define which
AFTER trigger fires first and which fires last. All other AFTER triggers besides
the first and last fire in an undefined order which you cannot control.
AFTER is the default in SQL Server 2000. You could not specify AFTER or
INSTEAD OF in SQL Server version 7.0 or earlier, all triggers in those versions
operated as AFTER triggers.
• INSTEAD OF
Constraints and triggers each have benefits that make them useful in special
situations. The primary benefit of triggers is that they can contain complex
processing logic that uses Transact-SQL code. Therefore, triggers can support all of
the functionality of constraints; however, triggers are not always the best method for
a given feature.
Entity integrity should always be enforced at the lowest level by indexes that are
part of PRIMARY KEY and UNIQUE constraints or are created independently of
constraints. Domain integrity should be enforced through CHECK constraints, and
referential integrity (RI) should be enforced through FOREIGN KEY constraints,
assuming their features meet the functional needs of the application.
Triggers are most useful when the features supported by constraints cannot meet
the functional needs of the application. For example:
• FOREIGN KEY constraints can validate a column value only with an exact
match to a value in another column, unless the REFERENCES clause defines a
cascading referential action.
www.gcreddy.com 2
www.gcreddy.com
Triggers can cascade changes through related tables in the database; however,
these changes can be executed more efficiently through cascading referential
integrity constraints.
• Triggers can disallow or roll back changes that violate referential integrity,
thereby canceling the attempted data modification. Such a trigger might go
into effect when you change a foreign key and the new value does not match
its primary key.
• If constraints exist on the trigger table, they are checked after the INSTEAD
OF trigger execution but prior to the AFTER trigger execution. If the
constraints are violated, the INSTEAD OF trigger actions are rolled back and
the AFTER trigger is not executed.
Example:
While entering the details of a new employee, the salary has to be entered
according to the grade. If the grade it changed, the basic salary should also be
changed accordingly. Instead of manually doing this, the HR manager wanted the
basic salary of the employees to be entered automatically whenever an employee is
added or grade is changed.
www.gcreddy.com 3
www.gcreddy.com
An insert/update is written for the table emp. Whenever a new record is inserted or
updated, the new grade is obtained from the inserted table. The corresponding basic
salary is obtained from the table sal_info and the basic salary in the emp table is set
to this value.
This trigger can be checked by inserting a record with a null value for the field bsal.
When a select statement is given, the value for bsal will also be present.
Altering a Trigger:
The definition of an existing trigger can be altered without dropping it. The
altered definition replaces the definition of the existing trigger with the new
definition.
Syntax
www.gcreddy.com 4
www.gcreddy.com
Example:
Dropping a Trigger:
Syntax:
Syntax:
www.gcreddy.com 5