Bird
0
0

You want a trigger that prevents updating a user's email to an invalid format and also logs the old and new emails. Which approach correctly combines validation and logging?

hard📝 Application Q9 of 15
PostgreSQL - Triggers in PostgreSQL
You want a trigger that prevents updating a user's email to an invalid format and also logs the old and new emails. Which approach correctly combines validation and logging?
ARaise exception if invalid email, but do not log changes
BInsert old and new emails into log table after returning NEW, then check email format
CIn the trigger function, first check email format and raise exception if invalid, then insert old and new emails into a log table before returning NEW
DLog changes only if email is valid, but do not raise exceptions
Step-by-Step Solution
Solution:
  1. Step 1: Validate email before allowing update

    Raise exception first to block invalid data.
  2. Step 2: Log old and new emails before returning NEW

    Logging must happen before returning to ensure it runs.
  3. Final Answer:

    In the trigger function, first check email format and raise exception if invalid, then insert old and new emails into a log table before returning NEW -> Option C
  4. Quick Check:

    Validate first, then log, then return NEW [OK]
Quick Trick: Validate before logging and returning NEW in trigger [OK]
Common Mistakes:
  • Logging after RETURN NEW
  • Not raising exception on invalid data
  • Logging without validation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes