Bird
0
0

You wrote this trigger but it causes a runtime error:

medium📝 Debug Q14 of 15
SQL - Triggers
You wrote this trigger but it causes a runtime error:
CREATE TRIGGER trg_log AFTER INSERT ON sales FOR EACH ROW BEGIN INSERT INTO sales_log VALUES (NEW.id, NEW.amount); UPDATE sales SET amount = amount * 1.1 WHERE id = NEW.id; END;

What is the likely cause of the error?
ATrigger tries to update the same table it is triggered on, causing recursion
BMissing semicolon after INSERT statement
CNEW keyword cannot be used in AFTER INSERT triggers
Dsales_log table does not exist
Step-by-Step Solution
Solution:
  1. Step 1: Identify trigger actions

    The trigger inserts into sales_log and then updates sales table, which is the same table that fired the trigger.
  2. Step 2: Understand recursion risk

    Updating sales inside an AFTER INSERT trigger on sales causes the trigger to fire again, leading to infinite recursion and runtime error.
  3. Final Answer:

    Trigger tries to update the same table it is triggered on, causing recursion -> Option A
  4. Quick Check:

    Updating trigger table inside trigger causes recursion error [OK]
Quick Trick: Avoid updating trigger table inside its own trigger [OK]
Common Mistakes:
  • Ignoring recursion from updating same table
  • Assuming missing semicolon causes runtime error
  • Believing NEW is invalid in AFTER INSERT triggers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes