character set和collation的是什么?

character set即字符集

我们常看到的UTF-8、GB2312、GB18030都是相互独立的character set。即对Unicode的一套编码。

那么如何理解Unicode与UTF-8、GB2312的区别呢?

打个比方,你眼前有一个苹果,在英文里称之为apple,而在中文里称之为苹果。

苹果这个实体的概念就是Unicode,而UTF-8,GB2312可以认为就是不同语言对苹果的不同称谓,本质上都是在描述苹果这个物。

collation即比对方法

用于指定数据集如何排序,以及字符串的比对规则。

character set与collation的关系

软件国际化是大势所趋,所以Unicode是国际化最佳的选择。当然为了提高性能,有些情况下还是使用latin1比较好。

MySQL有两个支持Unicode的character set:

  • ucs2:使用16bits来表示一个Unicode字符。
  • utf8:使用1~3bytes来表示一个Unicode字符。

选择哪个character set视情况而定,例如utf8表示latin字符只需要一个字节,所以当用户数据大部分为英文等拉丁字符时,使用utf8比较节省数据库的存储空间。据说SQL Server采用的是ucs2。

每个character set会对应一定数量的collation。查看方法是在MySQL的Console下输入:

show collation;

我们会看到这样的结果:

collation名字的规则可以归纳为这两类:

  • <character set>_<language/other>_<ci/cs>
  • <character set>_bin

例如:

utf8_danish_ci

ci是case insensitive的缩写,cs是case sensitive的缩写。即,指定大小写是否敏感。

utf8_bin是将字符串中的每一个字符用二进制数据存储,区分大小写。

奇怪的是utf8字符集对应的collation居然没有一个是cs的。

那么utf8_general_ci,utf8_unicode_ci,utf8_danish_ci有什么区别?他们各自存在的意义又是什么?

同一个character set的不同collation的区别在于排序、字符串对比的准确度(相同两个字符在不同国家的语言中的排序规则可能是不同的)以及性能。

例如:

utf8_general_ci在排序的准确度上要逊于utf8_unicode_ci,当然,对于英语用户应该没有什么区别。但性能上(排序以及比对速度)要略优于utf8_unicode_ci.例如前者没有对德语中ß=ss的支持。

而utf8_danish_ci相比utf8_unicode_ci增加了对丹麦语的特殊排序支持。

补充:

1、当表的character set是latin1时,若字段类型为nvarchar,则字段的字符集自动变为utf8。可见database character set,table character set,field character set可逐级覆盖。

2、在ci的collation下,如何在比对时区分大小写:

mysql> select * from pet;
+----------+-------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+----------+-------+---------+------+------------+-------+
| Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |
| whistler | Gwen | bird | NULL | 1988-09-25 | NULL |
+----------+-------+---------+------+------------+-------+
2 rows in set (0.00 sec) mysql> select * from pet where name = 'whistler';
+----------+-------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+----------+-------+---------+------+------------+-------+
| Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |
| whistler | Gwen | bird | NULL | 1988-09-25 | NULL |
+----------+-------+---------+------+------------+-------+
2 rows in set (0.00 sec) mysql> select * from pet where binary name = 'whistler';
+----------+-------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+----------+-------+---------+------+------------+-------+
| whistler | Gwen | bird | NULL | 1988-09-25 | NULL |
+----------+-------+---------+------+------------+-------+
1 row in set (0.00 sec) mysql> select * from pet where name = binary 'whistler';
+----------+-------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+----------+-------+---------+------+------------+-------+
| whistler | Gwen | bird | NULL | 1988-09-25 | NULL |
+----------+-------+---------+------+------------+-------+
1 row in set (0.00 sec)

推荐使用

select * from pet where name = binary 'whistler';

这样可以保证当前字段的索引依然有效,而

select * from pet where binary name = 'whistler';

会使索引失效。

参考:

http://zhongwei-leg.iteye.com/blog/899227(以上内容转自此篇文章)

http://blog.sina.com.cn/s/blog_9707fac301016wxm.html

http://blog.csdn.net/haiross/article/details/51273593

http://www.jb51.net/article/40854.htm

http://blog.csdn.net/chenghuan1990/article/details/10078931

http://stackoverflow.com/questions/367711/what-is-the-best-collation-to-use-for-MySQL-with-php

http://dev.MySQL.com/doc/refman/5.0/en/charset-Unicode-sets.html

http://dev.MySQL.com/doc/refman/5.1/en/show-collation.html

http://dev.MySQL.com/doc/refman/5.1/en/charset-binary-op.html

MySQL中character set与collation的理解(转)的更多相关文章

  1. 3个问题:MySQL 中 character set 与 collation 的理解;utf8_general_ci 与 utf8_unicode_ci 区别;uft8mb4 默认collation:utf8mb4_0900_ai_ci 的含义

    MySQL 中 character set 与 collation 的理解 出处:https://www.cnblogs.com/EasonJim/p/8128196.html 推荐: 编码使用 uf ...

  2. mysql 中 character set 与 collation 的理解

    character set 和 collation 的是什么? character set, 即字符集. 我们常看到的 utf-8, GB2312, GB18030 都是相互独立的 character ...

  3. MySQL中校验规则(collation)的选取对实际数据筛选的影响

    在mysql中存在着各种utf8编码格式,如下表:1)utf8_bin2)utf8_general_ci utf8_bin将字符串中的每一个字符用二进制数据存储,区分大小写.utf8_genera_c ...

  4. MYSQL中TIMESTAMP类型的默认值理解

    MYSQL中TIMESTAMP类型可以设定默认值,就像其他类型一样. 1.自动UPDATE 和INSERT 到当前的时间:表:----------- Table   Create Table      ...

  5. Mysql中各种与字符编码集(character_set)有关的变量含义

    mysql涉及到各种字符集,在此做一个总结. 字符集的设置是通过环境变量来设置的,环境变量和linux中的环境变量是一个意思.mysql的环境变量分为两种:session和global.session ...

  6. 【科普】MySQL中DDL操作背后的并发原理

    一. 简介 DQL:指数据库中的查询(select)操作. DML:指数据库中的插入(insert).更新(update).删除(delete)等行数据变更操作. DDL:指数据库中加列(add co ...

  7. MySQL基础知识:Character Set和Collation

    A character set is a set of symbols and encodings. A collation is a set of rules for comparing chara ...

  8. 关于MySQL中的自联结的通俗理解

    关于MySQL中的自联结的通俗理解 前言:最近在通过SQL必知必会这本书学习MySQL的基本使用,在学习中也或多或少遇到了点问题,我也正好分享给大家,我的这篇博客用到的所有表格的代码都是来自SQL必知 ...

  9. 如何理解 MySQL 中的 <=> 操作符?

    问题 : 我在看以前的一个开发者的代码时看到 WHERE p.name <=> NULL 在这个查询语句中 <=>符号是什么意思啊?是不是和 =号是一样啊?还是一个语法错误啊? ...

随机推荐

  1. hdu-4678-sg

    Mine Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submis ...

  2. 4.2 event

    using System; public delegate void DownloadStartHandler(object sender, DownloadStartEventArgs e); // ...

  3. oracle EBS grant 您不具有执行当前操作的足够权限。请与您的系统管理员联系。

    解决方式1: Set the profiles of the below three to 'None' 设置以下三个配置文件为无 FND_VALIDATION_LEVELFND 验证层 FND_FU ...

  4. mysql 查询某一主键在那些表中中被设置为外键了

    use information_schema; show tables; select * from KEY_COLUMN_USAGE where COLUMN_NAME='areaid';

  5. mysql创建索引-----高性能(五)

    转载地址:https://www.cnblogs.com/llzhang123/p/7889382.html 索引的创建可以在CREATE TABLE语句中进行,也可以单独用CREATE INDEX或 ...

  6. ShiroFilterFactoryBean 处理拦截资源文件问题(Shiro权限管理)

    一.需要定义ShiroFilterFactoryBean()方法,而ShiroFilterFactoryBean.class是实现了FactoryBean和BeanPostProcessor接口: 1 ...

  7. ps和fireworks切图网页优化,jpg为80时

  8. WLAN 802.11 a/b/g PHY Specification and EDVT Measurement V

    Receive Minimum Input Level (Sensitivity) 测试方法: Receiver Adjacent Channel Rejection (ACR) -For IEEE8 ...

  9. L220

    He must not allow this unusual barrier (obstacle) to stop him from fighting against the enemy.他绝不能让这 ...

  10. rem 移动端适配

    1.从网易与淘宝的font-size思考前端设计稿与工作流 http://www.cnblogs.com/lyzg/p/4877277.html 2.淘宝弹性布局方案lib-flexible实践 ht ...