Bird
0
0

Consider this trigger code snippet intended to prevent deletion of rows from Users table if the user is an admin:

medium📝 Debug Q14 of 15
SQL - Triggers
Consider this trigger code snippet intended to prevent deletion of rows from Users table if the user is an admin:
CREATE TRIGGER prevent_admin_delete BEFORE DELETE ON Users
FOR EACH ROW
BEGIN
  IF OLD.role = 'admin' THEN
    SIGNAL SQLSTATE '45000' MESSAGE_TEXT = 'Cannot delete admin user';
  END IF;
END;
What is the main issue if this trigger does not prevent admin deletions?
AThe SIGNAL statement syntax is incorrect
BThe condition should check NEW.role instead of OLD.role
CThe trigger does not specify FOR EACH ROW
DThe trigger should be AFTER DELETE, not BEFORE DELETE
Step-by-Step Solution
Solution:
  1. Step 1: Check trigger timing and row context

    BEFORE DELETE and FOR EACH ROW are correctly used to check OLD.role before deletion.
  2. Step 2: Verify SIGNAL syntax

    In standard SQL, SIGNAL syntax requires correct SQLSTATE and message format. If syntax is wrong, error won't raise and deletion proceeds.
  3. Final Answer:

    The SIGNAL statement syntax is incorrect -> Option A
  4. Quick Check:

    SIGNAL syntax error prevents trigger from blocking delete [OK]
Quick Trick: Check SIGNAL syntax carefully to block deletes [OK]
Common Mistakes:
  • Using AFTER DELETE instead of BEFORE DELETE
  • Checking NEW instead of OLD in DELETE triggers
  • Omitting FOR EACH ROW clause

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes