How do you deal with polymorphism in a database?

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

Example

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

13条回答
  •  遥遥无期
    2020-11-28 03:01

    There's three basic strategies for handling inheritance in a relational database, and a number of more complex/bespoke alternatives depending on your exact needs.

    • Table per class hierarchy. One table for the whole hierarchy.
    • Table per subclass. A separate table is created for every sub class with a 0-1 association between the subclassed tables.
    • Table per concrete class. A single table is created for every concrete class.

    Each of these appoaches raises its own issues about normalization, data access code, and data storage, although my personal preferance is to use table per subclass unless there's a specific performance or structural reason to go with one of the the alternatives.

提交回复
热议问题