【mysql】 数据库字符集和排序规则

库的字符集影响表和字段的字符集

  • 数据库字符集 》表的字符集 》 字段的字符集 (从前往后优先级由低到高,从左往右继承,如果表没设置字符集,继承数据库的,如果字段没设置,继承表的)
  • 数据库的字符集如果是utf8,表和字段的字符集继承
  • 字段的字符集和排序规则继承自表,例如表的字符集为latin1,那表的字段(字符型)都是latin1
  • 修改表的字符集对新增加字段有用,但是老字段字符集不变,应再编写脚本修改老数据的字符集
--- 修改数据库字符集和排序规则
alter database xc  character set utf8 collate utf8_general_ci;
--- 查看数据库的字符集
select * from information_schema.schemata where schema_name = 'db_name';
--- 修改表默认的字符集
alter table table_name character set gbk collate gbk_bin;
--- 修改表数据的字符集
alter table table_name convert to character set gbk collate gbk_bin; 
--- 查看表的字符集
select * from information_schema.tables where table_schema = 'db_name' and table_name = 'table_name';
--- 修改字段的字符集
alter table table_name change column_name varchar(50) character set gbk collate gbk_bin;
--- 查看字段的字符集
select * from information_schema.columns where table_schema = 'db_name' and table_name = 'table_name';

如需引用,请注明来源

猜你喜欢

转载自www.cnblogs.com/china-flint/p/10137154.html