为什么我的唯一约束显示在索引下而不是约束下?

发布于 2024-07-25 02:29:47 字数 262 浏览 1 评论 0原文

我在 tsql 表上创建了一个约束,如下所示:

alter table disabledqualities
add constraint uc_uIdQualCode
unique (userId, qualitycode)

在 MSSMStudio 中,约束显示在索引下而不是约束下。

为什么?

--编辑--
我知道它创建一个索引来强制执行约束,但是为什么会有一个名为“约束”的节点呢?

I created a constraint on my tsql table like this:

alter table disabledqualities
add constraint uc_uIdQualCode
unique (userId, qualitycode)

In MSSMStudio the constraint shows up under indexes rather then under constraints.

Why?

--EDIT--
I understand that it creates an index to enforce the constraint, but then why is there a node called "constraints"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

╰つ倒转 2024-08-01 02:29:47

SQL Server 在幕后创建一个索引来强制执行约束,

这里是另一种编写方式,通过添加非聚集告诉 sql server 使用非聚集索引,如果您没有聚集的 PK,您还可以创建聚集(默认)或另一个聚集索引已经

alter table disabledqualities
add constraint uc_uIdQualCode
unique nonclustered (userId, qualitycode)

[编辑]
该节点将添加检查约束,唯一约束将添加到索引下,

无论哪种方式都远离向导

SQL Server creates an index behind the scene to enforce the constraint

here is another way of writing that by adding nonclustered telling sql server to use a nonclustered index, you can also create a clustered on providing that you don't have a PK that is clustered (the default) or another clustered index already

alter table disabledqualities
add constraint uc_uIdQualCode
unique nonclustered (userId, qualitycode)

[edit]
that node is to add check constraint, unique constraints are added under indexes

either way stay away from wizards

℡Ms空城旧梦 2024-08-01 02:29:47

检查约束和默认约束显示在约束节点下。

Check constraints and default constraints are shown under the constraints node.

痞味浪人 2024-08-01 02:29:47

我想您可能不明白约束中可能会显示什么,因为您不知道检查约束是什么。 检查约束将检查插入或更新时的数据,以查看其是否满足某种业务规则。 它用于确保数据完整性。 例如,如果您有一个整数字段,该字段应仅包含 1,4 或 5 的值,那么您将设置一个检查约束以确保 9 不会添加到该字段中。 日期字段的检查约束可能指定它必须晚于 PlannedCompletionDate 字段的当前日期和时间,或者 CompletionDate 必须晚于 StartDate。 这些都是在限制下出现的事情。

I'm thinking perhaps you don't understand what might show up in constraints becasue you don't know what a check constraint is. A check constraint will check the data on insert or update to see if it meets some sort of business rule. It is used for ensuring data integrity. For instance if you have an integer field that should only contain the values of 1,4 or 5 then you would set up a check constraint to make sure that 9 isn't ever added to the field. A check constraint on date field might specify that it must be later than the current date and time for a field that is the PlannedCompletionDate or that CompletionDate must be later than StartDate. These are the kind of things that show up under constraints.

挽清梦 2024-08-01 02:29:47

SQL 使用索引来强制执行唯一约束。

SQL uses indexes to enforce unique constraints.

不…忘初心 2024-08-01 02:29:47

我知道它创建一个索引来强制执行约束,但是为什么会有一个名为“约束”的节点?

该节点用于显示CHECK约束。

I understand that it creates an index to enforce the constraint, but then why is there a node called "constraints"?

This node is to display CHECK constraints.

歌入人心 2024-08-01 02:29:47

因为UNIQUE约束可以在sysindexes系统视图中找到

select * from sysindexes where name = 'uc_uIdQualCode'

简直就是逻辑
要从 Studio 添加新的唯一约束,您需要单击“管理索引和键”按钮。 因此,如果您从索引添加它,则必须在索引中看到它:)

Because UNIQUE constraints can be found in sysindexes system view

select * from sysindexes where name = 'uc_uIdQualCode'

It is simply logical
To add new Unique constaint from Studio you need to click Manage Index and Keys button. So if you are adding it from Indexes, you have to see it in Indexes :)

南笙 2024-08-01 02:29:47

真正的答案是“因为微软是这么说的。如果你想要答案,你就得问他们”。

The true answer is "because Microsoft said so. If you want the answer, you'll have to ask them".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文