创建外键时的 MySQL 语法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6977086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:47:22 来源:igfitidea点击:
MySQL Syntax in creating Foreign Key
提问by aer
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;
回答by Doug
It looks like MySQL accepts it (doesn't complain about the syntax) but the foreign key is not actually created.
看起来 MySQL 接受它(不抱怨语法)但实际上并未创建外键。
To create this foreign key, run this command:
要创建此外键,请运行以下命令:
ALTER TABLE employee ADD CONSTRAINT fk_department FOREIGN KEY (departmentID) REFERENCES department (departmentID);
回答by user2725649
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)
)
回答by sagar more
FOREIGN KEY (departmentID) REFERENCES department(departmentID)
Thanks.!
谢谢。!