0
0
MySQLquery~10 mins

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

Choose your learning style9 modes available
Concept Flow - AFTER UPDATE triggers
UPDATE statement executed
Row(s) updated in table
AFTER UPDATE trigger fires
Trigger code runs using updated data
Trigger completes, control returns to UPDATE
UPDATE statement finishes
When an UPDATE changes rows, the AFTER UPDATE trigger runs immediately after the change, using the new data.
Execution Sample
MySQL
CREATE TRIGGER trg_after_update
AFTER UPDATE ON employees
FOR EACH ROW
BEGIN
  INSERT INTO audit_log(emp_id, action) VALUES (NEW.id, 'updated');
END;
This trigger logs an entry into audit_log every time an employee row is updated.
Execution Table
StepActionRow Data BeforeRow Data AfterTrigger Fired?Trigger Action
1UPDATE employees SET salary = 60000 WHERE id = 1{id:1, salary:50000}{id:1, salary:60000}NoNo action yet
2Row updated in employees table{id:1, salary:50000}{id:1, salary:60000}YesTrigger inserts audit_log(emp_id=1, action='updated')
3Trigger completes{id:1, salary:60000}{id:1, salary:60000}YesAudit log entry created
4UPDATE statement finishes{id:1, salary:60000}{id:1, salary:60000}YesAll done
💡 UPDATE completes after trigger runs for each updated row
Variable Tracker
VariableStartAfter Step 1After Step 2Final
salary50000600006000060000
trigger_firedNoNoYesYes
audit_log_entries0011
Key Moments - 2 Insights
Why does the trigger see the updated data, not the old data?
Because AFTER UPDATE triggers run after the row is changed, so NEW contains the new values as shown in execution_table step 2.
Does the trigger run once per statement or once per row?
It runs once for each row updated, as indicated by 'FOR EACH ROW' and the execution_table showing trigger firing after the row update.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the salary value in the updated row?
A60000
B50000
CNULL
D55000
💡 Hint
Check the 'Row Data After' column in step 2 of execution_table.
At which step does the trigger insert a new audit_log entry?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Trigger Action' column in execution_table rows.
If the UPDATE affects multiple rows, how many times does the AFTER UPDATE trigger run?
AOnce per statement
BNever
COnce per row updated
DOnly if rows have changed
💡 Hint
Refer to key_moments about 'FOR EACH ROW' and trigger firing.
Concept Snapshot
AFTER UPDATE triggers run after a row is updated.
They see the new data in NEW.
Trigger runs once per updated row.
Use FOR EACH ROW to specify per-row execution.
Commonly used for logging or cascading changes.
Full Transcript
When you run an UPDATE statement, the database changes the row data first. Then, if there is an AFTER UPDATE trigger, it runs immediately after each row is updated. The trigger can access the new row values using NEW. This trigger runs once for every row that the UPDATE changes. For example, if you update one employee's salary, the trigger logs this change in an audit table. The execution table shows the update happening, then the trigger firing, and finally the update finishing. This helps keep track of changes automatically.