Single Table Inheritance (STI)
This is a design pattern where a single database table is used to store multiple types of related objects that share common attributes. The relational databases don’t inherently support inheritance. However, STI is a technique used to represent a hierarchy of classes in a single table by using a column that indicates the type of each row.
For example
Suppose we have a table called vehicles that stores information about cars, trucks, and motorcycles. We can use STI to store all the vehicles in one table, with a column called type that specifies the subclass of each vehicle.
Advantages of Single Table Inheritance:
- It is simple and easy to implement.
- It supports polymorphism by simply changing the type of the row.
- It provides fast data access because the data is in one table.
- It facilitates ad-hoc reporting because all of the data is in one table.
The disadvantages of Single Table Inheritance:
- It increases coupling within the class hierarchy because all classes are directly coupled to the same table.
- It can waste space in the database if there are many null columns for attributes that are not shared by all subclasses.
- It can complicate the logic for indicating the type if there is significant overlap between subclasses.
Note: STI is suitable for simple and shallow class hierarchies where there is little or no overlap between subclasses.
Below is the implementation of Single Table Inheritance:
C++
class Animal < ActiveRecord::Base include EnumInheritance enum species : { dog : 1, cat : 2 } def self.inheritance_column_to_class_map = { dog : 'Dog' , cat : 'Cat' } def self.inheritance_column = 'species' end class Dog < Animal; end class Cat < Animal; end |
code explaination:
- class Animal: Think of this as a blueprint for storing information about animals in a database. It’s like a form with fields for things like the type of animal, its name, and other details.
- enum species: Here, we’re saying that animals can be either a “dog” or a “cat.” We use numbers (1 for dog, 2 for cat) to represent them. It’s like saying “1 means dog” and “2 means cat.”
- def self.inheritance_column_to_class_map: This part connects the names “dog” and “cat” to specific classes. So, if we have a “dog,” it’s connected to a class called “Dog,” and if it’s a “cat,” it’s linked to a class called “Cat.”
- def self.inheritance_column: This tells our system that we’ll use the “species” to figure out if an animal is a dog or a cat. It’s like a label on a box that says what’s inside.
- class Dog < Animal; end and class Cat < Animal; These lines create special instructions for our system. They say, “If it’s a dog, treat it like an ‘Animal,’ but also follow the rules for a ‘Dog’.” The same goes for cats – they’re animals too, but they also have their own cat rules.
Design Patterns for Relational Databases
Relational databases are a way of storing and managing data in software development. They help you keep your data in order and find it quickly. But to use relational databases well, you need to follow some patterns that solve common problems and make your data work better. In this article, we will look at different patterns for relational databases, and explain how they can help you with specific issues and improve your database performance.
Important Topics for the Design Patterns for Relational Databases
- What are relational databases?
- Design Patterns for Relational Databases
- 1. Single Table Inheritance (STI)
- 2. Class Table Inheritance (CTI)
- 3. Entity-Attribute-Value (EAV)
- 4. Composite Key
- 5. Multipart Index
- 6. Materialized View
- 7. Many-to-Many Relationship
- 8. Caching
- 10. Queueing
- 11. Audit Log
- 12. Versioning