MySQL Syntax in creating Foreign Key

喜欢而已 提交于 2019-12-19 05:45:20

问题


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;

回答1:


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);



回答2:


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)
) 



回答3:


FOREIGN KEY (departmentID) REFERENCES department(departmentID)

Thanks.!



来源:https://stackoverflow.com/questions/6977086/mysql-syntax-in-creating-foreign-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!