Insert into table with a foreign key

不羁的心 提交于 2021-01-29 14:52:31

问题


Using Postgresql

Customer

Nmb_id | Order History | Search |

Contacts

Primary_Key | Nmb_id_fk | Fax | Email | 

Nmb_id_fk is a foreign key to the Customer primary key Nmb_id.

Trying to insert data into a table with a FK relationship:

SQL:

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

Keep getting error:

ERROR: column "Nmb_id_fk" of relation "contacts" does not exist


回答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;


来源:https://stackoverflow.com/questions/53242023/insert-into-table-with-a-foreign-key

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!