如果要将某个类型的所有列更改为其他类型,可以使用如下查询生成查询:
select distinct concat('alter table ',
table_name,
' modify ',
column_name,
' ',
if(is_nullable = 'NO', ' NOT ', ''),
' NULL;')
from information_schema.columns
where table_schema = ''
and column_type = '';
例如,如果要更改列
tinyint(4)
到
bit(1)
,如下运行:
select distinct concat('alter table ',
table_name,
' modify ',
column_name,
' bit(1) ',
if(is_nullable = 'NO', ' NOT ', ''),
' NULL;')
from information_schema.columns
where table_schema = 'MyDatabase'
and column_type = 'tinyint(4)';
得到这样的输出:
alter table table1 modify finished bit(1) NOT NULL;
alter table table2 modify canItBeTrue bit(1) NOT NULL;
alter table table3 modify canBeNull bit(1) NULL;
!!不保留唯一约束,但应易于与其他约束一起修复
if
-参数到
concat
.如果需要的话,我会把它留给读者来实现。