比较varchar和char [英] Compare varchar with char
问题描述
在比较 varchar
和 char
时,我试图弄清楚Postgres的工作。
I am trying to figure out what Postgres does when comparing varchar
with char
.
这是我的测试之一:
test=# select 'a'::character varying = 'a '::character;
?column?
----------
t
test=# select 'ab'::character varying = 'ab '::character;
?column?
----------
f
我的虫子。有人知道这是怎么回事吗?
This looks like a bug to me. Does anyone know what is going on here? Are there good documents on this topic?
推荐答案
根本不是错误。
强制转换字符串文字'ab'
转换为字符
,这是您得到的:
Not a bug at all.
Casting the string literal 'ab '
to character
, this is what you get:
a
$ b没有长度说明符的$ b
字符
等效于字符(1)
。
character
without length specifier is equivalent tocharacter(1)
.
'a':: character(1)
然后将被强制为 varchar
(个字符变化
)以测试是否与'a':: varchar
或'ab':: varchar
并分别产生 TRUE
或 FALSE
。
'a'::character(1)
will then be coerced to varchar
(character varying
) to test for equality with 'a'::varchar
or 'ab'::varchar
and yield TRUE
or FALSE
respectively.
基本上,几乎没有充分的理由完全使用 c 字符。这是一种旧式的,已经不再有用。只需使用
文本
或 varchar
。
Basically, there is hardly any good reason to use character
at all. It's a legacy type that has outlived its usefulness. Just use text
or varchar
.
- Any downsides of using data type "text" for storing strings?
这篇关于比较varchar和char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!