3.5 Hours

8. Relational Schema Design Patterns

Patterns

Relational databases have been around for decades, and best practices have emerged. This module covers common relational schema design patterns: single-table inheritance, class-table inheritance, shared primary key, and the various patterns for handling relationships.

Understanding these patterns helps you design better relational schemas. They provide proven solutions to common problems. They also give you a vocabulary to discuss design decisions. This module covers the most useful patterns with examples.

Single-Table Inheritance

Single-table inheritance stores multiple types in a single table with a type column. This is simple and fast for simple hierarchies. The downside is that columns for different types are NULLable.

PatternProsConsBest For
Single-Table InheritanceSimple queries, no joinsMany NULL columns, sparse tableSimple hierarchies with few types
Class-Table InheritanceNo NULL columns, normalizedComplex queries with joinsComplex hierarchies with many types
SQL
-- Single-table inheritance
CREATE TABLE employees (
  id SERIAL PRIMARY KEY,
  employee_type TEXT NOT NULL CHECK (employee_type IN ('fulltime', 'contractor', 'intern')),
  name TEXT NOT NULL,
  salary NUMERIC(10,2),
  hourly_rate NUMERIC(10,2),
  contract_end DATE,
  school TEXT
);

-- Class-table inheritance
CREATE TABLE employees (
  id SERIAL PRIMARY KEY,
  name TEXT NOT NULL,
  employee_type TEXT NOT NULL
);

CREATE TABLE fulltime_employees (
  employee_id INT PRIMARY KEY REFERENCES employees(id),
  salary NUMERIC(10,2)
);

CREATE TABLE contractor_employees (
  employee_id INT PRIMARY KEY REFERENCES employees(id),
  hourly_rate NUMERIC(10,2),
  contract_end DATE
);

Knowledge Check

Ready to test your understanding of 8. Relational Schema Design Patterns?

Back