mysql 列名 小写_MYSQL脚本将列名转换为小写

bd96500e110b49cbb3cd949968f18be7.png

I am looking for a single MYSQL script to convert ALL column names in a database to lowercase in one go...

I have inherited a MYSQL database that has a lot of mixed case column names (150 tables with a strange naming convention) and I don't want to go through manually each table by table to do this.

Has anyone got such a script?

Thanks

解决方案

You can solve this task by building a script, starting with the output from this statement:

SELECT table_name, column_name, data_type

FROM information_schema.columns

WHERE table_schema = 'dbname';

ORDER BY table_name

Details about this feature can be found here "MYSQL::The INFORMATION_SCHEMA COLUMNS Table"

Then you can use the ALTER TABLE .. CHANGE feature to change the name of the columns

e.g.

ALTER TABLE mytable CHANGE old_name new_name varchar(5);

Different datatype have different requirements so you need the UNIONs:

SELECT 'ALTER TABLE '||table_name||' CHANGE '|| column_name||' '||lower(column_name)||' '||datatype||'('||CHAR(character_maximum_length)||');' AS Line

FROM information_schema.columns

WHERE table_schema = dbname and datatype in ( 'CHAR', 'VARCHAR' )

ORDER BY table_name

UNION

SELECT 'ALTER TABLE '||table_name||' CHANGE '|| column_name||' '||lower(column_name)||' '||datatype||'('||CHAR(numeric_precision)||');' AS Line

FROM information_schema.columns

WHERE table_schema = dbname and datatype in ( 'INTEGER' )

ORDER BY table_name

UNION

SELECT 'ALTER TABLE '||table_name||' CHANGE '|| column_name||' '||lower(column_name)||' '||datatype||'('||CHAR(numeric_precision)||','||CHAR(numeric_scale)|');' AS Line

FROM information_schema.columns

WHERE table_schema = dbname and datatype in ( 'FLOAT' )

ORDER BY table_name

UNION

SELECT 'ALTER TABLE '||table_name||' CHANGE '|| column_name||' '||lower(column_name)||' '||datatype||');' AS Line

FROM information_schema.columns

WHERE table_schema = dbname and datatype in ( 'DATE' )

ORDER BY table_name

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值