在postgres中将数据从父表插入子表

时间:2017-11-15 03:26:09

标签: postgresql jdbc

我想将父表中的数据插入到子表中,如果它不在子表中。

create table parents(
 c1 varchar  (100) primary key,
  c2 varchar  (100),

);
CREATE TABLE child
(
   c1 varchar (100) PRIMARY KEY, 
    c3 varchar (100),

     FOREIGN KEY (c1) REFERENCES parents(c1)
);

1 个答案:

答案 0 :(得分:0)

假设,通过数据,您只是指c1列,您可以使用从父级中选择的方式插入子级,您可以将该行过滤到子表中不存在的行。

insert into child (c1, c3)
select c1, c2
from parents
where c1 not in (
    select c1 from child
    );
相关问题