字段尾部空格无法识别

【 TiDB 使用环境】测试
具体操作:
mysql> insert into a select ‘x’;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> insert into a select 'x ';
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> create unique index uk_name on a(name);
ERROR 1062 (23000): Duplicate entry ‘x’ for key ‘a.uk_name’
mysql> select length(name) from a;
±-------------+
| length(name) |
±-------------+
| 1 |
| 3 |
±-------------+
2 rows in set (0.00 sec)

tidb字段内容尾部空格如何识别

是已知的问题,启用新的排序规则会有这个问题。
https://docs.pingcap.com/zh/tidb/dev/character-set-and-collation/#旧框架下的排序规则支持

在 4.0 版本之前,TiDB 中可以指定大部分 MySQL 中的排序规则,并把这些排序规则按照默认排序规则处理,即以编码字节序为字符定序。和 MySQL 不同的是,TiDB 不会处理字符末尾的空格,因此会造成区别

collation 的关系,show collation 里面可以看到 Pad_attribute,选 NO PAD 的 collation 就不会报错了

与 Mysql 一样,和 collation 有关,也可以看看 Mysql 官方文档:https://dev.mysql.com/doc/refman/8.4/en/charset-binary-collations.html
的 Trailing Space Handling in Comparisons 章节

此话题已在最后回复的 7 天后被自动关闭。不再允许新回复。