Recall & Review
beginner
What is the purpose of the UNIQUE constraint in SQL?
The UNIQUE constraint ensures that all values in a column or a group of columns are different from each other, preventing duplicate entries.
Click to reveal answer
beginner
Can a table have multiple UNIQUE constraints?
Yes, a table can have multiple UNIQUE constraints on different columns or sets of columns to enforce uniqueness in multiple ways.
Click to reveal answer
intermediate
How does UNIQUE constraint differ from PRIMARY KEY?
PRIMARY KEY uniquely identifies each row and does not allow NULLs. UNIQUE constraint also enforces uniqueness but allows NULL values (depending on the database).
Click to reveal answer
beginner
Write a SQL statement to add a UNIQUE constraint on the 'email' column of a table named 'users'.
ALTER TABLE users ADD CONSTRAINT unique_email UNIQUE (email);
Click to reveal answer
beginner
What happens if you try to insert a duplicate value into a column with a UNIQUE constraint?
The database will reject the insert or update operation and return an error because it violates the UNIQUE constraint.
Click to reveal answer
What does the UNIQUE constraint do in a SQL table?
✗ Incorrect
The UNIQUE constraint prevents duplicate values in the specified column(s).
Can a UNIQUE constraint column contain NULL values?
✗ Incorrect
Whether NULLs are allowed and how many depends on the database system; some allow multiple NULLs, others allow only one.
Which SQL command adds a UNIQUE constraint to an existing table?
✗ Incorrect
ALTER TABLE ... ADD UNIQUE is the correct syntax to add a UNIQUE constraint.
How many UNIQUE constraints can a table have?
✗ Incorrect
A table can have multiple UNIQUE constraints on different columns or column sets.
What error occurs if you insert a duplicate value into a UNIQUE column?
✗ Incorrect
Inserting a duplicate value violates the UNIQUE constraint and causes a constraint violation error.
Explain what the UNIQUE constraint does and how it differs from a PRIMARY KEY.
Think about uniqueness and NULL values.
You got /4 concepts.
Describe how to add a UNIQUE constraint to an existing table and what happens if a duplicate value is inserted.
Consider the SQL command and error behavior.
You got /3 concepts.