doufang7385 2014-10-21 16:55
浏览 92
已采纳

PHPMyAdmin在外键设置上抛出错误

I have two tables as below

1 : Table Name : parent Columns : parent_id(Primary Key) , title

2 : Table Name : child Columns : child_id(Primary Key) , title , fk_pid(Indexed)

I want to make reference between prent.parent_id and child.fk_pid . I have set type of both table to InnoDB. I have opened 'relation view' in the structure of child table. Then I have selected foreign key constraint for child.fk_pid and set to parent.parent_id and set options as below

constraint name : myFK on delete : restrict on update : restrict

but when I am clicking on save button, PHPMyAdmin is throwing following error.

#1452 - Cannot add or update a child row: a foreign key constraint fails (`fk`.`#sql-168_5f`, CONSTRAINT `myFk` FOREIGN KEY (`fk_pid`) REFERENCES `parent` (`parent_id`)) 

please help me

  • 写回答

1条回答 默认 最新

  • dqluw20882 2014-10-21 17:04
    关注

    MySQL won't create the foreign key if you have fk_pid in the child table that don't exist as id in the parent table. Check the child table for any references to a parent id that do not exist in the parent table.

    select fk_pid from child left join parent on child.fk_pid = parent.id where parent.id is null;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题