0

"HProblem with the fk and pk which are in the same table and related."

So, i want to insert in table 'employee' values but the program show me error:

Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (ikbo_6.employee, CONSTRAINT employee_ibfk_1 FOREIGN KEY (manager_id) REFERENCES employee (employee_id)).

insert into employee values
(1,'Mccain', 'Jhonny','D',1001,101,'2008-05-12',2000.90,100.45,801);

I tried first to add the data to pk_key and only then to the other columns, but this did not work.

insert into employee (last_name,first_name,middle_initial,manager_id,job_id,hire_date, salary, commission, department_id) values
('Mccain', 'Jhonny','D',1001,101,'2008-05-12',2000.90,100.45,801);

what am I doing wrong?

The struture employee_id int(6) not null auto_increment primary key,manager_id int(6)

and foreign key (manager_id) references Employee (employee_id)

relation key

A7DENAS
  • 9
  • 2
  • Does this answer your question? [MySQL foreign keys on self](https://stackoverflow.com/questions/7230145/mysql-foreign-keys-on-self) – P.Salmon May 02 '20 at 10:43
  • Obviously the row for the manager `1001` doesn't exist. Add that row first and then the employee. – Gordon Linoff May 02 '20 at 12:36
  • I have found a solution. I first added an entry for the PK column separately, and then separately for everyone else, and then I used default – A7DENAS May 09 '20 at 14:14

0 Answers0