如何将元素插入具有数组字段的列? [英] How to insert an element to a column with array field?
本文介绍了如何将元素插入具有数组字段的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我按如下方式创建了一个表
I created a table as follows
CREATE TABLE COMPANY (NO INT PRIMARY KEY NOT NULL, ID INT[] NOT NULL)
此处ID是数组类型。
现在,当我尝试使用
INSERT INTO company(no, id)VALUES (4, {1,2,3});
ERROR: syntax error at or near "{"
LINE 3: VALUES (4, {1,2,3});
有错误。
有人可以帮忙解决此问题吗?
There is an error. Could someone help in fixing this?
推荐答案
按照 https://www.postgresql.org/docs/current/static/arrays.html#ARRAYS-INPUT
添加引号:
将数组值写为文字常量,将元素
的值括在花括号中,并用逗号分隔。
To write an array value as a literal constant, enclose the element values within curly braces and separate them by commas.
t=# INSERT INTO company(no, id)VALUES (4, '{1,2,3}');
INSERT 0 1
或使用 array []
:
也可以使用ARRAY构造函数语法
The ARRAY constructor syntax can also be used
t=# INSERT INTO company(no, id)VALUES (5, array[1,2,3]);
INSERT 0 1
这篇关于如何将元素插入具有数组字段的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文