这样的东西可能吗?
INSERT INTO Table2 (val) VALUES ((INSERT INTO Table1 (name) VALUES ('a_title') RETURNING id));
像使用返回值作为值在第二个表中插入一行,并引用第一个表?
你可以这样做从Postgres 9.1开始:
with rows as ( INSERT INTO Table1 (name) VALUES ('a_title') RETURNING id ) INSERT INTO Table2 (val) SELECT id FROM rows
同时,如果你只对id感兴趣,你可以使用触发器:
create function t1_ins_into_t2() returns trigger as $$ begin insert into table2 (val) values (new.id); return new; end; $$ language plpgsql; create trigger t1_ins_into_t2 after insert on table1 for each row execute procedure t1_ins_into_t2();