T4 L5 Triggers
T4 L5 Triggers
Topic 4 Lesson 5
Creating active databases
Trigger DB Object
Trigger: subroutine that starts automatically if specified
change occurs to the DB
SHOW TRIGGERS;
ALL Triggers in DBMS using the System Catalog
SELECT * FROM Information_Schema.Triggers
WHERE Trigger_schema = 'database_name' AND
Trigger_name = 'trigger_name';
SELECT trigger_schema, trigger_name, action_statement FROM
information_schema.triggers;
Trouble with triggers
One DB Action can trigger multiple triggers
Execution of the order of the triggers used to be arbitrary, now is
determined by trigger create time. The trigger can also specify if it
should precede or follow a specific trigger
(FOLLOWS or PRECEDES are the keywords)
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
MySQL limits the use of triggers
• Triggers not introduced until MySQL 5.0
• Triggers cannot be activated for foreign key actions
• No triggers on the MySQL system catalog 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
Modifying a trigger
There is no edit operation defined for a trigger
CREATE TRIGGER …
DROP TRIGGER <TRIGGERNAME>;
CREATE TRIGGER …
Summary
Triggers allow you to create active databases, databases that
perform an action when a specific modification operation is
performed on a table.