• <legend id='h50yB'><style id='h50yB'><dir id='h50yB'><q id='h50yB'></q></dir></style></legend>

    <tfoot id='h50yB'></tfoot>
    <i id='h50yB'><tr id='h50yB'><dt id='h50yB'><q id='h50yB'><span id='h50yB'><b id='h50yB'><form id='h50yB'><ins id='h50yB'></ins><ul id='h50yB'></ul><sub id='h50yB'></sub></form><legend id='h50yB'></legend><bdo id='h50yB'><pre id='h50yB'><center id='h50yB'></center></pre></bdo></b><th id='h50yB'></th></span></q></dt></tr></i><div id='h50yB'><tfoot id='h50yB'></tfoot><dl id='h50yB'><fieldset id='h50yB'></fieldset></dl></div>

          <bdo id='h50yB'></bdo><ul id='h50yB'></ul>

        <small id='h50yB'></small><noframes id='h50yB'>

      1. 具有唯一字段的 MySQL 数据库忽略结尾空格

        MySQL database with unique fields ignored ending spaces(具有唯一字段的 MySQL 数据库忽略结尾空格)

        1. <i id='JiLGe'><tr id='JiLGe'><dt id='JiLGe'><q id='JiLGe'><span id='JiLGe'><b id='JiLGe'><form id='JiLGe'><ins id='JiLGe'></ins><ul id='JiLGe'></ul><sub id='JiLGe'></sub></form><legend id='JiLGe'></legend><bdo id='JiLGe'><pre id='JiLGe'><center id='JiLGe'></center></pre></bdo></b><th id='JiLGe'></th></span></q></dt></tr></i><div id='JiLGe'><tfoot id='JiLGe'></tfoot><dl id='JiLGe'><fieldset id='JiLGe'></fieldset></dl></div>

          <small id='JiLGe'></small><noframes id='JiLGe'>

        2. <tfoot id='JiLGe'></tfoot>
              <tbody id='JiLGe'></tbody>
            <legend id='JiLGe'><style id='JiLGe'><dir id='JiLGe'><q id='JiLGe'></q></dir></style></legend>

                  <bdo id='JiLGe'></bdo><ul id='JiLGe'></ul>
                  本文介绍了具有唯一字段的 MySQL 数据库忽略结尾空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的项目需要从用户开始输入,左边的间距和右边的间距,例如apple".如果用户输入'apple'或'apple',不管是单词左边一个空格还是多个空格,我都需要这样存储.

                  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.

                  此字段具有 Unique 属性,但我尝试在左侧插入带有空格的单词,并且效果很好.但是,当我尝试在右侧插入带有间距的单词时,它会修剪掉该单词右侧的所有间距.

                  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
                  

                  推荐答案

                  问题是 MySQL 在进行字符串比较时会忽略尾随空格.看http://dev.mysql.com/doc/refman/5.7/en/char.html

                  The problem is that MySQL ignores trailing whitespace when doing string comparison. See http://dev.mysql.com/doc/refman/5.7/en/char.html

                  所有 MySQL 归类都是 PADSPACE 类型.这意味着在比较 MySQL 中的所有 CHAR、VARCHAR 和 TEXT 值时,不考虑任何尾随空格.

                  All MySQL collations are of type PADSPACE. This means that all CHAR, VARCHAR, and TEXT values in MySQL are compared without regard to any trailing spaces.

                  ...

                  对于删除尾随填充字符或比较忽略它们的情况,如果列具有需要唯一值的索引,则将仅在尾随填充字符数上不同的值插入到列中将导致重复键错误.例如,如果表包含a",则尝试存储a"会导致重复键错误.

                  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.

                  (此信息适用于 5.7;对于 8.0,此更改,见下文)

                  (This information is for 5.7; for 8.0 this changed, see below)

                  like 运算符的部分给出了这种行为的示例(并表明 like 确实尊重尾随空格):

                  The section for the like operator gives an example for this behavior (and shows that like does respect trailing whitespace):

                  mysql> SELECT 'a' = 'a ', 'a' LIKE 'a ';
                  +------------+---------------+
                  | 'a' = 'a ' | 'a' LIKE 'a ' |
                  +------------+---------------+
                  |          1 |             0 |
                  +------------+---------------+
                  1 row in set (0.00 sec)
                  

                  不幸的是,UNIQUE 索引似乎使用标准字符串比较来检查是否已经存在这样的值,因此忽略了尾随空格.这与使用 VARCHARCHAR 无关,在这两种情况下插入都被拒绝,因为唯一性检查失败.如果有一种方法可以将 like 语义用于 UNIQUE 检查,那么我不知道.

                  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 using VARCHAR or CHAR, in both cases the insert is rejected, because the unique check fails. If there is a way to use like semantics for the UNIQUE check then I do not know it.

                  你可以做的是将值存储为 VARBINARY:

                  What you could do is store the value as VARBINARY:

                  mysql> create table test_ws ( `value` varbinary(255) UNIQUE );
                  Query OK, 0 rows affected (0.13 sec)
                  
                  mysql> insert into test_ws (`value`) VALUES ('a');
                  Query OK, 1 row affected (0.08 sec)
                  
                  mysql> insert into test_ws (`value`) VALUES ('a ');
                  Query OK, 1 row affected (0.06 sec)
                  
                  mysql> SELECT CONCAT( '(', value, ')' ) FROM test_ws;
                  +---------------------------+
                  | CONCAT( '(', value, ')' ) |
                  +---------------------------+
                  | (a)                       |
                  | (a )                      |
                  +---------------------------+
                  2 rows in set (0.00 sec)
                  

                  你最好不要在这个列上按字母顺序排序,因为排序会发生在字节值上,而这不是用户期望的(大多数用户,无论如何).

                  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).

                  另一种方法是修补 MySQL 并编写您自己的 NO PAD 类型的排序规则.不确定是否有人想这样做,但如果您想这样做,请告诉我 ;)

                  The alternative is to patch MySQL and write your own collation which is of type NO PAD. Not sure if someone wants to do that, but if you do, let me know ;)

                  同时,根据 https://dev.mysql.com/doc/refman/8.0/en/char.html :

                  大多数 MySQL 排序规则都有 PAD SPACE 的 pad 属性.例外是基于 UCA 9.0.0 及更高版本的 Unicode 排序规则,它们的 pad 属性为 NO PAD.

                  Most MySQL collations have a pad attribute of PAD SPACE. The exceptions are Unicode collations based on UCA 9.0.0 and higher, which have a pad attribute of NO PAD.

                  和 https://dev.mysql.com/doc/refman/8.0/en/charset-unicode-sets.html

                  基于 4.0.0 之后的 UCA 版本的 Unicode 排序规则在排序规则名称中包含版本.因此,utf8mb4_unicode_520_ci 基于 UCA 5.2.0 权重键,而 utf8mb4_0900_ai_ci 基于 UCA 9.0.0 权重键.

                  Unicode collations based on UCA versions later than 4.0.0 include the version in the collation name. Thus, utf8mb4_unicode_520_ci is based on UCA 5.2.0 weight keys, whereas utf8mb4_0900_ai_ci is based on UCA 9.0.0 weight keys.

                  所以如果你尝试:

                    create table test_ws ( `value` varbinary(255) UNIQUE )
                      character set utf8mb4 collate utf8mb4_0900_ai_ci;
                  

                  您可以插入带有或不带有尾随空格的值

                  you can insert values with and without trailing whitespace

                  您可以通过以下方式找到所有可用的 NO PAD 归类:

                  You can find all available NO PAD collations with:

                   show collation where Pad_attribute='NO PAD';
                  

                  这篇关于具有唯一字段的 MySQL 数据库忽略结尾空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Bogus foreign key constraint fail(虚假外键约束失败)
                  how to get last insert id after insert query in codeigniter active record(如何在codeigniter活动记录中插入查询后获取最后一个插入ID)
                  Force InnoDB to recheck foreign keys on a table/tables?(强制 InnoDB 重新检查表/表上的外键?)
                  How to auto generate migrations with Sequelize CLI from Sequelize models?(如何使用 Sequelize CLI 从 Sequelize 模型自动生成迁移?)
                  Clear MySQL query cache without restarting server(无需重启服务器即可清除 MySQL 查询缓存)
                  ALTER TABLE to add a composite primary key(ALTER TABLE 添加复合主键)

                      <bdo id='9oPFU'></bdo><ul id='9oPFU'></ul>

                    • <legend id='9oPFU'><style id='9oPFU'><dir id='9oPFU'><q id='9oPFU'></q></dir></style></legend>
                          <tfoot id='9oPFU'></tfoot>

                          <small id='9oPFU'></small><noframes id='9oPFU'>

                          1. <i id='9oPFU'><tr id='9oPFU'><dt id='9oPFU'><q id='9oPFU'><span id='9oPFU'><b id='9oPFU'><form id='9oPFU'><ins id='9oPFU'></ins><ul id='9oPFU'></ul><sub id='9oPFU'></sub></form><legend id='9oPFU'></legend><bdo id='9oPFU'><pre id='9oPFU'><center id='9oPFU'></center></pre></bdo></b><th id='9oPFU'></th></span></q></dt></tr></i><div id='9oPFU'><tfoot id='9oPFU'></tfoot><dl id='9oPFU'><fieldset id='9oPFU'></fieldset></dl></div>
                              <tbody id='9oPFU'></tbody>