数据库
首页 > 数据库> > MySQL外键约束-错误1452-无法添加或更新子行

MySQL外键约束-错误1452-无法添加或更新子行

作者:互联网

我已经使用了有关该主题的其他帖子,但是我没有运气.

这是我执行的代码:

UPDATE tblOrderItems SET `ItemID` = 0004 WHERE `OrderNum`= 203 AND `OrderItemID` = 26

这是我的错误:

Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`cai0066`.`tblOrderItems`, CONSTRAINT `ItemID` FOREIGN KEY (`ItemID`) REFERENCES `tblCatalogItems` (`ItemID`))

笔记:

>当我在tblOrderItems中插入或更新时,就会发生这种情况.
> tblCatalogItems的ItemID确实为0004.请参阅:this

这是MySQL Workbench生成的create语句:

delimiter $$

CREATE TABLE `tblCatalogItems` (
  `ItemID` varchar(10) NOT NULL DEFAULT '',
  `ItemName` varchar(50) DEFAULT NULL,
  `Wholesale` decimal(10,2) DEFAULT NULL,
  `Cost5-10` decimal(10,2) DEFAULT NULL,
  `Cost11-19` decimal(10,2) DEFAULT NULL,
  `Cost20` decimal(10,2) DEFAULT NULL,
  `Retail` decimal(10,2) DEFAULT NULL,
  PRIMARY KEY (`ItemID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1$$


delimiter $$

CREATE TABLE `tblItemCosts` (
  `Cost` decimal(10,2) DEFAULT NULL,
  `VendorID` int(11) NOT NULL,
  `ItemID` varchar(10) NOT NULL,
  KEY `VendorID_idx` (`VendorID`),
  KEY `ItemID_idx` (`ItemID`),
  CONSTRAINT `VendorID` FOREIGN KEY (`VendorID`) REFERENCES `tblVendors` (`VendorID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1$$


delimiter $$

CREATE TABLE `tblOrderItems` (
  `OrderItemID` int(11) NOT NULL AUTO_INCREMENT,
  `OrderNum` int(11) NOT NULL,
  `PayPalTxnID` int(10) DEFAULT NULL,
  `Description` varchar(225) DEFAULT NULL,
  `Quantity` int(11) DEFAULT NULL,
  `UnitPrice` decimal(10,2) DEFAULT NULL,
  `ItemStatus` varchar(30) DEFAULT NULL,
  `TrackingNumber` varchar(50) DEFAULT NULL,
  `ShippingCost` decimal(10,2) DEFAULT NULL,
  `ItemID` varchar(50) DEFAULT NULL,
  `TotalPrice` decimal(10,2) DEFAULT NULL,
  PRIMARY KEY (`OrderItemID`,`OrderNum`),
  UNIQUE KEY `PayPalTxnID_UNIQUE` (`PayPalTxnID`),
  KEY `PayPalTxnID_idx` (`PayPalTxnID`),
  KEY `UnitPrice_idx` (`ItemID`),
  KEY `OrderNum_idx` (`OrderNum`),
  CONSTRAINT `ItemID` FOREIGN KEY (`ItemID`) REFERENCES `tblCatalogItems` (`ItemID`),
  CONSTRAINT `OrderNum` FOREIGN KEY (`OrderNum`) REFERENCES `tblOrders` (`OrderNum`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `UnitPrice` FOREIGN KEY (`ItemID`) REFERENCES `tblCatalogItems` (`ItemID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=7678 DEFAULT CHARSET=latin1$$


delimiter $$

CREATE TABLE `tblOrderItemStatus` (
  `OrderItemID` int(11) NOT NULL,
  `OrderDate` varchar(12) DEFAULT NULL,
  `DesignProofSent` varchar(12) DEFAULT NULL,
  `SubmittedToProduction` varchar(12) DEFAULT NULL,
  `InProduction` varchar(12) DEFAULT NULL,
  `Shipped` varchar(12) DEFAULT NULL,
  PRIMARY KEY (`OrderItemID`),
  UNIQUE KEY `OrderItemID_UNIQUE` (`OrderItemID`),
  KEY `OrderItemID_idx` (`OrderItemID`),
  CONSTRAINT `OrderItemID` FOREIGN KEY (`OrderItemID`) REFERENCES `tblOrderItems` (`OrderItemID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1$$


delimiter $$

CREATE TABLE `tblOrders` (
  `OrderNum` int(11) NOT NULL AUTO_INCREMENT,
  `PayPalTxnID` int(10) DEFAULT NULL,
  `OrderDate` varchar(50) DEFAULT NULL,
  `OrderStatus` varchar(10) DEFAULT 'New',
  `RushFlag` bit(1) DEFAULT b'0',
  `ShipName` varchar(50) DEFAULT NULL,
  `ShipEmail` varchar(100) DEFAULT NULL,
  `ShipAddress1` varchar(50) DEFAULT NULL,
  `ShipAddress2` varchar(50) DEFAULT NULL,
  `ShipCity` varchar(50) DEFAULT NULL,
  `ShipState` char(2) DEFAULT NULL,
  `ShipZip` varchar(10) DEFAULT NULL,
  `ShippingCharge` decimal(10,2) DEFAULT NULL,
  `TotalCost` decimal(10,2) DEFAULT NULL,
  PRIMARY KEY (`OrderNum`),
  UNIQUE KEY `PayPalTxnID_UNIQUE` (`PayPalTxnID`)
) ENGINE=InnoDB AUTO_INCREMENT=346 DEFAULT CHARSET=latin1$$


delimiter $$

CREATE TABLE `tblVendors` (
  `VendorID` int(11) NOT NULL,
  `VendorName` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`VendorID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1$$

我在this相关帖子中尝试了该建议,但没有结果.这是一个尚未实际使用的新数据库.我刚刚用假数据填充了它.任何想法将不胜感激.

解决方法:

tblOrderItems有一个外键约束,它的ItemID需要引用tblCatalogItems中已经存在的ItemID.

CONSTRAINT `ItemID` FOREIGN KEY (`ItemID`) REFERENCES `tblCatalogItems` (`ItemID`),

该消息仅表示您正在尝试更新tblOrderItems以引用tblCatalogItems中具有ItemID = 0004的项目,但该项目不存在.

由于ItemID是一个varchar,因此您可能想引用0004,否则在转换为varchar之前可能会将其转换为int 4.如果ItemID = 0004的行实际存在,那可能是您的问题.

UPDATE tblOrderItems SET `ItemID` = '0004' WHERE `OrderNum`= 203 AND `OrderItemID` = 26

标签:foreign-keys,mysql-workbench,mysql,mysql-error-1452
来源: https://codeday.me/bug/20191031/1975606.html