Multiple foreign keys to a single column

后端 未结 7 2231
佛祖请我去吃肉
佛祖请我去吃肉 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.

提交回复
热议问题