Application 2017-10-01

Single Table Inheritance, Class Table Inheritance, and Concrete Class Inheritance

Explaining three patterns to represent object inheritance in databases: Single Table Inheritance, Class Table Inheritance, and Concrete Class Inheritance.

Read in: ja
Single Table Inheritance, Class Table Inheritance, and Concrete Class Inheritance

Overview

Relational databases do not support inheritance, so it is necessary to consider how to represent object inheritance relationships in the database. This post explains three patterns to express this: Single Table Inheritance, Class Table Inheritance, and Concrete Class Inheritance.

Note: This post does not discuss the advantages and disadvantages of implementing each pattern.

Prerequisites

We will consider four classes in this example.

people_class.png

The structure where Party People inherits from Rich People might be a bit confusing, but as long as the concept is conveyed, it’s fine.

People

This class has attributes common to all classes.

OrdinaryPeople

These are sensible and good ordinary people.

RichPeople

These are wealthy people with money and land.

Note: Units and other details are not considered.

PartyPeople

Party people.

Single Table Inheritance

Single Table Inheritance represents object inheritance relationships in a single table. The table includes a column (type) to determine the subclass.

single_table_inheritance_table.png

It seems that Rails supports the implementation of STI.

Class Table Inheritance

Class Table Inheritance represents object inheritance relationships by preparing one table per class. The superclass table contains columns held by the superclass, while the subclass table contains only columns held by the subclass.

class_table_inheritance.png

Concrete Class Inheritance

Concrete Class Inheritance represents object inheritance relationships by preparing tables that correspond only to concrete classes. Each table includes columns held by the superclass as common attributes.

concrete_table_inheritance.png

Thoughts

The choice of which pattern to implement depends on the consideration of the advantages and disadvantages of table design and the cost of application logic. Please point out any misleading or incorrect parts.

Reference Links

Tags: PofEAA
Share: 𝕏 Post Facebook Hatena
✏️ View source / Discuss on GitHub
β˜• Support

If you enjoy this blog, consider supporting it. Every bit helps keep it running!


Related Articles