Pergunta

I am a total beginner in MySQL

Whenever I try to add a foreign key to a field it produces this error:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ADD FOREIGN KEY ID

Here's the code to add foreign key:

ALTER TABLE 'table_name'
ADD CONSTRAINT 'FK_FKName'
ADD FOREIGN KEY table_name(column_name)
REFERENCES OtherTable_name(OtherTable_column_name);

Please try to help me

Foi útil?

Solução

you can try without constraint name :

ALTER TABLE 'table_name'
ADD FOREIGN KEY table_name(column_name)
REFERENCES OtherTable_name(OtherTable_column_name);

Outras dicas

I think your syntax is a little wrong.

Try this:

ALTER TABLE 'table_name'
ADD CONSTRAINT 'FK_Name' FOREIGN KEY ('coloumn_name')
    REFERENCES 'table_name'('coloumn_name');

The syntax is like , please update as

ALTER TABLE employee ADD CONSTRAINT fk_department

FOREIGN KEY (departmentID)

references department (departmentID);

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top