문제

I'm trying to add a constraint to my two tables, but I can't remember what the correct syntax is. Below are the two tables,

enter image description here

Is it.........

ALTER TABLE dispatch ADD CONSTRAINT fk_productlines FOREIGN KEY
(productlines_fkid) REFERENCES productlines(fkid)

Is that right? When I try

ALTER TABLE dispatch ADD CONSTRAINT fk_productlines FOREIGN KEY
(fkid) REFERENCES productlines(fkid)

I get the following message:

#1072 - Key column 'fkid' doesn't exist in table
도움이 되었습니까?

해결책

Is productlines.fkid a primary key ?

To make it a foreign key in dispatch it must be a primary key of productlines

To add a foreign key in dispatch :

ALTER TABLE dispatch 
ADD CONSTRAINT fk_productlines 
FOREIGN KEY (id) REFERENCES productlines(fkid)

다른 팁

ALTER TABLE product_lines ADD CONSTRAINT fk_productlines FOREIGN KEY
(fkid) REFERENCES dispatch(ID)

That should work.. Try it yourself!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top