在mysql中更改数据类型_如何在MySQL中更改列的数据类型?

如果要将某个类型的所有列更改为其他类型,可以使用如下查询生成查询:

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

.如果需要的话,我会把它留给读者来实现。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值