1.表字段修复
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_general_ci;' ) '修正SQL' FROM information_schema.`COLUMNS` WHERE COLLATION_NAME RLIKE 'utf8';
utf8 是我demo的模糊匹配排序规则,这里需要替换为你数据库中需要替换的字段的排序规则,
utf8mb4设置的是替换的字符集,
utf8mb4_general_ci设置的是替换的排序规则,
下面是运行结果:
把修正SQL 复制出来直接运行即可,
ALTER TABLE scm_users.users MODIFY COLUMN password varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; ALTER TABLE scm_warehouse.contact MODIFY COLUMN address varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; ALTER TABLE scm_warehouse.contact MODIFY COLUMN country varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; ALTER TABLE scm_warehouse.contact MODIFY COLUMN postal_code varchar(6) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
......
2.表修复
SELECT TABLE_SCHEMA '数据库', TABLE_NAME '表', TABLE_COLLATION '原排序规则', CONCAT( 'ALTER TABLE ', TABLE_SCHEMA, '.', TABLE_NAME, ' COLLATE=utf8mb4_general_ci;' ) '修正SQL' FROM information_schema.`TABLES` WHERE TABLE_COLLATION RLIKE 'utf8';
表修复只需要设置排序规则,字符集会自动设置到正确的标准,
下面是SQL跑出来的样例。
把修正SQL 复制出来直接运行即可。
3.数据库修复
#修改数据库 SELECT SCHEMA_NAME '数据库', DEFAULT_CHARACTER_SET_NAME '原字符集', DEFAULT_COLLATION_NAME '原排序规则', CONCAT( 'ALTER DATABASE ', SCHEMA_NAME, ' CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;' ) '修正SQL' FROM information_schema.`SCHEMATA` WHERE DEFAULT_CHARACTER_SET_NAME RLIKE 'utf8';
下面是SQL跑出来的样例。