在MySql中错误1025错误150

时间:2015-11-07 20:32:50

标签: mysql mysql-workbench

我试图在名为ProductInvoice的表中删除外键列。我试图删除的列名为PersonID,来自表Person。当我运行查询

ALTER TABLE ProductInvoice
DROP COLUMN PersonID;

我收到此错误...

Error Code: 1025. Error on rename of './jkripal/#sql-91c_19ff0' to './jkripal/ProductInvoice' (errno: 150)

关于如何解决这个问题的任何建议?我查看了这个网站,找不到任何有帮助的答案。

我也尝试了这些查询

ALTER TABLE ProductInvoice DROP FOREIGN KEY `fkPerson`;
ALTER TABLE ProductInvoice DROP COLUMN PersonID;

并收到此回复......

Error Code: 1025. Error on rename of './jkripal/ProductInvoice' to './jkripal/#sql2-91c-1a05a' (errno: 152)

这些是SHOW CREATE TABLE ProductInvoice

的结果
'ProductInvoice', 'CREATE TABLE `ProductInvoice` 
(\n  `ProductInvoiceID` int(11) NOT NULL AUTO_INCREMENT,
\n  `PersonID` int(11) DEFAULT NULL,
\n  `ProductID` int(11) NOT NULL,
\n  `InvoiceID` int(11) NOT NULL,
\n  `TravelDate` varchar(255) DEFAULT NULL,
\n  `TicketNote` varchar(255) DEFAULT NULL,
\n  `Quantity` int(11) DEFAULT NULL,
\n  `InsuranceTicketCode` varchar(255) DEFAULT NULL,
\n  PRIMARY KEY (`ProductInvoiceID`),
\n  KEY `fkPerson` (`PersonID`),
\n  KEY `fk_ProductInvoice_to_Product` (`ProductID`),
\n  KEY `fk_ProductInvoice_to_Invoice` (`InvoiceID`),
\n  CONSTRAINT `ProductInvoice_ibfk_1` FOREIGN KEY (`PersonID`) REFERENCES `Person` (`PersonID`),
\n  CONSTRAINT `ProductInvoice_ibfk_2` FOREIGN KEY (`ProductID`) REFERENCES `Product` (`ProductID`),
\n  CONSTRAINT `ProductInvoice_ibfk_3` FOREIGN KEY (`InvoiceID`) REFERENCES `Invoice` (`InvoiceID`)
\n) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8'

我也试过这个无济于事

ALTER TABLE ProductInvoice DROP FOREIGN KEY `fkPerson`;

DROP INDEX `fkPerson` ON ProductInvoice;
ALTER TABLE ProductInvoice DROP COLUMN PersonID;

1 个答案:

答案 0 :(得分:0)

您需要删除引用该列的外键约束。为此,您必须指定要通过CONSTRAINT的名称删除的外键。根据show create table中显示的输出,看起来约束名为ProductInvoice_ibfk_1

因此,首先删除外键约束:

 ALTER TABLE `ProductInvoice` DROP FOREIGN KEY `ProductInvoice_ibfk_1`;

然后你可以删除列。 (在删除列之前,可能还需要删除引用该列的索引。)