多个外键到单列 [英] Multiple foreign keys to a single column

查看:153
本文介绍了多个外键到单列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为客户/订单系统定义一个数据库,其中有两种非常不同类型的客户。因为他们是如此不同,有一个单一的客户表将是非常丑陋的(它将被填满空列,因为它们是无意义的一种类型)。



他们的订单虽然是相同的格式。可以在我的订单表中有一个 CustomerId 列,其中有两个客户类型的外键?我已经在SQL服务器中设置了它,它给了我没有问题创建关系,但我还没有尝试插入任何数据。



另外,我打算使用nHibernate作为ORM,可以通过这样的关系引入任何问题吗?

解决方案

p>否,您不能将单个字段作为两个不同表的外键。你怎么知道在哪里找钥匙?



你至少需要一个字段来告诉它是什么样的用户,还是两个单独的外键。 p>

您还可以将所有用户常用的信息放在一个表中,并为特定于用户类型的信息分别设置表格,以便您具有单个表格用户ID作为主键。


I'm defining a database for a customer/ order system where there are two highly distinct types of customers. Because they are so different having a single customer table would be very ugly (it'd be full of null columns as they are pointless for one type).

Their orders though are in the same format. Is it possible to have a CustomerId column in my Order table which has a foreign key to both the Customer Types? I have set it up in SQL server and it's given me no problems creating the relationships, but I'm yet to try inserting any data.

Also, I'm planning on using nHibernate as the ORM, could there be any problems introduced by doing the relationships like this?

解决方案

No, you can't have a single field as a foreign key to two different tables. How would you tell where to look for the key?

You would at least need a field that tells what kind of user it is, or two separate foreign keys.

You could also put the information that is common for all users in one table and have separate tables for the information that is specific for the user types, so that you have a single table with user id as primary key.

这篇关于多个外键到单列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆