Single Table Inheritance
Learn how to implement Single Table Inheritance in plain JavaScript web applications to efficiently represent model class hierarchies. Understand how to use discriminator columns and enforce constraints for subclass attributes within a single database table, improving your app's data structure and logic.
We'll cover the following...
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. ...