Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Unique Key and Index with IsUnique=Yes?

I have a table with a primary key, but I want two other columns to be constrained so the combination of the two is guaranteed always to be unique.

(a dumb example: In a BOOKS table, the IBAN column is the primary key, but the combination of the Title and Author columns should also always be unique.)

In the SQL Server Management Studio it's possible to either create a new Index and set IsUnique to Yes, or I can create a new Unique Key.

What is the difference between the two approaches, and which one suits best for which purposes?

like image 571
JacobE Avatar asked Feb 19 '09 11:02

JacobE


1 Answers

Creating a UNIQUE constraint is a clearer statement of the rule. The IsUnique attribute of the index is an implementation detail - how the rule is implemented, not what the rule is. The effect is the same though.

like image 125
Tony Andrews Avatar answered Sep 19 '22 20:09

Tony Andrews