Challenge - 5 Problems
Unique Index Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Effect of Unique Index on Duplicate Inserts
Given a table
users with a unique index on the email column, what happens when you try to insert two rows with the same email?SQL
CREATE TABLE users (id INT PRIMARY KEY, email VARCHAR(255)); CREATE UNIQUE INDEX idx_email ON users(email); INSERT INTO users (id, email) VALUES (1, 'alice@example.com'); INSERT INTO users (id, email) VALUES (2, 'alice@example.com');
Attempts:
2 left
💡 Hint
Think about what a unique index enforces on the column values.
✗ Incorrect
A unique index prevents duplicate values in the indexed column. Trying to insert a duplicate value causes an error.
🧠 Conceptual
intermediate1:30remaining
Unique Index vs Primary Key
Which statement correctly describes the difference between a unique index and a primary key in SQL?
Attempts:
2 left
💡 Hint
Consider NULL values and uniqueness rules.
✗ Incorrect
Primary keys enforce uniqueness and disallow NULLs. Unique indexes enforce uniqueness but allow multiple NULLs depending on the database.
📝 Syntax
advanced2:00remaining
Creating a Unique Index on Multiple Columns
Which SQL statement correctly creates a unique index on the combination of
first_name and last_name columns in the employees table?Attempts:
2 left
💡 Hint
Focus on the correct syntax order for creating unique indexes.
✗ Incorrect
The correct syntax is CREATE UNIQUE INDEX index_name ON table(columns). Other options have incorrect keyword order or invalid syntax.
❓ optimization
advanced1:30remaining
Performance Impact of Unique Indexes
How does adding a unique index on a frequently updated column affect database performance?
Attempts:
2 left
💡 Hint
Think about what the database must do to maintain uniqueness.
✗ Incorrect
Unique indexes require the database to verify no duplicates exist on each insert or update, which adds overhead.
🔧 Debug
expert2:30remaining
Diagnosing Unique Constraint Violation
You have a unique index on the
username column. After deleting a row with username 'john', you try to insert a new row with username 'john' but get a unique constraint violation error. What is the most likely cause?Attempts:
2 left
💡 Hint
Consider transaction states and locking behavior.
✗ Incorrect
If the delete is not committed or the row is locked, the unique index still considers the old value present, causing the violation.