|
| 1 | +# Table Inheritance |
| 2 | + |
| 3 | +**Category:** Data Access Pattern, Structural Pattern |
| 4 | + |
| 5 | +**Tags:** Decoupling, Inheritance, Polymorphism, Object Mapping, Persistence, Data Transformation |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## Intent |
| 10 | +The **Table Inheritance** pattern models a class hierarchy in a relational database. Each class in the hierarchy is mapped to its own table, and the tables are linked through foreign keys representing the inheritance structure. |
| 11 | + |
| 12 | +This pattern is particularly useful for organizing complex data models when different subclasses have distinct properties that need to be stored in separate tables. |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +## Real-World Example |
| 17 | +Imagine a vehicle management system with a `Vehicle` superclass and subclasses such as `Car` and `Truck`. |
| 18 | +- The `Vehicle` table stores attributes common to all vehicles. |
| 19 | +- The subclass tables (`Car` and `Truck`) store attributes specific to each subclass. |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +## Database Schema |
| 24 | + |
| 25 | +### Vehicle Table |
| 26 | +| Column | Description | |
| 27 | +|--------|-------------------------------------| |
| 28 | +| id | Primary key | |
| 29 | +| make | The make of the vehicle | |
| 30 | +| model | The model of the vehicle | |
| 31 | +| year | The manufacturing year of the vehicle | |
| 32 | + |
| 33 | +### Car Table |
| 34 | +| Column | Description | |
| 35 | +|------------------|-------------------------------------| |
| 36 | +| id | Foreign key referencing `Vehicle(id)` | |
| 37 | +| numberOfDoors | Number of doors in the car | |
| 38 | + |
| 39 | +### Truck Table |
| 40 | +| Column | Description | |
| 41 | +|-------------------|-------------------------------------| |
| 42 | +| id | Foreign key referencing `Vehicle(id)` | |
| 43 | +| payloadCapacity | Payload capacity of the truck | |
| 44 | + |
| 45 | +--- |
| 46 | + |
| 47 | +## Benefits |
| 48 | +- Decouples subclasses into their own tables for easier management and scalability. |
| 49 | +- Allows for flexibility in subclass-specific attributes without altering the base table. |
| 50 | + |
| 51 | +--- |
0 commit comments