我尝试使用 MySQL 工作台在 MySQL 中创建外键。但有一个错误
$ Executing:
ALTER TABLE `project_course`.`attendance`
ADD CONSTRAINT `FK_Student`
FOREIGN KEY ('idStudent')
REFERENCES `project_course`.`student` ('userid')
ON DELETE NO ACTION
ON UPDATE NO ACTION;
Operation failed: There was an error while applying the SQL script to the database.
ERROR 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 ''idStudent')
REFERENCES `project_course`.`student` ('userid')
ON DELETE NO A' at line 3
SQL Statement:
ALTER TABLE `project_course`.`attendance`
ADD CONSTRAINT `FK_Student`
FOREIGN KEY ('idStudent')
REFERENCES `project_course`.`student` ('userid')
ON DELETE NO ACTION
ON UPDATE NO ACTION
最佳答案
问题出在引号上(在 PC 上它位于 Enter 键周围)。您已使用它们代替反引号(在 PC 上,它位于 Esc 键下)。
ALTER TABLE `project_course`.`attendance`
ADD CONSTRAINT `FK_Student`
FOREIGN KEY (`idStudent`) # Change them here around `idStudent`
REFERENCES `project_course`.`student` (`userid`) # and here around `userid`
ON DELETE NO ACTION
ON UPDATE NO ACTION;
关于mysql - 在 MySQL 中创建外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33195866/