0
0
MySQLquery~20 mins

UNIQUE constraints in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Unique Constraint Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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');
AThe second row insertion fails with a duplicate key error.
BThe entire insert statement fails and no rows are inserted.
CBoth rows insert successfully because the UNIQUE constraint allows duplicates.
DOnly the first row inserts; the second row is ignored without error.
Attempts:
2 left
💡 Hint
UNIQUE constraints prevent duplicate values in the specified column.
🧠 Conceptual
intermediate
1: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?
APRIMARY KEY uniquely identifies rows and cannot be NULL; UNIQUE allows multiple NULLs.
BUNIQUE is faster than PRIMARY KEY for indexing.
CPRIMARY KEY allows duplicates; UNIQUE does not.
DUNIQUE constraints automatically create foreign keys.
Attempts:
2 left
💡 Hint
Think about NULL values and uniqueness.
📝 Syntax
advanced
1: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?
AALTER TABLE employees MODIFY phone_number UNIQUE;
BALTER TABLE employees ADD CONSTRAINT UNIQUE phone_number;
CALTER TABLE employees ADD UNIQUE (phone_number);
DALTER TABLE employees ADD UNIQUE phone_number;
Attempts:
2 left
💡 Hint
Look for the correct syntax to add a UNIQUE constraint with ALTER TABLE.
optimization
advanced
1: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?
AUNIQUE constraints slow down inserts because the database must check for duplicates.
BUNIQUE constraints speed up inserts by indexing the column.
CUNIQUE constraints have no impact on insert performance.
DUNIQUE constraints cause inserts to fail silently if duplicates exist.
Attempts:
2 left
💡 Hint
Think about what the database must do to enforce uniqueness.
🔧 Debug
expert
2:30remaining
Why does this UNIQUE constraint fail to prevent duplicates?
Given this table definition:

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?
ABecause order_date allows duplicates, which affects customer_id uniqueness.
BBecause UNIQUE constraints only apply to PRIMARY KEY columns.
CBecause the UNIQUE constraint was not created properly without a name.
DBecause NULL values in customer_id are allowed and can appear multiple times.
Attempts:
2 left
💡 Hint
Consider how UNIQUE constraints treat NULL values.