DELETE trigger in SQL - Time & Space Complexity
When a DELETE trigger runs, it reacts to rows being removed from a table. We want to understand how the time it takes grows as more rows are deleted.
How does the work inside the trigger scale with the number of deleted rows?
Analyze the time complexity of the following DELETE trigger.
CREATE TRIGGER trg_after_delete
AFTER DELETE ON Orders
FOR EACH ROW
BEGIN
INSERT INTO AuditLog(OrderID, DeletedAt)
VALUES (OLD.OrderID, NOW());
END;
This trigger runs after each row is deleted from the Orders table. It logs the deleted order's ID and time into an AuditLog table.
Look for repeated actions inside the trigger when multiple rows are deleted.
- Primary operation: The trigger runs once for each deleted row.
- How many times: Equal to the number of rows deleted in the DELETE statement.
As more rows are deleted, the trigger runs more times, doing the same insert each time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 inserts into AuditLog |
| 100 | 100 inserts into AuditLog |
| 1000 | 1000 inserts into AuditLog |
Pattern observation: The work grows directly with the number of deleted rows.
Time Complexity: O(n)
This means the time to complete the trigger grows linearly with how many rows are deleted.
[X] Wrong: "The trigger runs only once no matter how many rows are deleted."
[OK] Correct: The trigger runs once for each deleted row, so more rows mean more trigger executions.
Understanding how triggers scale helps you design efficient database actions and avoid surprises in performance when data changes.
"What if the trigger was defined FOR EACH STATEMENT instead of FOR EACH ROW? How would the time complexity change?"
Practice
DELETE trigger in a database?Solution
Step 1: Understand what triggers do
Triggers run automatically in response to certain database events like insert, update, or delete.Step 2: Focus on DELETE triggers
A DELETE trigger specifically runs when rows are deleted, allowing automatic actions during that event.Final Answer:
To automatically run code when rows are deleted from a table -> Option CQuick Check:
DELETE trigger = runs on row deletion [OK]
- Confusing DELETE triggers with INSERT triggers
- Thinking DELETE triggers prevent deletion by default
- Assuming DELETE triggers update rows instead
Employees?Solution
Step 1: Identify correct trigger timing syntax
The syntax for a BEFORE DELETE trigger includes 'BEFORE DELETE ON TableName'.Step 2: Check full syntax correctness
CREATE TRIGGER before_delete_employee BEFORE DELETE ON Employees FOR EACH ROW BEGIN --code END; correctly uses 'CREATE TRIGGER name BEFORE DELETE ON Employees FOR EACH ROW BEGIN ... END;'. Others misuse order or keywords.Final Answer:
CREATE TRIGGER before_delete_employee BEFORE DELETE ON Employees FOR EACH ROW BEGIN --code END; -> Option DQuick Check:
BEFORE DELETE ON Table = correct syntax [OK]
- Placing BEFORE DELETE after ON table
- Omitting FOR EACH ROW clause
- Using DELETE ON instead of BEFORE DELETE ON
id = 2 from Orders?
CREATE TRIGGER log_delete AFTER DELETE ON Orders FOR EACH ROW BEGIN INSERT INTO DeletedOrdersLog(order_id, deleted_at) VALUES (OLD.id, CURRENT_TIMESTAMP); END; -- Initial Orders table: -- id | product -- 1 | Book -- 2 | Pen -- 3 | Notebook -- DeletedOrdersLog is initially empty.
Solution
Step 1: Understand AFTER DELETE trigger behavior
The trigger runs after a row is deleted from Orders, so OLD.id refers to the deleted row's id.Step 2: Analyze trigger action
The trigger inserts a new row into DeletedOrdersLog with order_id = OLD.id (2) and current timestamp.Final Answer:
A new row with order_id = 2 is inserted into DeletedOrdersLog -> Option AQuick Check:
AFTER DELETE inserts log row with OLD.id [OK]
- Thinking OLD.id is not accessible in DELETE triggers
- Assuming trigger restores deleted rows
- Believing no action happens after delete
Users table if the user is an admin:
CREATE TRIGGER prevent_admin_delete BEFORE DELETE ON Users
FOR EACH ROW
BEGIN
IF OLD.role = 'admin' THEN
SIGNAL SQLSTATE '45000' MESSAGE_TEXT = 'Cannot delete admin user';
END IF;
END;
What is the main issue if this trigger does not prevent admin deletions?Solution
Step 1: Check trigger timing and row context
BEFORE DELETE and FOR EACH ROW are correctly used to check OLD.role before deletion.Step 2: Verify SIGNAL syntax
In standard SQL, SIGNAL syntax requires correct SQLSTATE and message format. If syntax is wrong, error won't raise and deletion proceeds.Final Answer:
The SIGNAL statement syntax is incorrect -> Option AQuick Check:
SIGNAL syntax error prevents trigger from blocking delete [OK]
- Using AFTER DELETE instead of BEFORE DELETE
- Checking NEW instead of OLD in DELETE triggers
- Omitting FOR EACH ROW clause
Products table that archives deleted rows into an ArchivedProducts table, but only if the product price is above 100. Which trigger definition correctly implements this logic?Solution
Step 1: Choose correct timing and row reference
AFTER DELETE triggers run after deletion; OLD refers to deleted row data. BEFORE DELETE can also work but OLD is preferred after deletion.Step 2: Check condition and insert logic
Condition must check OLD.price > 100. Insert must use OLD column values to archive deleted row data.Step 3: Validate options
CREATE TRIGGER archive_expensive_product AFTER DELETE ON Products FOR EACH ROW BEGIN IF OLD.price > 100 THEN INSERT INTO ArchivedProducts (id, price) VALUES (OLD.id, OLD.price); END IF; END; uses AFTER DELETE, OLD.price, and inserts OLD.id, OLD.price correctly. Others use NEW or wrong timing.Final Answer:
CREATE TRIGGER archive_expensive_product AFTER DELETE ON Products FOR EACH ROW BEGIN IF OLD.price > 100 THEN INSERT INTO ArchivedProducts (id, price) VALUES (OLD.id, OLD.price); END IF; END; -> Option BQuick Check:
AFTER DELETE + OLD.price condition + insert OLD columns = correct [OK]
- Using NEW instead of OLD in DELETE triggers
- Placing condition on NEW.price which is invalid
- Using BEFORE DELETE but referencing OLD incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
