Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unique Constraint vs Unique Index

I’m interested in learning which technique developers prefer to use to enforce uniqueness in SQL Server: UNIQUE CONSTRAINT or UNIQUE INDEX. Given that there is little difference in the physical implementation of each, how do you decide which is best?

Are there reasons other than performance to evaluate the best solution?

Are there database management advantages to one or the other?

like image 886
bobs Avatar asked Jul 21 '10 04:07

bobs


People also ask

What is the difference between unique constraint and unique index in Oracle?

A constraint has different meaning to an index. It gives the optimiser more information and allows you to have foreign keys on the column, whereas a unique index doesn't.

Is a unique constraint also an index?

Yes, absolutely. A unique constraint creates a unique index.

What is difference between constraint and index?

Index - improves the performance of retrieval and sort operations on Table data. Unique Constraints - a combination of values that uniquely identify a row in the Table. Foreign Key - a column (or collection of columns) that enforce a relationship between two Tables.

What is difference between index and unique index?

Index: It is a schema object which is used to provide improved performance in the retrieval of rows from a table. Unique Index: Unique indexes guarantee that no two rows of a table have duplicate values in the key column (or columns).


2 Answers

This MSDN article comparing the two is for SQL Server 2000: http://msdn.microsoft.com/en-us/library/aa224827(SQL.80).aspx

For most purposes, there's no difference - the constraint is implemented as an index under the covers. And though there's the ability to disable the constraint, it doesn't actually work in SQL Server.

It only matters if you want to tweak things like FILLFACTOR, etc for which way you want to implement the unique constraint.

SQL Server 2008+ added INCLUDE to provide more efficient covering indexes. Filtered indexes = unique constraint over a subset of rows/ignore multiple null etc.

like image 51
OMG Ponies Avatar answered Sep 30 '22 17:09

OMG Ponies


They are not significantly different. When you create a unique constraint, SQL Server will automatically create a unique index for you.

With the syntax for creating an index, you may have better control defining a unique index to specify clustered/nonclustered, included columns, filegroup, index filtering (SqlSvr2008), etc.

A constraint is preferable in most cases because it expresses the intent of the uniqueness: it is a constraint. An index does not convey this intent.

As for manageability, the impact is minimal. You can manage the index (rebuild, reorg) as if it were created independently of the constraint. The only difference is that the constraint depends on the index, so to drop the index, you must also drop the constraint.

like image 32
Jeff Meatball Yang Avatar answered Sep 30 '22 16:09

Jeff Meatball Yang