I want to use UUID() to generate primary keys.
After some investigation, I thought this should work:
INSERT INTO aTable (`id`) VALUES (UNHEX(REPLACE(UUID(),'-','')));
Where id is of the type BINARY(16).
Unfortunately, UUID() returns a value which uses the utf8_general_ci collation. The rest of my database uses utf8_unicode_ci, which means I get the following error:
#1270 - Illegal mix of collations (utf8_general_ci,COERCIBLE), (utf8_unicode_ci,COERCIBLE), (utf8_unicode_ci,COERCIBLE) for operation 'replace'
How can I persuade UUID() to play nicely and use utf8_unicode_ci?
解决方案INSERT INTO aTable (`id`) VALUES (UNHEX(REPLACE(UUID() COLLATE utf8_unicode_ci,'-','')));
本文探讨了在使用UUID作为MySQL数据库表主键时遇到的字符集冲突问题。通过调整UUID函数的字符集为utf8_unicode_ci,成功解决了非法字符集混合导致的错误。

4205

被折叠的 条评论
为什么被折叠?



