INSERT triggers fire when a new row is added to a table. BEFORE INSERT triggers allow you to modify or validate the incoming data before it is saved. AFTER INSERT triggers are used to take action based on the newly inserted row.
BEFORE INSERT Use Cases
- Automatically set a default value based on other columns
- Validate that required fields meet business rules
- Format data (e.g., uppercase a code field)
AFTER INSERT Use Cases
- Log the insertion to an audit table
- Update a summary or counter table
- Send a notification (via an application hook)
In a BEFORE INSERT trigger, you can modify NEW.column_name to change the value that gets stored. In an AFTER INSERT trigger, NEW is read-only and reflects what was actually stored.