PostgreSQL从继承到分区
(pg_partman)
PostgreSQL从继承到分区(一)
http://my.oschina.net/lianshunke/blog/205296
PostgreSQL从继承到分区(二)
http://my.oschina.net/lianshunke/blog/205296
PostgreSQL从继承到分区(三)
http://my.oschina.net/lianshunke/blog/205316
一、Inheritance
http://www.postgresql.org/docs/9.3/static/ddl-inherit.html
通过下面的简单例子说明什么是表继承:
1.1 单表继承
postgres=# CREATE DATABASE lsk;
CREATE DATABASE
postgres=# \c lsk
You are now connected to database "lsk" as user "postgres".
lsk=# CREATE TABLE t1(id int,name varchar(20));
CREATE TABLE
lsk=# CREATE TABLE t2(age int) inherits(t1);
CREATE TABLE
{在子表中新添的字段会汇同父表的所有字段一起作为子表字段}
lsk=# \d t1
Table "public.t1"
Column | Type | Modifiers
--------+-----------------------+-----------
id | integer |
name | character varying(20) |
Number of child tables: 1 (Use \d+ to list them.)
lsk=# \d t2
Table "public.t2"
Column | Type | Modifiers
--------+-----------------------+-----------
id | integer |
name | character varying(20) |
age | integer |
Inherits: t1
插入数据:
lsk=# INSERT INTO t1 VALUES (1,'lian1');
INSERT 0 1
lsk=# INSERT INTO t1 VALUES (2,'lian2');
INSERT 0 1
lsk=# INSERT INTO t2 VALUES (3,'lian3',11);
INSERT 0 1
lsk=# INSERT INTO t2 VALUES (4,'lian4',12);
INSERT 0 1
查看表结果:
lsk=# SELECT * from t1;
id | name
----+-------
1 | lian1
2 | lian2
3 | lian3
4 | lian4
(4 rows)
{从父表中查询将显示父表及子表的所有数据}
lsk=# SELECT * from t2;
id | name | age
----+-------+-----
3 | lian3 | 11
4 | lian4 | 12
(2 rows)
{从子表中查询只显示子表中的数据}
lsk=# SELECT * from only t1;
id | name
----+-------
1 | lian1
2 | lian2
(2 rows)
{通过ONLY关键字实现只对父表的查询}
1.2 多表继承
续1.1节内容
1.2.1 级联继承
创建t3表继承上节子表t2:
lsk=# CREATE TABLE t3() inherits(t2);
CREATE TABLE
lsk=# \d t3
Table "public.t3"
Column | Type | Modifiers
--------+-----------------------+-----------
id | integer |
name | character varying(20) |
age | integer |
Inherits: t2
插入测试数据:
lsk=# INSERT INTO t3 VALUES (5,'lian5',13);
INSERT 0 1
查看表结果:
lsk=# SELECT * from t1;
id | name
----+-------
1 | lian1
2 | lian2
3 | lian3
4 | lian4
5 | lian5
(5 rows)
lsk=# SELECT * from t2;
id | name | age
----+-------+-----
3 | lian3 | 11
4 | lian4 | 12
5 | lian5 | 13
(3 rows)
lsk=# SELECT * from t3;
id | name | age
----+-------+-----
5 | lian5 | 13
(1 row)
1.2.2 并行继承
创建t4表继承顶层父表t1:
lsk=# CREATE TABLE t4() inherits(t1);
CREATE TABLE
lsk=# \d t4
Table "public.t4"
Column | Type | Modifiers
--------+-----------------------+-----------
id | integer |
name | character varying(20) |
Inherits: t1
lsk=# \d+ t1
Table "public.t1"
Column | Type | Modifiers | Storage | Description
--------+-----------------------+-----------+----------+-------------
id | integer | | plain |
name | character varying(20) | | extended |
Child tables: t2,
t4
Has OIDs: no
lsk=# INSERT INTO t4 VALUES (6,'lian6');
INSERT 0 1
lsk=# SELECT * from t1;
id | name
----+-------
1 | lian1
2 | lian2
3 | lian3
4 | lian4
6 | lian6
5 | lian5
(6 rows)
{可以看出查询是按照分层顺序查询,即先并行再级联}
lsk=# SELECT * from t4;
id | name
----+-------
6 | lian6
(1 row)
1.3 查看数据来源
lsk=# SELECT p.relname,c.* FROM t1 c, pg_class p WHERE c.tableoid = p.oid;
relname | id | name
---------+----+-------
t1 | 1 | lian1
t1 | 2 | lian2
t2 | 3 | lian3
t2 | 4 | lian4
t4 | 6 | lian6
t3 | 5 | lian5
(6 rows)
PostgreSQL从继承到分区(一)
http://my.oschina.net/lianshunke/blog/205296
PostgreSQL从继承到分区(二)
http://my.oschina.net/lianshunke/blog/205296
PostgreSQL从继承到分区(三)
http://my.oschina.net/lianshunke/blog/205316
来源:oschina
链接:https://my.oschina.net/u/1011289/blog/205296