我在考虑区分大小写的情况下尝试选择MySQL中的项目,但是我遇到了让MySQL执行任何区分的问题。
例如,以下所有陈述
mysql> SELECT * FROM mytable WHERE name="alpha";
mysql> SELECT * FROM mytable WHERE name="ALPHA";
mysql> SELECT * FROM mytable WHERE name="aLpHa";
返回相同的结果:
+----+-------+
| id | name |
+----+-------+
| 1 | alpha |
+----+-------+
1 row in set (0.00 sec)
我认识到MySQL中的一些字符集和排序规则并不区分大小写,因此从默认位置更改字符集和排序规则是必要的步骤。我分别尝试changing and converting the table's character set and collate到utf8
和utf8_unicode_ci
,但我不确定它是否正常工作。
mysql> SHOW TABLE STATUS;
+---------+--------+---------+------------+-------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------+
| Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment |
+---------+--------+---------+------------+-------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------+
| mytable | InnoDB | 10 | Compact | 68886 | 83 | 5783552 | 0 | 7372800 | 17825792 | 67039 | 2014-08-02 15:05:07 | NULL | NULL | utf8_unicode_ci | NULL | | |
+---------+--------+---------+------------+-------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------+
1 row in set (0.04 sec)
mysql> SHOW FULL COLUMNS FROM mytable;
+-------+-------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+-------+-------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
| id | int(11) | NULL | NO | PRI | NULL | auto_increment | select,insert,update,references | |
| name | varchar(10) | utf8_unicode_ci | NO | | NULL | | select,insert,update,references | |
+-------+-------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
2 rows in set (0.01 sec)
从上面可以看出,表的排序规则现已更改为utf8_unicode_ci
,并且相关列的排序规则也已更改。
我需要执行任何额外的步骤,以便MySQL在字符大小写方面做出区分吗?我正在使用MySQL Server version: 5.5.37-0ubuntu0.13.10.1 (Ubuntu)
。
请注意,我不能简单地使用BINARY
关键字的查询,例如:
SELECT * FROM mytable WHERE BINARY name="alpha";
因为我实际上只是通过Django的ORM使用MySQL,据我所知,它不支持这种类型的查找。提前感谢您的帮助!
答案 0 :(得分:1)
您可以使用选项: 收集utf8_bin
实施例: http://sqlfiddle.com/#!2/60d73/1/0 与 http://sqlfiddle.com/#!2/60d73/2/0
您可以使用以下方式更改表格:
alter table mytable ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_bin;