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

Similar Reads

What are relational databases?

Relational databases are a type of database that store data in tables, which consist of rows and columns. Each row represents a record or an entity, and each column represents an attribute or a property of that entity. For example, a table of customers might have columns for name, email, phone number, and address. Each row in the table would have the information for one customer....

Design Patterns for Relational Databases

1. Single Table Inheritance (STI)...

1. 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....

2. Class Table Inheritance (CTI)

...

3. Entity-Attribute-Value (EAV)

This is a design pattern where each class in a hierarchy has its own database table, and the tables are linked by foreign keys. The relational databases don’t inherently support inheritance. However, CTI is a technique used to represent a hierarchy of classes in multiple tables by using inheritance relationships between tables....

4. Composite Key

This is a design pattern where each entity is represented by a set of attribute-value pairs, instead of having a fixed schema with predefined columns. The relational databases are based on a rigid structure that requires defining the attributes and data types of each entity beforehand. However, EAV is a technique used to represent entities with dynamic and variable attributes in a flexible way by using three tables: one for entities, one for attributes, and one for values....

5. Multipart Index

This is a design pattern where a combination of two or more columns is used to uniquely identify each row in a table, instead of having a single column as the primary key. The relational databases require defining a primary key for each table, which is a column or a set of columns that can distinguish each row from others. However, composite key is a technique used to create a primary key from multiple columns that together form a unique value for each row....

6. Materialized View

This is a design pattern where an index is created on two or more columns of a table, instead of having an index on a single column. The relational databases use indexes to speed up data access and manipulation by creating sorted structures or pointers that reference the rows in a table. However, multipart index is a technique used to create an index on multiple columns that together form a search criterion for queries or joins....

7. Many-to-Many Relationship

A materialized view is a pre-computed data set that is derived from a query and stored for later use. It can improve the performance of queries that use the same subquery results repeatedly, or that are complex or run on large data sets. A materialized view is updated automatically or on demand when the source data changes, so it always reflects the current state of the data. A materialized view is a type of cache that can be disposed and rebuilt from the source data....

8. Caching

A many-to-many relationship is a type of relationship between two entities in a database, where each entity can be associated with multiple instances of the other entity....

10. Queueing

Caching is a technique of storing frequently used or recently accessed data in memory or disk, to reduce latency and workload. Caching can improve the performance and scalability of applications that need to access data from remote or slow sources, such as databases or web services. Caching can also reduce the cost of data access by minimizing network traffic and resource consumption. Caching can be implemented at different levels, such as application level, database level, or network level....

11. Audit Log

Queueing is a technique of storing data or tasks in a buffer or list, to process them sequentially or asynchronously. Queueing can help manage concurrency and load balancing, by distributing work among multiple workers or threads. Queueing can also help improve reliability and fault tolerance, by ensuring that data or tasks are not lost or duplicated in case of failures or interruptions. Queueing can be implemented using various technologies, such as message brokers, message queues, or distributed streaming platforms....

12. Versioning

An audit log is a record of events or actions that occur in a system or application, such as user activities, system changes, security incidents, or errors. An audit log can help monitor and track the behavior and performance of a system or application, by providing information such as who did what, when, where, and why. An audit log can also help troubleshoot and debug issues, by providing details and context about the events or actions. An audit log can be stored in various formats and locations, such as text files, databases, or cloud services....