本文介绍了Sql Server Unique Key也是索引吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表中有一个列(例如UserName),我想确保它是唯一的。所以我为该列创建了一个唯一的密钥,并将其命名为IX_Users_UserName。

I've got a column in a table (eg. UserName) which I want to make sure is unique. So I create a unique key for that column and call it IX_Users_UserName.

现在,如果我根据用户名进行大量搜索,我想确保有用户该字段的索引。

Now, if I do lots of searching for users based on their username I want to make sure there is an index for that field.

我是否需要创建单独的索引,或者是唯一的密钥也被视为索引,就像主键是群集唯一键一样?

Do I need to create a separate index, or is the unique key also considered an index, just like the primary key is a clustered unique key?

推荐答案

更改表以向列添加唯一约束

Alter table to add unique constraint to column:

ALTER TABLE作者ADD CONSTRAINT
IX_Authors_Name UNIQUE(名称)GO

ALTER TABLE Authors ADD CONSTRAINT IX_Authors_Name UNIQUE(Name) GO

来自 MSDN 。

More information from MSDN.

FWIW - 如果您的约束没有创建索引,我会避免命名它 IX _ 因为通常会假设它与一个相关联(IX =指数)。

FWIW -- if your constraint doesn't create an index, I would avoid naming it IX_ as that would typically be assumed to be associated with one (IX = Index).

这篇关于Sql Server Unique Key也是索引吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!