[英]#1452 - Cannot add or update a child row: a foreign key constraint fails
NOTE: Before marking it as Duplicate, i want to tell you guys that i've gone through all these related problems in stackoverflow and other websites, but i didn't get any suitable solution, so that's why i am here for the help.注意:在将其标记为重复之前,我想告诉你们,我已经在 stackoverflow 和其他网站上解决了所有这些相关问题,但我没有找到任何合适的解决方案,所以这就是我来这里寻求帮助的原因。
I have two tables: employees and fines我有两个表:员工和罚款
In fines table fine_id
is the PK and employee_id
is the FK references to employees table PK employee_id
.在fines表中fine_id
是PK , employee_id
是对员工表PK employee_id
的FK引用。
Now, i am creating fine_id
(column in table employees ) as a FK reference to fine_id
in fines table.现在,我正在创建fine_id
(表employees中的列)作为对fines表中fine_id
的FK引用。
i am adding constraints via XAMPP PHPMYADMIN it auto generates my query which is:我通过XAMPP PHPMYADMIN添加约束,它会自动生成我的查询,即:
ALTER TABLE employees ADD CONSTRAINT fine_fk FOREIGN KEY (fine_id) REFERENCES fines(fine_id) ON DELETE CASCADE ON UPDATE CASCADE;
But, I am getting this error while running this SQL Query
但是,我在运行此SQL Query
时收到此错误
ems_db
. #sql-dd4_35
, CONSTRAINT fine_fk
FOREIGN KEY ( fine_id
) REFERENCES fines
( fine_id
) ON DELETE CASCADE ON UPDATE CASCADE) 1452 - 无法添加或更新子行:外键约束失败( ems_db
。 #sql-dd4_35
,CONSTRAINT fine_fk
FOREIGN KEY ( fine_id
) REFERENCES fines
( fine_id
) ON DELETE CASCADE ON UPDATE CASCADE)Here are attached images:这里附上图片:
employees table员工表
fines table罚款表
error错误
Thanks.谢谢。
somehow i managed to solve my issue myself,我设法自己解决了我的问题,
the problem was that in my employees table i have the column fine_id
which i want to make a FK , that column value was empty in my existing record in employees table, although i have values in my fines table, but the column fine_id
in employees table was not filled, with the corresponding fine_id
of fines table, so before creating FK i have filled my column with the correct ID's of fines table, and my problem was gone away.问题是在我的employees表中我有fine_id
列,我想创建一个FK ,该列值在我现有的employees表记录中为空,虽然我的fines表中有值,但employees表中的fine_id
列没有用相应的罚款表fine_id
填充,所以在创建FK之前,我已经用正确的罚款表ID填充了我的列,我的问题就消失了。
I will suggest to use query to set foreign key.我会建议使用查询来设置外键。 Not graphically.不是图形化的。 That worked for me!这对我有用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.