Reputation: 1
I am getting this error:
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (
university
.registration
, CONSTRAINTregistration_ibfk_2
FOREIGN KEY (section_id
) REFERENCESSection
(section_id
))
This is my code
INSERT INTO Registration VALUES (24766, 1102, 'B', 'B');
CREATE TABLE Registration (
student_id INT,
section_id INT,
midterm_grade VARCHAR(5),
final_grade VARCHAR(5),
PRIMARY KEY (student_id, section_id),
FOREIGN KEY (student_id)
REFERENCES Student (student_id),
FOREIGN KEY (section_id)
REFERENCES Section (section_id)
);
Any help would be appreciated on fixing this problem.
Upvotes: 0
Views: 118
Reputation: 522797
This is a common error in MySQL, most likey caused by either that student_id
24766
does not exist in the Student
table, or section_id
1102
does not exist in the Section
table.
The fix is to simply make sure that your foreign keys in the Registration
table point to actual primary keys of records in the other two tables. So, you may need to insert some data to resolve this error.
Upvotes: 2