修改数据库编码格式语句
SELECT
CONCAT('ALTER DATABASE ',SCHEMA_NAME,' CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;') as 修改数据库编码格式语句
FROM information_schema.`SCHEMATA`
WHERE 1=1
AND SCHEMA_NAME = '数据库名称' #要修改的数据库名称
-- 修改所有自建库
-- AND SCHEMA_NAME NOT IN ('admindb','mysql','information_schema','performance_schema','sys');
AND DEFAULT_CHARACTER_SET_NAME <> 'utf8mb4'
AND (SELECT version()) REGEXP '^8'
修改表编码格式语句
SELECT CONCAT('ALTER TABLE `', TABLE_SCHEMA, '`.`', TABLE_NAME, '` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;') AS 修改表编码格式语句
FROM information_schema.`TABLES`
WHERE 1=1
AND TABLE_SCHEMA = '数据库名称' #要修改的数据库名称
-- 修改所有自建库
-- AND TABLE_SCHEMA not IN ('admindb', 'mysql', 'information_schema', 'performance_schema', 'sys')
AND TABLE_TYPE="BASE TABLE"
AND TABLE_COLLATION <> 'utf8mb4_general_ci';
AND (SELECT version()) REGEXP '^8' #只修改mysql8的
修改字段编码格式语句
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),(case when IFNULL(column_comment,'')='' then '' else concat(' COMMENT \'' , column_comment ,'\'') end),';') as 修改字段编码格式语句
FROM information_schema.COLUMNS
WHERE 1=1
and TABLE_SCHEMA = '数据库名称' #要修改的数据库名称
-- 修改所有自建库
-- and TABLE_SCHEMA NOT IN ('admindb', 'mysql', 'information_schema', 'performance_schema','sys')
and COLLATION_NAME <>'utf8mb4_general_ci' #被改的编码
and (SELECT version()) REGEXP '^8' #只修改mysql8的
PS: REGEXP 使用方法