Reputation: 1989
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;
Upvotes: 10
Views: 31394
Reputation: 11
FOREIGN KEY (departmentID) REFERENCES department(departmentID)
Thanks.!
Upvotes: 1
Reputation: 6442
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);
Upvotes: 19
Reputation: 51
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)
)
Upvotes: 5