Last Updated : 23 Jul, 2025
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 overview of the BEFORE UPDATE trigger including its syntax, use cases, and practical examples.
Introduction to MySQL TriggersThe MySQL triggers are database objects that are automatically executed or fired when certain events occur. These events can be INSERT, UPDATE, or DELETE operations on the table. The Triggers help enforce business rules, validate data, and maintain data integrity.
Understanding BEFORE UPDATE TriggersThe BEFORE UPDATE trigger is executed before an update operation is performed on the table. This type of trigger can be used to validate or modify data before it is updated in the database. For example, we might use a BEFORE UPDATE trigger to ensure that certain business rules are enforced or to automatically update a timestamp column.
Syntax of BEFORE UPDATE TriggerThe basic syntax for creating a BEFORE UPDATE trigger in MySQL is as follows:
CREATE TRIGGER trigger_name
BEFORE UPDATE
ON table_name
FOR EACH ROW
BEGIN
-- Trigger logic here
END;
Let's create a simple example to show how to create a BEFORE UPDATE trigger. Suppose we have a table called the employees with the following structure:
CREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
position VARCHAR(100),
salary DECIMAL(10, 2),
last_modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
We want to create a BEFORE UPDATE trigger that updates the last_modified column with current timestamp whenever an employee's information is updated.
DELIMITER //CREATE TRIGGER before_employee_update
BEFORE UPDATE
ON employees
FOR EACH ROW
BEGIN
SET NEW.last_modified = CURRENT_TIMESTAMP;
END;
//DELIMITER ;
In this example:
INSERT INTO employees (name, position, salary) VALUES ('John Doe', 'Developer', 70000.00);Update Data to Trigger the BEFORE UPDATE Trigger
INSERT INTO employees (name, position, salary) VALUES ('Jane Smith', 'Manager', 80000.00);
The Update an employee's details to the activate the trigger:
UPDATE employees SET salary = 75000.00 WHERE name = 'John Doe';Check the Contents of the employees Table
Retrieve the data from the employees table to see the effect of the trigger:
SELECT * FROM employees;
Output:
Output Example: Enforcing Business RulesSuppose we have a table products with the columns id, name, price, and stock. We want to ensure that the price of the product cannot be negative. We can create a BEFORE UPDATE trigger to enforce this rule.
1. Create the products TableCREATE TABLE products (2. Create the BEFORE UPDATE Trigger
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
price DECIMAL(10, 2),
stock INT
);
DELIMITER //3. Insert Sample Data
CREATE TRIGGER before_product_update
BEFORE UPDATE
ON products
FOR EACH ROW
BEGIN
IF NEW.price < 0 THEN
SET NEW.price = 0;
END IF;
END;
//
DELIMITER ;
INSERT INTO products (name, price, stock) VALUES ('Product A', 100.00, 10);4. Update Data to Trigger the BEFORE UPDATE Trigger
INSERT INTO products (name, price, stock) VALUES ('Product B', 200.00, 20);
Attempt to update the price of 'Product A' to a negative value:
UPDATE products SET price = -50 WHERE name = 'Product A';5. Check the Results
Retrieve the data from the products table to see the effect of the trigger:
SELECT * FROM products;
Output:
Output ConclusionThe BEFORE UPDATE trigger in MySQL is a powerful tool for enforcing business rules, validating data, and maintaining data integrity. By understanding how to create and use these triggers we can enhance the functionality and reliability of the database applications. This article has provided an overview of the BEFORE UPDATE trigger its syntax and practical examples.
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