Unique key vs. unique index on SQL Server 2008
A unique constraint is implemented behind the scenes as a unique index, so it doesn't really matter how you specify it. I tend to implement it simply as:
ALTER TABLE dbo.foo ADD CONSTRAINT UQ_bar UNIQUE(bar);
Some people create a unique index instead, e.g.
CREATE UNIQUE INDEX IX_UQ_Bar ON dbo.foo(bar);
The difference is in the intent - if you are creating the constraint to enforce uniqueness/business rules, you create a constraint, if you are doing so to assist query performance, it might be more logical to create a unique index. Again, under the covers it's the same implementation, but the road you take to get there may help document your intent.
I think there are multiple options to adhere to both previous Sybase functionality as well as to adhere to the ANSI standard (even though unique constraints don't adhere to the standard 100%, since they only allow one NULL value - a unique index, on the other hand, can work around this by adding a WHERE
clause (WHERE col IS NOT NULL
) on SQL Server 2008 and greater).
One additional thing to mention, is that if you create index, you can specify included columns , this can help your sql code to work faster if there is some searching by country_name.
CREATE UNIQUE NONCLUSTERED INDEX IX_UQ_Bar
ON dbo.foo (
bar
)
INCLUDE (foo_other_column)
GO
SELECT foo_other_column FROM Foo WHERE bar = 'test'
SQL server will store "foo_other_column" in index itself. In case of unique constraint it will first find index of 'test', then will search for row in foo table and only there it will take "foo_other_column".
Other than the excellent answers above, I'd add my 2 cents here.
Unique key is a constraint and it uses unique index to enforce itself. Just as primary key is usually enforced by a clustered unique index. Logically speaking, a constraint and an index are two different things. But in RDBMS, a constraint can be physically implemented through an index.
If a table is created with an unique constraint in sql server, you will see both a constraint object and a unique index
create table dbo.t (id int constraint my_unique_constraint unique (id));
select [Constraint]=name from sys.key_constraints
where parent_object_id = object_id('dbo.t');
select name, index_id, type_desc from sys.indexes
where object_id = object_id('dbo.t')
and index_id > 0;
we will get the following (a constraint and an index)
However, if we do not create a constraint but just a unique index as the following
create table dbo.t2 (id int );
create unique index my_unique_constraint on dbo.t2 (id);
select [Constraint]=name from sys.key_constraints
where parent_object_id = object_id('dbo.t2');
select name, index_id, type_desc from sys.indexes
where object_id = object_id('dbo.t2')
and index_id > 0
You will see there is NO constraint object created (only an index created).
From "theoretical" perspective, in SQL Server, a constraint is an object with object_id value and is schema-bound, while an index is not an object and has no object_id value and no schema related.
There is no difference between unique index or a unique constraint and also there is no performance difference. However there are some differences for creation where some index creation options are not available for unique constraints.