Bird
0
0

You have a unique index on the username column. The following query causes an error:

medium📝 Debug Q14 of 15
SQL - Indexes and Query Performance
You have a unique index on the username column. The following query causes an error:
INSERT INTO users (username) VALUES ('john');
INSERT INTO users (username) VALUES ('john');

What is the best way to fix this error?
AChange the second insert to a different username
BRemove the unique index on <code>username</code>
CInsert NULL instead of 'john' in the second insert
DUse a SELECT query instead of INSERT
Step-by-Step Solution
Solution:
  1. Step 1: Identify cause of error

    The unique index prevents duplicate usernames. Inserting 'john' twice violates this.
  2. Step 2: Choose a fix that respects uniqueness

    Changing the second insert to a different username avoids duplicates and keeps data integrity.
  3. Final Answer:

    Change the second insert to a different username -> Option A
  4. Quick Check:

    Fix duplicates by changing values [OK]
Quick Trick: Change duplicate value to unique to fix insert error [OK]
Common Mistakes:
  • Removing unique index (loses data integrity)
  • Inserting NULL (may not be allowed or unique)
  • Using SELECT instead of INSERT (doesn't fix duplicates)

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes