NEW
Also known as: Database Trigger, Event Handler
A database trigger is procedural logic automatically executed in response to specified database events on a table or view.
DESCRIPTION
Triggers fire when events such as INSERT, UPDATE, or DELETE occur, enforcing rules, auditing changes, or propagating updates. They execute server-side and can be row-level or statement-level, before or after events. While powerful for encapsulating logic close to data, triggers can obscure data flow, complicate debugging, and impact performance. Proper governance, documentation, and testing are essential. For integration, change data capture or event streaming may be preferable to complex trigger chains.
ALTERNATIVE DEFINITIONS
Wikipedia
A database trigger is procedural code that is automatically executed in response to certain events on a particular table or view.
Stresses automatic execution tied to data events.
PostgreSQL Documentation
A trigger is a specification that the database should automatically execute a function whenever a certain type of operation is performed.
Explains triggers as specifications that call functions on operations.
RELATED TERMS
Stored Procedure Constraints Event Transaction ACID Change Data Capture Replication Transaction Audit Data Rules Engine Event Sourcing Audit Logging Referential Integrity Event-Driven Architecture Database Function
COMPETING TERMS
Scheduled Job Polling Application Logic
KNOWLEDGE AREAS
Data Storage & Operations Data Engineering Software Engineering Data Architecture Data Integration & Interoperability
TARGET AUDIENCE
Data Engineer Software Developer Data Architect Application Administrator
EXTERNAL SOURCES
- Wikipedia - Database trigger
- PostgreSQL - Comprehensive trigger documentation
- MySQL - Trigger syntax and examples
- Microsoft Learn - SQL Server trigger overview
TOP QUESTIONS
When should I use a Trigger instead of application logic?
Use triggers to enforce critical data integrity, auditing, or cross-table invariants close to the data. Avoid business workflows better handled by services or event streams.
Category: Opinion
What risks come with Triggers?
They can hide side effects, create performance hotspots, and lead to recursion. Poorly documented triggers hamper debugging and maintenance.
Category: Risks
How do Triggers interact with transactions?
Trigger execution participates in the same transaction, preserving ACID properties. Errors typically roll back the encompassing statement or transaction.
Category: Example
What’s a common mistake when writing Triggers?
Performing complex external I/O inside triggers, causing latency and deadlocks. Keep trigger logic minimal and idempotent.
Category: Mistakes
How can Triggers support Change Data Capture?
Triggers can populate audit tables or message queues on data changes. However, log-based CDC is often more performant and less intrusive.
Category: Management
What is a BEFORE vs AFTER Trigger?
BEFORE triggers run prior to modifying data, useful for validation or transformation. AFTER triggers fire post-change, ideal for logging or propagation.
Category: Example
Are Triggers portable across databases?
No. Syntax and behavior vary across vendors, hindering portability. Abstract via functions or limit usage for cross-DB compatibility.
Category: Opinion
How to test Triggers effectively?
Use unit tests on trigger functions and integration tests covering edge cases, transaction rollbacks, and concurrency scenarios.
Category: Testing
What’s an alternative to Triggers for event-driven behavior?
Consider scheduled jobs, application-layer hooks, or log-based CDC streaming into a message broker for decoupled processing.
Category: Alternatives