Last answered:

12 Jun 2021

Posted on:

10 Jun 2021



14:26:26 insert into dept_emp  (                  emp_no,      dept_no,      from_date,      to_date  )  values  (                  999903,      'd005',      '1997-10-01',      '9999-01-01'  ) Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`employees`.`dept_emp`, CONSTRAINT `dept_emp_ibfk_1` FOREIGN KEY (`emp_no`) REFERENCES `employees` (`emp_no`) ON DELETE CASCADE) 0.000 sec

Am getting this error , may you kindly assist on what I am getting wrong

Can someone help on this error from assignment on lesson 10(SQL INSERT statement)

1 answers ( 0 marked as helpful)
Posted on:

12 Jun 2021


Hi Zenzo!

Thanks for reaching out.

Please stick to our general request to execute all code you see in the lectures and the exercises, in the given order. Doing this will prevent you from encountering some errors, such as this one – Error Code: 1452.
This error appears if you have already created another table, employees, where you have missed inserting data about the individual with id 999903. The relationship you have established between employees and dept_emp requires that you first insert a record in employees, and then insert a (related) record in dept_emp.
ON DELETE CASCADE means that if you remove record 999903 from employees, record 999903 will automatically be removed from dept_emp as well.
In brief, make sure the relationship between employees and dept_emp is valid and 999903 exists in employees so that you don’t get the same error the next time you try inserting 999903 in dept_emp.
SOLUTION: As explained in the article preceding this video, double-check if you’ve first inserted information about employee number 999903 in the employees table. Only then you should proceed with inserting information in the titles and dept_emp tables.

Hope this helps.

Submit an answer