Open In App

MySQL AFTER UPDATE Trigger

Last Updated : 29 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Triggers in MySQL are special stored programs that are automatically executed when a specific event occurs on the table such as an INSERT, UPDATE, or DELETE. An AFTER UPDATE trigger is a type of trigger that executes after an UPDATE statement is performed on the table. This article provides a complete guide on how to create and use AFTER UPDATE triggers in MySQL including detailed examples and best practices.

Introduction to MySQL Triggers

The MySQL triggers are used to perform the automatic actions in response to certain events on the table. The Triggers can help enforce business rules maintain data integrity and perform automated tasks. They are created to execute either before or after an event occurs.

  • BEFORE Trigger: The Executes before the specified event.
  • AFTER Trigger: The Executes after the specified event.

The Triggers can be useful for tasks such as validating the data before it's committed, logging changes, or synchronizing data across tables.

Understanding AFTER UPDATE Triggers

An AFTER UPDATE trigger is invoked after an UPDATE statement is executed on the table. This allows us to perform additional actions once the update operation has been completed such as the logging changes or updating related tables.

Use Cases for AFTER-UPDATE Triggers

  • Logging Changes: The Record details the update operation for auditing purposes.
  • Maintaining Audit Trails: Keep track of the historical changes made to rows.
  • Synchronizing Data: Update related tables or perform additional calculations based on the updated data.

Syntax and Basic Usage

The basic syntax for creating an AFTER UPDATE trigger in MySQL is as follows:

CREATE TRIGGER trigger_name

AFTER UPDATE ON table_name

FOR EACH ROW

BEGIN

-- Trigger body

END;

  • trigger_name: The name of the trigger.
  • table_name: The name of the table on which the trigger is defined.
  • FOR EACH ROW: Indicates that the trigger will execute for each row affected by the UPDATE statement.
  • BEGIN ... END;: The block where we define the actions to be performed by the trigger.

Example: Logging Changes

This example demonstrates how to create an AFTER UPDATE trigger that logs changes to the separate audit table whenever an update occurs on the employees table.

1. Create the employees Table and Populate It with Sample Data

CREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
salary DECIMAL(10, 2)
);
INSERT INTO employees (name, salary) VALUES ('John Doe', 50000.00);
INSERT INTO employees (name, salary) VALUES ('Jane Smith', 60000.00);

2. Create the employee_audit Table

CREATE TABLE employee_audit (
id INT AUTO_INCREMENT PRIMARY KEY,
employee_id INT,
old_salary DECIMAL(10, 2),
new_salary DECIMAL(10, 2),
changed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

3. Create the AFTER UPDATE Trigger

DELIMITER //

CREATE TRIGGER log_salary_update
AFTER UPDATE ON employees
FOR EACH ROW
BEGIN
INSERT INTO employee_audit (employee_id, old_salary, new_salary)
VALUES (OLD.id, OLD.salary, NEW.salary);
END;
//
DELIMITER ;

4. Perform an Update Operation on the employees Table

Let's update the salary of John Doe:

UPDATE employees
SET salary = 55000.00
WHERE name = 'John Doe';

5. Verify That the Changes Have Been Logged in the employee_audit Table

SELECT * FROM employee_audit;

Output:

Output
Output

Conclusion

The MySQL AFTER UPDATE triggers are powerful tools for automating actions in response to data modifications. Whether we need to log changes maintain an audit trail or update related data AFTER UPDATE triggers can help us achieve these goals efficiently. By following best practices and using the triggers wisely we can enhance the functionality and reliability of the MySQL database.


Next Article
Article Tags :

Similar Reads