为什么使用主键?

发布于 2024-08-20 04:37:52 字数 131 浏览 2 评论 0原文

除了标识表中的唯一列之外,还使用什么主键?难道不能通过简单地在列上使用自动增量约束来完成此操作吗?我知道 PK 和 FK 用于关联不同的表,但这不能仅使用 join 来完成吗?

基本上,在使用主键连接时,数据库正在做什么来提高性能?

What are primary keys used aside from identifying a unique column in a table? Couldn't this be done by simply using an autoincrement constraint on a column? I understand that PK and FK are used to relate different tables, but can't this be done by just using join?

Basically what is the database doing to improve performance when joining using primary keys?

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

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

发布评论

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

评论(6

述情 2024-08-27 04:37:52

主要是为了外键的引用完整性,当您有 PK 时,它还会在幕后创建一个索引,这样您在查找值时就不需要表扫描

Mostly for referential integrity with foreign keys,, When you have a PK it will also create an index behind the scenes and this way you don't need table scans when looking up values

情绪少女 2024-08-27 04:37:52

RDBMS 提供程序通常经过优化以处理具有主键的表。大多数存储有助于优化查询计划的统计信息。这些统计信息对于性能非常重要,尤其是在较大的表上,如果没有主键,它们将无法正常工作,并且最终会得到不可预测的查询响应时间。

大多数数据库最佳实践书籍建议无一例外地使用主键创建所有表,遵循这种做法是明智的。对于初级软件开发人员来说,没有什么比构建没有引用完整性的数据库更能说明问题的了!

RDBMS providers are usually optimized to work with tables that have primary keys. Most store statistics which helps optimize query plans. These statistics are very important to performance especially on larger tables and they are not going to work the same without primary keys, and you end up getting unpredictable query response times.

Most database best practices books suggest creating all tables with a primary key with no exceptions, it would be wise to follow this practice. Not many things say junior software dev more than one who builds a database without referential integrity!

情独悲 2024-08-27 04:37:52

有些 PK 只是一个自动递增列。此外,您通常会使用 PK 和 FK 加入。必须有某种关系才能进行连接。此外,大多数 DBMS 默认情况下会自动为 PK 建立索引,这提高了连接性能以及基于 ID 查询特定记录的性能。

Some PKs are simply an auto-incremented column. Also, you typically join USING the PK and FK. There has to be some relationship to do a join. Additionally, most DBMS automatically index PKs by default, which improves join performance as well as querying for a particular record based on ID.

别忘他 2024-08-27 04:37:52

您可以在查询中不使用主键进行联接,但是,您必须定义一个主键来强制执行数据完整性约束,至少对于 SQL Server 来说是这样。 (外键等..)

另外,这里有一篇有趣的文章供您阅读 主键

You can join without a primary key within a query, however, you must have a primary key defined to enforce data integrity constraints, at least with SQL Server. (Foreign Keys, etc..)

Also, here is an interesting read for you on Primary Keys.

慈悲佛祖 2024-08-27 04:37:52

在 Microsoft Access 中,如果您有一个链接表(例如 SQL Server),则源表必须具有主键才能使链接表可写。至少,Access 2000 和 SQL Server 6.5 就是这样。以后的版本可能会有所不同。

In Microsoft Access, if you have a linked table to, say, SQL Server, the source table must have a primary key in order for the linked table to be writeable. At least, that was the case with Access 2000 and SQL Server 6.5. It may be different with later versions.

情魔剑神 2024-08-27 04:37:52

密钥关系到数据完整性和身份识别。通过在数据库中设置约束来排除可能违反密钥的“坏”数据,可以保证密钥的唯一性。以这种方式保证数据完整性规则的事实正是使密钥可以用作标识符的原因。这适用于任何键。按照惯例,每个表的一个键称为“主”键,但这并不会使其他备用键变得不那么重要。

在实践中,我们需要能够对所有类型的数据(不仅仅是数字)强制执行唯一性规则,以满足数据质量和可用性的要求。

Keys are about data integrity as well as identification. The uniqueness of a key is guaranteed by having a constraint in the database to keep out "bad" data that would otherwise violate the key. The fact that data integrity rules are guaranteed in that way is precisely what makes a key usable as an identifier. That goes for any key. One key per table by convention is called a "primary" key but that doesn't make other alternate keys any less important.

In practice we need to be able to enforce uniqueness rules against all types of data (not just numbers) to satisfy the demands of data quality and usability.

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