MySQL插入数据时报错1452

MySQL Error 1452 when inserting data

我在导入我的旧 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

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)

property的完整代码table:

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

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.

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.

To know more Go to this link

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

您必须先将行插入 interiors table。