这是我的疑问:
create table if not exists applications (
idResearch int not null,
idProf char(10) not null,
primary key (idResearch, idProf),
foreign key (idResearch) references research(idResearch),
foreign key (idProf) references professor(idProf)
);
这是表格:
INSERT INTO `university`.`applications` (`idResearch`, `idProf`) VALUES ('10', '123456789');
INSERT INTO `university`.`applications` (`idResearch`, `idProf`) VALUES ('10', '432156789');
INSERT INTO `university`.`applications` (`idResearch`, `idProf`) VALUES ('10', '789654321');
INSERT INTO `university`.`applications` (`idResearch`, `idProf`) VALUES ('11', '876098432');
INSERT INTO `university`.`applications` (`idResearch`, `idProf`) VALUES ('11', '234567890');
INSERT INTO `university`.`applications` (`idResearch`, `idProf`) VALUES ('12', '432156789');
INSERT INTO `university`.`applications` (`idResearch`, `idProf`) VALUES ('12', '789654321');
INSERT INTO `university`.`applications` (`idResearch`, `idProf`) VALUES ('13', '876098432');
INSERT INTO `university`.`applications` (`idResearch`, `idProf`) VALUES ('14', '876098432');
INSERT INTO `university`.`applications` (`idResearch`, `idProf`) VALUES ('14', '987654321');
INSERT INTO `university`.`applications` (`idResearch`, `idProf`) VALUES ('15', '432156789');
INSERT INTO `university`.`applications` (`idResearch`, `idProf`) VALUES ('16', '123456789');
INSERT INTO `university`.`applications` (`idResearch`, `idProf`) VALUES ('16', '432156789');
INSERT INTO `university`.`applications` (`idResearch`, `idProf`) VALUES ('16', '789654321');
INSERT INTO `university`.`applications` (`idResearch`, `idProf`) VALUES ('17', '789654321');
INSERT INTO `university`.`applications` (`idResearch`, `idProf`) VALUES ('17', '432156789');
15:26:13创建表如果不存在应用程序(idResearch int not null,idProf char(10)not null,主键(idResearch,idProf),外键(idResearch)引用研究(idResearch),外键( idProf)引用教授(idProf))错误代码:1215。无法添加外键约束0.109秒
答案 0 :(得分:0)
查询中的语法没有问题,可能是数据问题。
在执行插入查询之前,请检查以下内容
截断\删除并删除'应用程序'手动表,然后再次创建它。然后执行插入查询。
答案 1 :(得分:0)
我会在foreign key
语句中更改以下内容:
constraint FKResearch foreign key (idResearch) references research(idResearch),
constraint FKProfessor foreign key (idProf) references professor(idProf)
这样,当遇到错误时,MySQL会告诉你哪些外键约束被违反了。
一旦你知道,你必须检查列名是否匹配,你试图插入的值实际上在引用的表中有相应的父值,如果两列的定义是 相同(检查排序规则和编码,大多数情况下问题都在那里)。