Why triggers are needed in SQL - Performance Analysis
We want to understand how the work done by database triggers grows as the data changes.
How does the time to run triggers change when more rows are affected?
Analyze the time complexity of this trigger example.
CREATE TRIGGER update_stock
AFTER INSERT ON sales
FOR EACH ROW
BEGIN
UPDATE products
SET stock = stock - NEW.quantity
WHERE product_id = NEW.product_id;
END;
This trigger updates product stock after each sale is inserted.
Look for repeated actions in the trigger.
- Primary operation: The trigger runs once for each new sale row inserted.
- How many times: It runs as many times as there are inserted rows.
More inserted rows mean more trigger executions.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 stock updates |
| 100 | 100 stock updates |
| 1000 | 1000 stock updates |
Pattern observation: The work grows directly with the number of inserted rows.
Time Complexity: O(n)
This means the time to run the trigger grows in a straight line with the number of rows inserted.
[X] Wrong: "Triggers run only once no matter how many rows are inserted."
[OK] Correct: Triggers defined FOR EACH ROW run once per row, so more rows mean more executions.
Understanding how triggers scale helps you design efficient databases and avoid slowdowns as data grows.
"What if the trigger was defined FOR EACH STATEMENT instead of FOR EACH ROW? How would the time complexity change?"
Practice
Triggers are special procedures that run automatically. What is their main purpose?Solution
Step 1: Understand what triggers do
Triggers run automatically when data changes happen in a table, like insert, update, or delete.Step 2: Identify the main purpose
The main purpose is to automate actions without manual intervention, such as enforcing rules or logging changes.Final Answer:
To automatically perform actions when data changes happen -> Option DQuick Check:
Triggers run automatically = D [OK]
- Thinking triggers are for manual updates
- Confusing triggers with creating tables
- Assuming triggers write reports
Employees?Solution
Step 1: Identify correct trigger timing and event
The question asks for a trigger after INSERT on Employees, so syntax must say AFTER INSERT ON Employees.Step 2: Check syntax structure
CREATE TRIGGER trg AFTER INSERT ON Employees BEGIN ... END; matches the correct order: CREATE TRIGGER name AFTER INSERT ON table BEGIN ... END;Final Answer:
CREATE TRIGGER trg AFTER INSERT ON Employees BEGIN ... END; -> Option AQuick Check:
Correct trigger syntax = C [OK]
- Mixing BEFORE and AFTER keywords
- Placing table name in wrong position
- Using ON before event instead of after
Orders:CREATE TRIGGER trg_log AFTER UPDATE ON Orders BEGIN INSERT INTO AuditLog VALUES('Order updated'); END;What happens when an order is updated?
Solution
Step 1: Understand trigger action on UPDATE
The trigger runs AFTER UPDATE on Orders and inserts a row into AuditLog with text 'Order updated'.Step 2: Identify the effect of the trigger
Each time an order is updated, a new row is added to AuditLog recording the event.Final Answer:
A new row with 'Order updated' is added to AuditLog -> Option BQuick Check:
Trigger inserts log row = B [OK]
- Thinking trigger cancels update
- Assuming no action happens automatically
- Confusing trigger with delete operation
CREATE TRIGGER trg_check BEFORE INSERT ON Users BEGIN IF NEW.age < 18 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Age must be 18 or older'; END IF; END;
But it does not prevent inserting users younger than 18. What is the likely error?
Solution
Step 1: Analyze trigger structure with multiple statements
The trigger has IF and SIGNAL inside BEGIN...END, which requires proper statement delimiters (like semicolons) to separate commands.Step 2: Identify missing delimiter issue
Without delimiters, the database cannot parse the trigger body correctly, so the condition check fails.Final Answer:
The trigger does not have a proper delimiter for multiple statements -> Option AQuick Check:
Missing delimiters cause trigger failure = A [OK]
- Changing BEFORE to AFTER incorrectly
- Assuming SIGNAL syntax is wrong without checking
- Believing triggers can't access NEW data
PriceHistory table automatically. Which trigger design best achieves this?Solution
Step 1: Understand when old price is available
The old price is available after the update operation starts but before the new data replaces it, so a trigger on UPDATE is needed.Step 2: Choose trigger timing to save old price
Using AFTER UPDATE ensures the update succeeded, and the trigger can insert the old price (accessible via OLD) into PriceHistory.Final Answer:
An AFTER UPDATE trigger on Products that inserts old price into PriceHistory -> Option CQuick Check:
Save old price after update = A [OK]
- Using BEFORE DELETE instead of UPDATE
- Using AFTER INSERT which doesn't track price changes
- Choosing BEFORE UPDATE which may block or cause errors
Ready to prove it?
Pass the test and this pattern moves unknown
