SQL UNIQUE
Constraint
SQL UNIQUE Constraint
The UNIQUE constraint uniquely identifies each record in a database table.
Both UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a column or a set of columns.
A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it.
Note that each table can have multiple UNIQUE constraints, but each table can only have one PRIMARY KEY constraint.
SQL UNIQUE Constraint When Creating a Table
The following SQL creates a UNIQUE constraint on the "P_Id" column when the "Persons" table is created:
MySQL:
SQL Server / Oracle / MS Access:
To name a UNIQUE constraint and define a UNIQUE constraint on multiple columns, use the following SQL syntax:
MySQL / SQL Server / Oracle / MS Access:
SQL UNIQUE Constraint When Altering a Table
When a table has already been created, to create a UNIQUE constraint on the "P_Id" column, use the following SQL:
MySQL / SQL Server / Oracle / MS Access:
To name a UNIQUE constraint and define a UNIQUE constraint on multiple columns, use the following SQL syntax:
MySQL / SQL Server / Oracle / MS Access:
Dropping a UNIQUE Constraint
To drop a UNIQUE constraint, use the following SQL:
MySQL:
SQL Server / Oracle / MS Access: