A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/mysql/mysql-after-update-trigger/ below:

MySQL AFTER UPDATE Trigger - GeeksforGeeks

MySQL AFTER UPDATE Trigger

Last Updated : 23 Jul, 2025

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.

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 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;

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 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.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4