Inheritance is the key concept of object-oriented programming.
A subclass is basically inherits the properties of its parent class.
Let’s say, for our application we define basic class
We need also
Maintainer classes as a custom type of
so we inherit them from the super class.
Maintainer subclasses, which are inherited from
password fields as common but also some additional specific fields.
When we want to store those objects into a relational database system,
they need to be mapped as tables.
However, relational databases don’t support inheritance.
In this example we have one base class and two subclasses to map into the relational database as tables.
There are three approaches with their trade-offs to do that.
1) Single table
In this approach, all fields, which are defined under a super (parent) class, are stored in a single table.
It is easy to query and retrieve different types from one table without need of join statements.
However, a query to get
Customer class object would also return irrelevant
hence all regarding fields should be specified in the select statement.
Another problem is that, it is not possible to use constrains such as not null for a subclass.
customerNumber is an essential field for all
Yet applying not null constrain for
customerNumber would prevent us from storing other objects without
customerNumber such as
2) Class Table Inheritance
In this approach, there exist one database table per class.
Separate tables provide consistent data storage with constrain definitions but it is more complex to query a subclass.
It requires to write some join statements which reduces the performance.
For example, to get a
Customer object, it needs to be join with
3) Concrete Table Inheritance
In this approach, there exist a table for each concrete class.
Every concrete class has a table with duplicated fields.
In case of updating a field type of the base class would require to migrate multiple tables.
For example, if we change character size of
password field, we need to alter both
It might lead a conflict.