在学习工作中,我们会遇到向一些已经使用过程中的表中添加新字段,而且不允许为空。
这里以某个表为例:
alter table sys_org add (Is_bottom_gain char(1) null) ;
alter table sys_org add (Is_bottom_unit char(1) null) ;
alter table sys_org add (Belong_Financial_industry varchar2(100) null) ;
alter table sys_org add (Belong_profit_centre varchar2(100) null) ;
alter table sys_org add (Belong_Financial_org_cost varchar2(100) null) ;
alter table sys_org add (Belong_Financial_org_profit varchar2(100) null default '0') ;
alter table sys_org add (Account_org_name varchar2(100) null) ;
comment on column SYS_ORG.Is_bottom_gain
is '是否底层利润中心';
comment on column SYS_ORG.Is_bottom_unit
is '是否底层单位';
comment on column SYS_ORG.Belong_Financial_industry
is '财务所属行业';
comment on column SYS_ORG.Belong_profit_centre
is '所属利润中心';
comment on column SYS_ORG.Belong_Financial_org_cost
is '所属财务组织';
comment on column SYS_ORG.Belong_Financial_org_profit
is '所属财务组织';
comment on column SYS_ORG.Account_org_name
is '核算组织名称';
---------------------------我现在要新加两个不为空字段
alter table sys_org modify Is_bottom_gain default '0';
alter table sys_org modify Is_bottom_unit default '0';
ALTER TABLE sys_org MODIFY Is_bottom_gain not NULL;
ALTER TABLE sys_org MODIFY Is_bottom_unit not NULL;
直接设置not null 不可行。因为存在之前的数据。
所以这里可以先默认给他加上默认值 0;执行之后再重新设置这一列not null.