建立表之间的一对多关系
我有两个表,我需要在它们之间建立一对多关系,例如: 1 客户可以有多个订单。 在订单表上创建键以便订单中可以有许多行与一个/相同的客户详细信息相关的好方法是什么? 当有 2 行具有相同的 CustomerID 插入到订单中时(CustomerID 外键上的一对多关系),我可以遇到这样的情况吗?
即, 客户表有列:
CustomerID (key)
Name
OtherColumns
订单:
<IsaKeyNeeded>
customerID (foreign key)
OrderName
我的另一个问题是“订单”是否需要有自己的密钥?
I have two tables and I need to establish a 1-many relation among them, as an example:
1 Customer can have many Order(s).
What is a good way to create keys on the Order table such that there can be many rows in Orders, relating to one/same Customer details? i.e can I have cases when there are 2 rows with same CustomerID inserted into Order (1-many relation on CustomerID foreign key)
Assume
Customer table has columns:
CustomerID (key)
Name
OtherColumns
Order:
<IsaKeyNeeded>
customerID (foreign key)
OrderName
Another question I have is does 'Order' need to have it's own key?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已正确设置...订单表应该有一个指向客户表的外键。这建立了一个客户与多个订单的关系。只是不要将 CustomerID 设置为唯一键。
要回答您的其他问题...是的,订单表应该有自己的主键。
You have it set up correctly ... the Order table should have a foreign key to the Customer table. This establishes the relationship of one customer to many orders. Just do not make the CustomerID a unique key.
To answer your other question ... yes, the Order table should have it's own primary key.