I have two tables in the database:
First Table: Students
Field Type Null Key Default
id int(11) NO PRI NULL
class_id int(11) NO PRI NULL
admission_id int(11) NO PRI NULL
school_id int(11) NO PRI NULL
name varchar(64) YES NULL
admin_date datetime YES NULL
Second Table: Admission
Field Type Null Key Default
id int(11) NO PRI NULL
class_id int(11) NO PRI 1
school_id int(11) NO PRI NULL
name varchar(64) YES NULL
admin_date datetime YES NULL
Now I want to fill in the class_id column of Admission table which I introduced later into the table. By default all the class_id are set to 1 and hence need to be updated according to student.
What I was doing :
Update Admission
set class_id = (select class_id from Students where Students.school_id = Admission.school_id);
and i get an error message : ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails
Can you help me in place where I’m going wrong or any other alternative solution to fill in column.
Thanks in advance
Try this ::