CSC475 ยท 2081 ยท 5 marks
What are triggers? What are the uses of triggers?
What are triggers? What are the uses of triggers? [5]
Quick answer
A trigger is a special type of stored procedure (or rule) in a database that is automatically executed (fired) in response to certain events occurring on a specified table or view. Triggers are a core component of active databases, where the database itself can react to changes without requiring explicit user commands.
Triggers in Database Systems
Definition of Triggers
A trigger is defined using the general form:
Event --> Condition --> Action (ECA Model)
- Event: An operation that fires the trigger (INSERT, UPDATE, DELETE)
- Condition: An optional check that must be satisfied
- Action: The procedure that executes when the trigger fires
Uses of Triggers
1. Enforcing Business Rules
Triggers automatically enforce complex business rules that cannot be handled by simple constraints. For example, ensuring that a bank account balance never goes below zero after a withdrawal.
2. Fraud Detection and Security Monitoring
In a banking system, triggers can monitor customer transactions and detect potential fraud activities, such as:
- A high-value transaction exceeding a threshold
- A series of unusual transactions from the same account at different locations within a short period
- The trigger fires automatically and can alert the system or block the transaction.
3. Maintaining Data Integrity
Triggers ensure referential integrity and consistency across related tables, especially in situations where foreign key constraints alone are insufficient.
4. Automatic Auditing and Logging
Triggers can automatically record changes (INSERT, UPDATE, DELETE) made to sensitive tables into an audit log table, keeping a history of all modifications for accountability.
5. Derived Data Maintenance
Triggers can automatically update derived or summary data. For example, when a new sale is recorded, a trigger can automatically update the total sales figure in a summary table.
6. Replication and Synchronization
Triggers can be used to keep multiple copies of data synchronized by propagating changes from one table to related tables automatically.
Summary Table
| Use | Description |
|---|---|
| Business Rules | Enforce complex constraints automatically |
| Fraud Detection | Monitor and react to suspicious transactions |
| Data Integrity | Maintain consistency across tables |
| Auditing | Log all changes for tracking |
| Derived Data | Auto-update computed/summary values |
| Synchronization | Keep related data consistent |
Triggers make the database active rather than passive, enabling it to respond intelligently to data changes without manual intervention.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.