MySQL AFTER UPDATE Trigger
Last Updated :
29 Jul, 2024
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:
OutputConclusion
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.
Similar Reads
MySQL BEFORE UPDATE Trigger
The MySQL triggers are a powerful feature that allows the execute a set of SQL statements automatically in response to certain events on a table. One type of trigger is the BEFORE UPDATE trigger which is invoked before an update operation is performed on the table. This article provides a complete o
4 min read
MySQL Create Trigger
Database triggers are specialized procedures that automatically respond to certain events on a table or view. These events include actions such as INSERT, UPDATE, or DELETE. Triggers can be used to enforce complex business rules, maintain audit trails, or synchronize data across tables. In MySQL, tr
4 min read
MySQL After Insert Trigger
An "AFTER INSERT" trigger in MySQL automatically executes specified actions after a new row is inserted into a table. It is used to perform tasks such as updating related tables, logging changes or performing calculations, ensuring immediate and consistent data processing.In this article, We will le
4 min read
MySQL AFTER DELETE Trigger
In MySQL, triggers are a concept where SQL developers can define automatic actions to occur in response to specific changes in a database table. An AFTER DELETE trigger, for instance, is invoked automatically after a row is deleted from a table, allowing developers to perform additional operations s
4 min read
MySQL DROP Trigger
In MySQL, triggers automatically perform actions when events like INSERT, UPDATE, or DELETE occur on a table However, there are situations where a trigger may not be necessary and its logic may need to be updated. In such cases, MySQL provides the DROP TRIGGER statement to delete an existing trigger
4 min read
MySQL Before Insert Trigger
MySQL BEFORE INSERT triggers are essential tools for maintaining data quality and enforcing business rules at the database level. These triggers automatically execute a series of SQL statements before a new row is inserted into a table, allowing for data validation, modification, and enhancement.An
5 min read
MySQL BEFORE DELETE Trigger
MySQL BEFORE DELETE trigger is a powerful tool that automatically performs specific actions before an DELETE operation is executed on a table. This feature allows us to handle tasks such as logging deleted records, enforcing business rules or validating data before it is removed from the database.In
3 min read
Python SQLite - Update Data
In this article, we will discuss how we can update data in tables in the SQLite database using Python - sqlite3 module. The UPDATE statement in SQL is used to update the data of an existing table in the database. We can update single columns as well as multiple columns using UPDATE statement as per
7 min read
MySQL UPDATE Statement
MySQL is a popular relational database management system used in applications ranging from small projects to large enterprises. The UPDATE statement in MySQL is essential for modifying existing data in a table. It's commonly used to correct errors, update values, and make other necessary changes. Th
6 min read
MySQL UPDATE JOIN
A widely used open-source relational database management system that allows you to efficiently store, organize, and retrieve data. Developed by Oracle, My SQL is widely used for building and managing databases that handle interactive websites and applications. We'll discuss the syntax, and demonstra
6 min read