考虑以下模式
CREATE TYPE episode_id AS
(
season_nr int,
episode_nr int,
show_id bigint
);
CREATE TABLE episodes
(
id episode_id primary key,
title varchar(255) not null,
release_date timestamptz null
);
I want to insert to multiple rows in one query;
insert into episodes (id.season_nr, id.episode_nr, id.show_id, title, release_date)
values
(1, 1, 58811, 'Pilot', null),
(1, 2, 58811, 'Vector', null),
(1, 3, 58811, '274', null),
(1, 4, 58811, 'Single Strand', null),
(1, 5, 58811, 'The White Room', null);
它抛出:
> ERROR: multiple assignments to same column "id" SQL state: 42601
我的客户端库不支持 ROW
语法。我怎样才能做到这一点?
最佳答案
正确的语法:
INSERT INTO episodes (id, title, release_date)
VALUES
((1, 1, 58811), 'Pilot', null),
((1, 2, 58811), 'Vector', null);
您不能在 INSERT
语句的列列表中引用 id
的单个属性,只能引用整个 列。
添加的括号使三个值成为行类型/复合类型。
关于sql - 复合类型的多个插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22735060/