Horje
MySQL AFTER UPDATE Trigger

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.

FAQs on MySQL AFTER UPDATE Trigger

Can AFTER UPDATE triggers be used with other types of triggers?

Yes, AFTER UPDATE triggers can be used the alongside BEFORE UPDATE, BEFORE INSERT, AFTER INSERT and other triggers in the same database.

How do OLD and NEW values work in AFTER UPDATE triggers?

The OLD refers to the row’s values before the update while NEW refers to the row’s values after the update.

Can an AFTER UPDATE trigger update the same table it is defined on?

Yes, an AFTER UPDATE trigger can update the same table but be cautious of the potential recursive triggers and performance implications.

How can I manage performance issues related to triggers?

The Monitor the performance of the triggers keep them simple and avoid complex operations within the trigger body.




Reffered: https://www.geeksforgeeks.org


Databases

Related
MySQL SQL Injection MySQL SQL Injection
MySQL Indexes MySQL Indexes
How to Select Data from two Different Servers in MySQL? How to Select Data from two Different Servers in MySQL?
MySQL Group By Clause MySQL Group By Clause
Migrating Data from SQL Server to MariaDB Migrating Data from SQL Server to MariaDB

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
21