简体   繁体   English

当我们的桌子不需要主键时?

[英]When we don't need a primary key for our table?

我们是否会设计一个不需要主键的表?

No. 没有。

The primary key does a lot of stuff behind-the-scenes, even if your application never uses it. 即使您的应用程序从未使用它,主键也会在幕后执行很多操作。

For example: clustering improves efficiency (because heap tables are a mess). 例如: 集群提高了效率(因为堆表很乱)。

Not to mention, if ANYONE ever has to do something on your table that requires pulling a specific row and you don't have a primary key, you are the bad guy. 更不用说,如果任何人必须在你的桌子上做某事需要拉一个特定的行并且你没有主键,那么你就是坏人。

Yes. 是。

If you have a table that will always be fetched completely, and is being referred-to by zero other tables, such as some kind of standalone settings or configuration table, then there is no point having a primary key, and the argument could be made by some that adding a PK in this situation would be a deception of the normal use of such a table. 如果你有一个总是被完全提取的表,并且被零其他表(例如某种独立设置或配置表)引用,那么没有必要有一个主键,并且可以建立参数有些人认为在这种情况下添加PK会欺骗这种表的正常使用。

It is rare , and probably when it is most often done it is done wrongly , but they do exist, and such instances can be valid. 这种情况很少见 ,并且可能在最常见的情况下完成它是错误的 ,但它们确实存在,并且这种情况可能是有效的。

Depends. 要看。

What is primary key / unique key? 什么是主键/唯一键?

In relational database design, a unique key can uniquely identify each row in a table, and is closely related to the Superkey concept. 在关系数据库设计中,唯一键可以唯一地标识表中的每一行,并且与Superkey概念密切相关。 A unique key comprises a single column or a set of columns. 唯一键包括单个列或一组列。 No two distinct rows in a table can have the same value (or combination of values) in those columns if NULL values are not used. 如果未使用NULL值,则表中的两个不同行可以在这些列中具有相同的值(或值的组合)。 Depending on its design, a table may have arbitrarily many unique keys but at most one primary key. 根据其设计,表可以具有任意多个唯一键但最多只有一个主键。

So, when you don't have to differentiate (uniquely identify) each row, 因此,当您不必区分(唯一标识)每一行时,
you don't have to use primary key 您不必使用主键

For example, a big table for logs, 例如,一个用于日志的大表,
without using primary key, you can have fairly smaller size of data and faster for insertion 不使用主键,您可以拥有相当小的数据大小,并且插入速度更快

Primary key not mandatory but it is not a good practice to create tables without primary key. 主键不是强制性的,但创建没有主键的表不是一个好习惯。 DBMS creates auto-index on PK, but you can make a column unique and index it, eg user_name column in users table are usually made unique and indexed, so you may choose to skip PK here. DBMS在PK上创建自动索引,但是您可以使列唯一并对其进行索引,例如, users表中的user_name列通常是唯一的并且已编制索引,因此您可以选择在此处跳过PK。 But it is still a bad idea because PK can be used as foreign key for referential integrity. 但它仍然是一个坏主意,因为PK可以用作引用完整性的外键。

In general, you should almost always have PK in a table unless you have very strong reason to justify not having a PK. 一般来说,除非你有充分的理由证明没有PK,否则你应该几乎总是在表中使用PK。

Link tables (in many to many relationship) may not have a primary key. 链接表(在多对多关系中)可能没有主键。 But, I personally like to have PK in those tables as well. 但是,我个人也喜欢在这些表中使用PK。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM