0

I am trying to insert statements into my database. Here are the insert statements:

insert into advisor values ('00001', '11111');
insert into advisor values ('00002', '22222');
insert into advisor values ('00003', '33333');
insert into advisor values ('00004', '44444');
insert into advisor values ('00005', '55555');
insert into advisor values ('00006', '66666');
insert into advisor values ('00007', '77777');
insert into advisor values ('00008', '88888');
insert into advisor values ('00009', '99999');
insert into advisor values ('00010', '10101');"

I am getting an error on this one:

insert into advisor values ('00004', '44444');

with this error:

Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails     (`university database`.`instructor`, CONSTRAINT instructor_ibfk_1 FOREIGN KEY (`department_name`) REFERENCES `department` (`department_name`) ON DELETE SET NULL)

Can anyone lend any knowledge to this error?

Thanks!

  • The department_name from department table should contain a record with value 0004 (or 4444) whichever is the referenced key – Habrashat Nov 20 '13 at 22:51
  • @JonathandeM. @HarshaBhat what about this error? What is it referencing that is incorrect? Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`university database`.`instructs`, CONSTRAINT `instructs_ibfk_1` FOREIGN KEY (`course_id`, `section_id`, `semester`, `year`) REFERENCES `section` (`course_id`, `section_id`, `semester`, `year`) – user2856210 Nov 20 '13 at 23:36

1 Answers1

1

The error is self explanatory. The value(s) you are entering must first exist in the referenced table in the foreign key constraint.

You should also use a column list in your INSERT statements.

Kermit
  • 33,827
  • 13
  • 85
  • 121
  • I think the op does'nt know what a foreign key is. Could you post a simple explanation for him? – Jorge Campos Nov 20 '13 at 22:50
  • @JorgeCampos No. Minimal understanding of the question being asked is required. – Kermit Nov 20 '13 at 22:51
  • @FreshPrinceOfSO thanks! I found the error in my referenced table. Little errors throw it off! – user2856210 Nov 20 '13 at 23:15
  • @FreshPrinceOfSO what about this error? What is it referencing that is incorrect? Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`university database`.`instructs`, CONSTRAINT `instructs_ibfk_1` FOREIGN KEY (`course_id`, `section_id`, `semester`, `year`) REFERENCES `section` (`course_id`, `section_id`, `semester`, `year`) – user2856210 Nov 20 '13 at 23:36
  • @user2856210 That's a composite key reference. – Kermit Nov 21 '13 at 14:36