PostgreSQL表继承(inherits)的使用

PostgreSQL 支持表继承,首先定义一个父表,然后使用关键字 INHERITS 定义一个子表,子表继承父表的所有字段定义。

定义一个父表:
create table parent(id int, name varchar(50));

定义一个子表,从 parent 表继承:
create table child(age int) inherits (parent);

插入数据:
insert into parent values(1, 'a');
insert into child values(2, 'b', 2);

查询父表、子表数据:

db=# select * from parent; id | name ----+------ 1 | a 2 | b (2 rows) db=# select * from child; id | name | age ----+------+----- 2 | b | 2 (1 row) db=# select * from only parent; id | name ----+------ 1 | a (1 row)