MySQL中的唯一约束

发布于 2024-07-30 11:31:03 字数 86 浏览 10 评论 0原文

唯一约束默认是索引吗? 如果不是,在 SELECT ... WHERE 子句中使用唯一约束时,它是否具有与索引列相同的性能结果?

谢谢

Is a unique constraint an index by default? If not, does a unique constraint has the same performance results as an indexed column when using it in the SELECT ... WHERE clause?

Thanks

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

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

发布评论

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

评论(5

梦毁影碎の 2024-08-06 11:31:03

唯一约束必然是索引。 您通常将其定义为“UNIQUE INDEX”。 在任何情况下都需要索引来有效地实现唯一约束,因此拥有一个索引并没有什么缺点。

A unique constraint is necessarily an index. You normally define it as "UNIQUE INDEX". An index would be required in any case to efficiently implement a unique constraint, so having one is no disadvantage.

疯到世界奔溃 2024-08-06 11:31:03

约束实际上与索引有很大不同:它只是说 MySQL 应该为您强制执行唯一性。 然而,索引(尽管它可以是唯一的)与硬盘上的物理顺序有关,或者与允许在列上进行高效搜索的附加结构(通常是树)有关。

然而,您可能会将所有这些与定义(通常是聚集的)唯一索引的主键混淆。

A constraint is actually very different from an index: It just says that MySQL is supposed to enforce uniqueness for you. An index however (although it can be unique) is about the physical ordering on your harddisk, or about additional structures (usually a tree) that allows for efficient searching on the column.

You might however be confusing all this with primary keys which define a (usually clustered) unique index.

迷迭香的记忆 2024-08-06 11:31:03

唯一约束是一种表达事实的方式,即某些事物(如属性值的某些组合)在整个关系(“表”)的范围内必须是唯一的。

这是逻辑设计的层面。

索引可能是帮助执行此类约束的有用方法。

这是物理设计层面的。

某些 DBMS 产品可能会根据某些逻辑设计构造(例如 UNIQUE 约束)的存在来推断某些物理设计构造(例如某些索引的存在)。 其他人可能不会。

A unique constraint is a way for expressing the fact that something (like some combination of attribute values) must be unique within the scope of an entire relation ("table")).

That is at the level of LOGICAL design.

An index is a possibly useful means in helping to enforce such a constraint.

That is at the level of PHYSICAL design.

Some DBMS products might infer certain physical design constructs, such as the presence of some index, from the presence of certain logical design constructs, such as a UNIQUE constraint. Others might not.

一身骄傲 2024-08-06 11:31:03

UNIQUE 实际上是对索引的约束,所以是的,UNIQUE 意味着您要强制执行唯一性的字段上有一个索引。

UNIQUE is actually a constraint on an index, so yes, UNIQUE implies that there is an index on the field you're enforcing uniqueness on.

残月升风 2024-08-06 11:31:03

在开始使用唯一约束之前检查主键约束。

主键约束相当于声明唯一约束和非空约束。 如果主键中有多个列,则每一列都会获得非空约束,但唯一约束适用于所有列。

当您声明主键时,DBMS 将为您创建一个索引。 如果您愿意,可以删除索引,但是当 DBMS 进行表扫描以检查唯一性时,您将获得糟糕的性能。

主键约束强制实体完整性,而引用(外键)约束强制引用完整性。 它们共同为确保数据完整性大有帮助。

Check out the primary key constraint before you start using unique constraints.

A primary key constraint amounts to declaring a unique constraint and a not null constraint. If there is more than one column in the primary key, each column gets a not null constraint, but the unique constraint applies to all the columns taken together.

When you declare a primary key, the DBMS will create an index for you. You can drop the index if you like, but you'll get horrible performance when the DBMS does table scans to check for uniqueness.

Primary key constraints enforce entity integrity, while REFERENCES (foreign key) constraints enforce referential integrity. Together, they go a long way towards ensuring data integrity.

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