Multiple foreign keys to a single column

后端 未结 7 2225
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-14 16:41

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 wo

相关标签:
7条回答
  • 2020-12-14 17:12

    As noted, if the key is, say, 12345, how would you know which table to look it up in? You could, I suppose, do something to insure that the key values for the two tables never overlapped, but this is too ugly and painful to contemplate. You could have a second field that says which customer type it is. But if you're going to have two fields, why not have one field for customer type 1 id and another for customer type 2 id.

    Without knowing more about your app, my first thought is that you really should have a general customer table with the data that is common to both, and then have two additional tables with the data specific to each customer type. I would think that there must be a lot of data common to the two -- basic stuff like name and address and customer number at the least -- and repeating columns across tables sucks big time. The additional tables could then refer back to the base table. As there is then a single key for the base table, the issue of foreign keys having to know which table to refer to evaporates.

    0 讨论(0)
  • 2020-12-14 17:14

    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.

    0 讨论(0)
  • 2020-12-14 17:21

    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.

    0 讨论(0)
  • 2020-12-14 17:28

    Two distinct types of customer is a classic case of types and subtypes or, if you prefer, classes and subclasses. Here is an answer from another question.

    Essentially, the class-table-inheritance technique is like Arnand's answer. The use of the shared-primary-key technique is what allows you to get around the problems created by two types of foreign key in one column. The foreign key will be customer-id. That will identify one row in the customer table, and also one row in the appropriate kind of customer type table, as the case may be.

    0 讨论(0)
  • 2020-12-14 17:31
    1. Create a "customer" table include all the columns that have same data for both types of customer.
    2. Than create table "customer_a" and "customer_b"
    3. Use "customer_id" from "consumer" table as foreign key in "customer_a" and "customer_b"

                      customer
                          |
           ---------------------------------
           |                               |
      cusomter_a                      customer_b
      
    0 讨论(0)
  • 2020-12-14 17:35

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

    0 讨论(0)
提交回复
热议问题