作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个包含复合主键“name”和“id”的表。这些字段实际上是“名称”、“ID”、“电话”、“金额”、“单位”、“别名”。我有疑问
insert into MyTable (name,id,phone,amount) select "henry" as name, id,phone,amount from anotherTable
on duplicate key update phone=values(phone),amount=values(amount).
MySQL 吐出以下错误:
Cannot add or update a child row: a foreign key constraint fails.
顺便说一句,“id”是一个外键。
有什么帮助吗?
按照下面的要求,其他表的架构是
CREATE TABLE `otherTable` (
`otherId` int(11) NOT NULL AUTO_INCREMENT,
`DOBId` int(11) NOT NULL,
`bankAccount` int(11) DEFAULT NULL,
`partialAmount` int(11) NOT NULL DEFAULT '0',
`id` int(11) NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`notes` varchar(299) DEFAULT NULL,
`latitude` decimal(8,5) DEFAULT NULL,
`longitude` decimal(8,5) DEFAULT NULL,
PRIMARY KEY (`otherId `),
KEY `DOBId ` (`DOBId `),
KEY `bankAccount ` (`bankAccount `),
KEY `id ` (`id `)
) ENGINE=InnoDB AUTO_INCREMENT=3305 DEFAULT CHARSET=utf8;
对于我的表
CREATE TABLE `myTable` (
`name` int(11) NOT NULL,
`id` int(11) NOT NULL,
`appleNumber` int(11) DEFAULT NULL,
`amount` int(11) DEFAULT NULL,
`windowsNumber` int(11) DEFAULT NULL,
`phone` int(11) DEFAULT NULL,
`pens` int(11) DEFAULT NULL,
`pencils` int(11) DEFAULT NULL,
PRIMARY KEY (`name`,`id`),
KEY `id` (`id`),
CONSTRAINT `myTable_ibfk_1` FOREIGN KEY (`id`) REFERENCES `yet_another` (`id`)
最佳答案
问题似乎是您对 myTable
的 FK 约束正在引用 yet_another
的 ID ,所以当您从 anotherTable
插入 id 时你正在打破这个 FK 约束。 anotherTable
中可能有 ID yet_another
中不存在的表。
根据您发布的抽象架构,了解这是在黑暗中拍摄的照片。如果您想要更可靠的答案,我必须查看实际架构。
关于mysql - 重复 key 更新失败,错误为 "Cannot add or update a child row",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9490449/
我是一名优秀的程序员,十分优秀!