创建测试表:
hank=> create table t_p (c1 int,c2 varchar(20),c3 timestamp);
CREATE TABLE
hank=> create table t_p_1 (like t_p including all) inherits(t_p);
NOTICE: merging column "c1" with inherited definition
NOTICE: merging column "c2" with inherited definition
NOTICE: merging column "c3" with inherited definition
CREATE TABLE
hank=> create table t_p_2 (like t_p including all) inherits(t_p);
NOTICE: merging column "c1" with inherited definition
NOTICE: merging column "c2" with inherited definition
NOTICE: merging column "c3" with inherited definition
CREATE TABLE
查看父表和子表结构
hank=> \d+ t_p*
Table "hank.t_p"
Column | Type | Modifiers | Storage | Stats target | Description
--------+-----------------------------+-----------+----------+--------------+-------------
c1 | integer | | plain | |
c2 | character varying(20) | | extended | |
c3 | timestamp without time zone | | plain | |
Child tables: t_p_1,
t_p_2
Table "hank.t_p_1"
Column | Type | Modifiers | Storage | Stats target | Description
--------+-----------------------------+-----------+----------+--------------+-------------
c1 | integer | | plain | |
c2 | character varying(20) | | extended | |
c3 | timestamp without time zone | | plain | |
Inherits: t_p
Table "hank.t_p_2"
Column | Type | Modifiers | Storage | Stats target | Description
--------+-----------------------------+-----------+----------+--------------+-------------
c1 | integer | | plain | |
c2 | character varying(20) | | extended | |
c3 | timestamp without time zone | | plain | |
Inherits: t_p
插入测试数据:
hank=> insert into t_p_1 select generate_series(1,10000000),'dba',now();
INSERT 0 10000000
hank=> insert into t_p_2 select generate_series(1,10000000),'dba',now();
INSERT 0 10000000
测试1:添加无默认值的字段
hank=> alter table t_p add column c4 varchar(20);
ALTER TABLE
速度很快,不会重写表
测试2:添加有默认值的字段
hank=> alter table t_p add column c5 varchar(20) default 'test';
ALTER TABLE
查看表内容
hank=> select * from t_p_1 limit 10;
c1 | c2 | c3 | c4 | c5
----+-----+----------------------------+----+------
1 | dba | 2018-01-08 17:25:59.703244 | | test
2 | dba | 2018-01-08 17:25:59.703244 | | test
3 | dba | 2018-01-08 17:25:59.703244 | | test
4 | dba | 2018-01-08 17:25:59.703244 | | test
5 | dba | 2018-01-08 17:25:59.703244 | | test
6 | dba | 2018-01-08 17:25:59.703244 | | test
7 | dba | 2018-01-08 17:25:59.703244 | | test
8 | dba | 2018-01-08 17:25:59.703244 | | test
9 | dba | 2018-01-08 17:25:59.703244 | | test
10 | dba | 2018-01-08 17:25:59.703244 | | test
需要写表,所以添加默认值得字段要注意
测试3:首先添加无默认值得字段,然后再给已经添加的字段添加默认值
hank=> alter table t_p add column c6 varchar(20);
ALTER TABLE
hank=> alter table t_p alter column c6 set default 'hank';
ALTER TABLE
都很快就完成,没有重写表
查看表内容
hank=> select * from t_p_1 limit 10;
c1 | c2 | c3 | c4 | c5 | c6
----+-----+----------------------------+----+------+----
1 | dba | 2018-01-08 17:25:59.703244 | | test |
2 | dba | 2018-01-08 17:25:59.703244 | | test |
3 | dba | 2018-01-08 17:25:59.703244 | | test |
4 | dba | 2018-01-08 17:25:59.703244 | | test |
5 | dba | 2018-01-08 17:25:59.703244 | | test |
6 | dba | 2018-01-08 17:25:59.703244 | | test |
7 | dba | 2018-01-08 17:25:59.703244 | | test |
8 | dba | 2018-01-08 17:25:59.703244 | | test |
9 | dba | 2018-01-08 17:25:59.703244 | | test |
10 | dba | 2018-01-08 17:25:59.703244 | | test |
插入一行数据:
hank=> insert into t_p_1 select 1,'dba',now();
INSERT 0 1
查看数据:
hank=> select * from t_p_1 where c1=1;
c1 | c2 | c3 | c4 | c5 | c6
----+-----+----------------------------+----+------+------
1 | dba | 2018-01-08 17:25:59.703244 | | test |
1 | dba | 2018-01-08 17:39:16.124239 | | test | hank
由此看出,先加字段,再设置默认值是不写历史数据的,只有新数据插入才使用默认值。
由以上实例可以看出,加字段,再加默认值和加带有默认值的字段是不一样的,希望使用的时候注意这个区别。