MySQL子表需要有主键吗?
我(所有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您需要在
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.当我过去这样做时,我总是使用自动递增主键和外键。您可以加入外键并具有所需的一对多关系。您是否反对在表中拥有主键和外键?我一直发现它对于快速识别第二个表中的行很有用。
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.
http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html