创建外键的MySQL语法 [英] MySQL Syntax in creating Foreign Key

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

问题描述


$ b

 创建表部门

departmentID int not null auto_increment主键,
name varchar(30)
)type = InnoDB;

创建表员工

employeeID int not null auto_increment主键,
名称varchar(80),$ b $ job varchar(30),
departmentID int not null引用department(departmentID)
)type = InnoDB;


解决方案

MySQL接受它(不抱怨关于语法),但实际上并没有创建外键。

要创建这个外键,运行这个命令:

  ALTER TABLE雇员ADD CONSTRAINT fk_department FOREIGN KEY(departmentID)REFERENCES department(departmentID); 


Is this syntax correct in creating a Foreign Key?

create table department
(
  departmentID int not null auto_increment primary key,
  name varchar(30)
) type=InnoDB;

create table employee
(
  employeeID int not null auto_increment primary key,
  name varchar(80),
  job varchar(30),
  departmentID int not null references department(departmentID)
) type=InnoDB;

解决方案

It looks like MySQL accepts it (doesn't complain about the syntax) but the foreign key is not actually created.

To create this foreign key, run this command:

ALTER TABLE employee ADD CONSTRAINT fk_department FOREIGN KEY (departmentID) REFERENCES department (departmentID);

这篇关于创建外键的MySQL语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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