From the course: SQL Server 2008 Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Creating unique constraints

Creating unique constraints - SQL Server Tutorial

From the course: SQL Server 2008 Essential Training

Start my 1-month free trial

Creating unique constraints

Another kind of constraint you can work with is what's called a unique constraint. Now in fact, we already have one of these on most of our tables. If you have a primary key, you have a unique constraint. That primary key cannot be duplicated. In this case, there can only be one ProductID with 1008 in it. But occasionally, you will want to define another column as being a unique constraint, that the value cannot occur more than once in the entirety of the column in that table. Now right now none of these columns should be made unique. Certainly not UnitsInStock and certainly not Color. That repeats quite a lot. We might have ProductName made unique, but not the way that we work with it. For example, I have a Road-650 58 in Black and I have a Road-650 58 product in Red and a Road-650 58 product in Green. I can't make ProductName unique. What would be interesting, however, is if I could force uniqueness on a combination of the two. What that allows me to do is make sure that they aren't…

Contents