postgresql 表继承
可以按如下语句创建表
CREATE TABLE capitals ( name text, population real, altitude int, -- (in ft) state char(2) ); CREATE TABLE non_captals ( name text, population real, altitude int -- (in ft) ); CREATE VIEW cities AS SELECT name, population, altitude FROM capitals UNION SELECT name, population, altitude FROM non_capitals;
使用表继承
CREATE TABLE cities ( name text, population real, altitude int -- (in ft) ); CREATE TABLE capitals ( state char(2) ) INHERITS (cities);
一张表可以继承多张表
往cities表里插入几条数据
再往capitals插入数据
查询时使用如下语句查询
SELECT name, altitude FROM cities WHERE altitude > 500;
可以看到把wuhan也查出来了
也可以只查一张表里面的
SELECT name, altitude FROM ONLY cities WHERE altitude > 500;
结果没有wuhan