Bird
0
0

Given the table employees with a UNIQUE constraint on email, what happens if you run this query?

medium📝 query result Q4 of 15
SQL - Table Constraints
Given the table employees with a UNIQUE constraint on email, what happens if you run this query?

INSERT INTO employees (id, email) VALUES (1, 'john@example.com');
INSERT INTO employees (id, email) VALUES (2, 'john@example.com');
AThe second insert fails due to UNIQUE constraint violation
BBoth inserts succeed, duplicates allowed
CThe first insert fails, second succeeds
DBoth inserts fail due to syntax error
Step-by-Step Solution
Solution:
  1. Step 1: Understand UNIQUE constraint effect on inserts

    The UNIQUE constraint prevents duplicate values in the constrained column.
  2. Step 2: Analyze the inserts

    The first insert adds 'john@example.com' successfully; the second insert tries to add the same email, causing a violation.
  3. Final Answer:

    The second insert fails due to UNIQUE constraint violation -> Option A
  4. Quick Check:

    UNIQUE constraint blocks duplicate inserts [OK]
Quick Trick: Duplicate values violate UNIQUE and cause insert failure [OK]
Common Mistakes:
MISTAKES
  • Assuming duplicates are allowed with UNIQUE
  • Thinking first insert fails
  • Confusing UNIQUE with PRIMARY KEY behavior

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes