一、修改所有表中的varchar和char字段以及排序集(dataBaseName修改为库名)
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 = 'dataBaseName'
AND (DATA_TYPE = 'varchar' OR DATA_TYPE = 'char')
注:修改后关于已添加的字符串类型的注释将会消失,需要重新添加
二、修改所有表的编码及排序集(dataBaseName修改为库名)
SELECT
CONCAT(
'ALTER TABLE ',
TABLE_NAME,
' CONVERT TO CHARACTER SET utf8mb4;'
)
FROM
information_schema. TABLES
WHERE
TABLE_SCHEMA = 'dataBaseName';