在使用mysql数据库时,不注意的情况下,使用中文唯一键会碰到,不同中文字对应同一唯一键的情况。比如“四”“似”。这时,在插入db的时候,mysql会报错。
mysql 帮助文档有下面一段说明。
All string comparisons are case-insensitive by default, with sort ordering determined by the current character set (ISO-8859-1 Latin1 by default). If you don't like this, you should declare your columns with the BINARY
attribute or use the BINARY
cast, which causes comparisons to be done using the underlying character code values rather then a lexical ordering.
所以,在用中文作为主键,或唯一键的时候,最好在建表的时候给中文字段添加 BINARY 属性。