BEFORE UPDATE triggers in MySQL - Time & Space Complexity
When using BEFORE UPDATE triggers in MySQL, it's important to understand how the time to run the trigger changes as the number of rows updated grows.
We want to know how the work done by the trigger scales when many rows are updated.
Analyze the time complexity of the following trigger code.
CREATE TRIGGER before_update_example
BEFORE UPDATE ON employees
FOR EACH ROW
BEGIN
IF NEW.salary < OLD.salary THEN
SET NEW.salary = OLD.salary;
END IF;
END;
This trigger checks if the new salary is less than the old salary before an update, and if so, it resets the new salary to the old one.
Look at what repeats when the trigger runs.
- Primary operation: The trigger runs once for each row being updated.
- How many times: Equal to the number of rows affected by the UPDATE statement.
As you update more rows, the trigger runs more times.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 trigger executions |
| 100 | 100 trigger executions |
| 1000 | 1000 trigger executions |
Pattern observation: The work grows directly with the number of rows updated.
Time Complexity: O(n)
This means the time to complete the update grows in a straight line with the number of rows affected.
[X] Wrong: "The trigger runs only once no matter how many rows are updated."
[OK] Correct: The trigger runs separately for each row, so more rows mean more trigger executions.
Understanding how triggers scale helps you write efficient database code and shows you know how database internals work, a useful skill in many jobs.
"What if the trigger contained a query that itself scanned the whole table? How would that affect the time complexity?"
Practice
BEFORE UPDATE trigger in MySQL?Solution
Step 1: Understand trigger timing
BEFORE UPDATEtriggers run before the data is changed in the table.Step 2: Purpose of the trigger
This timing allows checking or modifying the new data before it is saved.Final Answer:
To execute code before a row is updated, allowing data validation or modification. -> Option AQuick Check:
BEFORE UPDATE triggers run before update [OK]
- Confusing BEFORE UPDATE with AFTER UPDATE triggers
- Thinking triggers prevent updates entirely
- Assuming triggers create backups automatically
BEFORE UPDATE trigger on a table named employees?Solution
Step 1: Check trigger timing and table
The syntax requiresBEFORE UPDATE ON employeesto specify timing and table.Step 2: Confirm trigger body and FOR EACH ROW
The trigger must includeFOR EACH ROWand a BEGIN...END block for multiple statements.Final Answer:
CREATE TRIGGER before_update_employees BEFORE UPDATE ON employees FOR EACH ROW BEGIN SET NEW.salary = NEW.salary * 1.1; END; -> Option DQuick Check:
Correct syntax includes BEFORE UPDATE ON and FOR EACH ROW [OK]
- Using AFTER UPDATE instead of BEFORE UPDATE
- Omitting ON keyword before table name
- Missing FOR EACH ROW clause
CREATE TABLE products (id INT, price DECIMAL(5,2)); CREATE TRIGGER price_check BEFORE UPDATE ON products FOR EACH ROW BEGIN IF NEW.price < 0 THEN SET NEW.price = 0; END IF; END;
What happens if you run:
UPDATE products SET price = -10 WHERE id = 1;
Solution
Step 1: Understand trigger logic
The trigger checks if the new price is less than 0, then sets it to 0.Step 2: Apply update and trigger effect
Updating price to -10 triggers the BEFORE UPDATE trigger, which changes it to 0 before saving.Final Answer:
The price is set to 0 because the trigger changes negative prices to zero. -> Option CQuick Check:
BEFORE UPDATE can modify NEW values [OK]
- Thinking triggers run after update
- Assuming negative price is saved as is
- Expecting syntax error from trigger
CREATE TRIGGER trg BEFORE UPDATE ON orders FOR EACH ROW BEGIN SET NEW.status = 'processed'; END;
But when updating rows, the status does not change. What is the likely problem?
Solution
Step 1: Check trigger syntax requirements
MySQL requires changing the delimiter when creating triggers with BEGIN...END blocks.Step 2: Understand effect of missing delimiter
Without changing delimiter, the trigger creation fails silently or is incomplete, so it doesn't run.Final Answer:
The trigger syntax is missing a delimiter change, causing a syntax error. -> Option AQuick Check:
Use DELIMITER to create triggers with BEGIN...END [OK]
- Not changing delimiter before CREATE TRIGGER
- Thinking NEW cannot be assigned in BEFORE UPDATE
- Assuming AFTER UPDATE is needed to modify NEW
account_balance in the users table. Which BEFORE UPDATE trigger code correctly enforces this rule?Solution
Step 1: Understand the requirement
The trigger must prevent lowering the account_balance by resetting it to the old value if decreased.Step 2: Analyze options
CREATE TRIGGER check_balance BEFORE UPDATE ON users FOR EACH ROW BEGIN IF NEW.account_balance < OLD.account_balance THEN SET NEW.account_balance = OLD.account_balance; END IF; END; resets NEW.account_balance to OLD.account_balance if it is less, correctly preventing decrease.Step 3: Check other options
The code that signals an error prevents increases instead of decreases. AFTER UPDATE triggers cannot modify NEW values. The code that checks if balance is negative only prevents negative values but not decreases to lower positive values.Final Answer:
CREATE TRIGGER check_balance BEFORE UPDATE ON users FOR EACH ROW BEGIN IF NEW.account_balance < OLD.account_balance THEN SET NEW.account_balance = OLD.account_balance; END IF; END; -> Option BQuick Check:
Use BEFORE UPDATE and compare NEW vs OLD to prevent decrease [OK]
- Using AFTER UPDATE to modify NEW values
- Signaling error for wrong condition
- Only checking for negative values, not decreases
Ready to prove it?
Pass the test and this pattern moves unknown
