Challenge - 5 Problems
Unique Constraint Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What happens when inserting duplicate values in a UNIQUE column?
Consider a table
users with a UNIQUE constraint on the email column. What will be the result of this query?INSERT INTO users (id, email) VALUES (1, 'a@example.com'), (2, 'a@example.com');MySQL
CREATE TABLE users (id INT PRIMARY KEY, email VARCHAR(255) UNIQUE); INSERT INTO users (id, email) VALUES (1, 'a@example.com'), (2, 'a@example.com');
Attempts:
2 left
💡 Hint
UNIQUE constraints prevent duplicate values in the specified column.
✗ Incorrect
The UNIQUE constraint on the email column prevents duplicate emails. In a multi-row INSERT, if any row violates the constraint, the entire statement fails with a duplicate key error, and no rows are inserted.
🧠 Conceptual
intermediate1:30remaining
How does UNIQUE constraint differ from PRIMARY KEY?
Which statement correctly describes the difference between a UNIQUE constraint and a PRIMARY KEY constraint in MySQL?
Attempts:
2 left
💡 Hint
Think about NULL values and uniqueness.
✗ Incorrect
PRIMARY KEY columns must be unique and cannot contain NULL values. UNIQUE constraints enforce uniqueness but allow multiple NULLs because NULL is not considered equal to another NULL.
📝 Syntax
advanced1:30remaining
Which statement correctly adds a UNIQUE constraint to an existing column?
You have a table
employees with a column phone_number. Which SQL statement correctly adds a UNIQUE constraint to phone_number?Attempts:
2 left
💡 Hint
Look for the correct syntax to add a UNIQUE constraint with ALTER TABLE.
✗ Incorrect
The correct syntax to add a UNIQUE constraint on a column is: ALTER TABLE table_name ADD UNIQUE (column_name); Options B, C, and D are syntactically incorrect.
❓ optimization
advanced1:30remaining
What is a performance impact of UNIQUE constraints on large tables?
Which statement best describes the performance impact of UNIQUE constraints on large tables during data insertion?
Attempts:
2 left
💡 Hint
Think about what the database must do to enforce uniqueness.
✗ Incorrect
When inserting data, the database checks the UNIQUE constraint by searching the index to ensure no duplicates exist. This check adds overhead and slows down inserts compared to columns without constraints.
🔧 Debug
expert2:30remaining
Why does this UNIQUE constraint fail to prevent duplicates?
Given this table definition:
Why might duplicates still appear in
CREATE TABLE orders (order_id INT PRIMARY KEY, customer_id INT, order_date DATE, UNIQUE (customer_id));Why might duplicates still appear in
customer_id after inserting multiple rows?Attempts:
2 left
💡 Hint
Consider how UNIQUE constraints treat NULL values.
✗ Incorrect
UNIQUE constraints allow multiple NULL values in the constrained column because NULL is not considered equal to another NULL. If customer_id is NULL in multiple rows, duplicates appear despite the UNIQUE constraint.