开发者

Relational Data: entity inheritance approaches. Best practice

开发者 https://www.devze.com 2023-01-08 06:58 出处:网络
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 se

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!


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.


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


If you are using an ORM to implement your classes, the ORM tools you are using will provide you options, generally two options, one class one table or one parent class one table and each table for each children class. I am using XPO from Devexpress.com, one ORM framework. It offers these two options.

If you use ORM, I am afraid there are no other generic options.

Ying

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号