0
0
SQLquery~10 mins

DELETE trigger in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - DELETE trigger
DELETE command issued
Trigger activated BEFORE or AFTER DELETE
Trigger code executes
DELETE operation completes
Transaction commits or rolls back
When a DELETE command runs, the DELETE trigger activates before or after the row is removed, running its code to react or enforce rules.
Execution Sample
SQL
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 each deleted employee before the row is removed.
Execution Table
StepActionTrigger EventTrigger Code ExecutionResult
1DELETE FROM employees WHERE id=3;BEFORE DELETEInsert OLD.id=3 into audit_logRow with id=3 marked for deletion
2Trigger code runsBEFORE DELETEAudit log updated with id=3Audit log has new entry for deleted id=3
3Row deleted from employeesDELETE operationNo trigger codeRow with id=3 removed from employees
4Transaction commitsPost DELETENo trigger codeDeletion finalized
💡 DELETE completes after trigger code runs and row is removed
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
OLD.idN/A333N/A (row deleted)
audit_log entriesemptyemptycontains id=3 entrycontains id=3 entrycontains id=3 entry
employees rowscontains id=3contains id=3contains id=3id=3 removedid=3 removed
Key Moments - 3 Insights
Why can we use OLD.id in the trigger code?
Because DELETE triggers have access to the OLD row data before it is deleted, as shown in execution_table step 2 where OLD.id=3 is used.
Does the trigger run before or after the row is deleted?
In this example, the trigger is BEFORE DELETE, so it runs before the row is removed, as seen in step 2 before step 3 deletes the row.
What happens if the trigger code fails?
If the trigger code fails, the DELETE operation is rolled back and the row is not deleted, ensuring data integrity.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of OLD.id during the trigger execution?
AThe new id
BNULL
C3
DNot accessible
💡 Hint
Check the 'Trigger Code Execution' column in step 2 where OLD.id=3 is used.
At which step is the employee row actually removed from the table?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Result' column to see when the row is removed.
If the trigger was AFTER DELETE instead of BEFORE DELETE, when would the audit_log be updated?
ABefore the row is deleted
BAfter the row is deleted
CAt the same time as deletion
DIt would not run
💡 Hint
Consider the trigger event timing and when the code runs relative to deletion.
Concept Snapshot
DELETE trigger syntax:
CREATE TRIGGER name BEFORE|AFTER DELETE ON table
FOR EACH ROW BEGIN ... END;

- Runs when a row is deleted
- Access OLD row data
- Can run before or after deletion
- Useful for logging or enforcing rules
Full Transcript
A DELETE trigger runs automatically when a DELETE command removes rows from a table. It can run before or after the row is deleted. The trigger code can access the OLD row data to perform actions like logging. In the example, a BEFORE DELETE trigger inserts the deleted row's id into an audit log before the row is removed. The execution steps show the DELETE command triggers the code, which updates the audit log, then the row is deleted, and finally the transaction commits. Variables like OLD.id hold the deleted row's data during trigger execution. If the trigger fails, the deletion is rolled back to keep data safe.