How to use Unique Key Constraint on Column in SQL

March 09, 2014 , , , 0 Comments

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:

 Col_name [, col_name [, col_name [, …]]])
Col_name [, col_name [, col_name [, …]]]
  • 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.
This constraint is also applied on columns as same as primary key constraint.

Primary Key Constraint
Foreign Key constraint

Rhett Butler

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google