UPDATE triggers have access to both OLD (the row before the change) and NEW (the row after the change). This makes them ideal for change tracking and data validation scenarios.
Comparing OLD and NEW
Inside an UPDATE trigger, you can compare OLD.column and NEW.column to detect which values changed. This is essential for fine-grained audit logging — only recording changes, not entire row snapshots.
Common UPDATE Trigger Patterns
- Automatically update an updated_at timestamp
- Prevent changes to immutable fields (e.g., created_by)
- Track field-level changes in an audit log
- Sync derived fields when source fields change
A BEFORE UPDATE trigger can modify NEW values (e.g., automatically set updated_at = NOW()), saving you from having to include this in every application UPDATE query.