Bird
0
0

You want to ensure that a user's email is unique and not empty using a trigger. Which approach correctly combines data validation and uniqueness check in PostgreSQL?

hard📝 Application Q15 of 15
PostgreSQL - Triggers in PostgreSQL
You want to ensure that a user's email is unique and not empty using a trigger. Which approach correctly combines data validation and uniqueness check in PostgreSQL?
ACreate a BEFORE INSERT OR UPDATE trigger that raises exception if NEW.email is empty or exists in the table
BUse a UNIQUE constraint on email column only, no trigger needed
CCreate an AFTER INSERT trigger that deletes duplicates after insertion
DUse a trigger that sets empty emails to a default value
Step-by-Step Solution
Solution:
  1. Step 1: Understand validation needs

    Email must be non-empty and unique before saving data.
  2. Step 2: Choose trigger timing and logic

    BEFORE INSERT OR UPDATE trigger can check NEW.email and query table for duplicates, raising exception if invalid.
  3. Final Answer:

    Create a BEFORE INSERT OR UPDATE trigger that raises exception if NEW.email is empty or exists in the table -> Option A
  4. Quick Check:

    Validate and check uniqueness before insert/update [OK]
Quick Trick: Use BEFORE trigger to check and stop invalid data [OK]
Common Mistakes:
  • Relying only on UNIQUE constraint without empty check
  • Using AFTER trigger to fix duplicates (too late)
  • Setting default instead of raising error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes