postgres database object relational modeling - ghdrako/doc_snipets GitHub Wiki

obraz

In the given example, car_class is a parent class, and claim, body, and repair are child classes that inherit from car_class. This means that the child classes inherit the attributes and behaviors defined in the parent class. With the \d+ command on the table, you can identify the parent child class definitions to confirm the object-oriented methodology of PostgreSQL. Database. Object relational DDL:

create table car_class (vin char(19), make char(20), model 
char(20));
create table car_body (veh_type char(20)) INHERITS (car_class);

Object-relational DML:

insert into car_class values ('1GMDX03E8VD266902', 'Honda', 'Accord');

This command inserts a row into the car_class table with specific values for the vin, make, and model columns.

insert into car_body values ('1 KMHD25LE1DU04202', 'Honda', 'Accord', 'Compact');

This command inserts a row into the car_body table, which inherits the columns from the car_class table and adds the veh_type column specific to the car_body table.

postgres=# create table car_class (vin char(19), make char(20), 
model char(20));
create table car_body (veh_type char(20)) INHERITS (car_class);
CREATE TABLE
CREATE TABLE
postgres=# insert into car_class values ('1GMDX03E8VD266902', 
'Honda', 'Accord');
INSERT 0 1
postgres=# insert into car_body values ('1 KMHD25LE1DU04202', 
'Honda', 'Accord', 'Compact');
INSERT 0 1
postgres=# select * from car_class;
         vin         |         make         |        model
---------------------+---------------------+-------------
 1GMDX03E8VD266902   | Honda                | Accord
 1 KMHD25LE1DU04202  | Honda                | Accord
(2 rows)

If you examine the above listing, the class call has values from car_ body, without any join between these two tables, due to the properties of inheritance.