You are on page 1of 5

Table Inheritance

Example:
Create Table:
create table orders(orderno serial, flightname varchar(100),boarding varchar(100),status
varchar(100),source varchar(100));

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

create table agent_booking (commision int) inherits(orders);

Insert Records:

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);

insert into agent_booking(flightname,boarding,status,source,commision)


values('etihad','aud','ontime','agent001',1000);

insert into agent_booking(flightname,boarding,status,source,commision)


values('emirates','dxb','ontime','agent007',1300);

Select Parent Table:

nano=# \set AUTOCOMMIT off

nano=# select * from orders;

orderno | flightname | boarding | status | source

---------+------------+----------+--------+-----------

1 | aircanada | xyz | ontime | employees

2 | nippon | chn | ontime | website

3 | luftansa | chn | ontime | app

5 | etihad | aud | ontime | agent001


6 | emirates | dxb | ontime | agent007

(5 rows)

Update Parent:

nano=# update orders set status='Cancelled';

UPDATE 5

nano=# select * from orders;

orderno | flightname | boarding | status | source

---------+------------+----------+-----------+-----------

1 | aircanada | xyz | Cancelled | employees

2 | nippon | chn | Cancelled | website

3 | luftansa | chn | Cancelled | app

5 | etihad | aud | Cancelled | agent001

6 | emirates | dxb | Cancelled | agent007

(5 rows)

nano=# rollback;

ROLLBACK

nano=# select * from orders;

orderno | flightname | boarding | status | source

---------+------------+----------+--------+-----------

1 | aircanada | xyz | ontime | employees

2 | nippon | chn | ontime | website

3 | luftansa | chn | ontime | app

5 | etihad | aud | ontime | agent001

6 | emirates | dxb | ontime | agent007

(5 rows)
nano=# update only orders set status='Cancelled';

UPDATE 1

nano=# select * from orders;

orderno | flightname | boarding | status | source

---------+------------+----------+-----------+-----------

1 | aircanada | xyz | Cancelled | employees

2 | nippon | chn | ontime | website

3 | luftansa | chn | ontime | app

5 | etihad | aud | ontime | agent001

6 | emirates | dxb | ontime | agent007

(5 rows)

Delete:
nano=# delete from orders;

DELETE 5

nano=# select * from orders;

orderno | flightname | boarding | status | source

---------+------------+----------+--------+--------

(0 rows)

nano=# rollback;

WARNING: there is no transaction in progress

ROLLBACK

nano=# select * from orders;

orderno | flightname | boarding | status | source

---------+------------+----------+--------+--------

(0 rows)
Drop Table:
nano=# drop table orders;

ERROR: cannot drop table orders because other objects depend on it

DETAIL: table online_booking depends on table orders

table agent_booking depends on table orders

HINT: Use DROP ... CASCADE to drop the dependent objects too.

nano=# drop table orders cascade;

NOTICE: drop cascades to 2 other objects

DETAIL: drop cascades to table online_booking

drop cascades to table agent_booking

DROP TABLE

nano=# delete from orders;

DELETE 5

nano=# select * from orders;

orderno | flightname | boarding | status | source

---------+------------+----------+--------+--------

(0 rows)

nano=# rollback;

WARNING: there is no transaction in progress

ROLLBACK

nano=# select * from orders;

orderno | flightname | boarding | status | source

---------+------------+----------+--------+--------

(0 rows)
nano=# drop table orders;

ERROR: cannot drop table orders because other objects depend on it

DETAIL: table online_booking depends on table orders

table agent_booking depends on table orders

HINT: Use DROP ... CASCADE to drop the dependent objects too.

nano=# drop table orders cascade;

NOTICE: drop cascades to 2 other objects

DETAIL: drop cascades to table online_booking

drop cascades to table agent_booking

DROP TABLE

You might also like