Pattern: One Inheritance Tree One Table

Abstract

The pattern demonstrates a way to map a complete inheritance hierarchy to a single database table.

Problem

How do you map an inheritance hierarchy of classes to database tables?

Forces

The forces relevant here, besides the General Forces  are:

Solution

Use the union of all attributes of all objects in the inheritance hierarchy as the columns of a single database table. Use Null values to fill the unused fields in each record.

Structure


 

Example Resolved

The table design for our running example looks as follows.

Consequences

Implementation

Related Patterns

See also Representing Inheritance in a Relational Database [Bro+96] or see the same thing as Single Inheritance Table from Martin Fowler



[1] For a brief discussion of lock escalation see [Dat95 , page 406].