问题
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