Search code examples
mysqlstringtrim

MYSQL disable Auto-Trim


If I enter two Strings with only white space. I will Get this error message:

ERROR 1062: Duplicate entry ' ' for key 'PRIMARY'

How can I Turn off "Auto-Trim" ?

I'm Using this Charset: uft8-uft8_bin and This Datatype: Varchar.


Solution

  • According to the SQL 92 documentation, when two strings are compared they are first made equal in length by padding the shortest string with spaces.

    Search for 8.2 <comparison predicate> in the document.

    If the length in characters of X is not equal to the length
    in characters of Y, then the shorter string is effectively
    replaced, for the purposes of comparison, with a copy of
    itself that has been extended to the length of the longer
    string by concatenation on the right of one or more pad char-
    acters, where the pad character is chosen based on CS. If
    CS has the NO PAD attribute, then the pad character is an
    implementation-dependent character different from any char-
    acter in the character set of X and Y that collates less
    than any string under CS. Otherwise, the pad character is a
    <space>.
    

    So in other words. Its not about storing the value with the amount of spaces you entered, but its the comparisment it does to check for duplicate primary key. So you cannot have two strings with a different amount of spaces act as a primary key