
今天接到一个外包的需求,一个表中的字段含有大写也含小写,要全部转换为小写,批量转换。
表结构相关的信息肯定是从information_schema中搞,sql语句如下:
SELECT CONCAT('ALTER TABLE `', table_schema, '`.`', table_name,
'` CHANGE `', column_name, '` `', LOWER(column_name), '` ', column_type,
IF(IS_NULLABLE='NO',' NOT NULL',''),
IF(column_default <> '', CONCAT(' DEFAULT ', column_default), ''),
' ', extra )
FROM information_schema.columns
WHERE column_name REGEXP '^[A-Z]' COLLATE utf8_bin
AND table_schema <> 'information_schema';
原理是从information_schema动态拼出修改的sql语句,demo得到的结果如下:
ALTER TABLE `test`.`haha` CHANGE `AAA` `aaa` int(255) unsigned zerofill NOT NULL auto_increment
ALTER TABLE `test`.`haha` CHANGE `BBB` `bbb` int(11)
ALTER TABLE `test`.`haha` CHANGE `CCC` `ccc` int(11)
sql有了,拷贝出来批量执行一下就行了,settled~~




近期评论