MySQL數據表中內容大小寫區分的設置 MYSQL在默認的情況下查詢是不區分大小寫的,例如:
mysql> create table t1( -> name varchar(10)); Query OK, 0 rows affected (0.09 sec) mysql> insert into t1 values('you'),('You'),('YOU'); Query OK, 3 rows affected (0.05 sec) Records: 3 Duplicates: 0 Warnings: 0
對這個表,缺省情況下,下面兩個查詢的結果是一樣的:
mysql> select * from t1 where name = 'you'; +------+ | name | +------+ | you | | You | | YOU | +------+ 3 rows in set (0.00 sec) mysql> select * from t1 where name = 'YOU'; +------+ | name | +------+ | you | | You | | YOU | +------+ 3 rows in set (0.00 sec)
如果想讓MYSQL知道你輸入的字母是大寫還是小寫的,修改表:
mysql> alter table t1 change name name varchar(10) binary; Query OK, 3 rows affected (0.20 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from t1 where name = 'you'; +------+ | name | +------+ | you | +------+ 1 row in set (0.00 sec) mysql> select * from t1 where name = 'YOU'; +------+ | name | +------+ | YOU | +------+ 1 row in set (0.00 sec)
如果你只是想在SQL語句中實現的話:
mysql> select * from t1 where name = binary 'YOU'; +------+ | name | +------+ | YOU | +------+ 1 row in set (0.02 sec) mysql> select * from t1 where name = binary 'you'; +------+ | name | +------+ | you | +------+ 1 row in set (0.00 sec)
如果不想這麼麻煩而想服務一開啟就讓大小寫一致的話: 可以修改my.ini或者my.cnf
[mysqld] lower_case_table_names=1 (0:區分;1:不區分)
然後重啟MYSQL服務。
mysql> show variables like '%case_table%'; +------------------------+-------+ | Variable_name | Value | +------------------------+-------+ | lower_case_table_names | 1 | +------------------------+-------+ 1 row in set (0.00 sec)