0
0
MySQLquery~10 mins

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

Choose your learning style9 modes available
Concept Flow - AFTER INSERT triggers
New row inserted into table
AFTER INSERT trigger fires
Trigger executes its statements
Trigger completes
Insert operation completes
After a new row is inserted into a table, the AFTER INSERT trigger automatically runs its defined actions before the insert operation finishes.
Execution Sample
MySQL
CREATE TRIGGER log_after_insert
AFTER INSERT ON employees
FOR EACH ROW
BEGIN
  INSERT INTO audit_log (emp_id, action) VALUES (NEW.id, 'inserted');
END;
This trigger logs each new employee inserted into the employees table by adding a record to audit_log.
Execution Table
StepActionTable StateTrigger ActionTrigger Effect
1Insert new employee row (id=101, name='Alice')employees: +1 row (id=101, name='Alice')Trigger fires AFTER INSERTPrepare to insert audit log record
2Trigger inserts audit log row (emp_id=101, action='inserted')audit_log: +1 row (emp_id=101, action='inserted')Trigger executes INSERTAudit log updated
3Trigger completesNo changeTrigger endsInsert operation completes
4Insert operation completesemployees and audit_log updatedNo triggerFinal state committed
💡 Trigger runs after each insert row; stops after executing its statements and insert completes
Variable Tracker
VariableStartAfter Step 1After Step 2Final
NEW.idundefined101101101
NEW.nameundefinedAliceAliceAlice
audit_log rows0011
employees rows0111
Key Moments - 3 Insights
Why does the trigger use NEW.id instead of just id?
The trigger uses NEW.id to refer to the inserted row's id value; this is shown in execution_table step 2 where NEW.id=101 is used to insert into audit_log.
Does the trigger run before or after the row is inserted?
The trigger runs after the row is inserted, as shown in execution_table step 1 where the employee row is added before the trigger fires.
Can the trigger modify the inserted row?
No, AFTER INSERT triggers cannot change the inserted row; they only run after insertion, shown by no changes to employees after trigger execution.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of NEW.id when the trigger inserts into audit_log?
ANULL
Bundefined
C101
D0
💡 Hint
Check execution_table row 2 under 'Trigger Action' and 'Trigger Effect' columns
At which step does the trigger finish its execution?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at execution_table row 3 where it says 'Trigger ends'
If the trigger was BEFORE INSERT instead of AFTER INSERT, when would it run?
ABefore the row is inserted
BAfter the row is inserted
CAfter the trigger completes
DIt would not run
💡 Hint
Compare the concept_flow diagram showing AFTER INSERT timing
Concept Snapshot
AFTER INSERT triggers run automatically after a new row is added.
They can perform actions like logging or updating other tables.
Use NEW.column to access inserted row values.
They cannot modify the inserted row.
Trigger runs once per inserted row after insertion completes.
Full Transcript
An AFTER INSERT trigger in MySQL runs automatically after a new row is inserted into a table. When you insert a row, the trigger fires and executes its statements, such as inserting a log record into another table. The trigger can access the inserted row's data using NEW.column syntax. It cannot change the inserted row itself because it runs after insertion. The insert operation completes only after the trigger finishes. This helps automate tasks like auditing or cascading actions after data is added.