0
0
MySQLquery~20 mins

AFTER UPDATE triggers in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
AFTER UPDATE Trigger Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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);
AOne row in audit_log: emp_id=3, old_salary=5000, new_salary=6000
BNo rows in audit_log
COne row in audit_log: emp_id=3, old_salary=6000, new_salary=6000
DMultiple rows in audit_log with old_salary=5000
Attempts:
2 left
💡 Hint
The trigger runs after the update and compares old and new salary values.
🧠 Conceptual
intermediate
1:30remaining
Understanding WHEN AFTER UPDATE triggers fire
Which of the following statements about AFTER UPDATE triggers in MySQL is true?
AAFTER UPDATE triggers fire before the update operation starts.
BAFTER UPDATE triggers fire after each row is updated.
CAFTER UPDATE triggers can modify the row being updated directly.
DAFTER UPDATE triggers do not have access to OLD and NEW row values.
Attempts:
2 left
💡 Hint
Think about when the trigger runs relative to the update and what data it can access.
📝 Syntax
advanced
2: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;
AMissing DELIMITER change before and after trigger definition
BTrigger must be BEFORE UPDATE, not AFTER UPDATE
COLD and NEW cannot be used in AFTER UPDATE triggers
DFOR EACH ROW should be FOR EACH RECORD
Attempts:
2 left
💡 Hint
MySQL requires changing the delimiter when defining triggers with multiple statements.
🔧 Debug
advanced
2:30remaining
Why does this AFTER UPDATE trigger cause an infinite loop?
Consider this trigger on table 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?
AAFTER UPDATE triggers cannot contain UPDATE statements.
BThe trigger uses NOW() which is not allowed in triggers.
CThe trigger lacks a WHERE clause to limit updates.
DThe trigger updates the same table, causing it to fire repeatedly.
Attempts:
2 left
💡 Hint
Think about what happens when the trigger updates the same table it is defined on.
optimization
expert
3: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?
ACheck status change outside the trigger in application code before updating.
BUse BEFORE UPDATE trigger instead of AFTER UPDATE.
CUse IF OLD.status <> NEW.status THEN INSERT ... END IF; inside the trigger.
DINSERT INTO log_table VALUES (...) unconditionally in the trigger.
Attempts:
2 left
💡 Hint
Triggers can check old and new values to decide if action is needed.