Bird
0
0

You want to ensure no duplicate usernames are inserted into the accounts table using an INSERT trigger. Which method is the most effective?

hard📝 Application Q9 of 15
SQL - Triggers
You want to ensure no duplicate usernames are inserted into the accounts table using an INSERT trigger. Which method is the most effective?
AIn the trigger, check if NEW.username exists; if yes, raise an error to prevent insertion.
BAllow insertion and then delete duplicates in a scheduled cleanup job.
CUse a trigger to update existing rows with the same username instead of inserting.
DIgnore duplicates by silently skipping the insert operation in the trigger.
Step-by-Step Solution
Solution:
  1. Step 1: Understand the goal

    Prevent duplicate usernames at insert time.
  2. Step 2: Evaluate options

    In the trigger, check if NEW.username exists; if yes, raise an error to prevent insertion. uses the trigger to check for existing username and raises an error, effectively preventing duplicates.
  3. Step 3: Why others are incorrect

    B delays handling duplicates, C modifies data incorrectly, D silently ignores which can cause data inconsistency.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Trigger should validate and block duplicates immediately [OK]
Quick Trick: Raise error in trigger if duplicate found [OK]
Common Mistakes:
  • Trying to fix duplicates after insertion
  • Modifying existing rows instead of blocking duplicates
  • Ignoring duplicates without notification

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes