---
title: "mysql 的 case (in)sensitivity"
author: "FZtree"
date: "2018-08-20T06:24:56Z"
tags: []
categories: ["数据库"]
slug: "mysql-case-in-sensitivity"
draft: false
lastmod: "2018-08-20T06:27:57Z"
id: 2428958
url: "https://fztree.iteye.com/blog/2428958"
---

在mysql中存在着各种utf8编码格式，如下表：

 

1）utf8\_bin

utf8\_bin将字符串中的每一个字符用二进制数据存储，区分大小写。

 

2）utf8\_general\_ci

utf8\_genera\_ci不区分大小写，ci为case insensitive的缩写，即大小写不敏感。

 

3）utf8\_general\_cs

utf8\_general\_cs区分大小写，cs为case sensitive的缩写，即大小写敏感。

 

执行如下命令：

 

create table test\_bin (

  name varchar(32) not null primary key,

  age int unsigned not null

) engine = InnoDB COLLATE=utf8\_bin;

 

create table test\_ci (

  name varchar(32) not null primary key,

  age int unsigned not null

) engine = InnoDB COLLATE=utf8\_general\_ci;

 

create table test\_cs (

  name varchar(32) not null primary key,

  age int unsigned not null

) engine = InnoDB COLLATE=utf8\_general\_cs;

 

1,2命令成功 3执行失败，不支持utf8\_genral\_cs。

 

insert into test\_bin values('Alice', 18);

insert into test\_bin values('alice', 20);

以上命令能够执行成功，因为utf8\_bin是以十六进制方式存储数据，两条记录的主键不重复。

 

insert into test\_ci values('Alice', 18);

insert into test\_ci values('alilce', 20);

以上命令执行失败，因为utf8\_general\_ci不区分大小写，两条记录的主键重复。

 

最后 看下官方的文档

case (in)sensitivity  

https://dev.mysql.com/doc/refman/5.0/en/case-sensitivity.html

 

problems 

https://bugs.mysql.com/bug.php?id=65830 

https://bugs.mysql.com/bug.php?id=50909 

https://bugs.mysql.com/bug.php?id=63164