Triggers and Events: Kathleen Durant PHD Cs 3200
Triggers and Events: Kathleen Durant PHD Cs 3200
1
Lecture Outline
• Trigger Description
• My SQL trigger example
• My SQL event example
2
Triggers
• Trigger: procedure that starts automatically if specified
changes occur to the DBMS
• A trigger has three parts:
• Event
• Change to the database that activates the trigger
• Condition
• Query or test that is run when the trigger is activated
• Action
• Procedure that is executed when the trigger is activated and its
condition is true
3
Trigger Options
• Event can be insert, delete, or update on DB table
• Condition:
• Condition can be a true/false statement
• All employee salaries are less than $100K
• Condition can be a query
• Interpreted as true if and only if answer set is not empty
• Action can perform DB queries and updates that depend on:
• Answers to query in condition part
• Old and new values of tuples modified by the statement that
activated the trigger
• Action can also contain data-definition commands, e.g., create
new tables 4
When to Fire the Trigger
• Triggers can be executed once per modified record or once per
activating statement
• Row-level trigger versus a Statement Level Trigger
• Trigger looking at the set of records that are modified versus the actual
individual values of the old and the new values
• Should trigger action be executed before or after the statement
that activated the trigger?
• Consider triggers on insertions
• Trigger that initializes a variable for counting how many new tuples are
inserted: execute trigger before insertion
• Trigger that updates this count variable for each inserted tuple: execute
after each tuple is inserted (might need to examine values of tuple to
determine action)
5
• Trigger can also be run in place of the action
Trigger Example
• CREATE TRIGGER YoungSailorUpdate
AFTER INSERT ON SAILORS
REFERENCING NEW TABLE NewSailors
FOR EACH STATEMENT
INSERT
INTO YoungSailors(sid, name, age, rating)
SELECT sid, name, age, rating
FROM NewSailors N
WHERE N.age <= 18
Trigger has
access to 6
NEW and
OLD values
Trouble with Triggers
• Action can trigger multiple triggers
• Execution of the order of the triggers is arbitrary
• Challenge: Trigger action can fire other triggers
• Very difficult to reason about what exactly will happen
• Trigger can fire “itself” again
• Unintended effects possible
• Introducing Triggers leads you to deductive databases
• Need rule analysis tools that allow you to deduce truths about the data
7
MY SQL limits the use of
triggers
• Triggers not introduced until 5.0
• Not activated for foreign key actions
• No triggers on the mysql system database
• Active triggers are not notified when the meta data of the
table is changed while it is running
• No recursive triggers
• Triggers cannot modify/alter the table that is already being
used
• For example the table that triggered it
8
MY SQL Trigger
CREATE TRIGGER <trigger-name> trigger_time trigger_event
ON table_name
FOR EACH ROW
BEGIN
END
• Syntax
• Trigger_time is [BEFORE | AFTER]
• Trigger_event [INSERT|UPDATE|DELETE]
• Other key words – OLD AND NEW
• Naming convention for a trigger
trigger_time_tablename_trigger_event
• Found in the directory associated with the database
• File tablename.tdg – maps the trigger to the correspnoding table
• Triggername.trn contains the trigger definition
Reviewing your trigger
• Go to the trigger directory and read the file (.trg)
Program Data\MySQL\MySQL5.5\data\<db-name>\*.trg
15