What is a Trigger?
A trigger is a special kind of stored operation that is automatically executed in response to specific changes (like INSERT, UPDATE, or DELETE) on a table.
Triggers help enforce rules, log changes, validate data, and perform automatic updates without needing manual action from a user or application.
Types of Triggers:
In MySQL, triggers are defined based on two factors:
- Timing: When it runs (BEFORE or AFTER)
- Event: What triggers it (INSERT, UPDATE, DELETE)
So, there are six types of triggers:
- BEFORE INSERT
- AFTER INSERT
- BEFORE UPDATE
- AFTER UPDATE
- BEFORE DELETE
- AFTER DELETE
Limitations of Triggers:
- A table can have only one trigger per timing and event (e.g., only one BEFORE INSERT).
- Triggers can’t call other triggers or start/commit transactions.
- No nested triggers (one trigger can’t fire another).
Too many triggers can affect performance.