mysql varbinary 与 varchar

标签 mysql character collation

我们使用 varchar(255) 在 mysql 中存储“关键字”。我们面临一个问题,即 mysql 会忽略“=”中用于比较目的的所有尾随空格。它确实尊重“like”比较中的尾随空格,但它不允许我们在 varchar 列中存储带有和不带有尾随空格的相同单词(如果它上面有“UNIQUE”索引)。

因此,我们正在考虑改用 varbinary。当列值中有多字节字符时,有人能提出建议吗?

最佳答案

安多马尔

我们使用 5.0.5 版。所有 mysql 版本都忽略尾随空格进行比较。来自手册:

All MySQL collations are of type PADSPACE. This means that all CHAR and VARCHAR values in MySQL are compared without regard to any trailing spaces. This is true for all MySQL versions, and it makes no difference whether your version trims trailing spaces from VARCHAR values before storing them

此外,mysql 认为在索引中有/没有尾随空格的文本是重复的:

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 will result in a duplicate-key error. For example, if a table contains 'a', an attempt to store 'a ' causes a duplicate-key error.

而且,我们绝对需要关键字索引。 所以,我想我们有两个选择:varbinary 或 text。我们将评估“文本”的性能以及 varbinary 的多字节功能。

关于mysql varbinary 与 varchar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/973923/

相关文章:

php - 使用 php 和 mysqli 保存到 mysql 数据库

sorting - 在 Scala 中扩展 Seq.sortBy

mysql - 使用 wordpress 和 mysql 容器的 Docker Nginx 反向代理错误网关

java - “填充”标签中的 Unicode 字符

php - Apache 或 PHP 生成前置换行符

java - MySQL——问号

php - 如何通过计算在一个查询中更新数据库中的多行

mysql - 如何将子查询结果中的第一个值设置为 SQL 中的变量

mysql - 将多个表合并为一个表

character - 在 Mac OSX 上使用 eclipse,我得到随机的不可见字符\160。为什么?