Module - 7 Triggers
Module - 7 Triggers
Overview
Introducing Triggers
Creating, Altering, and Dropping Triggers
Working with Triggers
Implementing Triggers
Lesson: Introducing Triggers
Altering a Trigger
Changes the definition without dropping the trigger
Can disable or enable a trigger
USE Northwind
GO
ALTER TRIGGER Empl_Delete ON Employees
FOR DELETE
AS
IF @@ROWCOUNT > 6
BEGIN
print(
'You cannot delete more than six employees at a time.')
ROLLBACK TRANSACTION
END
The DROP TRIGGER Statement
Dropping a Trigger
USE Northwind
GO
DROP TRIGGER Empl_Delete
GO
Lesson: Working with Triggers
TRIGGER
INSERT Actions
statement to aExecute
table with an INSERT Trigger Defined
INSERTINSERT
[OrderStatement
Details] to a Table with an INSERT
VALUES
1
(10525,
Trigger Code:
USE2,Northwind
Trigger 19.00,
Defined5, 0.2)
CREATE TRIGGER OrdDet_Insert
ON [Order
OrderID Details]
ProductID UnitPrice Quantity Discount
2 INSERT
AS
Statement
FOR INSERT
10522 10 Logged 31.00 7 0.2
UPDATE10523
P SET 41 9.65 9 0.15
3 UnitsInStock
Trigger Actions
10524 = 7 (P.UnitsInStock
Executed 30.00 24– I.Quantity)
FROM Products AS P INNER JOIN Inserted AS I
0.0
10525
ON P.ProductID 2 = I.ProductID
19.00 5 0.2
DELETEActions
Trigger Statement
Execute
to a table with a DELETE Trigger Defined
DELETE Categories
Statement to a Table with a DELETE
1
DELETE
USE
Categories
Northwind
WHERE
Statement
= 4 Defined
CREATE TRIGGER CategoryID CategoryName Description
Category_Delete Picture
CategoryID
ON Categories Soft drinks,
FOR DELETE 1 Beverages 0x15…
coffees…
AS
2 DELETE Statement Logged = 1
UPDATE P SET Discontinued
2 Condiments
Sweet and 0x15…
FROM Products AS P INNER JOIN deleted savory …AS d
ON P.CategoryID = d.CategoryID
Desserts,
3 Trigger Actions Executed
3 Confections
candies, …
0x15…
4 Products
Dairy Products Cheeses 0x15…
ProductID Discontinued … …
1 0
2 10
3 0
4 0
How UPDATE Triggers Work
UPDATE Statement
TRIGGER Actions Execute
to a table with an UPDATE Trigger Defined
USE Northwind Customers
UPDATE
GO Customers
1
SET
CREATEUPDATE
TRIGGER Statement
CustomerID
WHEREONCustomerID
Trigger
= ‘AHORN’to a Table
Customer_Update
= ‘AROUT’
Defined
Customers
with an UPDATE
CustomerID CompanyName ..
FOR UPDATE ALFKI Alfreds Futterkiste ~~~
AS Ana Trujillo
IF UPDATE (CustomerID) ANATR ~~~
Emparedados y helados
2
BEGIN UPDATE Statement Logged as INSERT
***** Statements
Customer ID cannot be ANTON
modified.',
and
RAISERROR ('Transaction cannot be processed.\
DELETE Antonio Moreno
10, 1) ~~~
ROLLBACK TRANSACTION Taquería
END Customers
AROUT Around the Horn ~~~
3
UPDATE
Trigger Actions Executed CustomerID
Statement
Transaction
CompanyName ..
cannot be logged as INSERT and DELETE Statements
ALFKI Alfreds Futterkiste ~~~
processed.
inserted Ana Trujillo
ANATR ~~~
***** CustomerAround
AHORN ID cannot be
the Horn ~~~ Emparedados y helados
modified Antonio Moreno
deleted ANTON ~~~
Taquería
AROUT Around the Horn ~~~ AROUT Around the Horn ~~~
How INSTEAD OF Triggers Work
3 base
the
CustomersMex
table
Allows Updates to Views Not Previously Updateable
Original
CustomerID Insert
CompanyName CountryCustomersGer
Phone …
ANATR to the
Ana Customers
Trujill… Mexico CustomerID CompanyName Country Phone …
ANTONView Does
Antonio M…Not Mexico ALFKI Alfreds Fu… Germany 030-0074321 ~~~
Occur BLAUS Blauer Se… Germany 0621-08460 ~~~
CENTC Centro Co… Mexico
DRACD Drachenb… Germany 0241-039123 ~~~
How Nested Triggers Work
Performance Considerations
Best Practices
Example: Auditing with Triggers
Example: Enforcing Business Rules
Performance Considerations
Employees
UPDATE Employees EmployeeID … Salary
SET Salary=40000 1 30,000
Updated
WHERE EmployeeId = 2 2 40,000
3 30,000
4 30,000
AuditLog
EntryID EntryDate UserID Activity
Modified Salary of EmployeeID: 2
1 2005/6/2 dbo
From 30000 To 40000
Example: Enforcing Business Rules