Bird
0
0

Identify the issue in this BEFORE DELETE trigger snippet:

medium📝 Debug Q7 of 15
PostgreSQL - Triggers in PostgreSQL
Identify the issue in this BEFORE DELETE trigger snippet:
IF OLD.user_id IS NULL THEN
RAISE EXCEPTION 'User ID missing';
END IF;
AOLD record is not available in BEFORE DELETE triggers
BTrigger function must return NEW, not OLD
CThe condition should check NEW.user_id instead
DRAISE EXCEPTION cannot be used in triggers
Step-by-Step Solution
Solution:
  1. Step 1: Recall record availability in DELETE triggers

    In DELETE triggers, OLD record is available, NEW is not.
  2. Step 2: Check the trigger timing

    BEFORE DELETE triggers have access to OLD record, so the condition is valid.
  3. Step 3: Identify the actual problem

    Trigger functions must return OLD in DELETE triggers, not NEW.
  4. Final Answer:

    Trigger function must return OLD, not NEW -> Option B
  5. Quick Check:

    DELETE triggers return OLD to proceed [OK]
Quick Trick: OLD exists in DELETE triggers, NEW does not; return OLD in DELETE triggers [OK]
Common Mistakes:
  • Using NEW in DELETE triggers
  • Assuming RAISE EXCEPTION is invalid in triggers
  • Confusing BEFORE and AFTER trigger record availability

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes