MySQL子表需要有主键吗?

发布于 2024-11-13 08:53:11 字数 455 浏览 1 评论 0原文

我(所有 InnoDB)有一个带有主键 some_id 的父表 tbl1 和带有 another_id 的子(引用)表 tbl2 > 引用tbl1.some_id 作为外键。 tbl2.another_id 不是 tbl2 的主键,因为这是一个一对多关系。我的子表是否也应该有一个主键 - 也许用于索引目的?

这里的线程几乎回答了我的问题,但提出了更多有关复合键的问题,因此我在其他地方发布了另一个关于该问题的问题......

I have (all InnoDB) a parent table tbl1 with primary key some_id and child (referencing) table tbl2 with another_id referencing tbl1.some_id as foreign key. tbl2.another_id is not the primary key of tbl2 as this is a one to many relationship. Should my child table also have a primary key - maybe for indexing purposes?

The thread here almost answered my question but raised more questions about composite keys so I posted another question about that elsewhere...

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

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

发布评论

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

评论(2

失与倦" 2024-11-20 08:53:11

是的,您需要在 tbl2 上有一个主键。这将使您能够唯一地标识每一行,这在将来有许多用途。您可以通过谷歌搜索了解按键的重要性。

Yes, you need to have a primary key on tbl2. This will allow you to uniquely identify each row which will be useful for many purposes in the future. You can read up on the importance of keys with a google search.

琴流音 2024-11-20 08:53:11

当我过去这样做时,我总是使用自动递增主键和外​​键。您可以加入外键并具有所需的一对多关系。您是否反对在表中拥有主键和外键?我一直发现它对于快速识别第二个表中的行很有用。

"InnoDB permits a foreign key to reference any index column or group of columns. 
However, in the referenced table, there must be an index where the referenced 
columns are listed as the first columns in the same order."

http://dev.mysql.com/doc /refman/5.1/en/innodb-foreign-key-constraints.html

When i've done this in the past i have always used an auto incrementing primary key along with a foreign key. You can join on the foreign key and have the required one to many relationship. Are you against having a primary and foreign key in a table? I've always found it useful for quickly identifying rows in the second table.

"InnoDB permits a foreign key to reference any index column or group of columns. 
However, in the referenced table, there must be an index where the referenced 
columns are listed as the first columns in the same order."

http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html

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