Database Systems
Session 17
Chapter 7 – Triggers
Objectives
1 Know what is a trigger
2 Know why use triggers
3 Know components of triggers
4 Know how to create a trigger
Contents
1 Trigger definition
2 Why use triggers?
3 Components of trigger
4 Trigger Guideline
1. Trigger definition
A trigger is a procedure that is invoked by
the DBMS as a response to a special
change (insert, delete, update to a particular
relation or transaction end)
Triggers are available in most current
commercial DB products
Trigger carry out actions when their
triggering conditions are met
Why use triggers?
Triggers can implement business rules
• E.g. creating a new loan when a customer’s
account is overdrawn.
Triggers be used to ensure data integrity
Triggers may also be used to maintain
data in related database tables.
• E.g. Updating derived attributes when underlying
data is changed, or maintaining summary data.
Trigger components
Event (activates the trigger)
A specified modification to the DB
• May be an insert, deletion or change.
• May be limited to special tables.
• The trigger may fire before or after the transaction
Condition (test whether the triggers should
run)
A Boolean expression or a query
• If the query answer set is non-empty it evaluates to
true, otherwise false.
• If the condition is true the trigger action occurs
Trigger components
Action (what happens if the trigger runs)
A trigger’s action can be very far-ranging, e.g.
• Execute queries.
• Make modifications to the DB.
• Create new tables.
• Call procedures
Trigger guideline
The key elements and the order in the syntax for
triggers:
1. The CREATE TRIGGER statement
2. The clause indicating the trigger event and telling
whether the trigger uses the database state.
3. A REFERENCING clause to allow the condition and
action of the trigger to refer to the tuple being modified.
4. A clause telling whether trigger executes once for each
modified row or once for all the modifications made by
one SQL statement.
5. The condition, which uses the keyword WHEN and a
boolean expression.
6. The action, consisting of one or more SQL statements.
Trigger guideline
Example of trigger (in SQL Server):
1. CREATE TRIGGER NetWorthTrigger
2. AFTER UPDATE OF netWorth ON MovieExec
3. REFERENCING
OLD ROW AS Oldtuple,
NEW ROW AS Newtuple
4. FOR EACH ROW
5. WHEN (Oldtuple.netWorth > Newtuple.netWorth)
6. UPDATE MovieExec
SET netWorth = Oldtuple.netWorth
WHERE cert# = Newtuple.cert#;
The options for trigger design
Synchronization of the trigger with the
activating statement
BEFORE
AFTER
Possible triggering events are:
UPDATE
INSERT
DELETE
An OF clause is not permitted for INSERT and DELETE events;
these events make sense for entire tuples only
The options for trigger design
There are two kinds of triggers
Statement-level trigger: executed once for all
the tuples are changed in one SQL statement.
REFERENCING NEW TABLE AS newtuples,
OLD TABLE AS oldtuples
Row-level trigger: executed once for each
modified tuple.
REFERENCING OLD AS oldtuple,
NEW AS newtuple,
Newtuples, oldtuples, oldtuple, newtuple can be used in
the CONDITION and ACTION clause
The options for trigger design
Number of Activations of the trigger
Once per modified tuple: FOR EACH ROW
Once per activating statement (default).
WHEN clause is optional.
The action of trigger can consist of
multiple SQL statements, surrounded by
BEGIN… END.
Trigger guideline
Example of trigger (in SQL Server):
1. CREATE TRIGGER AvgNetWorthTrigger
2. AFTER UPDATE OF netWorth ON MovieExec
3. REFERENCING
OLD ROW AS OldStuff,
NEW ROW AS NewStuff
4. FOR EACH ROW
5. WHEN (500000 > (SELECT AVG(netWorth) FROM MovieExec))
6. BEGIN
DELETE FROM MovieExec
WHERE (name, address, cert#, netWorth) IN NewStuff;
INSERT INTO MovieExec (SELECT * from OldStuff);
END;