开发者

MySQL Syntax in creating Foreign Key

Is this syntax correct in creating a Foreign Key?

create table department
(
  de开发者_开发技巧partmentID 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);


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


FOREIGN KEY (departmentID) REFERENCES department(departmentID)

Thanks.!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜