I have a database, trying to transfer it to hosting on MySQL. Created, say, two tables:

  • Table1 (ID1, Name1)
  • Table2 (ID2,ID1, Name2, Quantity)

ID1 (in Table 1) and ID2 are primary, and how to make ID2 refer to ID1? Something like this does not work:

 ALTER TABLE Table2 FOREIGN KEY ID2 REFERENCES Table1 (ID1) 
  • one
    Why ID2, not ID1? And missing ADD: ALTER TABLE Table2 ADD FOREIGN KEY ID1 REFERENCES Table1 (ID1) - alexlz
  • this does not change the error ... you have an error in your SQL syntax; If you’re the right line , you ’ll find out what you’re looking
  • sorry, of course, the list of columns must be enclosed in brackets. ALTER TABLE Table2 ADD FOREIGN KEY (ID1) REFERENCES Table1 (ID1); - alexlz
  • Show the result: SHOW CREATE TABLE Table1; SHOW CREATE TABLE Table2; - razielsd

1 answer 1

made from comments.


missing ADD :

 ALTER TABLE Table2 ADD FOREIGN KEY (ID1) REFERENCES Table1 (ID1);