唯一键和IsUnique=Yes的索引之间有什么区别?[英] What is the difference between Unique Key and Index with IsUnique=Yes?

本文是小编为大家收集整理的关于唯一键和IsUnique=Yes的索引之间有什么区别?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个带有主键的表,但是我希望其他两个列受到约束,因此两者的组合始终是唯一的.

(一个愚蠢的示例:在书籍表中,iban列是主要键,但是标题和作者列的组合也应该始终是唯一的.)

SQL Server Management Studio中可以创建一个新索引并将Isunique设置为"是",或者我可以创建一个新的唯一键.

两种方法有什么区别,哪种方法最适合哪种目的?

推荐答案

创建唯一的约束是对规则的更清晰的陈述.该索引的Isunique属性是实现细节 - 该规则的实施方式,而不是该规则是什么.效果是相同的.

其他推荐答案

2之间有明显的差异. 独特的约束定义了哪些列组合必须是唯一的. 唯一的索引只是确保以上始终有效的一种方式. 但是,有可能具有支持独特约束的非唯一索引. (如果约束是可推迟的=仅在提交时间有效,但可以在交易的中间被打破)

其他推荐答案

只是为了使您知道,创建唯一约束SQL Server时,SQL Server将在场景后面创建索引

本文地址:https://www.itbaoku.cn/post/597692.html

问题描述

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?

推荐答案

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.

其他推荐答案

There is a clear difference between the 2. A unique constraint defines what combination of columns has to be unique. A unique index is just a way of making sure the above is always valid. But it's possible to have a non-unique index supporting a unique constraint. (if the constraint is deferable = Only has to be valid at commit time but is allowed to be broken in the middle of a transaction)

其他推荐答案

Just so that you know, when you create a unique constraint SQL Server will create an index behind the scenes