码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2021-06-21 20:31:26      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:字段名   def   ase   批量修改   技术   修改   字符集   mic   ima   

问题

数据库大小写不敏感

库名,表名敏感:
配置参数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‘;

同上

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

标签:字段名   def   ase   批量修改   技术   修改   字符集   mic   ima   

原文地址:https://www.cnblogs.com/liuqiang88/p/14912045.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!