MySQL 批量修改库、表、列的排序规则
一、表字段修复:SELECT TABLE_SCHEMA '数据库',TABLE_NAME '表',COLUMN_NAME '字段',CHARACTER_SET_NAME '原字符集',COLLATION_NAME '原排序规则',CONCAT('ALTER TABLE ', TABLE_SCHEMA,'.',TABLE_NAME, ' MODIFY COLUMN ',COLUMN_NAME,' '
·
一、表字段修复:
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 'latin1';
latin1 是我demo的模糊匹配排序规则,这里需要替换为你数据库中需要替换的字段的排序规则,utf8mb4设置的是替换的字符集,utf8mb4_general_ci设置的是替换的排序规则,可以换为需要修正为什么标准,下面是SQL跑出来的样例。
把修正的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;
ALTER TABLE scm_warehouse.contact MODIFY COLUMN contact_person varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE scm_warehouse.contact MODIFY COLUMN contact_no varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE scm_warehouse.contact MODIFY COLUMN mobile varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE scm_warehouse.contact MODIFY COLUMN email varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE scm_warehouse.contact MODIFY COLUMN remark text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE scm_warehouse.warehouse MODIFY COLUMN warehouse_no varchar(6) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE scm_warehouse.warehouse MODIFY COLUMN name varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE scm_warehouse.warehouse_category MODIFY COLUMN name varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE scm_warehouse.warehouse_op_hours MODIFY COLUMN start_time varchar(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE scm_warehouse.warehouse_op_hours MODIFY COLUMN end_time varchar(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
二、表修复:
改变表
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 'latin1';
表修复只需要设置排序规则,字符集会自动设置到正确的标准,替换的话,跟上面一样,下面是SQL跑出来的样例。
把SQL复制出来运行,表就会修复了
三、 数据库修复:
#修改数据库
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';
作者:李飞_fd28
链接:https://www.jianshu.com/p/107aa90dc41b
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
更多推荐
所有评论(0)