Skip to content

Trigger in DBMS

A Trigger is a special type of stored procedure that automatically executes when a specified event occurs in a database table.

The operation that activates the trigger.

Examples:

  • INSERT
  • UPDATE
  • DELETE

A query/check evaluated when the trigger is activated.

If the condition is TRUE, the trigger executes.

The procedure/statements executed when the condition is satisfied.

Trigger = Event + Condition + Action

  1. An event occurs on a table
  2. Condition is checked
  3. If condition is TRUE → action executes automatically
CREATE TRIGGER trigger_name
BEFORE | AFTER INSERT | UPDATE | DELETE
ON table_name
FOR EACH ROW
WHEN (condition)
BEGIN
action;
END;
CREATE TRIGGER check_salary
BEFORE INSERT
ON employee
FOR EACH ROW
WHEN (NEW.salary < 10000)
BEGIN
UPDATE employee
SET salary = 10000;
END;
  • BEFORE Trigger
  • AFTER Trigger
  • Row Level Trigger
  • Statement Level Trigger
  • Automatic validation
  • Auditing/logging
  • Maintaining integrity
  • Security enforcement
  • Automatic updates