MySQL 批量修改庫、表、列的排序規則,處理資料庫大小寫不敏感問題。
阿新 • • 發佈:2021-06-21
問題
資料庫大小寫不敏感
庫名,表名敏感:
配置引數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';
同上