复合类型数组的正确语法
Correct syntax for array of composite type
CREATE TYPE pencil_count AS(
pencil_color varchar(30),
count integer
);
CREATE TABLE pencils(id serial, pencils_ pencil_count[]);
INSERT INTO pencils(pencils_) VALUES('{("blue",5),("red",2)}');
这不起作用并给出错误:
Malformed array literal.
如果我想在不使用 ARRAY[...]
的情况下添加此复合数组,正确的语法是什么?
I want to add this composite array without using ARRAY
您可以使用:
INSERT INTO pencils(pencils_)
VALUES('{"(\"blue\",5)","(\"red\",2)"}');
Remember that what you write in an SQL command will first be interpreted as a string literal, and then as a composite. This doubles the number of backslashes you need (assuming escape string syntax is used).
The string-literal processor removes one level of backslashes.
The ROW constructor syntax is usually easier to work with than the composite-literal syntax when writing composite values in SQL commands. In ROW, individual field values are written the same way they would be written when not members of a composite.
目前的建议不是最佳的。有一个更简单的解决方案和实际适用的解释。
如有疑问,请 Postgres 向您展示:
CREATE TEMP TABLE pencil_count ( -- table also registers row type
pencil_color varchar(30)
, count integer
);
CREATE TEMP TABLE pencils (
id serial
, pencils_ pencil_count[]
);
插入 2 个基本行:
INSERT INTO pencil_count VALUES ('red', 1), ('blue', 2);
查看基本行类型的语法:
SELECT p::text AS p_row FROM pencil_count p;
p_row
----------
(red,1)
(blue,2)
查看行数组的语法:
SELECT ARRAY(SELECT p FROM pencil_count p)::text AS p_row_arr;
p_row_arr
------------------------
{"(red,1)","(blue,2)"}
您只需要用双引号将每一行文字括起来 - 这只需要禁用每个行类型中 逗号 的特殊含义。
如果没有额外的特殊字符,额外的(转义的)双引号将是多余的噪音。
None 这与 转义字符串语法 有任何关系,自 Postgres 以来,它已被默认 关闭 9.1.您必须通过前缀 E
来显式声明转义字符串语法,例如 E'string\n'
。但是没有充分的理由这样做。
更多解释的相关答案:
- Insert text with single quotes in PostgreSQL
- How to pass custom type array to Postgres function
- PL/pgSQL Array of Rows
CREATE TYPE pencil_count AS(
pencil_color varchar(30),
count integer
);
CREATE TABLE pencils(id serial, pencils_ pencil_count[]);
INSERT INTO pencils(pencils_) VALUES('{("blue",5),("red",2)}');
这不起作用并给出错误:
Malformed array literal.
如果我想在不使用 ARRAY[...]
的情况下添加此复合数组,正确的语法是什么?
I want to add this composite array without using ARRAY
您可以使用:
INSERT INTO pencils(pencils_)
VALUES('{"(\"blue\",5)","(\"red\",2)"}');
Remember that what you write in an SQL command will first be interpreted as a string literal, and then as a composite. This doubles the number of backslashes you need (assuming escape string syntax is used).
The string-literal processor removes one level of backslashes.
The ROW constructor syntax is usually easier to work with than the composite-literal syntax when writing composite values in SQL commands. In ROW, individual field values are written the same way they would be written when not members of a composite.
目前的建议不是最佳的。有一个更简单的解决方案和实际适用的解释。
如有疑问,请 Postgres 向您展示:
CREATE TEMP TABLE pencil_count ( -- table also registers row type
pencil_color varchar(30)
, count integer
);
CREATE TEMP TABLE pencils (
id serial
, pencils_ pencil_count[]
);
插入 2 个基本行:
INSERT INTO pencil_count VALUES ('red', 1), ('blue', 2);
查看基本行类型的语法:
SELECT p::text AS p_row FROM pencil_count p;
p_row
----------
(red,1)
(blue,2)
查看行数组的语法:
SELECT ARRAY(SELECT p FROM pencil_count p)::text AS p_row_arr;
p_row_arr
------------------------
{"(red,1)","(blue,2)"}
您只需要用双引号将每一行文字括起来 - 这只需要禁用每个行类型中 逗号 的特殊含义。
如果没有额外的特殊字符,额外的(转义的)双引号将是多余的噪音。
None 这与 转义字符串语法 有任何关系,自 Postgres 以来,它已被默认 关闭 9.1.您必须通过前缀 E
来显式声明转义字符串语法,例如 E'string\n'
。但是没有充分的理由这样做。
更多解释的相关答案:
- Insert text with single quotes in PostgreSQL
- How to pass custom type array to Postgres function
- PL/pgSQL Array of Rows