mysql 批量修改字符集以及字符集排序

SELECT CONCAT('ALTER TABLE `', table_name, '` MODIFY `', column_name, '` ', DATA_TYPE, '(', CHARACTER_MAXIMUM_LENGTH, ') CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci', (CASE WHEN IS_NULLABLE = 'NO' THEN ' NOT NULL' ELSE '' END), ';')
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'crm'
AND (DATA_TYPE = 'varchar' or Data_Type = 'text')
AND
(
    CHARACTER_SET_NAME != 'utf8mb4'
    OR
    COLLATION_NAME != 'utf8mb4_general_ci'
);

SELECT TABLE_SCHEMA '数据库',TABLE_NAME '表',TABLE_COLLATION '原排序规则',CONCAT('ALTER TABLE ',TABLE_SCHEMA,'.',TABLE_NAME, ' CHARACTER SET utf8mb4 COLLATE=utf8mb4_general_ci;') '修正SQL'
FROM information_schema.`TABLES` where TABLE_COLLATION != 'utf8mb4_general_ci'

 

 

posted @ 2022-07-05 10:23  在路上的白羊  阅读(119)  评论(0编辑  收藏  举报