Add a Unique Constraint to a Column


InterServer Web Hosting and VPS
Sony Playstation PS4 Pro Custom Build NVMe SSD Drive - Enclosure case and Crucial NVMe SSD
PS4 Pro Custom External SSD NVMe Drive

How I built a PS4 Pro Custom External SSD NVMe Drive...

NUC8i7BEH with memory
Intel NUC8I7BEH Review and Custom Build

This is my review of the Intel NUC8i7BEH and a summary of my build...

Card image cap
Asus M5A88-M EVO Review

This is my review of the Asus M5A88-M EVO motherboard...

Corsair Vengeance Blue 8GB (2x4GB) DDR3 PC3-12800C9 1600MHz Dual Channel Kit
Corsair Vengeance Blue Review

This is my review of the Corsair Vengeance Blue 8GB (2x4GB) DDR3 PC3-12800C9 1600MHz Dual Channel Kit memory...

Robotic Arm with USB PC Interface Review
Robotic Arm with USB PC Interface Review

This is my review of the Robotic Arm with USB PC Interface...

Add a Unique Constraint to a Column

Here we look at how to add a unique constraint to a database column.

You may have a column in your database for which you want to enforce the values to be unique. You can do this with a primary key, but you may have other columns which aren't your primary key but you still you want to have unique (or distinct) values in the column.

In the example below, we add a Unique constraint to our column called 'myColumnName', which will then ensure that only unqiue values are allowed in that column.


-- Make the myColumnName column store only unqiue values
ALTER TABLE myTable ADD UNIQUE (myColumnName)

In the second example, shown below, we make the 'myColumnName' and 'mySecondColumnName' columns combined together store unique values. That is, when the value of 'myColumnName' is combined with the value of 'mySecondColumnName', the combined value is unique, almost like a Composite key.


-- Make the myColumnName and mySecondColumnName columns combined store only unqiue values
ALTER TABLE myTable ADD UNIQUE (myColumnName, mySecondColumnName)

Sponsored Links
SolarSystem - AngularJS Demo
SolarSystem - Angular JS Demo

Check out my first Angular JS demo hosted in Azure, it helped win my first Angular contract. Put the sound on.