由于数据库中的数据表和表字段的字符集和排序规则不统一,找了很多帖子,最后发现如下脚本很好用。
用法儿是:先执行如下脚本生成修改数据表和表字段的脚本,然后再执行这些生成的脚本。
1. 修改指定数据库中所有varchar类型的表字段的字符集为UTF8,并将排序规则修改为utf8_general_ci
SELECT CONCAT(‘ALTER TABLE `‘, table_name, ‘` MODIFY `‘, column_name, ‘` ‘, DATA_TYPE, ‘(‘, CHARACTER_MAXIMUM_LENGTH, ‘) CHARACTER SET UTF8 COLLATE utf8_general_ci‘, (CASE WHEN IS_NULLABLE = ‘NO‘ THEN ‘ NOT NULL‘ ELSE ‘‘ END), ‘;‘)
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = ‘databaseName‘
AND DATA_TYPE = ‘varchar‘
AND
(
CHARACTER_SET_NAME != ‘utf8‘
OR
COLLATION_NAME != ‘utf8_general_ci‘
);
2. 修改指定数据库中所有数据表的字符集为UTF8,并将排序规则修改为utf8_general_ci
SELECT CONCAT(‘ALTER TABLE ‘, table_name, ‘ CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;‘)
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = ‘databaseName‘
原文地址:https://www.cnblogs.com/denghuachengle/p/10436452.html