码迷,mamicode.com
首页 > 数据库 > 详细

MySQL的UNIQUE KEY对数据中字母的大小写不敏感

时间:2016-11-03 18:43:28      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:mysql   engine   code   record   方法   row   set   字母   nod   

今天遇到一个坑,对于下列这样一个表:

CREATE TABLE `test3` (
  `id` int(11) NOT NULL,
  `name` char(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

它的数据如下:

+----+------+
| id | name |
+----+------+
|  1 | aaa  |
|  2 | Aaa  |
+----+------+

现在我想在这个表上的name字段加一个唯一索引,发现无法创建:

root:test_4> alter table test3 add unique index uniq_name(name);
ERROR 1062 (23000): Duplicate entry ‘aaa‘ for key ‘uniq_name‘

发现UNIQUE KEY对字母的大小写不敏感,具体原因现在还没查到,查到解决方法如下:

root:test_4> alter table test3 add unique index uniq_name(name);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

也就是将name列的类型由varchar改为varchar binary类型,这样就可以解决了。

具体原因待后续查找。

MySQL的UNIQUE KEY对数据中字母的大小写不敏感

标签:mysql   engine   code   record   方法   row   set   字母   nod   

原文地址:http://www.cnblogs.com/wzzz123/p/6027553.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!