Trigger
A trigger is database code that executes automatically when specific events (INSERT/UPDATE/DELETE) occur, used for auditing, cascading, and constraints.
Purpose
To execute logic automatically on data changes without explicit application calls.
Mechanism
BEFORE/AFTER × row-level/statement-level triggers. When the event fires, the trigger function runs automatically. Commonly used for audit logs, cascading updates, and complex constraint checks. But implicit execution is hard to debug and may cascade unexpectedly.
Use cases
- Data-change audit logs
- Cascading updates and sync
- Complex constraint checks
- Automatic materialized-view refresh
Examples
- Input
AFTER UPDATE ON users → log changeOutputChange recorded automaticallyAudit trigger
Common errors
- Trigger cascades cause recursion and deadlocks
- Limit trigger depth and avoid cycles
Related terms
Stored Procedure
A stored procedure is a precompiled SQL program stored in the database that can be invoked on demand, reducing network round trips and SQL-injection risk. GLOSSARY
View
A view is a stored query definition (a virtual table) holding no data, computed dynamically at query time, used to simplify queries, isolate security, and abstract. GLOSSARY
Transaction
A transaction is a group of operations executed as a single logical unit satisfying ACID properties — all or nothing. GLOSSARY