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 'apple ', whether it is one space or multiple space on the left or right of the word, I need to store it that way.
This field has the Unique attribute, but I attempt to insert the word with spacing on the left, and it works fine. But when I attempt to insert the word with spacing on the right it trims off all the spacing from the right of the word.
So I am thinking of adding a special character to the right of the word after the spacing. But I am hoping there is a better solution for this issue.
CREATE TABLE strings
( id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
string varchar(255) COLLATE utf8_bin NOT NULL,
created_ts timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id), UNIQUE KEY string (string) )
ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_bin
The problem is that MySQL ignores trailing whitespace when doing string comparison. See http://dev.mysql.com/doc/refman/5.7/en/char.html
The section for the
like
operator gives an example for this behavior (and shows thatlike
does respect trailing whitespace):Unfortunately the
UNIQUE
index seems to use the standard string comparison to check if there is already such a value, and thus ignores trailing whitespace. This is independent from usingVARCHAR
orCHAR
, in both cases the insert is rejected, because the unique check fails. If there is a way to uselike
semantics for theUNIQUE
check then I do not know it.What you could do is store the value as
VARBINARY
:You better do not want to do anything like sorting alphabetically on this column, because sorting will happen on the byte values instead, and that will not be what the users expect (most users, anyway).
The alternative is to patch MySQL and write your own collation which is of type NOPAD. Not sure if someone wants to do that, but if you do, let me know ;)
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).
Thanks to @kennethc. His answer works for me. Add a string length field to the table and to the unique key.
In MySQL it's possible to update the string length field with couple of triggers like this:
This is not about CHAR vs VARCHAR. SQL Server does not consider trailing spaces when it comes to string comparison, which is applied also when checking a unique key constraint. So it is not that you cannot insert value with trailing spaces, but once you insert, you cannot insert another value with more or fewer spaces.
As a solution to your problem, you can add a column that keeps the length of the string, and make the length AND the string value as a composite unique key constraint.
In SQL Server 2012, you can even make the length column as a computed column so that you don't have to worry about the value at all. See http://sqlfiddle.com/#!6/32e94 for an example with SQL Server 2012. (I bet something similar is possible in MySQL.)