Database Trigger
ELI5 — The Vibe Check
A database trigger is an automatic response to data changes. Insert a row? The trigger fires. Update a column? The trigger fires. It's like a motion sensor — something moves and it activates. Great for audit logs and auto-updating timestamps, but overuse makes debugging a nightmare.
Real Talk
A database trigger is a stored procedure that automatically executes in response to DML (INSERT, UPDATE, DELETE) or DDL events on a table or view. Triggers can execute BEFORE or AFTER the event, access OLD and NEW row values, and modify data or raise exceptions. Common uses include audit trails, referential integrity, computed columns, and replication.
Show Me The Code
CREATE TRIGGER audit_orders
AFTER UPDATE ON orders
FOR EACH ROW
INSERT INTO order_audit (order_id, old_status, new_status, changed_at)
VALUES (OLD.id, OLD.status, NEW.status, NOW());
When You'll Hear This
"The trigger automatically logs every status change to the audit table." / "Be careful with triggers — they add implicit behavior that's hard to trace."