Class Table Inheritance (CTI)

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.

For example

Suppose we have a table called vehicles that stores information about vehicles, and two tables called cars and trucks that store information about specific types of vehicles. We can use CTI to store all the vehicles in separate tables, with foreign keys that reference the parent table.

Advantages of the Class Table Inheritance are

  • It preserves data integrity and consistency by using foreign keys and constraints.
  • It avoids wasting space in the database by storing only relevant attributes for each subclass.
  • It allows adding new subclasses easily by creating new tables.

Disadvantages of the Class Table Inheritance are

  • It complicates data access and manipulation by requiring joins or unions between tables.
  • It reduces performance and scalability by increasing the number of queries and joins.
  • It makes ad-hoc reporting difficult because the data is spread across multiple tables.

Note: CTI is suitable for complex and deep class hierarchies where there is significant difference between subclasses.

CTI diagram:

Below is the explanation of the above diagram:

  • The diagram shows three tables: Footballers, Cricketers, and Players. Each table has a primary key, which is a field that uniquely identifies each record in the table. The primary key is shown with an underline in the diagram. The tables also have foreign keys, which are fields that reference the primary key of another table. The foreign keys are shown with an arrow in the diagram.
  • The Footballers table has fields for the player’s name and club. The name field is the primary key of the table, and it references the name field of the Players table. The club field stores the name of the football club that the player belongs to.
  • The Cricketers table has fields for the player’s name, batting average, and bowling average. The name field is the primary key of the table, and it references the name field of the Players table. The batting average field stores the number of runs scored by the player per innings in cricket. The bowling average field stores the number of runs conceded by the player per wicket taken in cricket.
  • The Players table has fields for the player’s name and sport. The name field is the primary key of the table, and it stores the full name of the player. The sport field stores the name of the sport that the player plays, such as football or cricket.

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