2013-05-11 54 views
2

我正在创建一个类似于以下情况的数据库。我可以从两个表中分配一个外键吗?

表1:

CustomerType1(柱:客户ID,...)

表2:

CustomerType2(柱:客户ID,...)

表3:

Orders(Columns:OrderId,CustomerId ...)

否w,如何将订单表的customerId与CustomerType1和CustomerType2表的customerId列相关联?

(我的工作在Windows手机应用程序,所以如果你能帮助我在创造类似上述情况的数据库,使用的属性比将是有益的)

感谢

+0

是你能解决你的问题?我的回答对你有帮助吗? – Juampi 2013-05-12 03:17:58

回答

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柱的装置向客户表的引用。也可以通过使用Orders.CustomerIdCustomer.CustomerId列来实现对订单表和客户表的引用。

为了清楚起见,表CustomerType1CustomerType2Orders都将有一个外键约束如下:

FOREIGN KEY (CustomerId) REFERENCES Customer(CustomerId) 
+0

但我又如何将客户表的customerId引用到前两个表的customerId中。情况再次类似。 – vijay053 2013-05-11 14:29:01

+0

不,您要做的是您使CustomerType1.CustomerId引用Customer.CustomerId和CustomerType2.CustomerId引用Customer.CustomerId。而不是相反。外键引用有一个方向。 – Juampi 2013-05-11 14:29:44

相关问题