Bird
0
0

Given this trigger snippet for audit logging:

medium📝 Command Output Q13 of 15
SQL - Triggers
Given this trigger snippet for audit logging:
CREATE TRIGGER audit_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 will be inserted into audit_log when a product's price changes from 100 to 120?
A(product_id: NULL, old_price: 100, new_price: 120)
B(product_id: product's id, old_price: NULL, new_price: NULL)
C(product_id: product's id, old_price: 120, new_price: 100)
D(product_id: product's id, old_price: 100, new_price: 120)
Step-by-Step Solution
Solution:
  1. Step 1: Understand OLD and NEW in triggers

    OLD refers to the row before update, NEW refers to the row after update.
  2. Step 2: Analyze the insert statement

    The trigger inserts OLD.id, OLD.price, and NEW.price, so product_id is the old id, old_price is 100, new_price is 120.
  3. Final Answer:

    (product_id: product's id, old_price: 100, new_price: 120) -> Option D
  4. Quick Check:

    OLD = before update, NEW = after update [OK]
Quick Trick: OLD = old row, NEW = new row in update triggers [OK]
Common Mistakes:
  • Mixing OLD and NEW values
  • Assuming NULL values for unchanged columns
  • Confusing product_id with price values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes