DBMS Topics
Triggers in SQL
Last Updated : 21 May, 2026
A trigger is a stored procedure that automatically executes fires in response to specific events on a table or view. Triggers are event-driven and cannot be called manual
What is a Trigger?
A trigger is a stored procedure that automatically executes (fires) in response to specific events on a table or view. Triggers are event-driven and cannot be called manually.
Trigger Components
Every trigger has these components:
| Component | Options | Description |
|---|---|---|
| Event | INSERT, UPDATE, DELETE | What DML event fires the trigger |
| Timing | BEFORE, AFTER | When the trigger fires |
| Level | FOR EACH ROW, FOR EACH STATEMENT | Row-level or statement-level |
| Table | Any base table | Which table is being monitored |
| Body | SQL / PL/SQL block | Actions to perform |
Trigger Syntax (MySQL)
CREATE TRIGGER trigger_name
{BEFORE | AFTER} {INSERT | UPDATE | DELETE}
ON table_name
FOR EACH ROW
BEGIN
-- trigger body
END;BEFORE vs. AFTER Triggers
| BEFORE Trigger | AFTER Trigger |
|---|---|
| Fires before the DML operation | Fires after the DML operation |
| Can modify NEW values (for INSERT/UPDATE) | Cannot change NEW values |
| Used for validation | Used for auditing, cascading |
| Can cancel the operation (via SIGNAL) | Operation already done |
OLD and NEW References
Inside a row-level trigger, you can access:
NEW.column— the new value being inserted/updatedOLD.column— the previous value before update/delete
| Operation | OLD | NEW |
|---|---|---|
| INSERT | Not available | New row values |
| UPDATE | Previous values | New values |
| DELETE | Previous values | Not available |
Example 1 — Audit Trigger (AFTER UPDATE)
Log every salary change to an audit table.
-- Create audit table
CREATE TABLE SalaryAudit (
audit_id INT AUTO_INCREMENT PRIMARY KEY,
emp_id INT,
old_salary DECIMAL(10, 2),
new_salary DECIMAL(10, 2),
changed_by VARCHAR(100) DEFAULT CURRENT_USER(),
changed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Create trigger
CREATE TRIGGER trg_salary_audit
AFTER UPDATE ON Employee
FOR EACH ROW
BEGIN
IF OLD.salary <> NEW.salary THEN
INSERT INTO SalaryAudit (emp_id, old_salary, new_salary)
VALUES (OLD.emp_id, OLD.salary, NEW.salary);
END IF;
END;
-- Test
UPDATE Employee SET salary = 90000 WHERE emp_id = 101;
SELECT * FROM SalaryAudit;Example 2 — BEFORE INSERT Validation
Prevent inserting employees with salary below minimum.
CREATE TRIGGER trg_validate_salary
BEFORE INSERT ON Employee
FOR EACH ROW
BEGIN
IF NEW.salary < 15000 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Salary cannot be below minimum wage of 15000';
END IF;
END;
-- Test (will throw error)
INSERT INTO Employee VALUES (110, 'Test', 10000, 1, CURRENT_DATE);
-- Error: Salary cannot be below minimum wage of 15000Example 3 — BEFORE UPDATE (Auto-Capitalize Name)
CREATE TRIGGER trg_capitalize_name
BEFORE INSERT ON Employee
FOR EACH ROW
BEGIN
SET NEW.name = CONCAT(UPPER(LEFT(NEW.name, 1)), LOWER(SUBSTRING(NEW.name, 2)));
END;Example 4 — AFTER DELETE (Log Deletions)
CREATE TABLE DeletedEmployees (
emp_id INT,
name VARCHAR(100),
deleted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TRIGGER trg_log_delete
AFTER DELETE ON Employee
FOR EACH ROW
BEGIN
INSERT INTO DeletedEmployees (emp_id, name)
VALUES (OLD.emp_id, OLD.name);
END;Example 5 — Maintaining Derived Data
Automatically update a department's employee count.
-- Trigger for INSERT
CREATE TRIGGER trg_inc_emp_count
AFTER INSERT ON Employee
FOR EACH ROW
BEGIN
UPDATE Department SET emp_count = emp_count + 1
WHERE dept_id = NEW.dept_id;
END;
-- Trigger for DELETE
CREATE TRIGGER trg_dec_emp_count
AFTER DELETE ON Employee
FOR EACH ROW
BEGIN
UPDATE Department SET emp_count = emp_count - 1
WHERE dept_id = OLD.dept_id;
END;Managing Triggers
-- View all triggers
SHOW TRIGGERS;
SHOW TRIGGERS FROM university;
-- View trigger details
SELECT * FROM INFORMATION_SCHEMA.TRIGGERS
WHERE TRIGGER_SCHEMA = 'university';
-- Drop a trigger
DROP TRIGGER IF EXISTS trg_salary_audit;
-- Disable/Enable (MySQL 8+)
-- MySQL does not support DISABLE TRIGGER natively
-- Use conditional logic inside trigger body insteadAdvantages and Disadvantages of Triggers
| Advantages | Disadvantages |
|---|---|
| Automatic enforcement of business rules | Hard to debug and trace |
| Data integrity maintained automatically | Can slow down DML operations |
| Auditing and logging without app code | Trigger chains can be complex |
| Cascading actions across tables | Not visible in application code |
| Enforces complex constraints | Can cause unexpected side effects |
Trigger Flow
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Triggers in SQL.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this DBMS topic.
Search Terms
dbms, database management system, database notes, sql, unit, triggers, triggers in sql
Related DBMS Topics