Bird
0
0

Given the table products with a UNIQUE constraint on product_code, what happens when you run this SQL?

medium📝 query result Q13 of 15
SQL - Table Constraints
Given the table products with a UNIQUE constraint on product_code, what happens when you run this SQL?
INSERT INTO products (product_code, name) VALUES ('X123', 'Item A');
INSERT INTO products (product_code, name) VALUES ('X123', 'Item B');
AThe second insert fails with a UNIQUE constraint violation error
BThe second insert overwrites the first row
CBoth rows are inserted successfully
DThe database ignores the second insert silently
Step-by-Step Solution
Solution:
  1. Step 1: Analyze UNIQUE constraint effect on the insert statements

    The UNIQUE constraint on product_code means no two rows can have the same product_code value. The first insert adds 'X123' successfully. The second insert tries to add the same product_code 'X123', which violates the UNIQUE constraint, causing an error.
  2. Final Answer:

    The second insert fails with a UNIQUE constraint violation error -> Option A
  3. Quick Check:

    Duplicate insert on UNIQUE column = error [OK]
Quick Trick: Duplicate UNIQUE values cause insert errors [OK]
Common Mistakes:
MISTAKES
  • Assuming duplicates overwrite existing rows
  • Thinking duplicates are ignored silently
  • Believing both inserts succeed

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes