- 查看 my.ini 配置文件的服务端默认字符集
# 服务端默认字符集
character_set_server = utf8
- 查看当前数据库字符集
show VARIABLES like 'character%';
image.png
- 修改数据库字符集
alter database auth character set utf8;
注意:修改完数据库字符集,需要重启mysql数据库
- 修改表字符集
ALTER TABLE 表名 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
- 生成所有表修改字符集语句
SELECT TABLE_NAME,CONCAT('ALTER TABLE ',TABLE_NAME,' DEFAULT CHARACTER SET ',a.DEFAULT_CHARACTER_SET_NAME,' COLLATE ',a.DEFAULT_COLLATION_NAME,';') 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='数据库名'
- 修改列字符集
ALTER TABLE 表名 CHANGE 列名 列名 VARCHAR( 100 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL;
- 生成所有列修改字符集语句
select b.table_name,b.column_name,b.character_set_name,b.collation_name
,CONCAT('ALTER TABLE ',b.table_name,' CHANGE ',b.column_name ,' ',b.column_name,' ',b.DATA_TYPE,'(',b.CHARACTER_MAXIMUM_LENGTH,') ', 'CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL;') 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='linlonge'
网友评论