MySQL 创建外键的语法

MySQL Syntax in creating Foreign Key(MySQL 创建外键的语法)

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

问题描述

这种语法在创建外键时是否正确?

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;

推荐答案

看起来 MySQL 接受它(不抱怨语法),但实际上并没有创建外键.

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

本文标题为:MySQL 创建外键的语法

基础教程推荐