Single Table Inheritance

Let’s learn about Single Table Inheritance.

Single-level class hierarchy

Consider the single-level class hierarchy shown in the figure below as the design for the model classes of an MVC app. This hierarchy is an incomplete disjoint segmentation of the class Book.

In this kind of a case, whenever we have a model class hierarchy with only one or a few levels of subtyping and each subtype has only a few additional properties, then it’s preferable to use Single Table Inheritance. We model a single table that contains columns for all attributes. This is so that the columns that represent additional attributes of segment subclasses, also known as segment attributes, are optional. We can see this in the SQL table model in the figure below:

It’s a common approach to add a special discriminator column to represent the category of each row that corresponds to the subclass instantiated by the represented object. ...