在读pg的doc 的时候, value storage 的时候顺手测试了一下示例数据,结果就出问题了
PostgreSQL:digoal=> create table char_test(info char(20));
CREATE TABLE
digoal=> insert into char_test values ('abcdef');
INSERT 0 1
digoal=> select info,length(info) from char_test ;
info | length
----------------------+--------
abcdef | 6
(1 row)
而 doc 文档里的结果为 length 为 20
难道是文档错误??
在oracle 里做了测试确实是20 。
Oracle:
SQL> create table char_test (info char(20));
Table created.
SQL> insert into char_test values ('abcdef');
1 row created.
SQL> select info,length(info) from char_test;
INFO LENGTH(INFO)
-------------------- ------------
abcdef 20
一时很郁闷!!
找找资料解决问题:
看看两个数据库的函数解释。
Oracle :
The LENGTH functions return the length of char. LENGTH calculates length using characters as defined by the input character set. If char has datatype CHAR, then the length includes all trailing blanks. If char is null, then this function returns null. PostgreSQL :
length :
Number of characters in string 显然PostgreSQL未计算blank字符的长度.
再看看PostgreSQL的HISTORY文件 : 确实有类似改动。
HISTORY: * Add array_length() to return the length of an array for a specified
HISTORY: * The length() function no longer counts trailing spaces in CHAR(n)
HISTORY: * Make length() disregard trailing spaces in CHAR(n) (Gavin)
HISTORY: counted by length().
HISTORY: * The function "octet_length()" now returns the uncompressed data
HISTORY: * New function bit_length() (Peter E)
HISTORY: * Add pg_database_encoding_max_length() (Tatsuo)
HISTORY:Make char_length()/octet_length including trailing blanks (Tom)
问题终于搞清楚了 文档错误了
我顺便在文档里添加了一个comment 把pg 的history 加上了,这回不会误导人了。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/133735/viewspace-706772/,如需转载,请注明出处,否则将追究法律责任。