使列唯一强制创建索引吗?

Fra*_*ger 4 sql sql-server

在SQL Server 2005+(我使用两者)中,是否向UNIQUE列添加约束会自动创建索引,还是我还应该CREATE INDEX

And*_*mar 8

请参阅此MSDN文章:

数据库引擎自动创建UNIQUE索引以强制执行UNIQUE约束的唯一性要求.

如果您确实创建了一个索引,那么最终会得到两个索引,如下例所示:

create table TestTable (id int)
alter table TestTable add constraint unique_id unique (id)
create unique index ix_TestTable_id on TestTable (id)

select * from sys.indexes where [object_id] = object_id('TestTable')
Run Code Online (Sandbox Code Playgroud)

这将在TestTable上显示两个唯一索引; 以及表示表本身的HEAP.