Skip to content

RootDBA

DBA expertise: PostgreSQL, SQL Server & MySQL

Menu
  • Home
  • blog
  • PostgreSql
  • MySql
  • Sql Server
  • About AHMED – Root DBA
Menu

PostgreSql table inheritance , table partion

Posted on September 1, 2025September 1, 2025 by AHMED

table inheritance

To convey the idea that child tables inherit all columns from the parent master table while also allowing child tables to have additional fields, with the ability to specify the “ONLY” keyword for queries on specific tables, you can rewrite the statement as follows:

“We enable child tables to inherit all columns from the parent master table, permitting them to include additional fields unique to each child table. Furthermore, the ‘ONLY’ keyword can be utilized to specify that a query should exclusively target a particular table and not affect any others.

Please note that any updates or deletions made to the parent table will automatically propagate to the child table due to their inheritance relationship.

for testing i have created a new database called testinhernttable
then i will create table called orders .

create table orders(orderno serial, flightname varchar(100),boarding varchar(100),status varchar(100),source varchar(100));

We will create a child table called ‘online.book’ and specify its inheritance using the INHERITS syntax.
inherits(partent_table_name)

create table online_booking (price int) inherits(orders);

To check the description of the child table and verify its inheritance from the parent “order” table, you can use the following phrase:

\d online_booking 

“We will inspect the child table’s metadata and confirm whether it inherits from the parent ‘order’ table.”

you will also notice that child table inheritance all the column from parent table 

we can use the \d+ order on parent table and it will give more info on parent table and also will show if there child table 

now we will insert data to the table 

insert into orders(flightname,boarding,status,source) values(‘aircanada’,’xyz’,’ontime’,’employees’); insert into online_booking(flightname,boarding,status,source,price) values(‘nippon’,’chn’,’ontime’,’website’,5000); insert into online_booking(flightname,boarding,status,source,price) values(‘luftansa’,’chn’,’ontime’,’app’,3000);

When querying the parent table, it will display values from both the parent and child tables. However, any columns that were added exclusively by the child table and are not inherited from the parent will not be included in the query results

updating values in parent table

in child table you can update with no requirement , but for updating parent table it require to be caution due to fact that any changes will reflect on child 

to only update use only in syntax 

update only order set status=’delayed’;

copy table

copy is used to copy structure of table alone with the data to another table 

the way you implement this is by creating new table and using as table followed by existing table name
below is the syntax with data 

create table new_table as table exesting_table;

below is syntax without the data 

create table new_table as table exesting_table with no data ;
0
Visited 8 times, 1 visit(s) today
Category: PostgreSql, Postgresql Dba Guide

Post navigation

← Postgresql configuration files
PostgreSql tablespace →
© 2025 RootDBA | Powered by Minimalist Blog WordPress Theme