0
0
MySQLquery~10 mins

BEFORE UPDATE triggers in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - BEFORE UPDATE triggers
UPDATE statement issued
BEFORE UPDATE trigger fires
Trigger can modify NEW values
Row updated with possibly modified NEW values
AFTER UPDATE trigger fires (if exists)
UPDATE completes
When an UPDATE runs, the BEFORE UPDATE trigger runs first, letting you change new values before the row is saved.
Execution Sample
MySQL
CREATE TRIGGER before_update_example
BEFORE UPDATE ON employees
FOR EACH ROW
BEGIN
  SET NEW.salary = NEW.salary * 1.1;
END;
This trigger increases the salary by 10% before the update is saved.
Execution Table
StepActionOLD.salaryNEW.salaryTrigger EffectRow Update
1UPDATE employees SET salary = 5000 WHERE id = 1;50005000Trigger fires before updateNo change yet
2BEFORE UPDATE trigger runs50005000NEW.salary multiplied by 1.1NEW.salary = 5500
3Row updated with NEW.salary50005500Salary updated to 5500Row saved with salary=5500
4AFTER UPDATE trigger (if any) runs50005500No change in this exampleUpdate completes
💡 Update completes after BEFORE UPDATE trigger modifies NEW values
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
OLD.salary50005000500050005000
NEW.salary50005000550055005500
Key Moments - 3 Insights
Why does NEW.salary change but OLD.salary stays the same?
OLD.salary holds the original value before update; NEW.salary is what will be saved. The BEFORE UPDATE trigger can change NEW.salary but cannot change OLD.salary. See execution_table rows 2 and 3.
When exactly does the BEFORE UPDATE trigger run?
It runs after the UPDATE statement starts but before the row is saved, allowing changes to NEW values. This is shown in execution_table row 2.
Can the BEFORE UPDATE trigger cancel the update?
No, it cannot cancel the update but can modify NEW values before saving. To prevent update, you would need to raise an error inside the trigger.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is NEW.salary after the BEFORE UPDATE trigger runs?
A6000
B5000
C5500
D4500
💡 Hint
Check execution_table row 2 under NEW.salary and Trigger Effect columns.
At which step does the actual row update with the new salary happen?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at execution_table row 3 where Row Update column shows the row saved.
If the BEFORE UPDATE trigger did not exist, what would NEW.salary be at step 3?
A5000
B5500
COriginal salary
DNULL
💡 Hint
Without trigger, NEW.salary equals the value set by UPDATE statement, see variable_tracker NEW.salary start and after step 1.
Concept Snapshot
BEFORE UPDATE triggers run before a row is updated.
They can change NEW column values before saving.
OLD values remain unchanged.
Useful to enforce rules or modify data automatically.
Trigger syntax: CREATE TRIGGER name BEFORE UPDATE ON table FOR EACH ROW.
Full Transcript
When you run an UPDATE in MySQL, the BEFORE UPDATE trigger runs first. It lets you change the new values before the database saves them. For example, if you want to increase a salary by 10% automatically, you can write a BEFORE UPDATE trigger that multiplies NEW.salary by 1.1. The OLD values stay the same, so you can compare old and new if needed. The update finishes by saving the possibly changed NEW values. This helps keep data consistent or apply rules automatically.