mysql - SQL Cannot add or update a child row: a foreign key constraint -
create table employee( -> emp_no int(4), -> emp_fname varchar(50), -> emp_lname varchar(50), -> job_class varchar(4), -> primary key (emp_no), -> foreign key (job_class) references job (job_class) -> );
query ok, 0 rows affected (0.09 sec)
create table project( -> pro_no int(4), -> pro_name varchar(50), -> pro_leader varchar(50), -> emp_no int(4), -> primary key (pro_no), -> foreign key (emp_no) references employee (emp_no) -> );
query ok, 0 rows affected (0.23 sec)
create table assign( -> pro_no int(4), -> emp_no int(4), -> job_class varchar(4), -> assign_hours decimal(6,2), -> total_charge decimal(6,2), -> primary key (pro_no, emp_no), -> foreign key (pro_no) references project (pro_no), -> foreign key (emp_no) references employee (emp_no) -> );
query ok, 0 rows affected (0.08 sec)
insert employee (emp_no, emp_fname, emp_lname, job_class) values ('101', 'john','new','dd');
error 1452 (23000): cannot add or update child row: foreign key constraint fails
(o_m_m
.employee
, constraintemployee_ibfk_1
foreign key (job_class
) referencesjob
(job_class
))
can please me, sql working fine
i missing -> foreign key (job_class) references job (job_class) in employee added , populated
now receive error
foreign key relationships involve parent table holds central data values, , child table identical values pointing parent. foreign key clause specified in child table.
it reject insert or update operation attempts create foreign key value in child table if there no matching candidate key value in parent table.
perhaps, attempting insert foreign key value in child table doesn't exist in parent table.
courtesy: using foreign key constraints
Comments
Post a Comment