A trigger is code that fires automatically when rows are inserted, updated, or deleted. Used well, triggers enforce rules and build audit trails without touching application code; used carelessly, they hide logic where nobody looks.
Key Points
- Six types: BEFORE/AFTER × INSERT/UPDATE/DELETE
- Access changing data via NEW.column and OLD.column
- Classic use: AFTER UPDATE trigger writes old values to an audit table
- BEFORE INSERT can validate or normalise data (e.g., lowercase emails)
- One statement can fire many triggers — mind the performance cost
- Document triggers well; 'invisible' logic surprises future developers
.png)