单个列的多个外键

我正在为客户/订单系统定义一个数据库,其中有两种高度不同的客户类型.因为它们是如此不同,只有一个客户表会非常难看(它会充满空列,因为它们对于一种类型毫无意义).

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).

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

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.

另外,我打算使用 nHibernate 作为 ORM,这样做会不会有任何问题引入这样的关系?

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.

您还可以将所有用户通用的信息放在一个表中,并为特定于用户类型的信息使用单独的表,这样您就有一个以用户 ID 作为主键的表.

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.

相关文章