Challenge - 5 Problems
AFTER UPDATE Trigger Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of AFTER UPDATE trigger modifying another table
Given two tables
employees and audit_log, and an AFTER UPDATE trigger on employees that inserts a record into audit_log whenever an employee's salary changes, what will be the content of audit_log after this update?UPDATE employees SET salary = salary + 1000 WHERE id = 3;MySQL
CREATE TABLE employees (id INT PRIMARY KEY, name VARCHAR(50), salary INT); CREATE TABLE audit_log (log_id INT AUTO_INCREMENT PRIMARY KEY, emp_id INT, old_salary INT, new_salary INT); DELIMITER $$ CREATE TRIGGER trg_after_update_salary AFTER UPDATE ON employees FOR EACH ROW BEGIN IF OLD.salary <> NEW.salary THEN INSERT INTO audit_log(emp_id, old_salary, new_salary) VALUES (OLD.id, OLD.salary, NEW.salary); END IF; END$$ DELIMITER ; INSERT INTO employees VALUES (3, 'Alice', 5000);
Attempts:
2 left
💡 Hint
The trigger runs after the update and compares old and new salary values.
✗ Incorrect
The AFTER UPDATE trigger checks if the salary changed. Since the salary increased from 5000 to 6000, it inserts one row recording the old and new salaries.
🧠 Conceptual
intermediate1:30remaining
Understanding WHEN AFTER UPDATE triggers fire
Which of the following statements about AFTER UPDATE triggers in MySQL is true?
Attempts:
2 left
💡 Hint
Think about when the trigger runs relative to the update and what data it can access.
✗ Incorrect
AFTER UPDATE triggers run after each row is updated and have access to OLD and NEW values. They cannot modify the row being updated but can affect other tables.
📝 Syntax
advanced2:00remaining
Identify the syntax error in AFTER UPDATE trigger
Which option contains a syntax error in this AFTER UPDATE trigger definition?
MySQL
CREATE TRIGGER trg_after_update AFTER UPDATE ON employees FOR EACH ROW BEGIN INSERT INTO audit_log(emp_id, old_salary, new_salary) VALUES (OLD.id, OLD.salary, NEW.salary); END;
Attempts:
2 left
💡 Hint
MySQL requires changing the delimiter when defining triggers with multiple statements.
✗ Incorrect
In MySQL, when defining triggers with BEGIN...END blocks, you must change the delimiter temporarily to avoid syntax errors.
🔧 Debug
advanced2:30remaining
Why does this AFTER UPDATE trigger cause an infinite loop?
Consider this trigger on table
What is the cause of the infinite loop?
accounts that updates the last_modified column after any update:CREATE TRIGGER trg_after_update_accounts AFTER UPDATE ON accounts FOR EACH ROW BEGIN UPDATE accounts SET last_modified = NOW() WHERE id = NEW.id; END;What is the cause of the infinite loop?
Attempts:
2 left
💡 Hint
Think about what happens when the trigger updates the same table it is defined on.
✗ Incorrect
The trigger updates the same row in the table, causing the trigger to fire again and again, creating an infinite loop.
❓ optimization
expert3:00remaining
Optimizing AFTER UPDATE trigger to avoid unnecessary writes
You have an AFTER UPDATE trigger that logs changes only if the
status column changes. Which option is the most efficient way to write the trigger to avoid unnecessary inserts?Attempts:
2 left
💡 Hint
Triggers can check old and new values to decide if action is needed.
✗ Incorrect
Using an IF condition inside the trigger to compare OLD and NEW values prevents unnecessary inserts and improves performance.