Resolved: can't add row, I do may trail and it all back with errors
this is an example of errors
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 CASCADE)
example 2
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 CASCADE)
Example 3
Error Code: 1136. Column count doesn't match value count at row 1
same problem
do you find the solution?
Not yet
Hi Ahmed and Islam!
Thanks for reaching out.
The error is because you need to insert first the records in the parent table. It usually happens when you try to enter a row into a child table containing one or more foreign keys but the foreign key hasn't yet been entered into the parent table as a primary key. For example, if I want to enter emp_no
999903 into the dept_emp
table, I have to first enter emp_no
999903 into the employees
table.
Hope this helps.
Best,
Tsvetelin
Thanks Tsvetelin Tsonkov for your answer
Regards
Hi to All,
I have the same problem. Why do I have a problem, my code is exactly the same as in the solution, but mine does not work?! What should be inserted?
Thanks,
Anna
Hi Anna
Please check the answer below I found it reasonable
https://learn.365datascience.com/profile/tsvetelin-tsonkov/
Sorry, you send me a link to a locked profile.
I read Tsvelin's answer however still would like to know why in my case the code did not work out, the author has the same code and it worked right?
Thanks,
Anna
The problem is that when I want to enter emp_no in the parent table, such as employees, I will have to enter other values, such as birthdate, hire date, and first and last name. In that case, I still have the error.
the solution answer from the instructor is displaying an error too.
Please advise.
OK, so what is happening here is that we are trying to insert a row in the titles table for an employee that does not exist.
To verify this is true, we query the employees table and order in descending order.
/* Select from employees table in descending order to determine max employee number */
SELECT * FROM employees.employees ORDER BY emp_no DESC;
Return to the prior lecture and copy the insert statement, but change the employee number to 999903.
/* Insert new employee 999903 into employee table */
INSERT INTO employees.employees
(
emp_no,
birth_date,
first_name,
last_name,
gender,
hire_date
)
VALUES
(
999903,
'1973-3-26',
'Patricia',
'Lawrence',
'F',
'2005-01-01'
);
To verify, run the above SQL to ensure that the insert ran properly.
/* Select from employees table in descending order to determine max employee number */
SELECT * FROM employees.employees ORDER BY emp_no DESC;
Now employee 999903 is in the employees table; the insert into the titles table will work properly. I suggest modifying the insert to have a to_date of '9999-01-01'; that is how the current table looks populated for other employees.
/* Now insert new row into titles table */
INSERT INTO employees.titles
(
emp_no,
title,
from_date
)
VALUES
(
999903,
'Senior Engineer',
'1997-10-01'
'9999-01-01'
);
Hope this helps.
Cheers
We are all looking at the employees table, but the deptartments table is a foreign key parent table to the dept_no. :) (Lol chatGPT helped me out)
copy this and it will work
SELECT
*
FROM
employees
ORDER BY emp_no DESC
LIMIT 10;
INSERT INTO employees.employees
(
emp_no,
birth_date,
first_name,
last_name,
gender,
hire_date
)
VALUES
(
999903,
'1973-3-26',
'Patricia',
'Lawrence',
'F',
'2005-01-01'
);
INSERT INTO employees.titles
(
emp_no,
title,
from_date
)
VALUES
(
999903,
'Senior Engineer',
'1997-10-01'
);
SELECT
*
FROM
titles
ORDER BY emp_no DESC
LIMIT 10;