错误1452:无法添加或更新子行 [英] ERROR 1452: Cannot add or update a child row
问题描述
这是我的表关系的图片,通过工作台生成:关系
$ $ $ $ $ $ $ $ $ $ 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屋!