【问题标题】:MySQL Error 1452 when inserting dataMySQL 插入数据时出现错误 1452
【发布时间】:2018-09-18 11:13:26
【问题描述】:

我在导入旧的 SQL 文件并全部修复时不断出错,但我卡住了,无法理解这意味着什么。

改变表property添加约束property_ibfk_1外键 (intid) 参考interiors (id) 更新时删除级联 级联,添加约束 property_ibfk_2 外键 (owner) 参考accounts (id) 更新级联时删除设置空 MySQL 说:文档

1452 - 无法添加或更新子行:外键约束失败(ionicnew.#sql-252c_e1, CONSTRAINT property_ibfk_2 FOREIGN 键 (owner) 引用 accounts (id) 开 删除 设置 NULL 开 更新级联)

property表的完整代码:

CREATE TABLE `property` (
  `id` int(11) NOT NULL,
  `x` float NOT NULL,
  `y` float NOT NULL,
  `z` float NOT NULL,
  `a` float NOT NULL,
  `type` bit(32) NOT NULL,
  `intid` int(11) NOT NULL,
  `name` varchar(128) NOT NULL,
  `price` int(11) NOT NULL,
  `mapicon` tinyint(3) UNSIGNED NOT NULL,
  `status` tinyint(3) UNSIGNED NOT NULL,
  `point` int(10) UNSIGNED NOT NULL,
  `saleprice` int(11) NOT NULL DEFAULT '0',
  `owner` int(11) DEFAULT NULL,
  `money` int(11) NOT NULL DEFAULT '0',
  `level` tinyint(3) UNSIGNED NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `property`
  ADD PRIMARY KEY (`id`),
  ADD KEY `intid` (`intid`),
  ADD KEY `owner` (`owner`);

ALTER TABLE `property`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=86;

ALTER TABLE `property`
  ADD CONSTRAINT `property_ibfk_1` FOREIGN KEY (`intid`) REFERENCES `interiors` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `property_ibfk_2` FOREIGN KEY (`owner`) REFERENCES `accounts` (`id`) ON DELETE SET NULL ON UPDATE CASCADE;

如果需要,我可以上传完整的 SQL 文件。

【问题讨论】:

  • 可以为interiorsaccounts添加表格描述
  • 我们需要查看property 和另外两个引用表interiorsaccounts 中的数据。

标签: mysql sa-mp


【解决方案1】:

外键关系涉及一个父表,该表包含 中心数据值,以及具有相同值指向的子表 回到它的父级。 FOREIGN KEY 子句在子句中指定 表。

它将拒绝任何尝试创建的 INSERT 或 UPDATE 操作 如果没有匹配项,则子表中的外键值 父表中的候选键值。

要了解更多信息,请访问此link

所以您的错误Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails 本质上意味着,您正尝试向property 表中添加一行,而interiors 表中没有匹配行(intid)

您必须先将该行插入到您的interiors 表中。

【讨论】:

  • 谢谢!我通过禁用导入页面中的外键检查解决了我的问题。
  • 欢迎! @公平
猜你喜欢
  • 2017-01-03
  • 1970-01-01
  • 2012-01-25
  • 2017-06-20
  • 2014-11-21
  • 1970-01-01
  • 2011-09-04
  • 2012-04-02
  • 2017-09-26
相关资源
最近更新 更多