• 请不要在回答技术问题时复制粘贴 AI 生成的内容
xiangyuecn
V2EX  ›  程序员

发现多种数据库 group by 对字符串首尾空格的坑死人不偿命规范

  •  1
     
  •   xiangyuecn ·
    xiangyuecn · Nov 13, 2020 · 3869 views
    This topic created in 2183 days ago, the information mentioned may be changed or developed.

    rt 已测试 mysql sqlserver

    "字符串" "字符串 " 会分到同一组

    "字符串" " 字符串" 又不会分到同一组

    这种默认操作站在字符数据存储的角度,和默认不区分大小写一样愚蠢至极,本人观点。

    19 replies  •  2020-11-14 12:51:35 +08:00
    lxk11153
    lxk11153
    Nov 13, 2020
    1
    还有这个 feature [doge]
    xiangyuecn
    xiangyuecn
    OP
    Nov 13, 2020
    2
    都是空,表现还不一样:

    \u00a0==' ' mysql✅,sqlserver❌
    \u2000==' ' mysql✅,sqlserver❌
    \u3000==' ' mysql✅,sqlserver✅

    字符串首尾空格,似乎还影响到了 len 、length 、=、!= 、in,太可怕,这种功能却不是另外开一个明确表意的函数来做,黑暗、可怕
    DonaldY
    DonaldY
    Nov 13, 2020
    3
    所以,这种需要 group by 的字段,为什么会出现 空格。
    zzvgtiger
    zzvgtiger
    Nov 13, 2020
    4
    @DonaldY 表设计的时候能把后续所有需求都考虑到么?
    telung
    telung
    Nov 13, 2020❤️2
    5
    发现了这么大的 BUG, 快提给 MYSQL 团队,微软公司啊
    F281M6Dh8DXpD1g2
    F281M6Dh8DXpD1g2
    Nov 13, 2020
    6
    你用的是 varchar 还是 char?
    nikan999
    nikan999
    Nov 13, 2020
    7
    这种情况 是不是考虑数据严格相等比较好,不要去依赖数据库的具体实现
    xiangyuecn
    xiangyuecn
    OP
    Nov 13, 2020
    8
    @DonaldY #3 我也以为是 group by 有影响,但 len 、length 、=、!= 都有影响。并且空格这个定义太宽泛,不同语言实现完全不一致,数据库规范完全是在挖坑。


    js 可以 trim 掉常见但又敲不出的空格,如我 2 楼这 3 个

    java 只能 trim 掉 \u0020 键盘敲出来的这个空格,其他空格 trim 不掉

    其他语言没测试 trim,反正总有个地方会让你跳进去
    xiangyuecn
    xiangyuecn
    OP
    Nov 13, 2020
    9
    @telung #5 别闹😂 胡乱猜测他们妥协的原因也是因为历史原因,别人留下的屎山是不能乱动的
    xiangyuecn
    xiangyuecn
    OP
    Nov 13, 2020
    10
    @nikan999 #7 嗯,这是默认情况下才会出现的坑,如果我要特意去规避,自然有很多办法。但这种默认让普通的建表、查询很受伤
    syozzz
    syozzz
    Nov 13, 2020
    11
    从来还没注意过这个问题==
    CRVV
    CRVV
    Nov 13, 2020
    12
    https://www.postgresql.org/docs/current/datatype-character.html

    网页内搜索 SQL standard
    This somewhat bizarre exception is required by the SQL standard

    通常都用 VARCHAR 的,没这个问题

    或者上 PostgreSQL 全用 TEXT 完事。
    xuanbg
    xuanbg
    Nov 13, 2020
    13
    往后端传的时候都不 trim 掉首尾空格的吗?
    PopRain
    PopRain
    Nov 14, 2020 via Android❤️1
    14
    保存数据时随便保存,不注意规范数据,回头反过来赖数据库………尾部空格忽略掉的主要原因是最早数据库大部分用固定长度的 Char 类型,忽略尾部空格方便比较。
    xiangyuecn
    xiangyuecn
    OP
    Nov 14, 2020
    15
    @PopRain 典型的以偏概全😂 一棍子打死了有意义的尾部空格
    xiangyuecn
    xiangyuecn
    OP
    Nov 14, 2020
    16
    @PopRain 反倒是早期 char 类型,如果程序存入的不是固定长度,依赖数据库来保证长度一致性,那才是真正的乱存数据吧😂 数据库那调子规范总有那么点为了兼容而去兼容
    PopRain
    PopRain
    Nov 14, 2020
    17
    SQL Server follows the ANSI/ISO SQL-92 specification (Section 8.2, <Comparison Predicate>, General rules #3) on how to compare strings with spaces. The ANSI standard requires padding for the character strings used in comparisons so that their lengths match before comparing them. The padding directly affects the semantics of WHERE and HAVING clause predicates and other Transact-SQL string comparisons. For example, Transact-SQL considers the strings 'abc' and 'abc ' to be equivalent for most comparison operations.

    The only exception to this rule is the LIKE predicate. When the right side of a LIKE predicate expression features a value with a trailing space, SQL Server does not pad the two values to the same length before the comparison occurs. Because the purpose of the LIKE predicate, by definition, is to facilitate pattern searches rather than simple string equality tests, this does not violate the section of the ANSI SQL-92 specification mentioned earlier.
    EminemW
    EminemW
    Nov 14, 2020
    18
    为啥数据库会存首尾有空格的字符串,是有什么特殊作用吗
    HFcbyqP0iVO5KM05
    HFcbyqP0iVO5KM05
    Nov 14, 2020 via Android
    19
    明明就是数据库行为不符合预期,各种怪数据自身不规范的。
    就 "ABC" 能做字符串," ABC" 就不配?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1380 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 123ms · UTC 22:33 · PVG 06:33 · LAX 14:33 · JFK 17:33
    ♥ Do have faith in what you're doing.