Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple foreign keys to a single column

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?

like image 794
Aaron Powell Avatar asked Jul 27 '09 07:07

Aaron Powell


People also ask

Can one column have multiple foreign keys?

A single column can have multiple foreign key constraints.

Can you have 2 foreign keys in a table?

A table may have multiple foreign keys, and each foreign key can have a different parent table. Each foreign key is enforced independently by the database system. Therefore, cascading relationships between tables can be established using foreign keys.

Can you have 3 foreign keys?

A table can have multiple foreign keys based on the requirement.

How do I reference multiple foreign keys in SQL?

You can use the FOREIGN KEY REFERENCES constraint to implement a foreign key relationship in SQL Server. Specify the table name. Then specify in parenthesis the column name for the foreign key to reference it.


3 Answers

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.

like image 180
Guffa Avatar answered Nov 02 '22 14:11

Guffa


A foreign key can only reference a single primary key, so no. However, you could use a bridge table:

CustomerA <---- CustomerA_Orders ----> Order
CustomerB <---- CustomerB_Orders ----> Order

So Order doesn't even have a foreign key; whether this is desirable, though...

like image 36
Marc Gravell Avatar answered Nov 02 '22 13:11

Marc Gravell


I inherited a SQL Server database where this was done (a single column used in four foreign key relationships with four unrelated tables), so yes, it's possible. My predecessor is gone, though, so I can't ask why he thought it was a good idea.

He used a GUID column ("uniqueidentifier" type) to avoid the ambiguity problem, and he turned off constraint checking on the foreign keys, since it's guaranteed that only one will match. But I can think of lots of reasons that you shouldn't, and I haven't thought of any reasons you should.

Yours does sound like the classical "specialization" problem, typically solved by creating a parent table with the shared customer data, then two child tables that contain the data unique to each class of customer. Your foreign key would then be against the parent customer table, and your determination of which type of customer would be based on which child table had a matching entry.

like image 25
Dave G. Avatar answered Nov 02 '22 14:11

Dave G.