Will it ever happen that we design a table that doesn't need a primary key?
我们是否会设计一个不需要主键的表?
4 个解决方案
#1
19
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.
更不用说,如果有人需要在你的表上做一些事情,而你没有主键,那么你就是坏人。
#2
6
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.
这种情况很少见,而且很可能在大多数情况下都是错误的,但它们确实存在,这样的实例是有效的。
#3
3
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. 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. 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
例如,一个大的日志表,不使用主键,您可以拥有更小的数据大小和更快的插入速度
#4
1
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, e.g. user_name
column in users
table are usually made unique and indexed, so you may choose to skip PK here. But it is still a bad idea because PK can be used as foreign key for referential integrity.
主键不是强制的,但是创建没有主键的表不是一个好的实践。DBMS在PK上创建了自动索引,但是您可以创建一个独特的列,并且索引它,例如用户表中的user_name列通常是唯一的,并且是索引的,所以您可以选择在这里跳过PK。但是这仍然是一个坏主意,因为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。
#1
19
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.
更不用说,如果有人需要在你的表上做一些事情,而你没有主键,那么你就是坏人。
#2
6
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.
这种情况很少见,而且很可能在大多数情况下都是错误的,但它们确实存在,这样的实例是有效的。
#3
3
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. 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. 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
例如,一个大的日志表,不使用主键,您可以拥有更小的数据大小和更快的插入速度
#4
1
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, e.g. user_name
column in users
table are usually made unique and indexed, so you may choose to skip PK here. But it is still a bad idea because PK can be used as foreign key for referential integrity.
主键不是强制的,但是创建没有主键的表不是一个好的实践。DBMS在PK上创建了自动索引,但是您可以创建一个独特的列,并且索引它,例如用户表中的user_name列通常是唯一的,并且是索引的,所以您可以选择在这里跳过PK。但是这仍然是一个坏主意,因为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。