0

我收到一个我完全不明白的错误。我正在尝试创建一个表,这是代码:

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`) REFERENCES `estudiantes` (`studentid`))

似乎是什么问题?它创建了表,但它给了我这个问题并且没有记录插入到表中。哪个查询解决了这个问题?我是 mySQL 的新手,所以我使用了很多来自教授的参考资料和示例。

4

1 回答 1

0

表中有正确的对应记录estudiantes吗?

AFOREIGN KEY表示您需要引用表中的数据,否则INSERT将不起作用。

要修复它,首先创建并使用有效数据填充您的estudiantes和表。curso

于 2013-05-20T18:07:38.190 回答