Trigger in DBMS
A Trigger is a special type of stored procedure that automatically executes when a specified event occurs in a database table.
Components of Trigger
Section titled “Components of Trigger”1. Event
Section titled “1. Event”The operation that activates the trigger.
Examples:
- INSERT
- UPDATE
- DELETE
2. Condition
Section titled “2. Condition”A query/check evaluated when the trigger is activated.
If the condition is TRUE, the trigger executes.
3. Action
Section titled “3. Action”The procedure/statements executed when the condition is satisfied.
Formula
Section titled “Formula”Trigger = Event + Condition + Action
Working of Trigger
Section titled “Working of Trigger”- An event occurs on a table
- Condition is checked
- If condition is TRUE → action executes automatically
Basic Syntax
Section titled “Basic Syntax”CREATE TRIGGER trigger_nameBEFORE | AFTER INSERT | UPDATE | DELETEON table_nameFOR EACH ROWWHEN (condition)BEGIN action;END;Example
Section titled “Example”CREATE TRIGGER check_salaryBEFORE INSERTON employeeFOR EACH ROWWHEN (NEW.salary < 10000)BEGIN UPDATE employee SET salary = 10000;END;Types of Triggers
Section titled “Types of Triggers”Based on Timing
Section titled “Based on Timing”- BEFORE Trigger
- AFTER Trigger
Based on Level
Section titled “Based on Level”- Row Level Trigger
- Statement Level Trigger
Uses of Trigger
Section titled “Uses of Trigger”- Automatic validation
- Auditing/logging
- Maintaining integrity
- Security enforcement
- Automatic updates