An UPDATE trigger runs automatically when data in a table changes. OLD and NEW let you see the data before and after the change.
UPDATE trigger with OLD and NEW in SQL
CREATE TRIGGER trigger_name BEFORE|AFTER UPDATE ON table_name FOR EACH ROW BEGIN -- use OLD.column_name for old data -- use NEW.column_name for new data -- trigger logic here END;
OLD refers to the row before the update.
NEW refers to the row after the update.
CREATE TRIGGER log_update AFTER UPDATE ON employees FOR EACH ROW BEGIN INSERT INTO audit_log(employee_id, old_salary, new_salary) VALUES (OLD.id, OLD.salary, NEW.salary); END;
CREATE TRIGGER prevent_salary_drop BEFORE UPDATE ON employees FOR EACH ROW BEGIN IF NEW.salary < OLD.salary THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Salary cannot be decreased'; END IF; END;
This example creates two tables: employees and audit_log. It adds a trigger that logs salary changes after updates. Then it inserts one employee and updates their salary. Finally, it shows the audit log with the change.
CREATE TABLE employees ( id INT PRIMARY KEY, name VARCHAR(50), salary INT ); CREATE TABLE audit_log ( log_id INT AUTO_INCREMENT PRIMARY KEY, employee_id INT, old_salary INT, new_salary INT ); DELIMITER $$ CREATE TRIGGER log_salary_update AFTER UPDATE ON employees FOR EACH ROW BEGIN INSERT INTO audit_log(employee_id, old_salary, new_salary) VALUES (OLD.id, OLD.salary, NEW.salary); END$$ DELIMITER ; INSERT INTO employees VALUES (1, 'Alice', 5000); UPDATE employees SET salary = 5500 WHERE id = 1; SELECT * FROM audit_log;
Use BEFORE UPDATE if you want to change NEW values before they are saved.
Use AFTER UPDATE if you want to react after the update is done.
Not all databases support SIGNAL for errors; check your system.
UPDATE triggers run when data changes in a table.
OLD holds the data before the update; NEW holds the data after.
Use triggers to log, check, or modify data during updates.
Practice
OLD and NEW keywords represent in an UPDATE trigger?Solution
Step 1: Understand trigger timing
In anUPDATEtrigger,OLDholds the row data before the update happens.Step 2: Understand data after update
NEWholds the row data after the update is applied.Final Answer:
OLDis the data before update;NEWis the data after update -> Option DQuick Check:
OLD = before, NEW = after [OK]
- Confusing OLD and NEW roles
- Thinking both OLD and NEW hold same data
- Assuming OLD is after update
salary inside an UPDATE trigger?Solution
Step 1: Identify how to reference NEW values in triggers
Inside anUPDATEtrigger, you access new column values usingNEW.column_name.Step 2: Match syntax for column
So, the correct way issalaryNEW.salary.Final Answer:
NEW.salary -> Option AQuick Check:
Use NEW.column_name to get updated value [OK]
- Using OLD.salary to get new value
- Trying to SELECT from NEW as a table
- Incorrect UPDATE syntax inside trigger
UPDATE trigger:IF NEW.status = 'active' AND OLD.status = 'inactive' THEN
INSERT INTO log_table(action) VALUES('Activated');
END IF;What happens when a row's
status changes from 'inactive' to 'active'?Solution
Step 1: Understand the condition in the trigger
The condition checks if the new status is 'active' and the old status was 'inactive'.Step 2: Effect of condition being true
If true, it inserts a row intolog_tablewith action 'Activated'.Final Answer:
A new row with action 'Activated' is inserted intolog_table-> Option BQuick Check:
Condition true triggers insert [OK]
- Thinking OLD and NEW cannot be compared
- Assuming no insert happens on status change
- Believing update cancels automatically
CREATE TRIGGER trg_update BEFORE UPDATE ON employees
FOR EACH ROW
BEGIN
IF NEW.salary < OLD.salary THEN
SET NEW.salary = OLD.salary;
END IF;
END;But when you try to update salary to a lower value, it still changes. What is the problem?
Solution
Step 1: Check trigger syntax completeness
In many SQL systems, triggers require a delimiter change to define the body properly.Step 2: Missing delimiter causes trigger body to be incomplete
Without proper delimiters, the trigger may not compile or behave as expected.Final Answer:
The trigger syntax is missing a delimiter or terminator -> Option AQuick Check:
Triggers need delimiters to parse body correctly [OK]
- Assuming AFTER triggers can modify NEW
- Thinking NEW cannot be assigned in BEFORE triggers
- Ignoring delimiter requirements in SQL triggers
UPDATE trigger that prevents lowering a product's price. If the new price is less than the old price, the trigger should reset the price to the old value and log the attempt. Which trigger code correctly implements this?Solution
Step 1: Identify correct trigger timing and condition
Use a BEFORE UPDATE trigger to modify NEW values before update. Condition should check if NEW.price is less than OLD.price.Step 2: Correctly reset price and log attempt
If condition true, set NEW.price = OLD.price to prevent lowering, then insert a log record with OLD and attempted NEW price.Final Answer:
BEFORE UPDATE trigger that prevents lowering price and logs the attempt -> Option CQuick Check:
BEFORE UPDATE + NEW.price < OLD.price + reset + log [OK]
- Using AFTER UPDATE and trying to modify NEW
- Reversing price comparison logic
- Not logging the attempted price change
Ready to prove it?
Pass the test and this pattern moves unknown
