插入数据时出现MySQL错误1452

时间:2018-04-09 05:59:58

标签: mysql sa-mp

导入旧的SQL文件时我一直都会遇到错误并修复它们,但是我被卡住了,无法理解这意味着什么。

  

ALTER TABLE property ADD CONSTRAINT property_ibfk_1 FOREIGN KEY   (intid)参考interiorsid)ON更新级联更新   CASCADE,添加约束property_ibfk_2外键(owner)   REFERENCES accountsid)ON UPETE UP NULL更新CASCADE   MySQL说:文档

     

1452 - 无法添加或更新子行:外键约束失败(ionicnew#sql-252c_e1,CONSTRAINT property_ibfk_2 FOREIGN   KEY(owner)REFERENCES accountsid)ON DELETE SET NULL ON   更新CASCADE)

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文件。

1 个答案:

答案 0 :(得分:2)

  

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

     

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

     

了解更多内容转到此link

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

您必须先将行插入interiors表格。