Triggers are stored procedures that run automatically in response to data changes like inserts, updates, and deletes. There are two types of triggers: DDL triggers respond to data definition language statements like CREATE and ALTER, while DML triggers respond to data manipulation language statements that change data. DML triggers can be FOR or AFTER triggers that fire after the data change, or INSTEAD OF triggers that fire before and allow modifying the change. Triggers enable enforcing business rules and logging actions across tables in ways that constraints cannot.
2. Triggers
Specialized stored procedure that runs ‘automatically’
Another method to enforce rules
Typically used to enforce business rules not easily done
by constraint
Often used when need to log actions
3. DDL v. DML Triggers
DDL Triggers allow procedures to run as part of
CREATE, ALTER, and DROP statements
DML Triggers run in response to data changes –
INSERT, UPDATE, DELETE
4. DML Trigger Types
For/After: Fire after the action has occurred (tables
only)
Changes written to data tables Trigger fires
Instead Of: Fire after changes written to logical tables
but before written to data tables (tables or views)
Trigger fires Changes written to data tables
5. Respond to actions on data
Trigger is not called
Trigger is specific to a table (or view)
Table can have multiple triggers
One trigger can respond to different actions
6. Triggers & Constraints
Constraints give better performance
Triggers can span tables, databases, servers;
constraints are table specific
If testing a value inserted in one table depends on an
entry in another table, use a trigger
If testing a value depends on an entry in the current row,
a relationship, or function result, use a constraint
8. Related Tables
Triggers can use the Inserted and Deleted tables
Inserted table holds new or updated records
Deleted table holds deleted or original version of
updated records
9. Inserted & Deleted Tables
Action Inserted Table Deleted Table
Insert XX
Update XX XX
Delete XX
10. Triggers for Referential Integrity
If trying to enroll a student, add a trigger on
insert or update that tests for valid student id
in Students table
If Exists (Select * from Students
Where StudentID IN
(Select StudentID From Inserted))
Begin
Insert into Enrollments(…)
End
11. Update()
Can test whether a column has changed
Can use to determine if a calculated field need to be
updated
Update(columnname)
If Update(FirstName)
Print ‘FirstName has changed’
12. Updating Summary Information
If tracking Possible Points for a schedule, changing
Possible Points for an Assignment would require
change to the Schedule’s Possible Points
Use Update(PossiblePoints) in trigger for Assignment
insert, update
13. Instead Of Triggers
Can have one for each type of action
Can apply to views
Provides way to allow data modification in joined tables
Instead of triggers won’t work if have cascade update
or delete
Need to complete action!
If you’re executing code instead of deleting a row, the
row won’t be deleted if you don’t delete it in your code!