每个表都应该有一个主键吗? [英] Should each and every table have a primary key?

查看:172
本文介绍了每个表都应该有一个主键吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个数据库表,并且没有为其分配逻辑主键。所以,我正在考虑离开它没有主键,但我正在砍伐一点点内疚。我是不是该?

I'm creating a database table and I don't have a logical primary key assigned to it. So, I'm thinking about leaving it without a primary key, but I'm felling a bit guiltly about it. Should I?

每个表格是否都有主键?

编辑:好吧,好吧...我创建了主键!你现在高兴了? :)

Okay, okay... I've created the primary key! Are you happy now? :)

推荐答案

简短答案:

长的答案:


  • 您需要您的表格才能加入某些内容

  • 你希望你的表被聚集,你需要某种主键。

  • 如果你的表设计不需要一个主键,重新思考你的设计: 。为什么要保留相同的记录?

在MySQL中,如果没有明确指定,InnoDB存储引擎总是创建主键,

In MySQL, the InnoDB storage engine always creates a primary key if you didn't specify it explicitly, thus making an extra column you don't have access to.

请注意,主键可以是复合的。

Note that a primary key can be composite.

>如果有多对多链接表,则在链接中涉及的所有字段上创建主键。因此,您确保您没有两个或更多记录描述一个链接。

If you have a many-to-many link table, you create the primary key on all fields involved in the link. Thus you ensure that you don't have two or more records describing one link.

除了逻辑一致性问题,大多数RDBMS引擎将受益于将这些字段包含在唯一

Besides the logical consistency issues, most RDBMS engines will benefit from including these fields in a unique index.

由于任何主键涉及创建唯一索引,因此您应该声明它并获得逻辑一致性和性能。

And since any primary key involves creating a unique index, you should declare it and get both logical consistency and performance.

请参阅我的博客中的文章,了解为什么应该始终为唯一数据创建唯一索引:

See this article in my blog for why you should always create a unique index on unique data:

PS 有一些非常非常的特殊情况,

P.S. There are some very, very special cases where you don't need a primary key.

大多数情况下,它们包括日志表,它们由于性能原因没有任何索引。

Mostly they include log tables which don't have any indexes for performance reasons.

这篇关于每个表都应该有一个主键吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆