将数据插入 postgresql 中 table 的用户定义类型元素
Insert data into user-defined type elements of a table in postgresql
我定义了3种类型如下:
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
);
我还创建了一个 table 包含以上类型的元素:
CREATE TABLE Event (
"Event_id" serial PRIMARY KEY,
"description" EventDescription ARRAY
);
然后,在通过此命令向 table 中插入一些数据后:
insert into Event values (1,'{'L','felt report'}');
我收到这个错误:
ERROR: syntax error at or near "L"
LINE 1: insert into Event values (1,'{'L','felt report'}');
对于事件 table 中的元素 "dexcription" 我将“1”作为 event_id 和 'L' & 'felt report' 在数组中传递给 "text" 和 EventDescription 类型的 "type"。
有人可以告诉我正确的方法吗?
任何帮助,将不胜感激。
您需要将数组类型转换为 EventDescription[]
insert into Event values (1,
ARRAY[('L','felt report')]::EventDescription[]
);
我定义了3种类型如下:
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
);
我还创建了一个 table 包含以上类型的元素:
CREATE TABLE Event (
"Event_id" serial PRIMARY KEY,
"description" EventDescription ARRAY
);
然后,在通过此命令向 table 中插入一些数据后:
insert into Event values (1,'{'L','felt report'}');
我收到这个错误:
ERROR: syntax error at or near "L"
LINE 1: insert into Event values (1,'{'L','felt report'}');
对于事件 table 中的元素 "dexcription" 我将“1”作为 event_id 和 'L' & 'felt report' 在数组中传递给 "text" 和 EventDescription 类型的 "type"。
有人可以告诉我正确的方法吗? 任何帮助,将不胜感激。
您需要将数组类型转换为 EventDescription[]
insert into Event values (1,
ARRAY[('L','felt report')]::EventDescription[]
);