详细信息表是否包含主从关系中的 ID 列?
在主从关系中,明细表是否包含 ID 列?
或者,应该吗?
在 Northwind 数据库中,OrderDetail 没有 ID 列。
In Master-Detail relationships, do Detail-tables contain ID columns?
Or, should it?
In Northwind database, OrderDetail doesn't have an ID column.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
任何包含您希望能够检索的内容的数据表都应始终包含主键 - 这就是它的主要工作:唯一地标识单行。
仅仅因为详细信息属于主行并不意味着它们本身不是完整的数据行 - 它们需要以某种方式进行识别。
所以是的 - 我会建议任何详细表都具有某种形式的主键(“ID”)。无论是否以某种方式连接到主 ID,您都可以根据具体情况来决定 - 但必须有一种方法以某种方式独立地识别每个详细信息行。
如果您有一个 M:N 关系的链接表,通常情况下,其中只有两个外键列(对于表 1 和 2)。这通常已经足够好了,因为这两个 FK 的组合是独一无二的。
向此类表添加单独的 ID 的唯一原因是您需要添加描述关系本身的其他信息。但即便如此,只要 (key1, key2) 组合是唯一的,通常就足够了。
Any data table that you have that contains something you want to be able to retrieve should always contain a primary key - that's it's main job: uniquely identify a single row.
Just because the details belong to a master row doesn't mean they're not full data rows themselves - they need to be identifiable in some way.
So yes - I would recommend any detail table to have some form of a primary key ("ID"). Whether that's somehow connected to the master-ID or not is something you might decide on a case-by-case basis - but there was to be a way to identify each detail row independently somehow.
If you have a link table for a M:N relationship, typically, you'd only have the two foreign key columns (for tables 1 and 2) in there. That's typically good enough, since the combination of those two FK is unique.
The only reason to add a separate ID to such a table would be if you need to add additional pieces of information that describe the relationship per se. But even then, as long as the (key1, key2) combination is unique, that's typically good enough.