简体   繁体   English

插入数据时出现MySQL错误1452

[英]MySQL Error 1452 when inserting data

I keep getting errors when importing my old SQL file and fix them all,but I'm stuck and can't understand what this means. 导入旧的SQL文件并修复所有错误时,我总是收到错误消息,但是我被卡住了,无法理解这意味着什么。

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 MySQL said: Documentation ALTER TABLE property ADD CONSTRAINT property_ibfk_1 FOREIGN KEY( intid )引用interiorsid )ON在更新CASCADE时删除CASCADE,ADD CONSTRAINT property_ibfk_2 FOREIGN KEY( owner )参考accountsid )ON在更新CASCADE上表示为空:

1452 - Cannot add or update a child row: a foreign key constraint fails ( ionicnew . #sql-252c_e1 , CONSTRAINT property_ibfk_2 FOREIGN KEY ( owner ) REFERENCES accounts ( id ) ON DELETE SET NULL ON UPDATE CASCADE) ionicnew无法添加或更新子行:外键约束失败( ionicnew#sql-252c_e1 ,CONSTRAINT property_ibfk_2键( owner )参考accountsid )开

Full code of property table: 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;

I can upload the full SQL file if needed. 如果需要,我可以上传完整的SQL文件。

Foreign key relationships involve a parent table that holds the central data values, and a child table with identical values pointing back to its parent. 外键关系涉及一个包含中央数据值的父表,以及一个具有指向其父级的相同值的子表。 The FOREIGN KEY clause is specified in the child table. 在子表中指定FOREIGN KEY子句。

It will reject any INSERT or UPDATE operation that attempts to create a foreign key value in a child table if there is no a matching candidate key value in the parent table. 如果在父表中没有匹配的候选键值,它将拒绝任何试图在子表中创建外键值的INSERT或UPDATE操作。

To know more Go to this link 了解更多转到此链接

So your error Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails essentially means that, you are trying to add a row to your property table for which no matching row (intid) is present in interiors table. 因此,您的错误Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails本质(intid)意味着您正在尝试向property表中添加一行,而在interiors表中不存在(intid)匹配的行(intid)

You must first insert the row to your interiors table. 您必须首先将该行插入到interiors表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM