SQL Server - Bài 7

23 552 0
SQL Server - Bài  7

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

SQL Server - Bài

Implementing TriggerVu Tuyet Trinhtrinhvt-fit@mail.hut.edu.vnHanoi University of Technology1 MicrosoftMicrosoftIntroduction to TriggersWhat Is a Trigger?Uses of TriggersConsiderations for Using Triggers MicrosoftMicrosoftWhat Is a Trigger?Associated with a TableInvoked AutomaticallyCannot Be Called DirectlyIs Part of a Transaction MicrosoftMicrosoftOutline√Introduction to TriggersCreating, Altering, and Dropping TriggersWorking with TriggersUses of TriggerPerformance Consideration MicrosoftMicrosoftCreating and Managing TriggersCreating TriggersAltering and Dropping Triggers MicrosoftMicrosoftCreating TriggersRequires Appropriate PermissionsCannot 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 TriggersAltering a TriggerChanges the definition without dropping the triggerCan disable or enable a triggerDropping 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 WorkHow an INSERT Trigger WorksHow a DELETE Trigger WorksHow an UPDATE Trigger WorksHow 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

Ngày đăng: 15/11/2012, 10:59

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan