Bird
0
0

Given this trigger code:

medium📝 query result Q13 of 15
SQL - Triggers
Given this trigger code:
CREATE TRIGGER check_age BEFORE INSERT ON users FOR EACH ROW BEGIN IF NEW.age < 18 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Age must be 18 or older'; END IF; END;

What happens if you try to insert a user with age 16?
AThe insert fails with an error message 'Age must be 18 or older'
BThe age is automatically set to 18
CThe insert is ignored silently
DThe insert succeeds and user is added
Step-by-Step Solution
Solution:
  1. Step 1: Understand trigger logic

    The trigger checks if NEW.age is less than 18 and raises an error if true.
  2. Step 2: Apply to age 16

    Since 16 < 18, the trigger raises an error with message 'Age must be 18 or older', stopping the insert.
  3. Final Answer:

    The insert fails with an error message 'Age must be 18 or older' -> Option A
  4. Quick Check:

    BEFORE trigger can stop invalid inserts [OK]
Quick Trick: BEFORE triggers can block invalid data with SIGNAL [OK]
Common Mistakes:
  • Thinking insert succeeds despite error
  • Assuming silent ignore instead of error
  • Believing trigger changes data automatically

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes