我正在创建一个类似于以下情况的数据库。
表1:
CustomerType1(列:CustomerId,...)
表2:
CustomerType2(列:CustomerId,...)
表3:
订单(列:OrderId、CustomerId...)
现在,如何将订单表的 customerId 与 CustomerType1 和 CustomerType2 表的 customerId 列关联起来?
(我正在开发一个 Windows Phone 应用程序,因此如果您可以帮助我创建与上述情况类似的数据库中使用的属性,那将会很有帮助)
谢谢
最佳答案
您的数据库应由 4 个表组成:
- Customer(CustomerId, common stuff to all customers)
- CustomerType1(CustomerId, specific stuff to type 1 customers)
- CustomerType2(CustomerId, specific stuff to type 2 customers)
- Orders(OrderId, CustomerId, other order stuff)
表列 CustomerType1.CustomerId
和 CustomerType2.CustomerId
通过 Customer.CustomerId
列提供对 Customer 表的引用。此外,还可以通过使用 Orders.CustomerId
和 Customer.CustomerId
列来实现对 Orders 表和 Customer 表的引用。
为了清楚起见,表 CustomerType1
、CustomerType2
和 Orders
都将具有外键约束,如下所示:
FOREIGN KEY (CustomerId) REFERENCES Customer(CustomerId)
关于c# - 我可以从两个表中分配外键吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16498074/