表继承是PostgreSQL特有的,子表可以从父表中继承字段和一些属性。例如:
创建一张表“persons”作为父表:
test=# create table persons (
test(# id int primary key,
test(# name text not null,
test(# age int,
test(# sex boolean
test(# );
CREATE TABLE
创建子表“students”,students表比persons表多一个字段“class_no”:
test=# create table students (
test(# class_no int
test(# ) inherits (persons);
CREATE TABLE
查看“persons”,“students”的表结构:
test=# \d persons
Table "public.persons"
Column | Type | Modifiers
--------+---------+-----------
id | integer | not null
name | text | not null
age | integer |
sex | boolean |
Indexes:
"persons_pkey" PRIMARY KEY, btree (id)
Number of child tables: 1 (Use \d+ to list them.)
test=# \d students
Table "public.students"
Column | Type | Modifiers
----------+---------+-----------
id | integer | not null
name | text | not null
age | integer |
sex | boolean |
class_no | integer |
Inherits: persons
向子表“students”中插入两条记录,插入成功后,在父表中也可以查到这两条记录:
test=# insert into students values (1,'libei',24,true,1);
INSERT 0 1
test=# insert into students values (2,'guanyu',23,true,2);
INSERT 0 1
test=# select * from students;
id | name | age | sex | class_no
----+--------+-----+-----+----------
1 | libei | 24 | t | 1
2 | guanyu | 23 | t | 2
(2 rows)
test=# select * from persons;
id | name | age | sex
----+--------+-----+-----
1 | libei | 24 | t
2 | guanyu | 23 | t
(2 rows)
修改子表“students”中的数据,在父表中也可以查到修改后的结果:
test=# update students set age=20 where name ='guanyu';
UPDATE 1
test=# select * from persons;
id | name | age | sex
----+--------+-----+-----
1 | libei | 24 | t
2 | guanyu | 20 | t
(2 rows)
但向父表“persons”中插入一条数据,在子表“students”中确查不到这条数据:
test=# insert into persons values (3,'zhangfei',18,true);
INSERT 0 1
test=# select * from persons;
id | name | age | sex
----+----------+-----+-----
3 | zhangfei | 18 | t
1 | libei | 24 | t
2 | guanyu | 20 | t
(3 rows)
test=# select * from students;
id | name | age | sex | class_no
----+--------+-----+-----+----------
1 | libei | 24 | t | 1
2 | guanyu | 20 | t | 2
(2 rows)
总结:当查询父表时,会把子表中的数据也查询出来,反之则不行。如果只想查询父表自己的数据,在表名前加“only”即可:
test=# select * from only persons;
id | name | age | sex
----+----------+-----+-----
3 | zhangfei | 18 | t
(1 row)
所有父表的检查约束和非空约束都会被子表继承过来,其他类型的约束比如:唯一、主键、外键,则不会被继承。
一个子表可以从多个父表中继承,子表将拥有所有父表的列和自己定义的列。如果同一个字段名出现在多个父表中,或者同时出现在父表和子表的定义中,那么这些字段将会被“融合”,因此在子表中就只有一个这样的字段。但是这种“融合”要求字段的数据类型相同,否则会报错。融合的字段拥有父表字段的所有检查约束。
采用select、update、delete等命令操作父表时,也会同时操作相应的子表,当使用alter table修改父表的表结构时,也会同时修改子表的表结构,但“reindex”、“vacuum”命令不会影响到子表。此外,唯一约束、外键的作用域也不会扩大到子表上。