Can anyone help me with this database error, I'm inserting data on a table named
subjectdetails(
subjectid int primarykey,
subject varchar(50),
semid int //foreign key from the table semester
departmentid int(11) // foreign key from the table department
)
Now, when i insert a subject it only shows the error on SEMID like the below error Cannot add or update a child row: a foreign key constraint fails
(`studentcorner/subjectdetails`, CONSTRAINT `subjectdetails_ibfk_1` FOREIGN KEY
(`SEMID`) REFERENCES `semester` (`SEMID`) ON DELETE NO ACTION ON UPDATE NO ACTION)
Please help me with this..
The error is indicating that you have a foreign key constraint which cannot be satisfied. Specifically, you're trying to insert a subjectdetails
entry with an invalid semid
column. The value you're using for semid
is invalid because that value cannot be found inside the semester
table. I know this because your error message states as much.
To fix this error, ensure that the value you're trying to insert for the semid
column actually exists in the semesters
table first.
I would suggest going to the documentation and re-read the section on Using Foreign Key Constraints. The relevant quote from that page regarding your issue is this (emphasis mine):
For storage engines supporting foreign keys, MySQL rejects any INSERT or UPDATE operation that attempts to create a foreign key value in a child table if there is no a matching candidate key value in the parent table.