Question

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
Was it helpful?

Solution

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)

OTHER TIPS

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

That should work.. Try it yourself!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top