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',
(CASE WHEN IS_NULLABLE = 'NO' THEN ' NOT NULL' ELSE '' END),
(CASE WHEN COLUMN_COMMENT = '' THEN ' ' ELSE concat(' COMMENT''', COLUMN_COMMENT, '''') END),
';'
) '修正SQL'
FROM information_schema.`COLUMNS`
WHERE
-- -过滤正确排序规则
COLLATION_NAME != 'utf8mb4_general_ci'
AND TABLE_SCHEMA != 'information_schema'
-- -数据库名称
AND TABLE_SCHEMA = 'app_illegal_tx';
#修改表排序规则
SELECT TABLE_SCHEMA '数据库',
TABLE_NAME '表',
TABLE_COLLATION '原排序规则',
CONCAT('ALTER TABLE ', TABLE_SCHEMA, '.', TABLE_NAME,
' CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;') '修正SQL'
FROM information_schema.`TABLES`
WHERE TABLE_COLLATION != 'utf8mb4_general_ci'
and TABLE_SCHEMA = 'app_illegal_tx';
标签:编码,ci,NAME,utf8mb4,COLUMN,数据库,mysql,TABLE,SCHEMA From: https://www.cnblogs.com/cuiyueyang/p/17176248.html