在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
.如果需要的話,我會把它留給讀者來實現。
總結
以上是生活随笔為你收集整理的在mysql中更改数据类型_如何在MySQL中更改列的数据类型?的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: mariadb mysql同步_Cent
- 下一篇: 南核目录2020pdf_北核+南核|《消
