What is the purpose of the UNIQUE constraint in SQL?
A
To prevent NULL values in a column B
To ensure all values in a column are unique C
To link tables together D
To automatically assign default values
Analysis & Theory
The UNIQUE constraint enforces that no duplicate values exist in the column(s).
Which of the following statements correctly adds a UNIQUE constraint to a table column?
A
CREATE TABLE Employees (Email VARCHAR(100) UNIQUE); B
CREATE TABLE Employees (UNIQUE Email VARCHAR(100)); C
CREATE TABLE Employees (Email UNIQUE VARCHAR(100)); D
CREATE TABLE Employees (Email VARCHAR(100) NOT NULL UNIQUE NULL);
Analysis & Theory
The correct syntax is column_name datatype UNIQUE.
Can a column with a UNIQUE constraint have NULL values?
A
No, UNIQUE means NOT NULL B
Yes, but only one NULL value is allowed C
Yes, multiple NULL values are allowed D
Yes, but NULL must be unique
Analysis & Theory
In most databases, UNIQUE allows multiple NULLs because NULLs are not considered equal.
How is UNIQUE different from PRIMARY KEY?
A
UNIQUE allows duplicate values B
PRIMARY KEY can be applied to multiple columns only C
UNIQUE allows multiple NULLs; PRIMARY KEY does not allow NULLs D
They are exactly the same
Analysis & Theory
PRIMARY KEY implies NOT NULL + UNIQUE, but UNIQUE alone allows NULLs.
Which of the following creates a UNIQUE constraint with a custom name?
A
CREATE TABLE Users (Username VARCHAR(50), CONSTRAINT uq_username UNIQUE (Username)); B
CREATE TABLE Users (Username VARCHAR(50) CONSTRAINT UNIQUE uq_username); C
CREATE TABLE Users (UNIQUE Username VARCHAR(50) uq_username); D
CREATE TABLE Users (Username VARCHAR(50) UNIQUE NAME uq_username);
Analysis & Theory
CONSTRAINT constraint_name UNIQUE (column) is the correct syntax.
What happens if you try to insert a duplicate value in a UNIQUE column?
A
SQL ignores the duplicate B
SQL inserts NULL C
SQL raises an error D
SQL replaces the old value
Analysis & Theory
A constraint violation error is thrown.
Which of the following statements adds a UNIQUE constraint to an existing table?
A
ALTER TABLE Products ADD UNIQUE (ProductCode); B
ALTER TABLE Products UNIQUE ProductCode; C
ALTER TABLE Products MODIFY ProductCode UNIQUE; D
ALTER Products ADD UNIQUE ProductCode;
Analysis & Theory
ALTER TABLE ... ADD UNIQUE (column) is the correct way.
Which scenario is a good use case for a UNIQUE constraint?
A
Enforcing that every product has a price B
Ensuring no two customers have the same email address C
Requiring a column to have values between 1 and 100 D
Providing default values for missing data
Analysis & Theory
UNIQUE is perfect for ensuring no duplicate emails.
Can a table have multiple UNIQUE constraints?
A
No, only one is allowed B
Yes, but only one column per constraint C
Yes, you can have multiple UNIQUE constraints on different columns D
Yes, but only if combined with PRIMARY KEY
Analysis & Theory
Multiple UNIQUE constraints are allowed.
Which of the following correctly defines a UNIQUE constraint on multiple columns?
A
CREATE TABLE Orders (ProductID INT, OrderID INT, UNIQUE ProductID, OrderID); B
CREATE TABLE Orders (ProductID INT, OrderID INT, UNIQUE (ProductID, OrderID)); C
CREATE TABLE Orders (ProductID UNIQUE, OrderID UNIQUE); D
CREATE TABLE Orders (ProductID INT UNIQUE, OrderID);
Analysis & Theory
UNIQUE (col1, col2) creates a composite unique constraint.