SQL Server - Bài
Implementing TriggerVu Tuyet Trinhtrinhvt-fit@mail.hut.edu.vnHanoi University of Technology1 MicrosoftMicrosoftIntroduction to TriggersWhat Is a Trigger?Uses of TriggersConsiderations for Using Triggers MicrosoftMicrosoftWhat Is a Trigger?Associated with a TableInvoked AutomaticallyCannot Be Called DirectlyIs Part of a Transaction MicrosoftMicrosoftOutline√Introduction to TriggersCreating, Altering, and Dropping TriggersWorking with TriggersUses of TriggerPerformance Consideration MicrosoftMicrosoftCreating and Managing TriggersCreating TriggersAltering and Dropping Triggers MicrosoftMicrosoftCreating TriggersRequires Appropriate PermissionsCannot Contain Certain Statements Use NorthwindGOCREATE TRIGGER Empl_Delete ON EmployeesFOR DELETE ASIF (SELECT COUNT(*) FROM Deleted) > 1BEGIN RAISERROR( 'You cannot delete more than one employee at a time.', 16, 1) ROLLBACK TRANSACTIONEND MicrosoftMicrosoftAltering and Dropping TriggersAltering a TriggerChanges the definition without dropping the triggerCan disable or enable a triggerDropping a TriggerUSE NorthwindGOALTER TRIGGER Empl_Delete ON EmployeesFOR DELETE ASIF (SELECT COUNT(*) FROM Deleted) > 6BEGIN RAISERROR( 'You cannot delete more than six employees at a time.', 16, 1) ROLLBACK TRANSACTIONEND MicrosoftMicrosoftHow Triggers WorkHow an INSERT Trigger WorksHow a DELETE Trigger WorksHow an UPDATE Trigger WorksHow an INSTEAD OF Trigger Works MicrosoftMicrosoftHow an INSERT Trigger WorksINSERT statement to a table with an INSERT Trigger DefinedINSERT [Order Details] VALUES(10525, 2, 19.00, 5, 0.2)Order DetailsOrder DetailsOrderID105221052310524ProductID10417UnitPrice31.009.6530.00Quantity7924Discount0.20.150.0 5 19.002 0.210523Insert statement loggedinsertedinserted10523 2 19.00 5 0.2TRIGGER Actions ExecuteOrder DetailsOrder DetailsOrderID105221052310524ProductID10417UnitPrice31.009.6530.00Quantity7924Discount0.20.150.0 5 19.002 0.210523Trigger Code:USE NorthwindCREATE TRIGGER OrdDet_InsertON [Order Details]FOR INSERTASUPDATE P SET UnitsInStock = (P.UnitsInStock – I.Quantity)FROM Products AS P INNER JOIN Inserted AS ION P.ProductID = I.ProductIDUPDATE P SET UnitsInStock = (P.UnitsInStock – I.Quantity)FROM Products AS P INNER JOIN Inserted AS ION P.ProductID = I.ProductIDProductsProductsProductIDUnitsInStock… …1234151065202 15 MicrosoftMicrosoftActivating Insert TriggerStep 1INSERT statement to a table with an INSERT trigger definedStep 2INSERT Statement LoggedStep 3Trigger Actions Executed [...]... Product table checks the Order Details table Order Details Products ProductID UnitsInStock… … 1 2 3 4 15 0 10 65 20 OrderID ProductID UnitPrice Quantity Discount 10522 10523 10524 10525 10 2 41 7 31.00 19.00 9.65 30.00 7 9 24 0.2 0.15 0.0 'Transaction cannot be processed' 'This product has order history' Microsoft Performance Considerations Triggers Work Quickly Because the Inserted and Deleted Tables... Peacock Margaret Sales Rep ~~~ ***** Member number cannot be modified Employees UPDATE Statement logged as INSERT and DELETE Statements EmployeeIDLastNameFirstName Title inserted 1 Davolio Nancy Sales Rep 17 Fuller 2 Andrew Vice Pres ~~~ Pres Fuller Andrew R AndrewVice Barr 3 Leverling Janet Sales Rep deleted Peacock Margaret Sales 2 Fuller 4 Andrew Vice Pres ~~~ Rep Microsoft HireDate ~~~ ~~~ ~~~ ~~~ Activating... Trigger Actions Executed Microsoft How an UPDATE Trigger Works TRIGGER Actions Execute UPDATE Statement to a table with an UPDATE Trigger Defined USE Northwind GO UPDATE Employees CREATE EmployeeID = 17 SET TRIGGER Employee_Update ON Employees WHERE EmployeeID = 2 FOR UPDATE Employees AS AS IF UPDATE (EmployeeID) IF UPDATE (EmployeeID) EmployeeIDLastNameFirstName Title HireDate BEGIN TRANSACTION BEGIN . 0.2)Order DetailsOrder DetailsOrderID105221052310524ProductID10417UnitPrice31.009.6530.00Quantity7924Discount0.20.150.0 5 19.002 0.210523Insert statement. ExecuteOrder DetailsOrder DetailsOrderID105221052310524ProductID10417UnitPrice31.009.6530.00Quantity7924Discount0.20.150.0 5 19.002 0.210523Trigger Code:USE