
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How can we create multiple MySQL triggers for the same trigger event and action time?
MySQL 5.7.2+ allows us to create multiple triggers for the same event and action time in a table. Both the triggers will activate sequentially when the event occurs. It can be understood with the help of an example −
Example
In this example, we are creating multiple triggers for the same event say BEFORE UPDATE. The names of the triggers are ‘Studentdetail_before_update’ and ‘Studentdetail_before_update2’. They will activate sequentially when an event occurs. We are creating these triggers on the table ‘Student_detail’ having the following data −
mysql> Select * from Student_detail; +-----------+-------------+------------+ | Studentid | StudentName | address | +-----------+-------------+------------+ | 100 | Gaurav | Delhi | | 101 | Raman | Shimla | | 103 | Rahul | Jaipur | | 104 | Ram | Chandigarh | | 105 | Mohan | Chandigarh | +-----------+-------------+------------+ 5 rows in set (0.06 sec) mysql> Delimiter //
Now with the help of the following query, we will create the first trigger, which will be created by the same query as earlier.
mysql> Create Trigger studentdetail_before_update -> BEFORE UPDATE -> ON Student_detail -> FOR EACH ROW -> BEGIN -> DECLARE AUSER Varchar(40); -> SELECT USER() into AUSER; ->INSERT INTO Student_detail_updated(studentid, Updated_date,Updated_by) values(OLD.studentid,NOW(),AUSER); -> END; // Query OK, 0 rows affected (0.17 sec) mysql> Update student_detail SET Address = 'Ludhiana' Where studentName = 'Ram'; Query OK, 1 row affected (0.15 sec) Rows matched: 1 Changed: 1 Warnings: 0
After invoking the above-created trigger we got the following result −
mysql> Select * from student_detail_updated; +-----------+---------------------+----------------+ | studentid | Updated_date | Updated_by | +-----------+---------------------+----------------+ | 104 | 2017-11-22 16:17:16 | root@localhost | +-----------+---------------------+----------------+ 1 row in set (0.00 sec)
Now, the second trigger of the same event and action time can be created as follows −
mysql> Create Trigger studentdetail_before_update2 -> BEFORE UPDATE -> ON Student_detail -> FOR EACH ROW FOLLOWS studentdetail_before_update -> BEGIN -> DECLARE AUSER Varchar(40); -> SELECT USER() into AUSER; -> INSERT INTO Student_detail_updated(studentid, Updated_date,Updated_by) values(OLD.studentid,NOW(),AUSER); -> END; // Query OK, 0 rows affected (0.15 sec)
The above trigger will activate after the first trigger because we are using the keyword ‘FOLLOWS’.
mysql> Update Student_detail SET Address = 'Patiala' WHERE studentname = 'Mohan'; Query OK, 1 row affected (0.08 sec) Rows matched: 1 Changed: 1 Warnings: 0
Now, when we update the value, the following result set is showing two rows for the same event and action time. Second row represents the value after studentdetail_before_update trigger and third row represents the value after studentdetail_before_update2 trigger.
mysql> Select * from student_detail_updated; +-----------+---------------------+----------------+ | studentid | Updated_date | Updated_by | +-----------+---------------------+----------------+ | 104 | 2017-11-22 16:17:16 | root@localhost | | 105 | 2017-11-22 16:19:28 | root@localhost | | 105 | 2017-11-22 16:19:28 | root@localhost | +-----------+---------------------+----------------+ 3 rows in set (0.00 sec)
- Related Articles
- How can we see the information on triggers order in case of multiple triggers\nfor same event and action time?
- In which order MySQL will invoke the triggers if we created multiple triggers of same event and action time?
- How can we create and use a MySQL trigger?
- How can we create a MySQL one-time event that executes immediately?
- How can we create a MySQL one-time event that executes after some specified time interval?
- How can we use SIGNAL statement with MySQL triggers?
- How can we create a MySQL recurring event that executes after a specified time period and ends after a specified time period?
- How can we start MySQL event scheduler?
- How can I trigger a JavaScript click event?
- How to trigger the same function with jQuery multiple events?
- How can we get the metadata of triggers?
- How can we ENABLE AND DISABLE a particular MySQL event?
- What is MySQL event and how it is related to trigger?
- How we can find all the triggers associated with a particular MySQL table?
- How to get all the MySQL triggers and the triggers for only the current database
