Postgres从另一个表插入具有ID的行

时间:2019-05-02 19:56:25

标签: postgresql insert

在下面的代码中,当我插入example_actions时,example_id的值应从example引用,其中platformplatform_id与{{ 1}}表

我有下面的工作代码,但是我认为这不是最佳方法,尤其是当我不得不插入数百万条记录时。

example

应在create table example ( id serial primary KEY, platform text not null, platform_id int not null, unique(platform, platform_id) ); create table example_actions ( id serial primary key, platform text not null, platform_id int not null, example_id int not null, action text not null, action_date timestamp not null, unique(example_id, action, action_date) ) ; insert into example(platform, platform_id) values('miami', 1) ; insert into example_actions(platform, platform_id, example_id, action, action_date) select 'miami', 1, id, 'flew', '2018-01-01' from example where platform = 'miami' and platform_id = 1; insert into example_actions(platform, platform_id, example_id, action, action_date) select 'miami', 1, id, 'drove', '2018-01-01' from example where platform = 'miami' and platform_id = 1; 表中创建两行,其中example_actions均为1

0 个答案:

没有答案