Bird
Raised Fist0
MySQLquery~20 mins

AFTER UPDATE triggers in MySQL - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of an AFTER UPDATE trigger in MySQL?
easy
A. To execute actions automatically after a row is updated in a table
B. To prevent updates from happening on a table
C. To execute actions before a row is inserted
D. To delete rows after an update

Solution

  1. Step 1: Understand trigger timing

    An AFTER UPDATE trigger runs after a row in a table is updated.
  2. Step 2: Identify trigger purpose

    Its purpose is to perform extra actions automatically after the update, like logging or updating related data.
  3. Final Answer:

    To execute actions automatically after a row is updated in a table -> Option A
  4. Quick Check:

    AFTER UPDATE triggers run after update [OK]
Hint: AFTER UPDATE triggers run after row changes [OK]
Common Mistakes:
  • Confusing AFTER UPDATE with BEFORE UPDATE
  • Thinking triggers prevent updates
  • Mixing trigger types like INSERT or DELETE
2. Which of the following is the correct syntax to create an AFTER UPDATE trigger on a table named employees?
easy
A. CREATE TRIGGER trg AFTER INSERT ON employees FOR EACH ROW BEGIN END;
B. CREATE TRIGGER trg AFTER UPDATE ON employees FOR EACH ROW BEGIN END;
C. CREATE TRIGGER trg BEFORE UPDATE ON employees FOR EACH ROW BEGIN END;
D. CREATE TRIGGER trg AFTER UPDATE employees FOR EACH ROW BEGIN END;

Solution

  1. Step 1: Check trigger timing and event

    The trigger must be AFTER UPDATE on the employees table.
  2. Step 2: Verify syntax correctness

    CREATE TRIGGER trg AFTER UPDATE ON employees FOR EACH ROW BEGIN END; uses correct syntax: CREATE TRIGGER trg AFTER UPDATE ON employees FOR EACH ROW BEGIN END;
  3. Final Answer:

    CREATE TRIGGER trg AFTER UPDATE ON employees FOR EACH ROW BEGIN END; -> Option B
  4. Quick Check:

    Correct syntax includes 'AFTER UPDATE ON table' [OK]
Hint: Syntax: CREATE TRIGGER name AFTER UPDATE ON table [OK]
Common Mistakes:
  • Omitting ON keyword before table name
  • Using BEFORE instead of AFTER
  • Using wrong event like INSERT
3. Given this trigger on table products:
CREATE TRIGGER trg_after_update
AFTER UPDATE ON products
FOR EACH ROW
BEGIN
  INSERT INTO audit_log (product_id, old_price, new_price)
  VALUES (OLD.id, OLD.price, NEW.price);
END;
What happens when a product's price is updated?
medium
A. The audit_log table is deleted
B. The product's price is reverted to the old price
C. The update is blocked and no changes happen
D. A new row is added to audit_log with old and new prices

Solution

  1. Step 1: Understand trigger action

    The trigger inserts a row into audit_log after a product update.
  2. Step 2: Analyze inserted values

    It records OLD.id, OLD.price, and NEW.price to log price changes.
  3. Final Answer:

    A new row is added to audit_log with old and new prices -> Option D
  4. Quick Check:

    AFTER UPDATE triggers can log changes [OK]
Hint: AFTER UPDATE can log old and new values [OK]
Common Mistakes:
  • Thinking trigger changes original update
  • Confusing OLD and NEW values
  • Assuming trigger blocks update
4. You wrote this trigger but get an error:
CREATE TRIGGER trg AFTER UPDATE ON orders
FOR EACH ROW
BEGIN
  SET NEW.status = 'processed';
END;
What is the problem?
medium
A. The syntax for SET is incorrect inside triggers
B. The trigger must be BEFORE UPDATE to modify NEW values
C. You cannot modify NEW values in an AFTER UPDATE trigger
D. Triggers cannot use BEGIN...END blocks

Solution

  1. Step 1: Understand trigger timing restrictions

    In AFTER UPDATE triggers, NEW values are read-only and cannot be changed.
  2. Step 2: Identify correct timing to modify NEW

    To modify NEW values, use BEFORE UPDATE triggers instead.
  3. Final Answer:

    You cannot modify NEW values in an AFTER UPDATE trigger -> Option C
  4. Quick Check:

    AFTER UPDATE triggers cannot change NEW row [OK]
Hint: Modify NEW only in BEFORE UPDATE triggers [OK]
Common Mistakes:
  • Trying to change NEW in AFTER UPDATE triggers
  • Confusing BEFORE and AFTER triggers
  • Incorrect SET syntax blamed
5. You want to keep a count of how many times each user's email was updated in a table users. Which trigger code correctly updates a email_update_count column after each email change?
hard
A.
CREATE TRIGGER trg_email_update
BEFORE UPDATE ON users
FOR EACH ROW
BEGIN
  IF OLD.email != NEW.email THEN
    SET NEW.email_update_count = OLD.email_update_count + 1;
  END IF;
END;
B.
CREATE TRIGGER trg_email_update
AFTER UPDATE ON users
FOR EACH ROW
BEGIN
  IF OLD.email != NEW.email THEN
    UPDATE users SET email_update_count = email_update_count + 1 WHERE id = NEW.id;
  END IF;
END;
C.
CREATE TRIGGER trg_email_update
AFTER UPDATE ON users
FOR EACH ROW
BEGIN
  SET NEW.email_update_count = OLD.email_update_count + 1;
END;
D.
CREATE TRIGGER trg_email_update
AFTER UPDATE ON users
FOR EACH ROW
BEGIN
  IF OLD.email = NEW.email THEN
    UPDATE users SET email_update_count = email_update_count + 1 WHERE id = NEW.id;
  END IF;
END;

Solution

  1. Step 1: Identify when to modify the count

    The count should increase only if the email changes, so check if OLD.email != NEW.email.
  2. Step 2: Choose correct trigger timing to modify NEW

    To change email_update_count in the same row, use a BEFORE UPDATE trigger and set NEW.email_update_count.
  3. Step 3: Analyze options

    CREATE TRIGGER trg_email_update
    BEFORE UPDATE ON users
    FOR EACH ROW
    BEGIN
      IF OLD.email != NEW.email THEN
        SET NEW.email_update_count = OLD.email_update_count + 1;
      END IF;
    END;
    correctly uses BEFORE UPDATE, compares emails, and sets NEW value.
    CREATE TRIGGER trg_email_update
    AFTER UPDATE ON users
    FOR EACH ROW
    BEGIN
      IF OLD.email != NEW.email THEN
        UPDATE users SET email_update_count = email_update_count + 1 WHERE id = NEW.id;
      END IF;
    END;
    tries to update the same table in AFTER UPDATE, causing recursion.
    CREATE TRIGGER trg_email_update
    AFTER UPDATE ON users
    FOR EACH ROW
    BEGIN
      SET NEW.email_update_count = OLD.email_update_count + 1;
    END;
    tries to set NEW in AFTER UPDATE (not allowed).
    CREATE TRIGGER trg_email_update
    AFTER UPDATE ON users
    FOR EACH ROW
    BEGIN
      IF OLD.email = NEW.email THEN
        UPDATE users SET email_update_count = email_update_count + 1 WHERE id = NEW.id;
      END IF;
    END;
    updates count when emails are equal (wrong logic).
  4. Final Answer:

    BEFORE UPDATE trigger that sets NEW.email_update_count if OLD.email != NEW.email -> Option A
  5. Quick Check:

    Modify NEW in BEFORE UPDATE triggers [OK]
Hint: Change NEW only in BEFORE UPDATE, check OLD vs NEW values [OK]
Common Mistakes:
  • Trying to modify NEW in AFTER UPDATE triggers
  • Updating the same table inside AFTER UPDATE causing recursion
  • Wrong condition logic for email change
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard