Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
479 views
in Technique[技术] by (71.8m points)

mysql - Trailing whitespace in varchar needs to be considered in comparison

I am using a table with a varchar column. I did not realize that trailing whitespace is not considered in comparisons (and that, apparently, two values that differ only in amount of trailing whitespace will violate the uniqueness property, if specified).

I need to fix this in the table, preferably in place. Is there a recommended path to fixing a table like this in MySQL?

I am accessing the DB strictly through a program I control, so switching to a non-human readable format such as binary would be fine. But I am not sure how to do such a thing and don't want to destroy the table.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I have to assume you're using MySQL 5.x because MySQL 4.x doesn't store trailing spaces in a VARCHAR column.

Using the standard = operator in MySQL, as you indicated, trailing spaces are not considered:

SELECT 'this' = 'this ' returns TRUE

However, LIKE compares the strings character by character, so trailing spaces are significant.

SELECT 'this' LIKE 'this ' returns FALSE.

Both = and LIKE may be case insensitive, using the default collation. Use the COLLATE clause to specify the collation if you need to compare them in a case sensitive manner.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...