MySQL8改库,表,字段编码及排序规则

2023-12-14 12:00:13

修改数据库编码格式语句

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?使用方法

文章来源:https://blog.csdn.net/banyoyo/article/details/134929379
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。