用外键插入表中

时间:2018-11-10 18:18:16

标签: sql postgresql

使用Postgresql

客户

Nmb_id | Order History | Search |

联系人

Primary_Key | Nmb_id_fk | Fax | Email | 

Nmb_id_fk是客户主键Nmb_id的外键。

尝试将数据插入具有FK关系的表中

SQL:

insert into contacts (fax, email, Nmb_id_fk) 
values( '123', 'a@a.com', (select "Nmb_id" from customer where "Nmd_id" = 4));

继续出错:

  

错误:关系“联系人”的列“ Nmb_id_fk”确实   不存在

1 个答案:

答案 0 :(得分:1)

insert into contacts (fax, email, "Nmb_id_fk") 
select '123', 'a@a.com', c."Nmb_id" 
from customer c 
where c."Nmd_id" = 4;