mysql批量数据库字符集
1、查看当前数据库字符集
- show VARIABLES like 'character%';
2、修改数据库字符集(修改完数据库字符集,需要重启mysql数据库)
- alter database 数据库名 character set utf8mb4;
3、修改表存储引擎及字符集
- ALTER TABLE 表名 ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
生成修改所有表存储引擎及字符集语句
- SELECT TABLE_NAME,CONCAT('ALTER TABLE ',TABLE_NAME,' ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;') executeSQL FROM information_schema.SCHEMATA a,information_schema.TABLES b
- WHERE a.SCHEMA_NAME=b.TABLE_SCHEMA
- AND a.DEFAULT_COLLATION_NAME!=b.TABLE_COLLATION
- AND b.TABLE_SCHEMA='数据库名'
我是用Navicat运行后直接复制的,复制后在允许即可
4、修改列字符集
- ALTER TABLE 表名 CHANGE 列名 VARCHAR( 100 ) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL;
生成所有列修改字符集语句:
- select b.table_name,b.column_name,b.character_set_name,b.collation_name
- ,CONCAT('ALTER TABLE ',b.table_name,' MODIFY ',b.column_name,' ',b.DATA_TYPE,'(',b.CHARACTER_MAXIMUM_LENGTH,') ',CASE WHEN b.COLUMN_DEFAULT IS NULL THEN '' ELSE CONCAT('DEFAULT \'',b.COLUMN_DEFAULT,'\'') END,' COMMENT \'',b.COLUMN_COMMENT,'\';') executeSQL
- from information_schema.TABLES a,information_schema.COLUMNS b where b.character_set_name IS NOT NULL and a.TABLE_SCHEMA=b.TABLE_SCHEMA AND a.TABLE_NAME=b.TABLE_NAME
- AND a.TABLE_COLLATION!=b.COLLATION_NAME
- and a.TABLE_SCHEMA='数据库名'