我收到一个我根本不理解的错误。我正在尝试创建一个表,下面是代码:
create table matricula.curso_estudiantes
(codigo varchar(8) NOT NULL,
studentid varchar(8) NOT NULL,
grade varchar(1) NOT NULL,
term numeric(5) NOT NULL,
PRIMARY KEY (codigo, studentid, term),
FOREIGN KEY (codigo)
REFERENCES curso(codigo),
FOREIGN KEY (studentid)
REFERENCES estudiantes(studentid));
insert into matricula.curso_estudiantes values
("COMP2120","X00101010","C",201010),
("COMP2315","X00101010","B",201030),
("COMP2120","X00121111","B",201030),
("COMP2315","X00121111","A",201030),
("COMP2120","X00121234","A",201130),
("COMP2900","X00101010","C",201110),
("COMP3850","X00101010","B",201110),
("COMP2900","X00121111","B",201130),
("COMP3850","X00121111","A",201130),
("COMP2315","X00121234","A",201130),
("COMP2400","X00101010","C",201210),
("MATH1500","X00101010","B",201210),
("COMP2400","X00121111","B",201230),
("MATH1500","X00121111","A",201230),
("COMP3850","X00121234","A",201230),
("MATH1500","X00121234","W",201230);
但是我得到了这个错误:
错误代码:
约束失败(“ matricula”。`curso_estudiantes`,CONSTRAINT
“ curso_estudiantes_ibfk_2”外键(“ studentid”)参考
`estudiantes`(`studentid`))
似乎是什么问题?它创建了表,但是给了我这个问题,并且没有记录插入表中。哪个查询可以解决此问题?我是mySQL的新手,所以我使用了很多来自教授的参考资料和示例。
最佳答案
estudiantes
表中是否有正确的对应记录?FOREIGN KEY
表示您需要引用表中的数据,否则INSERT
将不起作用。
要修复此问题,请首先创建有效数据并在estudiantes
和curso
表中进行填充。
关于mysql - 表错误:1452无法添加或更新子行:外键?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16655526/