Postgresql 直接加缺省字段和先添加字段再设置缺省值区别

创建测试表:
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


由此看出,先加字段,再设置默认值是不写历史数据的,只有新数据插入才使用默认值。
由以上实例可以看出,加字段,再加默认值和加带有默认值的字段是不一样的,希望使用的时候注意这个区别。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值