Skip to content
Advertisement

Error by foreign key use in MySQL Workbench,error code 1064

1.Hello I have a problem with my SQL code.I become Error Code: 1064. You have an error in your SQL syntax.

2.Error:13:05:36 ADD CONSTRAINT FK_ID_Mannschaft FOREIGN KEY (ID_Mannschaft) REFERENCES Mannschaft(ID_Mannschaft) ON UPDATE CASCADE ON DELETE CASCADE Error Code: 1064. 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 CONSTRAINT FK_ID_Mannschaft FOREIGN KEY (ID_Mannschaft) REFERENCES `Man’ at line 1 0.032 sec

    ALTER TABLE `Trainier`
    ADD `ID_Mannschaft` INT NOT NULL,
    ADD CONSTRAINT FK_ID_Mannschaft 
    FOREIGN KEY (`ID_Mannschaft`) REFERENCES `Mannschaft`(`ID_Mannschaft`)
    ON UPDATE CASCADE ON DELETE CASCADE;

Advertisement

Answer

Assuming the structure given below, no error found, pls check data type and constraint.

Create Table Mannschaft (ID_Mannschaft Int, Constraint Primary Key (ID_Mannschaft));
Create Table Trainier (ID VarChar(40));


ALTER TABLE `Trainier`
    ADD `ID_Mannschaft` INT NOT NULL,
    ADD CONSTRAINT FK_ID_Mannschaft 
    FOREIGN KEY (`ID_Mannschaft`) REFERENCES `Mannschaft`(`ID_Mannschaft`)
    ON UPDATE CASCADE ON DELETE CASCADE;
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement