我收到以下错误,不确定问题是什么
Microsoft OLE DB Provider for ODBC Drivers错误' 80004005'
[MySQL] [ODBC 3.51驱动程序] [mysqld-5.5.32-31.0-log]无法删除或更新父行:外键约束失败 (
Databasename
。FADLSongs
,CONSTRAINTFK_FADLSongs_OrderID
外国 KEY(OrderID
)REFERENCESOrders
(OrderID
)ON DELETE NO ACTION ON ON 更新无行动)
查询其抛出错误
DELETE FROM Orders WHERE OrderID Not In (SELECT Distinct OrderID FROM MOrder) And
OrderID Not In (SELECT Distinct OrderID FROM PSGOrder)
我的表格结构如下
CREATE TABLE IF NOT EXISTS `MOrder` (
`MOrderID` int(11) NOT NULL AUTO_INCREMENT,
`MediaID` int(11) NOT NULL,
`OrderID` int(11) NOT NULL,
`MediaOption` tinyint(4) DEFAULT NULL,
`UnitCost` double NOT NULL,
`Quantity` int(11) NOT NULL,
`BonusDeduction` double DEFAULT NULL,
`MediaOptions` varchar(25) DEFAULT NULL,
PRIMARY KEY (`MOrderID`),
KEY `MediaID` (`MediaID`),
KEY `OrderID` (`OrderID`),
CONSTRAINT `FK_MOrder_MediaID` FOREIGN KEY (`MediaID`) REFERENCES `Media` (`MediaID`) ON DELETE NO ACTION ON UPDATE CASCADE,
CONSTRAINT `FK_MOrder_OrderID` FOREIGN KEY (`OrderID`) REFERENCES `Orders` (`OrderID`) ON DELETE NO ACTION ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `Orders` (
`OrderID` int(11) NOT NULL AUTO_INCREMENT,
`UserID` int(11) NOT NULL,
`PNMType` varchar(1) DEFAULT NULL,
`PNMDeliveryOption` varchar(1) DEFAULT NULL,
`PNMQuantity` smallint(5) unsigned DEFAULT NULL,
`PNMSongCost` double DEFAULT NULL,
`PNMDiscLabel` varchar(50) DEFAULT NULL,
`ShippingCost` double DEFAULT NULL,
PRIMARY KEY (`OrderID`),
KEY `IDX_Orders_CouponNo` (`CouponNo`),
KEY `IDX_Orders_VoucherNo` (`VoucherNo`),
KEY `OrderStatusID` (`OrderStatusID`),
KEY `UserID` (`UserID`),
CONSTRAINT `FK_Orders_CouponNo` FOREIGN KEY (`CouponNo`) REFERENCES `Coupons` (`CouponNo`) ON DELETE NO ACTION ON UPDATE CASCADE,
CONSTRAINT `FK_Orders_OrderStatusID` FOREIGN KEY (`OrderStatusID`) REFERENCES `OrderStatus_LU` (`OrderStatusID`) ON DELETE NO ACTION ON UPDATE CASCADE,
CONSTRAINT `FK_Orders_UserID` FOREIGN KEY (`UserID`) REFERENCES `Users` (`UserID`) ON DELETE NO ACTION ON UPDATE CASCADE,
CONSTRAINT `FK_Orders_VoucherNo` FOREIGN KEY (`VoucherNo`) REFERENCES `Vouchers` (`VoucherNo`) ON DELETE NO ACTION ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `PSGOrder` (
`PSGOrderID` int(11) NOT NULL AUTO_INCREMENT,
`OrderID` int(11) NOT NULL,
`SongID` int(11) NOT NULL,
`DateDownloaded` datetime DEFAULT NULL,
PRIMARY KEY (`PSGOrderID`),
KEY `IDX_PSGOrder_OrderID` (`OrderID`),
KEY `IDX_PSGOrder_SongID` (`SongID`),
CONSTRAINT `FK_PSGOrder_OrderID` FOREIGN KEY (`OrderID`) REFERENCES `Orders` (`OrderID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `FK_PSGOrder_SongID` FOREIGN KEY (`SongID`) REFERENCES `Songs` (`SongID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `FADLSongs` (
`FADLSongID` int(11) NOT NULL AUTO_INCREMENT,
`OrderID` int(11) NOT NULL,
`SongID` int(11) NOT NULL,
`DateDownloaded` datetime NOT NULL,
PRIMARY KEY (`FADLSongID`),
KEY `IDX_FADLSongs_OrderID` (`OrderID`),
KEY `IDX_FADLSongs_SongID` (`SongID`),
CONSTRAINT `FK_FADLSongs_OrderID` FOREIGN KEY (`OrderID`) REFERENCES `Orders` (`OrderID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `FK_FADLSongs_SongID` FOREIGN KEY (`SongID`) REFERENCES `Songs` (`SongID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=1768 DEFAULT CHARSET=utf8;
答案 0 :(得分:2)
以及
在这里看看
Deleting from a MySQL table with foreign key constraints
您可以将foreign key constraints
设置为0
并可以更新/删除该行,然后重新设置foreign key constraints
SET foreign_key_checks = 0
DELETE FROM Orders WHERE OrderID Not In (SELECT Distinct OrderID FROM MOrder) And OrderID Not In (SELECT Distinct OrderID FROM PSGOrder)
SET foreign_key_checks = 1