Last Updated : 23 Jul, 2025
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 INSERT trigger in MySQL is a special set of instructions that gets executed automatically before a new record is added to a table.
What is a BEFORE INSERT TriggerA BEFORE INSERT trigger in MySQL is created with the CREATE TRIGGER statement. The general syntax for creating a BEFORE INSERT trigger in MySQL is shown here:
CREATE TRIGGER trigger_name
BEFORE INSERT
ON table_name
FOR EACH ROW
BEGIN
-- SQL statements
END;
where,
Here are some examples:
Example 1: Auto-Setting a Timestamp Table Creation and TriggerCREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
position VARCHAR(100),
created_at TIMESTAMP
);CREATE TRIGGER before_employee_insert
BEFORE INSERT ON employees
FOR EACH ROW
BEGIN
SET NEW.created_at = NOW();
END;
Insert Data
INSERT INTO employees (name, position) VALUES ('John Doe', 'Manager');
Output:
SELECT * FROM employees;
id
name
position
created_at
1
John Doe
Manager
2024-08-01 12:34:56
Explanation:
Table Creation and Trigger
CREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
position VARCHAR(100),
salary DECIMAL(10, 2)
);CREATE TRIGGER validate_employee_salary
BEFORE INSERT ON employees
FOR EACH ROW
BEGIN
IF NEW.salary < 0 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Salary cannot be negative';
END IF;
END;
Insert Data
INSERT INTO employees (name, position, salary) VALUES ('Jane Smith', 'Developer', -5000.00);
Expected Output
fERROR 1644 (45000): Salary cannot be negative
Explanation:
Table Creation and Trigger
CREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
department VARCHAR(100)
);CREATE TRIGGER set_default_department
BEFORE INSERT ON employees
FOR EACH ROW
BEGIN
IF NEW.department IS NULL THEN
SET NEW.department = 'General';
END IF;
END;
Insert Data
INSERT INTO employees (name) VALUES ('Alice Johnson');
Expected Output
SELECT * FROM employees;
id
name
department
1
Alice Johnson
General
Explanation:
MySQL BEFORE INSERT triggers are much like diligent assistants who ensure everything is in order before a new record is added to your database. They automate the checks, validations, and modifications that need to be made in your incoming data to ensure the integrity and consistency of data without manual intervention. Be it setting defaults, validating data, or enforcing business rules, these triggers are very powerful tools that improve the reliability and robustness of your database operations. Accomplish core processes with reduced risks of errors and help to keep your data clean and correct using BEFORE INSERT triggers.
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