在 Postgres 中,除了最后一个表达式:'{null,}',您的所有表达式都是有效的,这会引发错误:
格式错误的数组字面量:“{null,}”
还值得注意的是null(和未定义的值)和{}(空数组)之间存在差异。假设您要写入具有not null 约束的列,null 将失败,而{} 将被允许。
Demo on DB Fiddle:
-- create the table
create table t (array_field int[])
-- insert values
insert into t values
('{1,2,3}'),
('{3,4,5}'),
('{null,2,3,4}'),
(null),
('{}')
;
-- 5 rows affected
-- won't work
insert into t values ('{null,}');
-- ERROR: malformed array literal: "{null,}"
-- LINE 1: insert into t values ('{null,}')
-- ^
-- DETAIL: Unexpected "}" character.
-- check the results
select array_field, array_length(array_field, 1) from t
数组字段 |数组长度
:----------- | ------------:
{1,2,3} | 3
{3,4,5} | 3
{NULL,2,3,4} | 4
空 |
空
{} |
空