How do you deal with polymorphism in a database?

后端 未结 13 1172
小蘑菇
小蘑菇 2020-11-28 02:11

Example

I have Person, SpecialPerson, and User. Person and SpecialPerson are just people - they don\

13条回答
  •  失恋的感觉
    2020-11-28 02:47

    What I'm going to say here is going to send database architects into conniptions but here goes:

    Consider a database view as the equivalent of an interface definition. And a table is the equivalent of a class.

    So in your example, all 3 person classes will implement the IPerson interface. So you have 3 tables - one for each of 'User', 'Person' and 'SpecialPerson'.

    Then have a view 'PersonView' or whatever that selects the common properties (as defined by your 'interface') from all 3 tables into the single view. Use a 'PersonType' column in this view to store the actual type of the person being stored.

    So when you're running a query that can be operated on any type of person, just query the PersonView view.

提交回复
热议问题