Course contentsShow
← Software Engineering
Lesson 932 of 1,93329. Object-Relational MappingFree lesson

Inheritance and Relational Mapping Strategies

Approaches to representing class hierarchies in tables: single table, class table, and concrete table.

Inheritance and Relational Mapping Strategies

What you'll learn: You'll discover the three main approaches for representing object-oriented class hierarchies in relational database tables.

When you have an inheritance hierarchy in your code—say, a Vehicle base class with Car and Truck subclasses—relational databases don't have a built-in concept of inheritance. You must choose how to flatten this hierarchy into tables.

The Three Strategies

Single Table Inheritance puts all classes in one table with a discriminator column. Every Vehicle, Car, and Truck goes into a single vehicles table with a type column indicating which class it represents. This means many columns might be NULL for certain types (trucks have cargo_capacity, cars don't). It's simple but can waste space.

Class Table Inheritance creates one table per class in the hierarchy. You'd have a vehicles table with shared fields, a cars table with car-specific fields, and a trucks table with truck-specific fields. Each subclass table references the parent via foreign key. This normalizes data nicely but requires joins to reconstruct objects.

Concrete Table Inheritance gives each concrete (instantiable) class its own complete table. Both cars and trucks tables contain all fields, including those inherited from Vehicle. No joins needed, but shared fields are duplicated across tables, making schema changes harder.

The Trade-offs

Single table is fastest for reads but wastes space. Class table is cleanest but requires joins. Concrete table duplicates data but performs well when you query one subclass at a time.

Key Takeaway: Inheritance doesn't map naturally to relational tables—choose single table for simplicity, class table for normalization, or concrete table for query performance, depending on your access patterns and how often the hierarchy changes.