在下面的代码中,当我插入example_actions
时,example_id
的值应从example
引用,其中platform
和platform_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