1. mysql 的默认规定 1.1 MySQL在Linux下数据库名、表名、列名、别名大小写规则是这样的: 1、数据库名与表名是严格区分大小写的; 2、表的别名是严格区分大小写的; 3、列名与列的别名在所有的情况下均是忽略大小写的; 4、变量名也是严格区分大小写的; 1.2 MySQL在Windows下都不区分大小写。 2. 解决方法 2.1 改变表结构 ALTER TABLE `tablename` MODIFY <a target=_blank target="_blank" href="https://www.baidu.com/s?wd=COLUMN&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y4m1wbPH0znW64PyPhrjn10ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnWcvP1nYnjmsP16vPjTYnW0zr0" class="baidu-highlight" rel="nofollow" style="color:rgb(45,100,179); text-decoration:none">COLUMN</a> `cloname` <a target=_blank target="_blank" href="https://www.baidu.com/s?wd=VARCHAR&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y4m1wbPH0znW64PyPhrjn10ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnWcvP1nYnjmsP16vPjTYnW0zr0" class="baidu-highlight" rel="nofollow" style="color:rgb(45,100,179); text-decoration:none">VARCHAR</a>(45) <a target=_blank target="_blank" href="https://www.baidu.com/s?wd=BINARY&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y4m1wbPH0znW64PyPhrjn10ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnWcvP1nYnjmsP16vPjTYnW0zr0" class="baidu-highlight" rel="nofollow" style="color:rgb(45,100,179); text-decoration:none">BINARY</a>; 2.2 强制类型转换 使用binary关键字 在字段或值前使用binary关键字 eg:select * from table_name where binary a like 'a%' <pre name="code" id="best-content-1876058963" class="best-text mb-10" style="margin-top:0px; margin-bottom:10px; padding:0px; font-family:arial,'courier new',courier,宋体,monospace,'Microsoft YaHei'; white-space:pre-wrap; word-wrap:break-word; font-size:14px; color:rgb(51,51,51); line-height:24px; background-color:rgb(243,255,236)"> eg:select * from table_name where a like binary 'a%'3. 注意事项
3.1 binary 不支持索引,查询效率底下,不建议考虑。上面这些sql语句乍看不会有什么问题,但是当表中的数据多了以后,问题就会凸显出来,用不到索引,就会导致查询效率非常低下。
eg:SELECT * FROM `tableName` WHERE ( BINARY weixin = 'value' ) LIMIT 1;
3.2 替代方案 支持索引,查询效率高
eg: SELECT * FROM `tableName` WHERE weixin = 'value' COLLATE utf8_bin LIMIT 1;
4. mysql字符集介绍
utf8_general_ci --不区分大小写
utf8_bin--区分大小写
*_bin: 表示的是binary case sensitive collation,也就是说是区分大小写的
*_cs: case sensitive collation,区分大小写
*_ci: case insensitive collation,不区分大小写