MySQL 创建外键的语法

2022-01-20 00:00:00 foreign-keys 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);

相关文章