postgresql - 插入到只有一个元素的自定义类型的表列

标签 postgresql

在PostgreSQL 10.3中,我尝试了以下两种场景。

1.

CREATE TYPE customtype AS (val VARCHAR(20));
CREATE TABLE structdatatable(id INTEGER, structdata customtype);

然后执行了下面的插入语句。

INSERT INTO structdatatable VALUES(1, ('abc'));

这会导致以下错误。

DETAIL:  Missing left parenthesis.

2.

CREATE TYPE customtype2 AS (val VARCHAR(20), val2 VARCHAR(20));
CREATE TABLE structdatatable2(id INTEGER, structdata customtype2);

然后执行了下面的插入语句。

INSERT INTO structdatatable2 VALUES(1, ('abc','def'));

这会成功执行并产生以下输出。

INSERT 0 1

我也执行了一个选择语句来验证。结果如下。

 id | structdata 
----+------------
  1 | (abc,def)
(1 row)

是什么导致了这种行为?

最佳答案

括号中的单个值不会自动检测为记录。您可以使用 row constructor使其明确的语法:

INSERT INTO structdatatable VALUES(1, row('abc'));

关于postgresql - 插入到只有一个元素的自定义类型的表列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50736972/

相关文章:

postgresql - 如何在 Ubuntu 上使用 pgAdmin 将 Postgres 连接到本地主机服务器?

postgresql - 如何修复postgresql服务器自动重启

php - pg_connect fatal error 使用 php

ruby-on-rails - 在 Postgresql 中按字母搜索与 ActiveRecord 中的 sqlite3

sql - Postgres - 如何将 enum_1 数组转换为 enum_2?

python - 2 列上的 Array_agg 输出未被识别为列表

sql - 为什么我能够从子查询中引用外部查询列?

postgresql - 如何在 PostgreSQL 中运行临时脚本?

sql - Postgres 触发器 : write old record to new table

java - 如何在 spring-boot 和 postgresql 中使用 postgis 类型地理?