Bird
0
0

You created this trigger function:

medium📝 Debug Q6 of 15
PostgreSQL - Triggers in PostgreSQL
You created this trigger function:
CREATE FUNCTION check_phone_format() RETURNS trigger AS $$ BEGIN IF NEW.phone !~ '^\\+\\d{10,15}$' THEN RAISE EXCEPTION 'Invalid phone format'; END IF; RETURN NEW; END; $$ LANGUAGE plpgsql;
However, invalid phone numbers are still inserted. What is the most probable cause?
AThe trigger function does not return OLD instead of NEW.
BThe regular expression syntax is incorrect for PostgreSQL.
CThe trigger is not attached to the table or event properly.
DThe exception message is not descriptive enough.
Step-by-Step Solution
Solution:
  1. Step 1: Understand the trigger function

    The function checks phone format and raises an exception if invalid.
  2. Step 2: Identify why invalid data passes

    If invalid phones are inserted, likely the trigger is not firing.
  3. Step 3: Common cause

    Triggers must be explicitly created and attached to table events; missing this step means the function never runs.
  4. Final Answer:

    The trigger is not attached to the table or event properly. -> Option C
  5. Quick Check:

    Trigger must be created and linked to table [OK]
Quick Trick: Trigger function alone doesn't enforce rules without trigger creation [OK]
Common Mistakes:
  • Assuming function runs without creating trigger
  • Misunderstanding regex syntax in PostgreSQL
  • Returning OLD instead of NEW in BEFORE INSERT triggers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes