南大通用 GBase 8s ALTER TABLE MODIFY 子句介绍

6 阅读3分钟

原文链接:www.gbase.cn/community/p…
更多精彩内容尽在南大通用GBase技术社区,南大通用致力于成为用户最信赖的数据库产品供应商。

在数据库开发中表结构的调整是一个常见的需求。GBase 8s 提供 ALTER TABLE MODIFY 子句,用于修改表中列的属性,包括数据类型、默认值、约束等。本文将介绍oracle模式下ALTER TABLE MODIFY子句的使用场景及限制,帮助你在实际开发中更灵活地调整表结构。

当修改列时,仅修改该列相关联的属性,包括DEFAULT缺省值、单列NULL或NOT NULL约束,可省略数据类型,该列数据类型与修改前数据类型保持一致。

示例1:

使用 MODIFY 子句更改列的数据类型,且改列上有默认值

在 oracle 模式下,使用 ALTER TABLE…MODIFY 语句修改列数据类型,被修改列上的默认约束保留。 例如,表 test_tab1 中 c1 列为 int 类型且存在 default 约束为 1201,修改 c1 数据类型为 varchar 类型后,默认约束保持不变。

> create table test_tab1(
c1 int default 1201,
c2 integer
);
Table created.
> alter table test_tab1 modify (c1 varchar(20));
Table altered.
> Insert into test_tab1(c2) values(1);
1 row(s) inserted.
> Select * from test_tab1;
C1                            C2
1201                           1
1 row(s) retrieved.

如果default值与要修改后的类型不能隐式转换时,将提示

> create table test_tab2(
c1 varchar2(10) default 'a',
c2 integer
);
Table created.
> alter table test_tab2 modify (c1 int);
591: Invalid default value for column/variable (c1).
Error in line 1
Near character position 37

示例2:

使用 MODIFY 子句给表中某列添加 NOT NULL 约束 

> create table test_tab3(
c1 int,
c2 char(2)
);
Table created.
> alter table test_tab3 modify c1  not null;
Table altered.
> insert into test_tab3(c2) values('a');
 391: Cannot insert a null into column (test_tab3.c1).
Error in line 1
Near character position 36

示例3:

使用 MODIFY 子句给表中某列添加默认值

> create table test_tab4(
c1 int,
c2 char(2)
);
Table created.
> alter table test_tab4 modify c1 default 1;  
Table altered.
> insert into test_tab4(c2) values('a');
1 row(s) inserted.
> select * from test_tab4;

        C1 C2
         1 a
1 row(s) retrieved.

限制:当修改有列约束与之相关联的列时,以下约束被删除: 
⚫ 所有单列约束被删除。 
⚫ 所有引用该列的引用约束被删除。 
⚫ 如果被修改列是多列主键或者唯一约束的一部分,则所有引用多列的引用约束也被删除。

示例4:

修改后改列的约束也会被删除 

> create table test_tab7(id int , id1 int ,s1 float constraint ck_1 check(s1>100));
Table created.
> insert into test_tab7 values(1,1,101);
1 row(s) inserted.
>  insert into test_tab7 values(2,1,90);
 530: Check constraint (root.ck_1) failed.
Error in line 1
Near character position 35
alter table test_tab7 modify s1 int;
Table altered.
> insert into test_tab7 values(2,1,90);
1 row(s) inserted.

示例5:

如果修改后的列是主键,主键及则引用改列的约束也会被删除


> create table test_tab5(id int primary key , s1 float);
Table created.
> create table test_tab6(id int , id1 int constraint f_id references test_tab5(id)  ,s1 float );
Table created.
> insert into test_tab5 values(1,100);
1 row(s) inserted.
> insert into test_tab5 values(2,200);
1 row(s) inserted.
> insert into test_tab6 values(1,2,45.67);
1 row(s) inserted.
> insert into test_tab6 values(2,3,55.67);
 691: Missing key in referenced table for referential constraint (root.f_id).
 111: ISAM error:  no record found.
Error in line 1
Near character position 38

> alter table test_tab5 modify id default 1010;
Table altered.
> insert into test_tab6 values(2,3,55.67);
1 row(s) inserted.

希望以上内容能帮助您在实际开发中更高效地调整表结构。如果在使用过程中遇到任何问题,欢迎随时在社区中提问,我们在这里为您提供支持!

原文链接:www.gbase.cn/community/p…
更多精彩内容尽在南大通用GBase技术社区,南大通用致力于成为用户最信赖的数据库产品供应商。