开发者

mysql result is "special character"-insensitive

开发者 https://www.devze.com 2022-12-21 00:22 出处:网络
It seems that when i alter a mysql table (on a utf-8 table/columns) unique that it returns a duplicate entry error.

It seems that when i alter a mysql table (on a utf-8 table/columns) unique that it returns a duplicate entry error.

Example:

ALTER TABLE name ADD UNIQUE(name)

error:

Duplicate entry 'Adé' for key 'name_UNIQUE'

I think it's because of the follow to rows in my d开发者_运维百科atabase

Ade, Adé

Is it possible to alter a table unique with special characters?

Thank you,

Bob


You need to set the collation on the column (or the whole table or the database -- I don't think connection scope will work for unique constraints) to one that respects the difference between "e" and "é". See here for an example of the effects that different collation settings can have.

Assuming that your charset is utf8, the safest collation you can use to avoid getting false-positives on your unique constraint would be utf8_bin, in which two strings never compare equal unless they're the identical string. You'll need to be careful about things like unicode normalization, though, or else you might end up with two keys that are the identical sequence of characters, in different normalization forms, which makes them unequal. Just a little thing to watch out for.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号