Single Table Inheritance

Single Table Inheritance, also known as Table per Hierarchy (TPH), stores all entities—superclass and subclass—in a single table. Every record in the table has a unique marker, known as a discriminator, that designates the particular kind of entity it represents. Because fewer tables and intricate connections are required, this method makes data storage and retrieval easier. But it could lead to sparse tables with a lot of null values for characteristics that don’t apply to some kinds of entities.

Example

Let us take a situation when we have two categories of workers: full-time and part-time.

CREATE TABLE employees (

id SERIAL PRIMARY KEY,

name VARCHAR(100),

type VARCHAR(20), — Discriminator column

salary DECIMAL,

hourly_rate DECIMAL

)

Let’s insert some sample data

— Full-time employee

INSERT INTO employees (name, type, salary) VALUES (‘Ravi Kumar’, ‘full-time’, 50000.00);

— Part-time employee

INSERT INTO employees (name, type, hourly_rate) VALUES (‘Rishi Patel’, ‘part-time’, 20.00);

Now, let’s query the employees table

SELECT * FROM employees;

The above query will return the following table as output:

id

name

type

salary

hourly_rate

1

Ravi Kumar

full-time

50000.00

NULL

2

Rishi Patel

part-time

NULL

20.00

In this example, full-time and part-time employees are stored in a single table called employees. The employee type is ascertained by using the type field as a discriminator. This method makes it simpler to manage and maintain the database schema and queries. However, maintaining data integrity and appropriately handling NULL values are crucial.

Inheritance Hierarchies in DBMS

Inheritance Hierarchies are crucial to building a structured and well-organized database. It is comparable to the idea of inheritance found in object-oriented programming languages. The main ideas of inheritance hierarchies in database management systems (DBMS) will be covered in this article, along with definitions, procedures, and in-depth examples to aid in understanding.

Similar Reads

What are Inheritance Hierarchies in DBMS?

In database management systems, inheritance hierarchies help create linkages between tables or entities, much like inheritance helps create links between classes in object-oriented programming. It explains the process by which a newly created table, often known as a child table, can inherit specific attributes and capabilities from an existing parent table. This lowers redundancy and improves data integrity by creating a hierarchical structure in the database....

Key Terminologies

Superclass: The class or table whose methods and attributes are inherited is called the superclass or base class. Another name for it is the parent class. Subclass/Derived Class: In an inheritance structure, a subclass is a class or table that receives some methods and attributes from another class. Another name for it is the child class....

Types of Inheritance Hierarchies

There are mainly three types of inheritance hierarchies in DBMS as follows:...

Single Table Inheritance

Single Table Inheritance, also known as Table per Hierarchy (TPH), stores all entities—superclass and subclass—in a single table. Every record in the table has a unique marker, known as a discriminator, that designates the particular kind of entity it represents. Because fewer tables and intricate connections are required, this method makes data storage and retrieval easier. But it could lead to sparse tables with a lot of null values for characteristics that don’t apply to some kinds of entities....

Class Table Inheritance

Table per Type (TPT), another name for Class Table Inheritance, is the process of making a separate table for every entity type, including the superclass and all of its subclasses. Only the properties pertinent to that particular entity type are contained in each table, and foreign keys are used to form associations between the tables. By cutting down on redundancy, this method preserves data integrity while providing improved data organization. To extract data from many tables, however, more intricate queries requiring joins can be needed....

Concrete Table Inheritance

Concrete Table Inheritance (CTI) assigns a unique table to every subclass or derived entity in an inheritance hierarchy. In CTI, every table denotes a certain class of object or entity, complete with both class-specific and parent-class-inherited properties....

Characteristics of Inheritance Hierarchies

Code Reusability: We may reduce redundancy in the database schema by reusing the attributes and functions from the superclass . Hierarchy Structure: The generalization-specialization relationship between entities is demonstrated by the hierarchical structure formed by the interaction between a superclass and its subclasses. Flexibility: Any modifications we make to the superclass will also affect the subclasses. This guarantees the database design’s coherence. Subclasses, however, may have special qualities that offer adaptability....

Frequently Asked Questions on Inheritance Hierarchies – FAQs

Is it possible to modify a subclass’s properties without having an impact on the superclass?...