Challenge - 5 Problems
BEFORE UPDATE Trigger Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of BEFORE UPDATE trigger modifying a column
Consider a MySQL table
What will be the
employees with columns id, name, and salary. A BEFORE UPDATE trigger is set to increase the salary by 10% before any update.What will be the
salary of employee with id=1 after running this update?UPDATE employees SET salary = 5000 WHERE id = 1;MySQL
CREATE TRIGGER before_salary_update BEFORE UPDATE ON employees FOR EACH ROW BEGIN SET NEW.salary = NEW.salary * 1.10; END;
Attempts:
2 left
💡 Hint
The trigger changes the salary before the update is applied.
✗ Incorrect
The BEFORE UPDATE trigger multiplies the new salary by 1.10, so 5000 becomes 5500 before saving.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in BEFORE UPDATE trigger
Which option contains a syntax error in this BEFORE UPDATE trigger definition for MySQL?
MySQL
CREATE TRIGGER before_update_test BEFORE UPDATE ON test_table FOR EACH ROW BEGIN SET NEW.value = OLD.value + 1; END;
Attempts:
2 left
💡 Hint
Check for missing semicolons inside the BEGIN...END block.
✗ Incorrect
Option C misses the semicolon after the SET statement, causing a syntax error.
❓ optimization
advanced2:30remaining
Optimizing BEFORE UPDATE trigger to avoid unnecessary updates
You have a BEFORE UPDATE trigger that sets
NEW.updated_at = NOW() on every update. Which option optimizes the trigger to update updated_at only if some column other than updated_at changes?MySQL
CREATE TRIGGER before_update_timestamp BEFORE UPDATE ON records FOR EACH ROW BEGIN SET NEW.updated_at = NOW(); END;
Attempts:
2 left
💡 Hint
Compare all relevant columns except updated_at to detect changes.
✗ Incorrect
Option D compares specific columns (col1, col2) using NULL-safe equality to detect changes and update timestamp only then.
🔧 Debug
advanced2:00remaining
Why does this BEFORE UPDATE trigger cause an infinite loop?
Given this trigger, why does updating the
status column cause an infinite loop?CREATE TRIGGER before_update_status BEFORE UPDATE ON orders FOR EACH ROW BEGIN SET NEW.status = 'processed'; END;Attempts:
2 left
💡 Hint
Think about what happens when the trigger changes a column every time an update runs.
✗ Incorrect
The trigger changes NEW.status every update, so MySQL sees a change and re-triggers the update, causing a loop.
🧠 Conceptual
expert2:00remaining
Understanding the order of execution in multiple BEFORE UPDATE triggers
If a MySQL table has two BEFORE UPDATE triggers defined, what is the guaranteed order of their execution?
Attempts:
2 left
💡 Hint
MySQL documentation does not specify a fixed order for multiple triggers of the same type.
✗ Incorrect
MySQL does not guarantee the order of execution for multiple triggers of the same timing and event on a table.