mysql - 在 MySQL 中创建外键

标签 mysql foreign-keys mysql-workbench

我尝试使用 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/

相关文章:

mySql 排名排序从-到

php - 记录不显示 PHP MySQL

sql-server - 优化删除大量外键引用的表

php - 使用 preRemove/postRemove 事件来获取哪些查询可以执行,哪些不可以

php - 简单: Passing PHP var into SQL fails MySqli query

php - MySql 使用左连接插入到 2 个不同的表中

mysql - 无法添加外键约束,Mysql

mysql - 如何在 mysql 数据库上/中运行脚本?

mysql - 使用列中的开始和结束索引修剪不需要的文本

mysql - 如何设置 MySQL Workbench 自动断开与服务器的连接?