为什么主键必须是最小的?
主键是唯一标识表中的行的属性或属性集。但主键不仅必须是唯一的,而且必须是最小的。为什么有必要?
A primary key is an attribute or set of attributes that uniquely identifies a row in a table. But a primary key has not only to be unique, but also minimal. Why is that necessary?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
识别最小键很重要的原因是为了确保对这些键的依赖关系不存在冗余。冗余可能会导致异常和不正确的结果。
极简性本质上是一个语义问题,而不是纯粹的结构特征,因此数据库实现不一定需要它。例如,SQL 允许您在任何超级键上创建“主键”,这可能不是不可约的超级键。
最小性与存储大小无关,因为最小意味着不可约,但并不意味着最小。
The reason why it's important to identify minimal keys is to ensure that there is no redundancy in dependencies on those keys. Redundancy can cause anomalies and incorrect results.
Minimality is essentially a semantic matter rather than purely a structural feature so it isn't necessarily required by the database implementation. For instance SQL allows you to create a "PRIMARY KEY" on any superkey, which may not be an irreducible superkey.
Minimality has nothing to do with storage size because minimal means irreducible, it does not mean smallest.
主键应该尽可能小,因为它们只需足够大来唯一标识行。就所使用的索引空间而言,任何其他操作都是浪费。
换句话说,如果我有一个唯一的列
username
和另一个唯一的列student_id
,那么从这两个列构建的主键就是一种浪费。处理这个问题的正常方法是使用一个作为主键,然后在另一个上有唯一的约束/索引。Primary keys should be minimal since they only have to be large enough to uniquely identify the row. Anything else is a waste in terms on index space used.
In other words, if I have a unique column
username
along with another unique columnstudent_id
, a primary key built from both of those is a waste. The normal way to handle that would be to use one as the primary key and then have a unique constraint/index on the other.