0
0
MySQLquery~5 mins

UNIQUE constraints in MySQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a UNIQUE constraint in a database?
A UNIQUE constraint ensures that all values in a column or a group of columns are different from each other. It prevents duplicate values in that column or combination of columns.
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 ensure uniqueness in each specified area.
Click to reveal answer
intermediate
How does a UNIQUE constraint differ from a PRIMARY KEY?
A PRIMARY KEY uniquely identifies each row and cannot be NULL. A UNIQUE constraint also enforces uniqueness but allows NULL values (unless specified otherwise).
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 the UNIQUE constraint is violated.
Click to reveal answer
What does a UNIQUE constraint do in a database table?
ADeletes duplicate rows automatically
BAllows duplicate values but no NULLs
CAutomatically creates an index without enforcing uniqueness
DPrevents duplicate values in a column or set of columns
Can a UNIQUE constraint column contain NULL values?
ANo, NULLs are not allowed
BYes, NULLs are allowed and can appear multiple times
CYes, but only one NULL is allowed
DIt depends on the database system
Which SQL command adds a UNIQUE constraint to an existing table?
AALTER TABLE table_name ADD UNIQUE (column_name);
BCREATE UNIQUE CONSTRAINT table_name (column_name);
CUPDATE TABLE table_name SET UNIQUE = TRUE;
DINSERT UNIQUE INTO table_name (column_name);
How many UNIQUE constraints can a table have?
AOnly one
BTwo
CMultiple on different columns or column sets
DNone
What error occurs if you insert a duplicate value into a UNIQUE constrained column?
ADuplicate entry error
BSyntax error
CNo error, duplicates allowed
DConnection error
Explain what a UNIQUE constraint is and how it helps maintain data integrity.
Think about how UNIQUE constraints stop repeated data in a column.
You got /4 concepts.
    Describe how to add a UNIQUE constraint to an existing table and what happens if a duplicate value is inserted.
    Recall the SQL command and the database behavior on duplicates.
    You got /4 concepts.