【问题标题】:Insert data into user-defined type elements of a table in postgresql在 postgresql 中将数据插入到表的用户定义类型元素中
【发布时间】:2018-12-15 15:48:47
【问题描述】:

我定义了以下三种类型:

CREATE TYPE EventDescriptionType AS ENUM (
 'felt report',
 'Flinn-Engdahl region',
 'local time',
 'tectonic summary',
 'nearest cities',
 'earthquake name',
 'region name'
);

CREATE TYPE ResourceReference AS (
  "resourceID" varchar(255)  
);

CREATE TYPE EventDescription AS (
 "text" text,
 "type" EventDescriptionType
);

我还创建了一个包含上述类型元素的表格:

CREATE TABLE Event (
 "Event_id"                   serial PRIMARY KEY,
 "description"                EventDescription ARRAY
);

然后,通过这个命令向这个表中插入一些数据后:

insert into Event values (1,'{'L','felt report'}');

我收到了这个错误:

错误:“L”处或附近的语法错误
第 1 行:插入事件值 (1,'{'L','felt report'}');

对于事件表中的元素“描述”,我将“1”作为 event_id 传递,并将“L”和“感觉报告”传递给一个数组,分别用于 EventDescription 类型的“文本”和“类型”。

有人可以告诉我正确的方法吗? 任何帮助,将不胜感激。

【问题讨论】:

    标签: arrays postgresql insert create-table sql-types


    【解决方案1】:

    您需要将数组类型转换为EventDescription[]

    insert into Event values (1,
                              ARRAY[('L','felt report')]::EventDescription[]
                              );
    

    sqlfiddle

    【讨论】:

    • 当然。我会这样做的:)
    猜你喜欢
    • 2022-11-17
    • 2016-03-05
    • 2017-09-23
    • 1970-01-01
    • 2019-09-21
    • 1970-01-01
    • 2019-02-07
    • 2014-03-17
    • 1970-01-01
    相关资源
    最近更新 更多