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

2020-11-13 15:10:01 +08:00
 xiangyuecn

rt 已测试 mysql sqlserver

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

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

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

2323 次点击
所在节点    程序员
19 条回复
lxk11153
2020-11-13 15:31:14 +08:00
还有这个 feature [doge]
xiangyuecn
2020-11-13 15:39:34 +08:00
都是空,表现还不一样:

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

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


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

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

其他语言没测试 trim,反正总有个地方会让你跳进去
xiangyuecn
2020-11-13 16:03:35 +08:00
@telung #5 别闹😂 胡乱猜测他们妥协的原因也是因为历史原因,别人留下的屎山是不能乱动的
xiangyuecn
2020-11-13 16:07:12 +08:00
@nikan999 #7 嗯,这是默认情况下才会出现的坑,如果我要特意去规避,自然有很多办法。但这种默认让普通的建表、查询很受伤
syozzz
2020-11-13 16:41:28 +08:00
从来还没注意过这个问题==
CRVV
2020-11-13 18:25:42 +08:00
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
2020-11-13 19:44:16 +08:00
往后端传的时候都不 trim 掉首尾空格的吗?
PopRain
2020-11-14 06:15:53 +08:00
保存数据时随便保存,不注意规范数据,回头反过来赖数据库………尾部空格忽略掉的主要原因是最早数据库大部分用固定长度的 Char 类型,忽略尾部空格方便比较。
xiangyuecn
2020-11-14 08:59:33 +08:00
@PopRain 典型的以偏概全😂 一棍子打死了有意义的尾部空格
xiangyuecn
2020-11-14 09:02:20 +08:00
@PopRain 反倒是早期 char 类型,如果程序存入的不是固定长度,依赖数据库来保证长度一致性,那才是真正的乱存数据吧😂 数据库那调子规范总有那么点为了兼容而去兼容
PopRain
2020-11-14 09:45:11 +08:00
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
2020-11-14 11:53:16 +08:00
为啥数据库会存首尾有空格的字符串,是有什么特殊作用吗
gulu
2020-11-14 12:51:35 +08:00
明明就是数据库行为不符合预期,各种怪数据自身不规范的。
就 "ABC" 能做字符串," ABC" 就不配?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/724866

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX