All MySQL collations are of type PAD SPACE. This means that all CHAR, VARCHAR, and TEXT values are compared without regard to any trailing spaces. “Comparison” in this context does not include the LIKE pattern-matching operator, for which trailing spaces are significant. For example:
For those cases where trailing pad characters are stripped or comparisons ignore them, if a column has an index that requires unique values, inserting into the column values that differ only in number of trailing pad characters results in a duplicate-key error. For example, if a table contains 'a', an attempt to store 'a ' causes a duplicate-key error.
在 mysql 里 ,字符串等值比较会自动去除右边空格,存储的时候不会改变用户输入的字符串。主键要判断唯一性,会自动去除右边空格。
(root@127.1:3304) [test]> select 'a '='a ', 'a'='a', 'a'='a ';
+---------------+---------+-----------+
| 'a '='a ' | 'a'='a' | 'a'='a ' |
+---------------+---------+-----------+
| 1 | 1 | 1 |
+---------------+---------+-----------+
1 row in set (0.00 sec)