GBase 8s
其他
文章
南大通用 GBase 8s ALTER TABLE MODIFY 子句介绍
发表于2026-02-04 17:11:56299次浏览9个评论
在数据库开发中表结构的调整是一个常见的需求。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.希望以上内容能帮助您在实际开发中更高效地调整表结构。如果在使用过程中遇到任何问题,欢迎随时在社区中提问,我们在这里为您提供支持!
评论
登录后才可以发表评论
枫溪发表于 6个月前
1
枫溪发表于 6个月前
2
枫溪发表于 6个月前
3
枫溪发表于 6个月前
4
枫溪发表于 6个月前
5
郝老师发表于 5个月前
6
GBase用户19279发表于 5个月前
学习
GBase用户19279发表于 4个月前
学习
青菇凉发表于 2个月前
闪闪发光
热门帖子
- 12025-12-01浏览数:182764
- 22023-05-09浏览数:25062
- 42023-09-25浏览数:18526
- 52020-05-11浏览数:17529