键"PRIMARY"的条目重复.忽略字符串的空格 [英] Duplicate entry for key 'PRIMARY' .Ignoring the spaces for strings
问题描述
mysql> CREATE TABLE primary2(boom text,id int,PRIMARY KEY(boom(5)) );
Query OK, 0 rows affected (0.04 sec)
mysql> INSERT INTO primary2 VALUES('viok',1);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO primary2 VALUES('viok',1);
ERROR 1062 (23000): Duplicate entry 'viok' for key 'PRIMARY'
mysql> INSERT INTO primary2 VALUES('viok ',1);
ERROR 1062 (23000): Duplicate entry 'viok ' for key 'PRIMARY'
STRING的"viok"和"viok"不同.如何添加带空格的字符串?
STRINGs 'viok' and 'viok ' are different .How to add a string with spaces?
推荐答案
问:如何添加带空格的字符串?
Q: How to add a string with spaces?
A:这是不可能的.
就MySQL如何将它们存储为索引条目而言,TEXT值'viok'和'viok'不是唯一的.这是已记录的行为:
The TEXT values 'viok' and 'viok ' are not unique, in terms of how MySQL stores those as index entries. This is documented behavior:
如果为TEXT列建立索引,则索引条目比较将在末尾加空格.这意味着,如果索引要求唯一值,则仅尾随空格数量不同的值将发生重复键错误.例如,如果表包含"a",则尝试存储"a"会导致重复键错误.对于BLOB列,情况并非如此.
If a TEXT column is indexed, index entry comparisons are space-padded at the end. This means that, if the index requires unique values, duplicate-key errors will occur for values that differ only in the number of trailing spaces. For example, if a table contains 'a', an attempt to store 'a ' causes a duplicate-key error. This is not true for BLOB columns.
这篇关于键"PRIMARY"的条目重复.忽略字符串的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!