0
CIS-282
Triggers Specialized stored procedure that runs ‘automatically’ Another method to enforce rules    Typically used to en...
DDL v. DML Triggers DDL Triggers allow procedures to run as part of  CREATE, ALTER, and DROP statements DML Triggers run...
DML Trigger Types For/After: Fire after the action has occurred (tables  only)   Changes written to data tables          ...
Respond to actions on data Trigger is not called Trigger is specific to a table (or view) Table can have multiple trigg...
Triggers & Constraints Constraints give better performance Triggers can span tables, databases, servers;  constraints ar...
SyntaxCREATE TRIGGER <trigger name>ON <table name>FOR <insert, update, delete>AS…
Related Tables Triggers can use the Inserted and Deleted tables Inserted table holds new or updated records Deleted tab...
Inserted & Deleted Tables      Action   Inserted Table Deleted Table  Insert            XX  Update            XX          ...
Triggers for Referential Integrity   If trying to enroll a student, add a trigger on    insert or update that tests for v...
Update() Can test whether a column has changed    Can use to determine if a calculated field need to be     updated Upd...
Updating Summary Information If tracking Possible Points for a schedule, changing  Possible Points for an Assignment woul...
Instead Of Triggers Can have one for each type of action Can apply to views    Provides way to allow data modification ...
Upcoming SlideShare
Loading in...5
×

Triggers

797

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
797
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
48
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Triggers"

  1. 1. CIS-282
  2. 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. 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. 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. 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. 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
  7. 7. SyntaxCREATE TRIGGER <trigger name>ON <table name>FOR <insert, update, delete>AS…
  8. 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. 9. Inserted & Deleted Tables Action Inserted Table Deleted Table Insert XX Update XX XX Delete XX
  10. 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. 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. 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. 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!
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×