Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
A Database Trigger is a named database object associated with a specific table that activates automatically when a designated Data Manipulation Language (DML) event occurs on that table.
Triggers provide event-driven automation at the storage layer, ensuring data integrity, enforcing complex business constraints, and recording audit logs regardless of which application or microservice initiates the change.
Every trigger definition requires three architectural specifications:
INSERT, UPDATE, or DELETE).BEFORE or AFTER the row modification is applied.$$ ext{Total Combinations} = 2 ext{ Timings} imes 3 ext{ Events} = mathbf{6 ext{ Standard Trigger Types}}$$
| Trigger Event Hook | Typical Architectural Purpose |
|---|---|
BEFORE INSERT | Validate input data, sanitize strings, assign default generated values. |
AFTER INSERT | Insert audit trail rows, update secondary counter/aggregation tables. |
BEFORE UPDATE | Verify permissions, validate price change thresholds, record previous values. |
AFTER UPDATE | Maintain history tables, notify downstream event queues. |
BEFORE DELETE | Check if deletion violates business rules, prevent deleting active accounts. |
AFTER DELETE | Clean up associated cache tables, archive deleted rows. |
FOR EACH ROW indicates that the trigger operates as a Row-Level Trigger: if an UPDATE statement modifies 100 rows, the trigger activates 100 times!Suppose we want to ensure all customer emails are converted to lowercase and trimmed before being saved to the database:
Now, if an application executes: INSERT INTO customers (first_name, email) VALUES ('Liam', ' LIAM@EXAMPLE.COM '); The trigger intercepts the row and permanently stores: liam@example.com.
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Stored Functions vs Stored Procedures Architecture | Utilizing OLD and NEW Pseudo-Records |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.