0
0
MySQLquery~20 mins

BEFORE UPDATE triggers in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BEFORE UPDATE Trigger Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of BEFORE UPDATE trigger modifying a column
Consider a MySQL table 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;
A5500
B5000
C4500
DNULL
Attempts:
2 left
💡 Hint
The trigger changes the salary before the update is applied.
📝 Syntax
intermediate
2: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;
ACREATE TRIGGER before_update_test BEFORE UPDATE ON test_table FOR EACH ROW BEGIN SET NEW.value = OLD.value + 1; END;;
BCREATE TRIGGER before_update_test BEFORE UPDATE ON test_table FOR EACH ROW BEGIN SET NEW.value = OLD.value + 1; END
CCREATE TRIGGER before_update_test BEFORE UPDATE ON test_table FOR EACH ROW BEGIN SET NEW.value = OLD.value + 1 END;
DCREATE 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.
optimization
advanced
2: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;
ABEGIN IF OLD.updated_at = NEW.updated_at THEN SET NEW.updated_at = NOW(); END IF; END;
BBEGIN IF OLD <> NEW THEN SET NEW.updated_at = NOW(); END IF; END;
CBEGIN IF OLD.updated_at <> NEW.updated_at THEN SET NEW.updated_at = NOW(); END IF; END;
DBEGIN IF NOT(OLD.col1 <=> NEW.col1 AND OLD.col2 <=> NEW.col2) THEN SET NEW.updated_at = NOW(); END IF; END;
Attempts:
2 left
💡 Hint
Compare all relevant columns except updated_at to detect changes.
🔧 Debug
advanced
2: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;
ABecause the trigger does not have a BEGIN...END block.
BBecause the trigger modifies NEW.status unconditionally, causing the update to fire repeatedly.
CBecause the trigger uses OLD.status instead of NEW.status.
DBecause the trigger is AFTER UPDATE, not BEFORE UPDATE.
Attempts:
2 left
💡 Hint
Think about what happens when the trigger changes a column every time an update runs.
🧠 Conceptual
expert
2: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?
AThe order of execution is not guaranteed and can vary.
BThe triggers execute in alphabetical order by trigger name.
CThe triggers execute in the order they were created.
DThe triggers execute in reverse order of creation.
Attempts:
2 left
💡 Hint
MySQL documentation does not specify a fixed order for multiple triggers of the same type.