0
0
MySQLquery~10 mins

DELETE triggers in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - DELETE triggers
DELETE command issued
Check for DELETE triggers
Trigger BEFORE DELETE?
YesExecute BEFORE DELETE trigger
Continue deletion
Delete row from table
Trigger AFTER DELETE?
YesExecute AFTER DELETE trigger
Row removed from table
When a DELETE command runs, MySQL checks for any DELETE triggers. It runs BEFORE DELETE triggers first, then deletes the row, then runs AFTER DELETE triggers.
Execution Sample
MySQL
CREATE TRIGGER trg_before_delete
BEFORE DELETE ON employees
FOR EACH ROW
BEGIN
  INSERT INTO audit_log VALUES (OLD.id, 'deleted');
END;
This trigger logs the id of an employee before that employee's row is deleted.
Execution Table
StepActionTrigger TypeRow DataEffect
1DELETE issued for employee id=5Noneid=5, name='John'Start delete process
2Check for BEFORE DELETE triggerBEFORE DELETEid=5, name='John'Trigger found, execute trigger
3Execute BEFORE DELETE triggerBEFORE DELETEid=5, name='John'Insert audit log with id=5
4Delete row from employeesNoneid=5, name='John'Row removed from employees table
5Check for AFTER DELETE triggerAFTER DELETEid=5, name='John'No AFTER DELETE trigger found
6End of DELETE operationNoneNoneDelete completed
💡 Delete operation ends after executing triggers and removing the row
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
Row to deleteid=5, name='John'id=5, name='John'id=5, name='John'DeletedDeleted
Audit log entriesemptyemptycontains id=5 deletion recordcontains id=5 deletion recordcontains id=5 deletion record
Key Moments - 2 Insights
Why does the BEFORE DELETE trigger run before the row is deleted?
The BEFORE DELETE trigger runs first to allow actions like logging or validation before the actual deletion happens, as shown in execution_table step 3.
What happens if there is no AFTER DELETE trigger?
If no AFTER DELETE trigger exists, MySQL simply skips that step and finishes the delete operation, as seen in execution_table step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of the row after step 4?
ARow still exists in the table
BRow is updated but not deleted
CRow is deleted from the table
DRow is duplicated
💡 Hint
Check the 'Effect' column at step 4 in the execution_table
At which step is the audit log updated?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Look for the step where the BEFORE DELETE trigger executes in the execution_table
If an AFTER DELETE trigger was added, when would it run?
ABefore the row is deleted
BAfter the row is deleted
CAt the same time as BEFORE DELETE trigger
DOnly if the BEFORE DELETE trigger fails
💡 Hint
Refer to the concept_flow diagram and execution_table step 5
Concept Snapshot
DELETE triggers run automatically when a DELETE command affects a table.
BEFORE DELETE triggers run before the row is removed.
AFTER DELETE triggers run after the row is removed.
Use triggers to log, validate, or modify related data during deletion.
Triggers execute for each row affected by the DELETE.
If no triggers exist, DELETE simply removes the row.
Full Transcript
When you issue a DELETE command in MySQL, the database checks if there are any DELETE triggers on the table. If a BEFORE DELETE trigger exists, it runs first before the row is deleted. This allows you to do things like log the deletion or check conditions. Then the row is removed from the table. After that, if there is an AFTER DELETE trigger, it runs to perform any follow-up actions. If no triggers exist, the row is simply deleted. This process happens for each row affected by the DELETE command.