Unique constraint is something like primary key constraint, which is used to make a record unique within all other records in sql database.
The unique constraint is used to enforce uniqueness on non-primary key columns. A primary key constrain column automatically includes a restriction for uniqueness. The unique constraint is similar to the primary key constraint except that it allows one NULL row.
Multiple unique constrains can be created on a table. The syntax of applying the unique constraint when creating table is:
CREATE TABLE table_Name
(
Col_name [CNTRAIN cstraint_name UNIQUE [CLUSTERED | NONCLUSTERED]
Col_name [, col_name [, col_name [, …]]])
Col_name [, col_name [, col_name [, …]]]
)
Where
Primary Key Constraint
Foreign Key constraint
The unique constraint is used to enforce uniqueness on non-primary key columns. A primary key constrain column automatically includes a restriction for uniqueness. The unique constraint is similar to the primary key constraint except that it allows one NULL row.
Multiple unique constrains can be created on a table. The syntax of applying the unique constraint when creating table is:
CREATE TABLE table_Name
(
Col_name [CNTRAIN cstraint_name UNIQUE [CLUSTERED | NONCLUSTERED]
Col_name [, col_name [, col_name [, …]]])
Col_name [, col_name [, col_name [, …]]]
)
Where
- Constraint_name species the name of the constraint to created.
- CLUSTERED | NONCLUSTERED are keywords that specify if a clustered or a nonclustered index is to be created for the unique constraint.
- Col_name specifies the name of the column(s) on which the unique constraint is to be defined.
Primary Key Constraint
Foreign Key constraint