Question12
Remaining:

What are triggers in SQL?

Sample Answer

Show Answer by Default

A Trigger r is a stored procedure that is automatically executed when a specific event occurs in the database, such as an INSERT, UPDATE, or DELETE on a particular table.

Types of Triggers:

  • DML Triggers: Respond to INSERT, UPDATE, or DELETE operations.
  • DDL Triggers: Respond to CREATE, ALTER, or DROP operations.
  • Row-level or Statement-level Triggers.

Advantages of triggers:

  • Automates checks and constraints.
  • Logs changes.
  • Maintains data integrity.

Example of creating a trigger:

MySQL 8.1
CREATE TRIGGER trg_after_insert_employee
AFTER INSERT ON employees
FOR EACH ROW
BEGIN
    INSERT INTO audit_log (employee_id, action, action_time)
    VALUES (NEW.id, 'INSERT', NOW());
END;