创建外键时的MySQL语法

时间:2011-08-08 01:49:02

标签: mysql foreign-keys

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

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;

3 个答案:

答案 0 :(得分:17)

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

要创建此外键,请运行以下命令:

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

答案 1 :(得分:4)

create table employee
(
  employeeID int not null auto_increment primary key,
  name varchar(80),
  job varchar(30),
  departmentID int not null ADD CONSTRAINT fk_department FOREIGN KEY (departmentID) references department(departmentID)
) 

答案 2 :(得分:1)

curl -X GET --header 'Accept:application/json; charset=utf-8' http://localhost:8087/v2/servers/_defaultServer_/vhosts/_defaultVHost_/applications/testlive/monitoring/historic

感谢!