In this article, I will talk about using the INHERIT feature while creating a table and the details about it.
INHERIT allows us to inherit a different table.
INHERIT table operations are generally used to design tables without connecting the primary key and the foreign key.
Let’s explain with an example.
1 | CREATE TABLE personel_name (name text,surname text ) |
Let’s create our field table INHERITS from our personnel_name table.
1 | CREATE TABLE personnel_communication(number integer,evaadres text ) INHERITS (personnel_name) |
We created both tables. As a result of this process, there are number and homeaddress columns in our personnel_communication table, but we can see our personnel_name columns in the personnel_communication table because we are doing INHERIT.
We can enter data into both tables by performing operations such as select, insert, update and delete in the INHERIT table.
Let’s insert our staff communication table and check if our first table is also affected by this insertion process.
1 2 3 | INSERT INTO public.personnel_communication( name, surname, number, homeaddress) VALUES('FARUK', 'ERDEM',06, 'ankara'); |
In the insert sentence above, we added the first and last name columns in the personnel_name table, because we want to add records to both of them. You can make different inserts in both tables.
Let’s check if data has been added to our personnel_name table: