无法添加或更新子行:数据存在时外键约束失败

问题描述 投票:0回答:1

我试图添加外键约束,但它失败了:

mysql> alter table order_info add constraint FKlnh846un1oe6hnwkqf5ovtjwo
foreign key (orderId) references orders (orderId);

ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails 
(`storefront`.`#sql-1601_28`, CONSTRAINT `FKlnh846un1oe6hnwkqf5ovtjwo`
 FOREIGN KEY (`orderId`) REFERENCES `orders` (`orderId`))




mysql> show create table orders;
| orders | CREATE TABLE `orders` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `amount` float NOT NULL,
  `couponCode` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `createdAt` datetime DEFAULT NULL,
  `email` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `orderId` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `phone` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `status` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `updatedAt` datetime DEFAULT NULL,
  `address_id` int(11) DEFAULT NULL,
  `deleted` tinyint(1) NOT NULL DEFAULT '0',
  `pGateway` varchar(255) COLLATE utf8_unicode_ci DEFAULT 'PAYU',
  PRIMARY KEY (`id`),
  UNIQUE KEY `UK_9erxssgysqmn8axwyq4er6hen` (`orderId`),
  KEY `FKf5464gxwc32ongdvka2rtvw96` (`address_id`),
  KEY `email_status_index` (`email`,`status`),
  KEY `createdAt_index` (`createdAt`),
  KEY `status_index` (`status`),
  CONSTRAINT `FKf5464gxwc32ongdvka2rtvw96` FOREIGN KEY (`address_id`) REFERENCES `address` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2961655 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |




mysql> show create table order_info;
| order_info | CREATE TABLE `order_info` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `createdAt` datetime DEFAULT NULL,
  `orderId` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `packageId` int(11) NOT NULL,
  `updatedAt` datetime DEFAULT NULL,
  `amount` int(11) NOT NULL,
  `status` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `expiry` datetime DEFAULT NULL,
  `child_id` int(11) DEFAULT NULL,
  `parent_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1916085 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |



mysql> select count(*) from orders;
+----------+
| count(*) |
+----------+
|  2773095 |
+----------+
1 row in set (0.69 sec)

mysql> select count(*) from order_info;
+----------+
| count(*) |
+----------+
|  1914367 |
+----------+
1 row in set (0.61 sec)

mysql> select orderId from order_info where orderId not in (select orderId from orders);
Empty set (5.07 sec)

mysql> select count(*) from order_info where orderId is null;
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.63 sec)

现在,两个表都有数据。订单表中不存在这样的orderId。两列的数据类型相同。那为什么会出错呢?究竟是什么被侵犯了?虽然我可以将外键检查设置为false然后创建密钥,但我想知道为什么这个约束失败了。

编辑:添加了最后一个查询,以验证要成为外键​​的列没有空值。

mysql foreign-keys
1个回答
0
投票

看来这个查询:

select orderId from order_info where orderId not in (select orderId from orders)

不是了解错误数据点的最佳人选。按照@ paul-spiegel的建议,NOT EXISTS更好。这是查询输出:

mysql> select count(*) from order_info oi where not exists 
( select null from orders o where o.orderId = oi.orderId);
+----------+
| count(*) |
+----------+
|        6 |
+----------+
1 row in set (31.27 sec)

order_info中有6行,orders表中不存在orderId。

NOT EXISTS的文档:https://dev.mysql.com/doc/refman/5.7/en/exists-and-not-exists-subqueries.html

相关:SELECT * WHERE NOT EXISTS

© www.soinside.com 2019 - 2024. All rights reserved.