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 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services