Full answer
sql
CREATE TABLE departments (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
dept_id INTEGER REFERENCES departments(id),
manager_id INTEGER REFERENCES employees(id),
salary INTEGER,
hire_date TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
(1,'Asha', 1,NULL,120000,'2019-03-01'),
(2,'Ravi', 1,1, 95000,'2020-07-15'),
(3,'Mei', 1,1, 95000,'2021-01-10'),
(4,'Tom', 2,1, 70000,'2022-05-20'),
(5,'Sara', 2,4, 62000,'2023-02-01'),
(6,'Diego',3,1, 55000,'2021-11-30'),
(7,'Lena', NULL,4, NULL, '2024-06-03'),
(8,'Omar', 3,6, 48000,'2024-09-09');sql
CREATE TABLE salary_audit (emp_id INTEGER, old_salary INTEGER, new_salary INTEGER, changed_at TEXT DEFAULT CURRENT_TIMESTAMP);
CREATE TRIGGER trg_salary_audit AFTER UPDATE OF salary ON employees
WHEN OLD.salary IS NOT NEW.salary
BEGIN
INSERT INTO salary_audit (emp_id, old_salary, new_salary) VALUES (OLD.id, OLD.salary, NEW.salary);
END;
UPDATE employees SET salary = 100000 WHERE id = 2;
UPDATE employees SET salary = 120000 WHERE id = 1;
SELECT emp_id, old_salary, new_salary FROM salary_audit;| emp_id | old_salary | new_salary |
|---|---|---|
| 2 | 95000 | 100000 |
The trigger fired only for Ravi's row. OLD and NEW refer to the row before and after the update. The WHEN clause skips updates that do not change the value, including Asha's, which set the salary to what it already was.
IS NOT is SQLite's NULL-safe inequality; PostgreSQL would use IS DISTINCT FROM. In PostgreSQL, a trigger calls a function written in PL/pgSQL rather than holding the statements inline.
Triggers are invisible to anyone reading the application code, so document them, and avoid business workflows (emails, external calls) inside them.
sql
CREATE TABLE departments (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
dept_id INTEGER REFERENCES departments(id),
manager_id INTEGER REFERENCES employees(id),
salary INTEGER,
hire_date TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
(1,'Asha', 1,NULL,120000,'2019-03-01'),
(2,'Ravi', 1,1, 95000,'2020-07-15'),
(3,'Mei', 1,1, 95000,'2021-01-10'),
(4,'Tom', 2,1, 70000,'2022-05-20'),
(5,'Sara', 2,4, 62000,'2023-02-01'),
(6,'Diego',3,1, 55000,'2021-11-30'),
(7,'Lena', NULL,4, NULL, '2024-06-03'),
(8,'Omar', 3,6, 48000,'2024-09-09');
CREATE TABLE salary_audit (emp_id INTEGER, old_salary INTEGER, new_salary INTEGER, changed_at TEXT DEFAULT CURRENT_TIMESTAMP);
CREATE TRIGGER trg_salary_audit AFTER UPDATE OF salary ON employees
WHEN OLD.salary IS NOT NEW.salary
BEGIN
INSERT INTO salary_audit (emp_id, old_salary, new_salary) VALUES (OLD.id, OLD.salary, NEW.salary);
END;
UPDATE employees SET salary = 100000 WHERE id = 2;
UPDATE employees SET salary = 120000 WHERE id = 1;
SELECT emp_id, old_salary, new_salary FROM salary_audit;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- Triggers run automatically on INSERT, UPDATE or DELETE
- OLD and NEW give the row before and after
- WHEN limits when the trigger fires
- Keep triggers small and documented
Common mistakes
- Auditing with application code that some paths bypass
- Putting slow or external work inside triggers
Follow-ups
- How would you also record who made the change?
- When would a statement-level trigger suit better than a row-level one?
Tips
- Mention the WHEN clause to skip no-op updates