Inheritance

PostgreSQL allows tables to inherit properties and attributes from other tables. This is useful in cases in which many tables are needed to hold very similar information. In these cases, it is often possible to create a parent table that holds the common data structures, allowing the children to inherit these structures from the parent.

For instance, let’s create a table called employees:

CREATE TABLE employees (name char(10), salary numeric(9,2)); 

Now you can create a specific table just for cooks, who happen to need all the information that other employees need:

 CREATE TABLE cooks (specialty char(10)) INHERITS(employees); SELECT * FROM cooks; name salary specialty --------------------- (0 rows) INSERT INTO cooks VALUES ('Bill', ...

Get PostgreSQL Essential Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.