复合类型数组的正确语法 [英] Correct syntax for array of composite type

查看:93
本文介绍了复合类型数组的正确语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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)}');

这不起作用并给出错误:

This doesn't work and gives error:


格式不正确的数组文字。

Malformed array literal.

如果我想添加此组合,正确的语法是什么数组而不使用 ARRAY [...]

What would be the correct syntax if I want to add this composite array without using ARRAY[...]?

推荐答案


我想添加此复合数组而不使用 ARRAY

I want to add this composite array without using ARRAY

您可以使用:

INSERT INTO pencils(pencils_) 
VALUES('{"(\"blue\",5)","(\"red\",2)"}');

SqlFiddleDemo


行类型

请记住,您在SQL命令中编写的内容将首先解释为字符串文字,然后解释为复合文字。

字符串字面量处理器会删除一个级别的反斜杠。

The string-literal processor removes one level of backslashes.

在SQL命令中编写复合值时,ROW构造函数语法通常比复合文字语法更易于使用。在ROW中,单个字段值的写入方式与非复合字段成员时的写入方式相同。

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.

这篇关于复合类型数组的正确语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆