错误1452:无法添加或更新子行 [英] ERROR 1452: Cannot add or update a child row

查看:193
本文介绍了错误1452:无法添加或更新子行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题,我试图添加一个外键引用另一个表,但由于某种原因失败。由于我对MySQL的知识有限,唯一可能会怀疑的是在引用我想引用的表上有一个不同的表上有一个外键。



这是我的表关系的图片,通过工作台生成:关系

$ $ $ $ $ $ $ $ $ $ CREATE TABLE` beds` $
` bedId int(11)NOT NULL,
`wardId` int(11)DEFAULT NULL,
`depId` int(11)DEFAULT NULL,
`desc` varchar(45)DEFAULT NULL,
PRIMARY KEY(`bedId`),
KEY`departmentId_idx`(`depId`),
KEY`wardId_idx`(`wardId`),
CONSTRAINT`departmentId` FOREIGN KEY (`depId`)
REFERENCES`department`(`Department_Id`)
ON UPDATE NO ACTION,
CONSTRAINT`wardId` FOREIGN KEY(`wardId`) (`wardId`)
ON DELETE NO ACTION ON UPDATE NO ACTION
)ENGINE = InnoDB DEFAULT CHARSET = latin1 $$






 错误1452:Can not添加或更新子行:外键约束失败
(`asiahospitaldb`.`beds`,CONSTRAINT`departmentId` FOREIGN KEY(`depId`)
REFERENCES`department`(`Department_Id`)
ON ON UPDATE NO ACTION删除任何操作)

SQL语句:

插入到`asiahospitaldb`.`Beds`(`bedId`,`wardId`,`` ('456','7444','4555','ikiuj')






解决方案



 错误1452:Can not添加或更新子行:外键约束
失败(`asiahospitaldb`.`beds`,CONSTRAINT`departmentId` FOREIGN KEY
(`depId`)参考`department`(`Department_Id`)ON DELETE NO
`在这里输入代码`ACTION ON UPDATE NO ACTION)

告诉你您插入的行需要出发的对应值你可以在 depId (如Omesh指出的)列中插入你的值/ department_id 。重要的是:
$ b


depId )参考部门( Department_Id

换句话说,您已经尝试在一个不存在的部门创建一张床。


I'm having a bit of a strange problem, I'm trying to add a foreign key to one table that references another, but it is failing for some reason. With my limited knowledge of MySQL, the only thing that could possibly be suspect is that there is a foreign key on a different table referencing the one I am trying to reference.

Here is a picture of my table relationships, generated via workbench: Relationships

CREATE TABLE `beds` (
  `bedId` int(11) NOT NULL,
  `wardId` int(11) DEFAULT NULL,
  `depId` int(11) DEFAULT NULL,
  `desc` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`bedId`),
  KEY `departmentId_idx` (`depId`),
  KEY `wardId_idx` (`wardId`),
  CONSTRAINT `departmentId` FOREIGN KEY (`depId`) 
     REFERENCES `department` (`Department_Id`) 
     ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `wardId` FOREIGN KEY (`wardId`) REFERENCES `wards` (`wardId`) 
     ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1$$


ERROR 1452: Cannot add or update a child row: a foreign key constraint fails
(`asiahospitaldb`.`beds`, CONSTRAINT `departmentId` FOREIGN KEY (`depId`) 
REFERENCES `department` (`Department_Id`) 
ON DELETE NO ACTION ON UPDATE NO ACTION)

SQL Statement:

INSERT INTO `asiahospitaldb`.`Beds` (`bedId`, `wardId`, `depId`, `desc`) 
VALUES ('456', '7444', '4555', 'ikiuj')


解决方案

This

ERROR 1452: Cannot add or update a child row: a foreign key constraint
fails (`asiahospitaldb`.`beds`, CONSTRAINT `departmentId` FOREIGN KEY
(`depId`)  REFERENCES `department` (`Department_Id`)  ON DELETE NO
`enter code here`ACTION ON UPDATE NO ACTION)

is telling you that the row you have inserted expects a corresponding value for department/department_id for the value you have inserted into column depId (as Omesh pointed out). The important bit is here:

(depId) REFERENCES department (Department_Id)

In other words, you have tried to create a bed in a non-existent department.

这篇关于错误1452:无法添加或更新子行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆