DELETE triggers in MySQL - Time & Space Complexity
When we use DELETE triggers in a database, we want to know how the time to run the DELETE changes as the data grows.
We ask: How does the work done by the trigger grow when more rows are deleted?
Analyze the time complexity of the following DELETE trigger.
CREATE TRIGGER before_delete_example
BEFORE DELETE ON employees
FOR EACH ROW
BEGIN
INSERT INTO audit_log(employee_id, action_time)
VALUES (OLD.id, NOW());
END;
This trigger runs before each row is deleted from the employees table and logs the deletion.
Look at what repeats when deleting multiple rows.
- Primary operation: The trigger runs once for each row deleted.
- How many times: Equal to the number of rows being deleted.
As you delete more rows, the trigger runs more times.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 trigger runs |
| 100 | 100 trigger runs |
| 1000 | 1000 trigger runs |
Pattern observation: The work grows directly with the number of rows deleted.
Time Complexity: O(n)
This means the time to complete the DELETE with trigger grows linearly with the number of rows deleted.
[X] Wrong: "The trigger runs only once no matter how many rows are deleted."
[OK] Correct: In MySQL, DELETE triggers run once per row, so more rows mean more trigger executions.
Understanding how triggers affect performance helps you write efficient database operations and explain your reasoning clearly.
"What if the trigger performed a complex query inside for each row? How would that affect the time complexity?"
Practice
Solution
Step 1: Understand what triggers do
Triggers run automatically when certain actions happen on a table, like DELETE.Step 2: Identify the purpose of DELETE triggers
DELETE triggers specifically run when rows are deleted to perform extra tasks.Final Answer:
To automatically run code when a row is deleted -> Option AQuick Check:
DELETE triggers = automatic code on row deletion [OK]
- Confusing DELETE triggers with INSERT or UPDATE triggers
- Thinking triggers create or modify tables
- Believing triggers run only manually
Solution
Step 1: Recall the correct trigger syntax
MySQL triggers use CREATE TRIGGER name BEFORE|AFTER event ON table FOR EACH ROW BEGIN ... END;Step 2: Match syntax for BEFORE DELETE
CREATE TRIGGER trg BEFORE DELETE ON table_name FOR EACH ROW BEGIN ... END; matches this exactly with BEFORE DELETE and correct structure.Final Answer:
CREATE TRIGGER trg BEFORE DELETE ON table_name FOR EACH ROW BEGIN ... END; -> Option AQuick Check:
Correct syntax = CREATE TRIGGER trg BEFORE DELETE ON table_name FOR EACH ROW BEGIN ... END; [OK]
- Using AFTER instead of BEFORE when asked
- Wrong event like UPDATE instead of DELETE
- Missing FOR EACH ROW clause
CREATE TABLE users (id INT, name VARCHAR(20)); CREATE TRIGGER log_delete AFTER DELETE ON users FOR EACH ROW BEGIN INSERT INTO deleted_log VALUES (OLD.id, OLD.name); END;
What happens when
DELETE FROM users WHERE id=1; is executed?Solution
Step 1: Understand AFTER DELETE trigger behavior
The trigger runs after a row is deleted and inserts OLD row data into deleted_log.Step 2: Analyze the DELETE command effect
Deleting id=1 removes that row and the trigger logs its data into deleted_log.Final Answer:
The row with id=1 is deleted and its data is inserted into deleted_log -> Option BQuick Check:
AFTER DELETE trigger logs deleted row [OK]
- Thinking trigger prevents deletion
- Assuming no logging happens
- Confusing OLD with NEW in DELETE triggers
CREATE TRIGGER trg BEFORE DELETE ON users BEGIN INSERT INTO deleted_log VALUES (OLD.id, OLD.name); END;
Solution
Step 1: Check trigger syntax requirements
MySQL requires FOR EACH ROW clause in row-level triggers.Step 2: Identify missing FOR EACH ROW
The trigger lacks FOR EACH ROW, causing syntax error.Final Answer:
Missing FOR EACH ROW clause -> Option DQuick Check:
FOR EACH ROW is mandatory in triggers [OK]
- Using NEW in DELETE triggers (allowed only OLD)
- Omitting FOR EACH ROW clause
- Incorrect BEGIN...END usage
orders table if the order status is 'shipped'. Which DELETE trigger logic is correct?Solution
Step 1: Understand how to prevent deletion
Use BEFORE DELETE trigger and SIGNAL to raise an error and stop deletion.Step 2: Analyze each option's logic
CREATE TRIGGER trg BEFORE DELETE ON orders FOR EACH ROW BEGIN IF OLD.status = 'shipped' THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Cannot delete shipped orders'; END IF; END; correctly uses BEFORE DELETE, checks OLD.status, and signals error to prevent deletion.Final Answer:
CREATE TRIGGER trg BEFORE DELETE ON orders FOR EACH ROW BEGIN IF OLD.status = 'shipped' THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Cannot delete shipped orders'; END IF; END; -> Option CQuick Check:
Use SIGNAL in BEFORE DELETE to block deletion [OK]
- Using AFTER DELETE to prevent deletion (too late)
- Using NEW in DELETE triggers (only OLD is valid)
- Trying to delete inside a DELETE trigger causing recursion
Ready to prove it?
Pass the test and this pattern moves unknown
