前言
继22.3版本兼容oracle alter table modify语法后,最近又接入mysql的应用,功能不足以应对现在的使用场景。要对mysql的alter table modify常用功能进行兼容。
oracle和mysql针对这个功能,区别在于:默认值(default)和非空约束(NOT NULL和NULL)位置可以交换;相同点:如果需要设置数据类型,则必须在默认值和非空约束前。
测试
新增测试用例如下(因为之前22.3兼容mysql alter modify修改表字段类型、默认值、非空约束已经涉及到大量的测试用例):
--默认值非空约束属性交换位置
create table test_modify(key1 int, key2 int);
alter table test_modify modify key1 varchar(100) not null default 'thanks';
insert into test_modify(key2) values (33);
select * from test_modify;drop table test_modify;
create table test_modify(key1 int,key2 int);
alter table test_modify modify key1 char not null;
alter table test_modify modify key1 not null; -- oracle fail
alter table test_modify modify key1 null;
alter table test_modify modify key1 default 'thanks'; --oracle fail
insert into test_modify(key2) values (33); -- fail when excute
alter table test_modify modify key1 varchar(100);
alter table test_modify modify key1 default 'thanks';
alter table test_modify modify key1 not null;
insert into test_modify(key2) values (33);
select * from test_modify;
drop table test_modify;
-- oracle default NULL, oracle is fail,but not lightdb and mysql
create table test_modify(key1 int,key2 int);
alter table test_modify modify key1 char null;
insert into test_modify(key2) values (33);
select * from test_modify;
drop table test_modify;
-- NOT NULL
create table test_modify(key1 int,key2 int);
alter table test_modify modify key1 char not null;
insert into test_modify(key2) values (33);
select * from test_modify;
drop table test_modify;
-- Abnormal
create table test_modify(key1 int,key2 int);
alter table test_modify modify key1 sfs char vvxvsv not null fgd default 'a'; --faild
rop table test_modify;
结论
(1)由于oracle默认属性是not null,因此不可以重复设置该属性;lightdb未作该限制。功能不影响。
(2)设置属性不合理,oracle会在修改默认属性对该类型长度进行判断;
lightdb不会立即报错,但是在执行插入的时候会报长度错误。功能不影响。