在MySQL中创建外键 [英] Creating foreign key in MySQL

查看:215
本文介绍了在MySQL中创建外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用MySQL工作台在MySQL中创建外键。但是有一个错误:

$ $ $ $ $ $ $ $ $ $执行
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;

操作失败:将SQL脚本应用于数据库时发生错误。
错误1064:您的SQL语法错误;检查与您的MySQL服务器版本相对应的手册,在idStudent附近使用正确的语法)
REFERENCES`project_course`.`student`('userid')
ON DELETE NO A'at line 3
SQL语句:
ALTER TABLE`project_course`.`attendance`
ADD CONSTRAINT`FK_Student`
FOREIGN KEY('idStudent')
REFERENCES`project_course`。 `学生'('userid')
在删除没有行动
在更新没有行动


问题在于引号(在PC上,它位于Enter键附近)。你已经使用它们而不是反引号(在PC上它位于Esc键下)。

pre $ ALTER TABLE`project_course`.`attendance `
ADD CONSTRAINT`FK_Student`
FOREIGN KEY(`idStudent`)#在`idStudent`附近改变它们
REFERENCES`project_course`.`student`(`userid`)# `userid`
ON DELETE NO ACTION
ON UPDATE NO ACTION;


I trying to create foreign key in MySQL using MySQL workbench. But there is an error

$ 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

解决方案

The problem is with the quotes (on PC it is located around Enter key). You have used them instead of backticks (on PC it is located under Esc key).

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中创建外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆