Kevin Ho
Kevin Ho

Reputation: 1

Cannot add or update child row MySQL Error 1452

I am getting this error:

ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (university.registration, CONSTRAINT registration_ibfk_2 FOREIGN KEY (section_id) REFERENCES Section (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

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

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

Related Questions