Middle
What are triggers? What types of triggers are there?
sobes.tech AI
Answer from AI
Triggers are special procedures in a database that automatically execute when certain events occur (for example, inserting, updating, or deleting data in a table).
Main types of triggers:
-
By timing:
- BEFORE (before the operation) — triggers that fire before the operation is executed.
- AFTER (after the operation) — triggers that fire after the operation is completed.
- INSTEAD OF — replaces the operation (usually used for views).
-
By operation type:
- INSERT — when new records are inserted.
- UPDATE — when existing records are updated.
- DELETE — when records are deleted.
For example, a BEFORE INSERT trigger can check the correctness of data before adding it, and an AFTER UPDATE trigger can keep a change log.
Triggers help automate business logic, ensure data integrity, and facilitate auditing without the need to change client code.