MySQL database with unique fields ignored ending spaces

后端 未结 4 418
别那么骄傲
别那么骄傲 2020-11-29 08:39

My projects requires to start inputs from the user with the spacing on the left and spacing on the right of a word, for example \'apple\'. If the user types in \' apple\' or

4条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 09:18

    You probably need to read about the differences between VARCHAR and CHAR types.

    The CHAR and VARCHAR Types

    When CHAR values are stored, they are right-padded with spaces to the specified length. When CHAR values are retrieved, trailing spaces are removed unless the PAD_CHAR_TO_FULL_LENGTH SQL mode is enabled.

    For VARCHAR columns, trailing spaces in excess of the column length are truncated prior to insertion and a warning is generated, regardless of the SQL mode in use. For CHAR columns, truncation of excess trailing spaces from inserted values is performed silently regardless of the SQL mode.

    VARCHAR values are not padded when they are stored. Trailing spaces are retained when values are stored and retrieved, in conformance with standard SQL.

    Conclusion: if you want to retain whitespace on the right side of a text string, use the CHAR type (and not VARCHAR).

提交回复
热议问题