What Is a Database Trigger?
A trigger is a stored procedure that automatically executes in response to a specific event — like an insert, update, or delete — on a given table.
Published November 19, 2026
A database trigger is code that runs automatically when a row is inserted, updated or deleted. It is used for audit logs, validation and keeping derived data in sync.
The concept
Triggers fire BEFORE or AFTER an event and can access the old and new row values (OLD and NEW).
They are invisible to application code, which makes behaviour surprising and debugging hard. Use sparingly.
Automatic reaction
Practical examples
MySQL audit trigger
CREATE TRIGGER users_audit AFTER UPDATE ON users
FOR EACH ROW
INSERT INTO audit_log (user_id, old_email, new_email, changed_at)
VALUES (OLD.id, OLD.email, NEW.email, NOW());BEFORE trigger validation
CREATE TRIGGER chk BEFORE INSERT ON orders FOR EACH ROW
BEGIN IF NEW.total < 0 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'negative total'; END IF; END;List and drop
SHOW TRIGGERS;
DROP TRIGGER users_audit;Common mistakes
- Hidden side effects confusing developers.
- Cascading triggers that loop.
- Heavy logic slowing every write.
Key takeaways
- Automatic code on data changes.
- OLD and NEW row access.
- Use sparingly and document.
FAQ
Trigger vs application code?
Triggers guarantee the rule for all clients; app code is easier to test.
Do triggers affect performance?
Yes, they add work to each affected statement.