Cannot insert 999903 into the dept_emp table?
When I try to insert this data value into the table. I receive this response,
"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)".
Thanks for reaching out.
We’ve answered this question here: https://365datascience.com/question/error-code-1452-cannot-add-or-update-a-child-row-a-foreign-key-constraint-fails-employees-titles-constraint-titles_ibfk_1-foreign-key-emp_no-references-employees-emp_no-on-delete-2/
But here’s the answer for your convenience.
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.
REASON FOR THE ERROR:
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.
I'm having this exact same problem, but I'm afraid the answer/article you provided doesn't help me. I did not create any other table with the employee number 999903. I created it as part of the INSERT statement lesson and entered the 6 fields as instructed. I then queried the employees table for the list of 10 employees by descending employee number to make sure he was in the database (which he was). Then I added his data to the titles table with no problem. When I got to the part of the exercise where we're supposed to enter data about him in the dept_emp table, that's when I ran into trouble and I keep getting this error 1452. I have executed all code in the order presented in the course, so the problem is definitely not that I did something out of order. There has to be some other issue going on that's causing this error..
I'm having the same probleme as Samantha and Sivajaran, I got 1452 ERROR each time while trying to run the code, and as Samantha said, me too I have executed all codes in the order presented in the course.
I have encountered similar error message but then i took the following steps to correct it.
1. insert the following information into the employees table before trying tom insert into the title table.
INSERT INTO employees
After inserting into the employees table, you can then go ahead to insert the information for the assignment into the title table and query.
I hope you don't mind if I join the conversation.
@Godwin: Indeed! Thank you very much for sharing this explanation
As you are saying, Samantha, we assume students run all code that we provide in the lectures and the exercises. We realise this is a lot of code, so we need to be careful about executing it in the correct order.
Here's the lesson where you can find the code Godwin is referring to:
Provided that you've also created the ON DELETE CASCADE query, you should be able to proceed without getting an error message.
Hope this helps but please feel free to get back to us should you need further assistance. Thank you.
The 365 Team
even after adding the @Godwin query any of you face any issue then that might be because in Excercise it was mentioned to add a department number as 5 even if we use
insert into dept_emp (emp_no, dept_no, from_date, to_date) values (999903,'5','1997-10-01','9999-01-01');
this through error as the dept_no is of char(4) type so we have to mention dept_no as 'd005' to meet the column dept_no type requirements
Hope this helps
Thanks for reaching out.
Thanks for sharing this piece of information with the Community!
Hope this helps.