A UNIQUE constraint ensures that values in the constrained column or column combination are unique according to the database's rules. It is commonly used for attributes that should not contain duplicate values.
Duplicate identifiers can cause data-quality problems and make it difficult to reliably identify business records.
A company may require every employee email address to be unique.
CREATE TABLE Employees (
EmployeeID INT,
Email VARCHAR(150) UNIQUE
);
UNIQUE constraints are useful for email addresses, account numbers, employee codes, usernames, reference numbers, and other identifiers.
- UNIQUE prevents duplicate values.
- It can be applied to one or multiple columns.
- It supports data quality.
- It is different from a PRIMARY KEY.
- NULL behavior can vary by database system.
What is a UNIQUE constraint?
A UNIQUE constraint ensures that duplicate values are not allowed for the constrained column or column combination.
How is UNIQUE different from PRIMARY KEY?
A primary key uniquely identifies rows and has primary-key-specific rules, while a table can generally have multiple UNIQUE constraints.