0

I have searched and read how to insert multiple rows in mySQL without typing insert again. But something is wrong. Can u please help me?

this is my code:

INSERT INTO studentsection (StudentSectionID,SectionID,StudentID)
VALUES ('4','1','13'),('5','2','2'),('6','2','6'),('7','2','10'),('8','2','14'),('9','3','3'),('10','3','7'),('40','3','11'),('11','3','15'),('12','4','4'),('13','4','8'),('14','4','12'),('15','4','16'),('16','5','1'),('17','5','5'),('18','5','9'),('19','5','13'),('20','6','2'),('21','6','6'),('22','6','10'),('23','6','14'),('24','7','3'),('25','7','7'),('26','7','11'),('27','7','15'),('28','8','4'),('29','8','8'),('30','8','12'),('31','8','16'),('32','9','1'),('33','9','5'),('34','9','9'),('35','9','13'),('36','10','2'),('37','10','6'),('38','10','10'),('39','10','14');

this is the error i get:

#1452 - Cannot add or update a child row: a foreign key constraint fails (`university`.`studentsection`, CONSTRAINT `studentsection_ibfk_1` FOREIGN KEY (`StudentID`) REFERENCES `student` (`StudentID`))

Whats wrong with my code?

2 Answers 2

1

Basically you probably trying to add studentsection with StudentID which have no corresponding record in other student table. Insert all students prior to executing that code.

Sign up to request clarification or add additional context in comments.

1 Comment

Ooh. Now i get it. I do have all the students. the problem is that in Insert Into I had to first write studentID and than SectionID in brackets. Thx. your answer helped alot.
0

You have a foreign key relation with another table called student with student id pointing to studentsection , ensure you have record in the other table before you go with this insertion.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.