Bird
0
0

Given this trigger function:

medium📝 Debug Q14 of 15
PostgreSQL - Triggers in PostgreSQL
Given this trigger function:
CREATE FUNCTION check_age() RETURNS trigger AS $$
BEGIN
  IF NEW.age < 18 THEN
    RAISE EXCEPTION 'Age must be 18 or older';
  END IF;
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

Which problem will occur if you create a BEFORE INSERT trigger using this function and try to insert a row with age = 16?
AAn error will be raised and insertion will stop
BThe trigger will silently ignore the age check
CThe row will be inserted with age 16
DThe age will be automatically set to 18
Step-by-Step Solution
Solution:
  1. Step 1: Analyze trigger logic

    If NEW.age is less than 18, the trigger raises an exception.
  2. Step 2: Understand effect of RAISE EXCEPTION

    Raising an exception stops the insert and returns an error to the client.
  3. Final Answer:

    An error will be raised and insertion will stop -> Option A
  4. Quick Check:

    RAISE EXCEPTION stops insert with error [OK]
Quick Trick: RAISE EXCEPTION in BEFORE trigger stops insert with error [OK]
Common Mistakes:
  • Thinking the row inserts anyway
  • Assuming age auto-corrects
  • Ignoring that exceptions stop execution

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes