Multiple Triggers
Understand how to define and manage multiple SQL triggers on the same table event. Learn to use FOLLOWS and PRECEDES keywords to control execution order, implement triggers for updating summary tables based on new data, and log actions in real time for data integrity and audit purposes.
We'll cover the following...
Multiple Triggers
It is possible to create triggers on a table whose action time and event are the same. Such triggers are fired in a sequence that is specified at the time of creation of the triggers. The FOLLOWS and PRECEDES keywords are used to define the sequence in which triggers associated with a table having the same action time and event execute.
Syntax
CREATE TRIGGER trigger_name [BEFORE | AFTER] [INSERT | UPDATE | DELETE]
ON table_name
[FOLLOWS | PRECEDES] existing_trigger_name
FOR EACH ROW
trigger_body
Connect to the terminal below by clicking in the widget. Once connected, the command line prompt will show up. Enter or copy-paste the command ./DataJek/Lessons/50lesson.sh and wait for the mysql prompt to start-up.
-
To demonstrate the order in which two triggers execute for the same event, we will create a simple example. Suppose that we want to perform two tasks when a new record is inserted in the Actors table. First, based on the gender of the actor, we want to update the GenderSummary table. Second, based on his/her marital status, we want to update the ...