SQL UNIQUE Constraint

The UNIQUE constraint ensures that all values in a column are different from each other.

In simple terms:
A column with a UNIQUE constraint cannot have duplicate values.

Key Points
   • Prevents duplicate values.
   • Can be applied to numeric or character columns.
   • Multiple UNIQUE constraints can exist in a table.

Example: Orders Table

Suppose every order must have a unique OrderNumber.

CREATE TABLE Orders (
    OrderID INT PRIMARY KEY,
    OrderNumber VARCHAR(20) UNIQUE,
    CustomerID INT,
    OrderDate DATE
);

Simple Definition
The UNIQUE constraint ensures that all values in a column or group of columns are unique and prevents duplicate data from being inserted.


Topics