MySQL 批量修改库、表、列的排序规则,处理数据库大小写不敏感问题。

时间:2021-06-21 20:31:26   收藏:0   阅读:0

问题

数据库大小写不敏感

库名,表名敏感:
配置参数lower_case_table_names 可选值 【0,1】0不敏感,1敏感
数据敏感:
排序规则:
   *_bin: 表示的是binary case sensitive collation,也就是说是区分大小写。
   *_ci: case insensitive collation,不区分大小写 。

修改排序规则sql

utf8mb4_general_ci 是字符集查询条件
CHARACTER SET utf8mb4 COLLATE utf8mb4_bin; 为要变更的字符集与排序规则

表字段修改sql

SELECT TABLE_SCHEMA ‘数据库‘,TABLE_NAME ‘表名‘,COLUMN_NAME ‘字段名‘,CHARACTER_SET_NAME ‘原字符集‘,COLLATION_NAME ‘原排序规则‘,CONCAT(‘ALTER TABLE ‘, TABLE_SCHEMA,‘.‘,TABLE_NAME, ‘ MODIFY COLUMN ‘,COLUMN_NAME,‘ ‘,COLUMN_TYPE,‘ CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;‘) ‘变更SQL‘
FROM information_schema.`COLUMNS` 
WHERE COLLATION_NAME RLIKE ‘utf8mb4_general_ci‘;

也可以按数据库过滤 ,增加 and TABLE_SCHEMA=‘dbName‘ ,其中dbName为数据库名称

结果图例

技术图片

复制变更sql 并执行

ALTER TABLE db_norisk_dev.cent_archive_category MODIFY COLUMN cate_name varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
ALTER TABLE db_norisk_dev.cent_archive_category MODIFY COLUMN created_name varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
ALTER TABLE db_norisk_dev.cent_archive_category MODIFY COLUMN updated_name varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
ALTER TABLE db_norisk_dev.cent_dict MODIFY COLUMN key_code varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
ALTER TABLE db_norisk_dev.cent_dict MODIFY COLUMN key_name varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
ALTER TABLE db_norisk_dev.cent_dict MODIFY COLUMN created_name varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
ALTER TABLE db_norisk_dev.cent_dict MODIFY COLUMN updated_name varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
ALTER TABLE db_norisk_dev.cent_dict_val MODIFY COLUMN dict_code varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
ALTER TABLE db_norisk_dev.cent_dict_val MODIFY COLUMN dict_val varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
ALTER TABLE db_norisk_dev.cent_dict_val MODIFY COLUMN created_name varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
ALTER TABLE db_norisk_dev.cent_dict_val MODIFY COLUMN updated_name varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;

表修改sql

SELECT TABLE_SCHEMA ‘数据库‘,TABLE_NAME ‘表名‘,TABLE_COLLATION ‘原排序规则‘,CONCAT(‘ALTER TABLE ‘,TABLE_SCHEMA,‘.‘, TABLE_NAME, ‘ COLLATE=utf8mb4_bin‘) ‘变更SQL‘
FROM information_schema.`TABLES`
WHERE TABLE_COLLATION RLIKE ‘utf8mb4_general_ci;‘;

同上

数据库修改sql

SELECT SCHEMA_NAME ‘数据库‘,DEFAULT_CHARACTER_SET_NAME ‘原字符集‘,DEFAULT_COLLATION_NAME ‘原排序规则‘,CONCAT(‘ALTER DATABASE ‘,SCHEMA_NAME,‘ CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;‘) ‘变更SQL‘
FROM information_schema.`SCHEMATA`
WHERE DEFAULT_CHARACTER_SET_NAME RLIKE ‘utf8mb4‘;

同上

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!