test=# CREATE TABLE cities ( -- 父表
test(# name text,
test(# population float,
test(# altitude int
test(# );
CREATE TABLE
test=# CREATE TABLE capitals ( -- 子表
test(# state char(2)
test(# ) INHERITS (cities);
CREATE TABLE
test=# select * from cities;
name | population | altitude
------+------------+----------
(0 rows)
test=# select * from capitals;
name | population | altitude | state
------+------------+----------+-------
(0 rows)
向父表插入2条数据
test=# INSERT INTO cities values('Las Vegas', 1.53, 2174);
INSERT 0 1
test=# INSERT INTO cities values('Mariposa',3.30,1953);
INSERT 0 1
向子表里也插一条
test=# INSERT INTO capitals values('Madison',4.34,845,'WI');
INSERT 0 1
向父表查询的时候 会把子表中的数据也一起Load出来
test=# SELECT name, altitude FROM cities WHERE altitude > 500;
name | altitude
-----------+----------
Las Vegas | 2174
Mariposa | 1953
Madison | 845
(3 rows)
但是单查子表的时候 只会出现一条数据
test=# SELECT name, altitude FROM capitals WHERE altitude > 500;
name | altitude
---------+----------
Madison | 845
(1 row)
晚上回去模拟多一点数据 看一下到底会有什么影响