Skip to main content
Participant
October 24, 2009
Question

Foreign key problem

  • October 24, 2009
  • 1 reply
  • 399 views

I have to tables "student" and "grade". the grade table has the following columns: grade_ID, grade_Value, student_ID and course_ID. the "student_ID" column is a foreign key to the student table. when I try to insert a grade_Value with a student_ID that is not in the student table I get the following error: "Cannot add or update a child row: a foreign key constraint fails (`resultdb/grade`, CONSTRAINT `student_grade` FOREIGN KEY (`student_ID`) REFERENCES `student` (`student_ID`))". How can I validate that the student_ID entred has a corresponding entry in the student table so that I can generate an apprpriate warning for the user on the form

This topic has been closed for replies.

1 reply

Participating Frequently
October 24, 2009

Use a stored procedure. The sproc cani first test if the key is valid. If it is, process and return sucess, otherwise return a failure message.