Create Multiple MySQL Triggers for the Same Event and Action Time



In MySQL, a trigger is a group of SQL statements performed or fired when a specified event is taken place on tables. The events can include INSERT, UPDATE, and DELETE. Such triggers are used for automating repetitive work, ensuring integrity in data, and keeping audit records without human involvement.

    The FOLLOWS keyword in MySQL allows the creation of several triggers for the same event and also allows you to select the order of execution.

Creating Multiple Triggers for the Same Event

When working with triggers, sometimes you might need to define several triggers for the same event since you may wish to perform different actions or record different types of data. For instance, you might have a single trigger log table changes while another sends email notifications. To achieve this, you will make use of multiple triggers, where a different trigger does each action.

Example

Problem Statement: Logging and Tracking Address Updates

In this example we need to create two triggers where the first trigger logs every update to the Student_detail_updated table, and the second trigger will track the changes and log them separately in the Student_detail_changes table.

Creating table

First, let us create Student_detail table, To do this we use CREATE command.

CREATE TABLE Student_detail (
    studentid INT PRIMARY KEY,
    studentname VARCHAR(100),
    address VARCHAR(100)
);

Table to log all updates (Student_detail_updated)

CREATE TABLE Student_detail_updated (
    studentid INT,
    updated_date DATETIME,
    updated_by VARCHAR(40)
);

Table to log address changes (Student_detail_changes):

CREATE TABLE Student_detail_changes (
    studentid INT,
    old_address VARCHAR(100),
    new_address VARCHAR(100),
    change_date DATETIME
);

Inserting sample data to the main table using INSERT command.

INSERT INTO Student_detail (studentid, studentname, address) 
VALUES (100, 'Gaurav', 'Delhi');
Creating first trigger

This trigger logs every update to the Student_detail_updated table. Following is the query to create a trigger.

DELIMITER //
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 //
DELIMITER ;
Creating Second trigger

This trigger only logs changes to the address in the Student_detail_changes table it can be created as follows -

DELIMITER //
CREATE TRIGGER studentdetail_before_update2
    BEFORE UPDATE ON Student_detail
    FOR EACH ROW
    FOLLOWS studentdetail_before_update
BEGIN
    IF OLD.address != NEW.address THEN
        INSERT INTO Student_detail_changes (studentid, old_address, new_address, change_date)
        VALUES (OLD.studentid, OLD.address, NEW.address, NOW());
    END IF;
END //
DELIMITER ;

The above trigger will activate after the first trigger because we are using the keyword ?FOLLOWS'.

Activating trigger

Now let us update Student_detail to check how both triggers work.

UPDATE Student_detail 
SET address = 'Ludhiana' 
WHERE studentname = 'Gaurav';
Verification

Checking for first trigger logs.

SELECT * FROM Student_detail_updated;

Following is the output of the above query ?

studentid updated_date updated_by
100 2024-11-07 16:00:00 root@localhost

From the above output, we can confirm thatthefirst Trigger successfully logged the update operation, including the student's ID, update timestamp, and the user who performed the update.

Checking for the second trigger logs.

SELECT * FROM Student_detail_changes;

Following is the output of the above query ?

studentid old_address new_address change_date
100 Delhi Ludhiana 2024-11-07 16:00:00

This output confirms that the second trigger makes sure to display the old and new address along with the timestamp when it was updated.

Updated on: 2025-01-22T17:34:29+05:30

714 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements