我可以从两个表中分配外键吗?

时间:2013-05-11 14:22:40

标签: c# database windows-phone-8 foreign-keys

我正在创建一个类似于以下情况的数据库。

表1:

CustomerType1(列:CustomerId,...)

表2:

CustomerType2(列:CustomerId,...)

表3:

订单(列:OrderId,CustomerId ...)

现在,我如何将customerId of orders表与CustomerType1和CustomerType2表的customerId列相关联?

(我正在开发一个Windows手机应用程序,所以如果你可以帮助我创建一个类似于上述情况的数据库所用的属性,那么它会很有帮助)

由于

1 个答案:

答案 0 :(得分:2)

您的数据库应由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.CustomerIdCustomerType2.CustomerId通过Customer.CustomerId列提供对Customer表的引用。此外,使用Orders.CustomerIdCustomer.CustomerId列可以实现对Orders表和Customer表的引用。

为清楚起见,表格CustomerType1CustomerType2Orders都有外键约束,如下所示:

FOREIGN KEY (CustomerId) REFERENCES Customer(CustomerId)