How do you effectively model inheritance in a database?

后端 未结 9 1493
南旧
南旧 2020-11-22 05:37

What are the best practices for modeling inheritance in databases?

What are the trade-offs (e.g. queriability)?

(I\'m most interested in SQL Server and .NET,

9条回答
  •  逝去的感伤
    2020-11-22 06:19

    There are two main types of inheritance you can setup in a DB, table per entity and table per Hierarchy.

    Table per entity is where you have a base entity table that has shared properties of all child classes. You then have per child class another table each with only properties applicable to that class. They are linked 1:1 by their PK's

    alt text

    Table per hierarchy is where all classes shared a table, and optional properties are nullable. Their is also a discriminator field which is a number that denotes the type that the record currently holds

    alt text SessionTypeID is discriminator

    Target per hierarchy is faster to query for as you do not need joins(only the discriminator value), whereas target per entity you need to do complex joins in order to detect what type something is as well as retreiuve all its data..

    Edit: The images I show here are screen shots of a project I am working on. The Asset image is not complete, hence the emptyness of it, but it was mainly to show how its setup, not what to put inside your tables. That is up to you ;). The session table holds Virtual collaboration session information, and can be of several types of sessions depending on what type of collaboration is involved.

提交回复
热议问题