Bird
0
0

Why does this trigger fail to log updates?

medium📝 Troubleshoot Q7 of 15
SQL - Triggers
Why does this trigger fail to log updates? CREATE TRIGGER audit_update AFTER UPDATE ON customers FOR EACH ROW BEGIN INSERT INTO audit_log(customer_id, action_time) VALUES (OLD.id, CURRENT_TIMESTAMP); END;
AIt uses OLD.id instead of NEW.id to log updated data
BAFTER UPDATE triggers cannot insert into other tables
CCURRENT_TIMESTAMP is not allowed in triggers
DFOR EACH ROW is missing in the trigger definition
Step-by-Step Solution
Solution:
  1. Step 1: Understand NEW vs OLD in UPDATE triggers

    NEW contains the new row data after update; OLD contains old data before update.
  2. Step 2: Identify correct value to log

    To log updated data, NEW.id should be used, not OLD.id.
  3. Final Answer:

    It uses OLD.id instead of NEW.id to log updated data -> Option A
  4. Quick Check:

    Use NEW.id to log updated rows in AFTER UPDATE triggers = It uses OLD.id instead of NEW.id to log updated data [OK]
Quick Trick: Use NEW for updated values, OLD for deleted or old values [OK]
Common Mistakes:
  • Using OLD.id to log updated data
  • Assuming triggers cannot insert data
  • Missing FOR EACH ROW clause

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes