Professional Documents
Culture Documents
Objectives
In this lesson, you will learn to: Create the INSERT, DELETE, and UPDATE triggers Modify triggers Drop triggers Enforce data integrity through triggers Use the AFTER and the INSTEAD OFF triggers
SQL/Lesson 10/Slide 1 of 49
SQL/Lesson 10/Slide 2 of 49
SQL/Lesson 10/Slide 3 of 49
SQL/Lesson 10/Slide 4 of 49
Result:
A trigger can be used to maintain data integrity
SQL/Lesson 10/Slide 5 of 49
SQL/Lesson 10/Slide 6 of 49
SQL/Lesson 10/Slide 8 of 49
SQL/Lesson 10/Slide 11 of 49
SQL/Lesson 10/Slide 12 of 49
SQL/Lesson 10/Slide 13 of 49
SQL/Lesson 10/Slide 14 of 49
SQL/Lesson 10/Slide 15 of 49
SQL/Lesson 10/Slide 16 of 49
SQL/Lesson 10/Slide 17 of 49
SQL/Lesson 10/Slide 18 of 49
AS
PRINT 'Deletion of Contract Recruiters is not allowed' ROLLBACK TRANSACTION RETURN
SQL/Lesson 10/Slide 19 of 49
SQL/Lesson 10/Slide 20 of 49
SQL/Lesson 10/Slide 21 of 49
SQL/Lesson 10/Slide 22 of 49
SQL/Lesson 10/Slide 23 of 49
SQL/Lesson 10/Slide 24 of 49
SQL/Lesson 10/Slide 25 of 49
SQL/Lesson 10/Slide 27 of 49
UPDATE ContractRecruiter
SET siPercentageCharge =siPercentageCharge+10
WHERE cContractRecruiterCode='0002
If the average exceeds the limit, then the trigger would generate an error message
SQL/Lesson 10/Slide 28 of 49
SQL/Lesson 10/Slide 29 of 49
SQL/Lesson 10/Slide 30 of 49
SQL/Lesson 10/Slide 31 of 49
Dropping the trigger and recreating it Using the ALTER TRIGGER statement
It is advisable to drop a trigger and recreate it, if the objects being referenced by it are renamed
SQL/Lesson 10/Slide 32 of 49
SQL/Lesson 10/Slide 33 of 49
Insert a row in Requisition table and verify that the trigger is working
Action: INSERT Requisition VALUES('000002','0001',getdate( ),getdate()+7,0001','North',20)
SQL/Lesson 10/Slide 36 of 49
SQL/Lesson 10/Slide 37 of 49
SQL/Lesson 10/Slide 38 of 49
SQL/Lesson 10/Slide 39 of 49
SQL/Lesson 10/Slide 40 of 49
sp_help trgDeleteContractRecruiter
The above command will give an error message as the trigger has been removed
SQL/Lesson 10/Slide 41 of 49
If changes are made to the master table, then the same changes are cascaded to all the dependent tables If some changes violate referential integrity, then all such changes are rejected, thereby canceling any attempt to modify data in the database
SQL/Lesson 10/Slide 45 of 49
SQL/Lesson 10/Slide 46 of 49
A magic table is a conceptual table that is structurally similar to the table on which a trigger is defined
SQL/Lesson 10/Slide 47 of 49
INSTEAD OF triggers can be used to perform another action such as a DML operation on another table or view
SQL/Lesson 10/Slide 49 of 49