Assignment No 7
Database Triggers in MySQL
What is a Trigger?
A trigger is a stored program that is automatically executed or fired when certain events
occur on a particular table.
Triggers help enforce business rules, maintain audit logs, and validate or transform data
during INSERT, UPDATE, or DELETE operations.
Types of Triggers in MySQL
In MySQL, triggers are classified based on:
1. Timing
- BEFORE Triggers: Execute before the triggering event occurs.
- AFTER Triggers: Execute after the triggering event has occurred.
2. Event
- Trigger events can be: INSERT, UPDATE, DELETE
3. Level
- MySQL only supports Row-Level triggers.
- Statement-Level triggers are not supported in MySQL (unlike Oracle or PostgreSQL).
Row-Level Triggers
MySQL triggers always operate per row. That means for each row affected by a DML
operation (INSERT, UPDATE, DELETE),
the trigger is fired once.
Syntax:
CREATE TRIGGER trigger_name
{BEFORE | AFTER} {INSERT | UPDATE | DELETE}
ON table_name
FOR EACH ROW
BEGIN
-- Trigger logic
END;
Statement-Level Triggers
MySQL does not support Statement-Level triggers. However, you can simulate them using
procedures or logging outside triggers.
- Row-Level: One trigger call per row affected.
- Statement-Level (Not in MySQL): One trigger call per SQL statement, regardless of affected
rows.
BEFORE vs AFTER Triggers in MySQL
BEFORE Triggers: Used to validate or modify values before they are written to the table.
AFTER Triggers: Used when you need to take action after the changes are committed to the
table.
Trigger Timing Comparison
Trigger Type Description
BEFORE Used to validate or modify values before
writing to table.
AFTER Used to take action after changes are
committed.
Tables for Example
CREATE TABLE employees (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50),
salary DECIMAL(10,2)
);
CREATE TABLE audit_log (
id INT AUTO_INCREMENT PRIMARY KEY,
action_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
action_type VARCHAR(20),
employee_id INT,
old_salary DECIMAL(10,2),
new_salary DECIMAL(10,2)
);
BEFORE INSERT Trigger
CREATE TRIGGER before_insert_employee
BEFORE INSERT ON employees
FOR EACH ROW
BEGIN
IF NEW.salary < 0 THEN
SET NEW.salary = 0;
END IF;
END;
AFTER INSERT Trigger
CREATE TRIGGER after_insert_employee
AFTER INSERT ON employees
FOR EACH ROW
BEGIN
INSERT INTO audit_log(action_type, employee_id, new_salary)
VALUES ('INSERT', NEW.id, NEW.salary);
END;
BEFORE UPDATE Trigger
CREATE TRIGGER before_update_employee
BEFORE UPDATE ON employees
FOR EACH ROW
BEGIN
IF NEW.salary < 0 THEN
SET NEW.salary = OLD.salary;
END IF;
END;
AFTER UPDATE Trigger
CREATE TRIGGER after_update_employee
AFTER UPDATE ON employees
FOR EACH ROW
BEGIN
INSERT INTO audit_log(action_type, employee_id, old_salary,
new_salary)
VALUES ('UPDATE', OLD.id, OLD.salary, NEW.salary);
END;
AFTER DELETE Trigger
CREATE TRIGGER after_delete_employee
AFTER DELETE ON employees
FOR EACH ROW
BEGIN
INSERT INTO audit_log(action_type, employee_id, old_salary)
VALUES ('DELETE', OLD.id, OLD.salary);
END;
Important Points
1. MySQL does not support nested triggers.
2. You cannot call COMMIT or ROLLBACK inside a trigger.
3. Use OLD.column_name to refer to data before the change.
4. Use NEW.column_name to refer to data after the change.
Summary Table of Trigger Types in MySQL
Trigger Type Timing Supported in MySQL Use Case
BEFORE INSERT Before Yes Validation or default
values
AFTER INSERT After Yes Logging or
notifications
BEFORE UPDATE Before Yes Data validation
before update
AFTER UPDATE After Yes Auditing changes
BEFORE DELETE Before Yes Restrict delete or
log intention
AFTER DELETE After Yes Logging or archiving
Statement-Level - No Use procedures or
logs instead
Conclusion
Triggers in MySQL are a powerful tool for automating checks, validations, logging, and
business rule enforcement.
MySQL supports only row-level triggers, but these can still be extremely useful in ensuring
data integrity and process control.