Skip to main content

How to Deal with Polymorphism in MySQL?

· 3 min read
Jack

Single Table Inheritance

Represents an inheritance hierarchy of classes as a single table that has columns for all the fields of the various classes.

For a full description see Patterns of Enterprise Application Architecture - Martin Fowler (The Addison-Wesley Signature Series) page 278

Single Table Inheritance Example Diagram

Relational databases don't support inheritance, so when mapping from objects to databases we have to consider how to represent our nice inheritance structures in relational tables. When mapping to a relational database, we try to minimize the joins that can quickly mount up when processing an inheritance structure in multiple tables. Single Table Inheritance maps all fields of all classes of an inheritance structure into a single table.

Class Table Inheritance

Represents an inheritance hierarchy of classes with one table for each class.

For a full description see Patterns of Enterprise Application Architecture - Martin Fowler (The Addison-Wesley Signature Series) page 285

Single Table Inheritance Example Diagram

A very visible aspect of the object-relational mismatch is the fact that relational databases don't support inheritance. You want database structures that map clearly to the objects and allow links anywhere in the inheritance structure. Class Table Inheritance supports this by using one database table per class in the inheritance structure.

Concrete Table Inheritance

Represents an inheritance hierarchy of classes with one table per concrete class in the hierarchy.

For a full description see Patterns of Enterprise Application Architecture - Martin Fowler (The Addison-Wesley Signature Series) page 293

Concrete Table Inheritance

As any object purist will tell you, relational databases don't support inheritance - a fact that complicates object-relational mapping. Thinking of tables from an object instance point of view, a sensible route is to take each object in memory and map it to a single database row. This implies Concrete Table Inheritance, where there's a table for each concrete class in the inheritance hierarchy.