Multiple Triggers
In this lesson, we will see how to write multiple triggers for a table which have the same action time and event.
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.
-- The lesson queries are reproduced below for convenient copy/paste into the terminal.-- Query 1CREATE TABLE GenderSummary (TotalMales INT NOT NULL,TotalFemales INT NOT NULL);CREATE TABLE MaritalStatusSummary (TotalSingle INT NOT NULL,TotalMarried INT NOT NULL,TotalDivorced INT NOT NULL);CREATE TABLE ActorsTableLog (RowId INT AUTO_INCREMENT PRIMARY KEY,ActorId INT NOT NULL,Detail VARCHAR(100) NOT NULL,UpdatedOn TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP);-- Query 2INSERT INTO GenderSummary (TotalMales, TotalFemales)Values ((SELECT COUNT(Gender) FROM Actors WHERE Gender = 'Male'),(SELECT COUNT(Gender) FROM Actors WHERE Gender = 'Female'));SELECT * FROM GenderSummary;INSERT INTO MaritalStatusSummary (TotalSingle, TotalMarried, TotalDivorced)Values ((SELECT COUNT(MaritalStatus) FROM Actors WHERE MaritalStatus = 'Single'),(SELECT COUNT(MaritalStatus) FROM Actors WHERE MaritalStatus = 'Married'),(SELECT COUNT(MaritalStatus) FROM Actors WHERE MaritalStatus = 'Divorced'));SELECT * FROM MaritalStatusSummary;-- Query 3DELIMITER **CREATE TRIGGER UpdateGenderSummaryAFTER INSERTON ActorsFOR EACH ROWBEGINDECLARE count INT;IF NEW.Gender = 'Male' THENUPDATE GenderSummarySET TotalMales = TotalMales+1;INSERT INTO ActorsTableLog (ActorId, Detail)VALUES (NEW.Id, 'TotalMales value of GenderSummary table changed.');ELSEUPDATE GenderSummarySET TotalFemales = TotalFemales+1;INSERT INTO ActorsTableLog (ActorId, Detail)VALUES (NEW.Id, 'TotalFemales value of GenderSummary table changed.');END IF;END **DELIMITER ;-- Query 4DELIMITER **CREATE TRIGGER UpdateMaritalStatusSummaryAFTER INSERTON ActorsFOR EACH ROWFOLLOWS UpdateGenderSummaryBEGINDECLARE count INT;IF NEW.MaritalStatus = 'Single' THENUPDATE MaritalStatusSummarySET TotalSingle = TotalSingle+1;INSERT INTO ActorsTableLog (ActorId, Detail)VALUES (NEW.Id, 'TotalSingle value of MaritalStatusSummary table changed.');ELSEIF NEW.MaritalStatus = 'Married' THENUPDATE MaritalStatusSummarySET TotalMarried = TotalMarried+1;INSERT INTO ActorsTableLog (ActorId, Detail)VALUES (NEW.Id, 'TotalMarried value of MaritalStatusSummary table changed.');ELSEUPDATE MaritalStatusSummarySET TotalDivorced = TotalDivorced+1;INSERT INTO ActorsTableLog (ActorId, Detail)VALUES (NEW.Id, 'TotalDivorced value of MaritalStatusSummary table changed.');END IF;END **DELIMITER ;-- Query 5INSERT INTO Actors (FirstName, SecondName, DoB, Gender, MaritalStatus, NetWorthInMillions)VALUES ('Tom', 'Hanks', '1956-07-09', 'Male', 'Married', 350);SELECT * FROM ActorsTableLog;-- Query 6SHOW TRIGGERS;-- Query 7SELECTtrigger_name,action_orderFROMinformation_schema.triggersWHEREtrigger_schema = 'MovieIndustry';
-
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 ...