7/31/2019 Session - Triggers
1/27
Implementing
Triggers
7/31/2019 Session - Triggers
2/27
Session Objectives
Define triggers List the uses of triggers
List the guidelines for creating triggers Explain the CREATE TRIGGER T-SQL
statement List the types of triggers
Explain INSERT triggers Explain UPDATE triggers Explain DELETE triggers
7/31/2019 Session - Triggers
3/27
Session Objectives (contd.)
Identify SQL statements that cannot be usedin triggers
Define nested and cascading triggers
Explain Instead Of triggers
List the factors that affect the performance of
triggers
7/31/2019 Session - Triggers
4/27
Triggers
Allen
Brian
Cathy
Derek
Greg
Mark
Carl
Kathy
Derek
7/31/2019 Session - Triggers
5/27
Triggers
Allen
Brian
Cathy
Derek
Greg
Mark
Carl
Kathy
Trigger Fired
Derek
Inserted
7/31/2019 Session - Triggers
6/27
Uses of Triggers
Compare versions of data
Read data from other tables in other
databases
Cascade changes or deletes throughoutrelated tables in a database
Reverse invalid changes Enforce more complex restrictions than
CHECK constraints
Execute local and remote stored procedures
7/31/2019 Session - Triggers
7/27
Creating Triggers
Syntax
CREATE TRIGGER Trigger_name
ON table
FOR {[DELETE] [,] [INSERT][,][UPDATE]}
[WITH ENCRYPTION]
AS Sql_statement
7/31/2019 Session - Triggers
8/27
Inserted & Deleted Tables
Logical tables that are accessible to triggers
Contain images of data prior to and after the
updation
Schema identical to the table being updated
7/31/2019 Session - Triggers
9/27
Considerations
A trigger can be associated with three actionsperformed on a table: INSERT, UPDATE, and
DELETE. A trigger applies to a single table.
The WITH ENCRYPTION option can hide a
trigger definition. An encrypted trigger can,however, not be decrypted.
A trigger can reference a view or a temporarytable, but cannot be associated with it.
7/31/2019 Session - Triggers
10/27
Considerations (contd.)
A trigger can include any number of SQLstatements.
By default, the permission to create thetrigger is available only to the owner of thedatabase. This permission is not transferable.
A trigger can be created only in the currentdatabase. However, it can refer to the objectsof another database.
7/31/2019 Session - Triggers
11/27
Types of Triggers
7/31/2019 Session - Triggers
12/27
Insert Trigger
CREATE TRIGGER CheckFare ON flight_details FOR INSERT AS
IF (SELECT fare FROM INSERTED AS i JOIN flight AS fON i.aircraft_code = f.aircraft_code JOIN airlines_master AS am
ON f.aircode = am.aircode WHERE i.class_code = 'FC'
AND am.airline_name = 'Indian Airlines') > 8000
BEGIN
PRINT 'CheckFareTrigger: Fare for FC class of Indian Airlines
flights cannot exceed 8000'
PRINT 'Change the fare to a value less than 8000'
ROLLBACK TRANSACTION
END
Example
7/31/2019 Session - Triggers
13/27
Insert Trigger (contd.)
7/31/2019 Session - Triggers
14/27
Update Triggers
7/31/2019 Session - Triggers
15/27
Column level Update trigger
CREATE TRIGGER NoUpdateMealcode
ON Meal
FOR UPDATE AS
IF UPDATE (Meal_code)
BEGIN
PRINT 'You cannot modify the meal codes'
ROLLBACK TRANSACTION
END
Example
7/31/2019 Session - Triggers
16/27
7/31/2019 Session - Triggers
17/27
Table level Update Trigger
CREATE TRIGGER NoUpdateSeats
ON Reservation
FOR UPDATE AS
IF (SELECT no_of_seats FROM inserted) > 5
BEGIN
PRINT 'You cannot book more than 5 seats'
ROLLBACK TRANSACTION
END
Example
7/31/2019 Session - Triggers
18/27
Table level Update Trigger (contd.)
7/31/2019 Session - Triggers
19/27
Delete Trigger
CREATE TRIGGER NoDeleteBA01
ON FlightFOR DELETE AS
IF (SELECT aircraft_code FROM deleted) = BA01
BEGIN
PRINT You cannot delete the details of aircraft
code BA01
ROLLBACK TRANSACTION
END
Example
7/31/2019 Session - Triggers
20/27
Delete Trigger (contd.)
7/31/2019 Session - Triggers
21/27
Statements not allowed in Triggers
7/31/2019 Session - Triggers
22/27
Nested Triggers
A trigger initiates another trigger
Nesting is permitted upto 32 levels
The @@nestlevel system global variableindicates the current nesting level
sp_configure enables or disables nesting of
triggers
7/31/2019 Session - Triggers
23/27
7/31/2019 Session - Triggers
24/27
INSTEAD OF Triggers
Contain code that replaces the original datamanipulation statement
Useful for data modifications through non-updateable views
Can only be based on one data modification
action
7/31/2019 Session - Triggers
25/27
7/31/2019 Session - Triggers
26/27
INSTEAD OF Trigger on view (contd.)
CREATE TRIGGER del_service
ON service_view
INSTEAD OF DELETE
AS
DELETE service WHERE service_code IN
(SELECT scode1 FROM DELETED)
DELETE airline_service WHERE service_code IN
(SELECT scode2 FROM DELETED)
7/31/2019 Session - Triggers
27/27
Performance of Triggers
Overhead associated with triggers is usuallylow
A significant amount of time is spent inreferencing tables other than logical tables
Deleted and inserted logical tables are always
present in memory