SQLIntermediate#triggers

What is a trigger?

A block of SQL that runs automatically BEFORE or AFTER an INSERT, UPDATE or DELETE on a table. Useful for auditing and derived columns, but heavy triggers hide logic and hurt performance.

Example
CREATE TRIGGER audit_salary AFTER UPDATE ON employees
FOR EACH ROW
INSERT INTO salary_audit(emp_id, old_sal, new_sal, changed_at)
VALUES (OLD.id, OLD.salary, NEW.salary, NOW());

Related Questions

1
SQLBeginner#aggregates#nulls

What are aggregate functions and how do NULLs affect them?

Open
2
SQLBeginner#nulls

How do you handle NULL values in SQL?

Open
3
SQLBeginner#functions

What is the difference between COALESCE and CASE?

Open