PostgreSQL
 sql >> Base de Dados >  >> RDS >> PostgreSQL

usando os ids retornados de insert into, para inserção de registro com chave estrangeira

with s(name, distance) as (
    values ('archer goblin', 10), ('dragon', 50)
), the_ids as (
    insert into monster(name) 
    select name
    from s
    returning id, name
)
insert into ranged_monster (id_monster, distance)
select id, distance
from
    s
    inner join
    the_ids using (name)