Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relational Data: entity inheritance approaches. Best practice

There are several approaches how to store entities hierarchy in relation database

For example there is person entity (20 basic attributes), student entity (the same as person but several new specific fields are present), employee (the same as person but some new fields are present) e.t.c.

When you advice to use (and not to use) the following data modeling approaches:

  • One big table with all possible fields + personType marker field (student or employee)
  • Table inheritance
  • One Table with XML field (or maybe another data type) to store all the custom fields
  • Something else but also relational...

Thank you in advance!

like image 822
Andrew Florko Avatar asked Jul 18 '10 09:07

Andrew Florko


People also ask

How do you model inheritance in a relational database?

You can represent inheritance in the database in one of two ways: Multiple tables that represent the parent class and each child class. A single table that comprises the parent and all child classes.

Does Entity Framework support inheritance?

By default, Entity Framework supports TPH inheritance, if you don't define any mapping details for your inheritance hierarchy.

Which inheritance strategy is better in hibernate?

If you have a single hierarchy, prefer To use Single Table Inheritance strategy. Joined strategy is better designed when you have a complex hierarchy but it suffers due To performance issues when querying for any entity. Save this answer.


2 Answers

A database models facts, not objects, and each table should model a relatively self-contained set of facts. The consequence of this is that your tables should look something like this:

person { person_id PK, name, dob, ... }
student { person_id PK FK(person.person_id), admission_id, year_started, ... }
employee { person_id PK FK(person.person_id), salary_bracket, ... }

An additional consequence is that a student can also be an employee, which probably models real life closer than an inheritance graph would.

like image 151
Marcelo Cantos Avatar answered Sep 24 '22 05:09

Marcelo Cantos


Have a look at the hibernate inheritance mapping docs. There you find three common approaches and a list of pros and cons of each.

like image 25
Alexander Torstling Avatar answered Sep 23 '22 05:09

Alexander Torstling