Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Inheritance in SQL? 
3.
Inheritance in Postgresql 
4.
Frequently Asked Questions
4.1.
What is the advantage of Inheritance?
4.2.
What is the difference between the char data type and the varchar data type in PostgreSQL?
4.3.
Why is PostgreSQL so widely used?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Concept of Inheritance with PostgreSQL

Author Nagendra
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Hi Ninjas!! Welcome to another article on PostgreSQL. Today, we will learn about Inheritance in PostgreSQL. PostgreSQL is a powerful, open-source object-relational database management system (ORDBMS). It is used to store data securely. 

Concept of Inheritance with PostgreSQL Image

This blog explains the details of the Concept of Inheritance with PostgreSQL.

Without further ado, let's get started.

What is Inheritance in SQL? 

Inheritance is one of the key concepts of object-oriented programming. It is a method of obtaining shared attributes between two objects by deriving one from the other.

The properties and operations of a parent type are automatically passed down to its subtypes. The inheritance connection is still active as well. Subtypes immediately inherit any modifications made to these attributes or methods in the parent. Any changes made to a supertype's attributes or methods also affect subtypes.

Chart Image

Inheritance allows sharing attributes among objects such that a subclass can inherit properties from its parent class. The primary key for subclasses must share the same database field(s) as the parent class. The primary keys can have different names in these two tables. If the subclass primary key is different from the parent primary key, the parent table primary key must be present in the subclass table. 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Inheritance in Postgresql 

A table in PostgreSQL can inherit from many tables. A query may include either all rows of a table or all rows of a table including its descendants. The INHERITS clause is used for using Inheritance in PostgreSQL. The child table had all the attributes of the parent table.

Syntax:

CREATE TABLE 
childtable

         INHERITS ( 
parenttable
 [, ...] )


Let's understand the implementation of Inheritance in Postgresql. 

CREATE TABLE billing (
  cust_id text,
  name text,
  item text,
 quantity int,   
);


CREATE TABLE address (
cust_id text,
name text,
cust_address text,
);
CREATE VIEW bill AS
  SELECT cust_id, name , item, quantity FROM billing
    UNION
  SELECT cust_id, name, cust_address FROM address;

 

In the above approach, if we update the billing table, we need to update the address table separately. This increases the problem of database anomaly.

Let's look at the efficient way of using Inheritance.

CREATE TABLE billing (
  cust_id text,
  name text,
  item text,
 quantity int,   
);


CREATE TABLE address (
cust_address text,
); INHERITS (billing);


In the above approach, if we update the billing table, the corresponding value in the billing table gets updated. This reduces the chances of database anomaly.

The address table will automatically have the attributes of the billing table.

Also read anomalies in database

Frequently Asked Questions

What is the advantage of Inheritance?

The main advantage of Inheritance is code reusability.

What is the difference between the char data type and the varchar data type in PostgreSQL?

The Char data type stores fixed-length characters in a column, whereas the varchar data type stores variable-length characters in a column.

Why is PostgreSQL so widely used?

PostgreSQL is a powerful database management system with numerous sophisticated functionalities and security features. It's also a community-driven database management system that's open-source and free to use.

Conclusion

In this article, we have discussed the details of the Concept of Inheritance with PostgreSQL.

We hope that the blog has helped you enhance your knowledge regarding the Concept of Inheritance with PostgreSQL. You can refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. To practice and improve yourself in the interview, you can also check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews. Do upvote our blogs to help other ninjas grow. Happy Coding!!

Previous article
More on Window Functions with PostgreSQL
Next article
Select where group by order by in postgresql
Live masterclass