我收到一个我根本不理解的错误。我正在尝试创建一个表,这里是代码:
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` FOREIGN KEY(`studentid`)参考文献 `estudiantes`(`studentid`))
似乎有什么问题?它创建了表,但它给了我这个问题,没有记录插入表中。哪个查询解决了这个问题?我是mySQL的新手,因此我使用了许多参考资料和教授的例子。
答案 0 :(得分:0)
您在estudiantes
表中是否有正确的相应记录?
FOREIGN KEY
表示您需要引用表格中的数据,否则INSERT
将无效。
要解决此问题,请先使用有效数据创建并填充estudiantes
和curso
表。