无法在MySQL中使用外键删除列

时间:2015-03-27 14:21:04

标签: mysql entity-framework foreign-keys key alter-table

我在Entityframework中使用ASP.NET创建了一个表格。我的数据库是MySQL。我在我的MySQL上运行了SHOW CREATE TABLE命令,得到了以下结果。

'AttendancePercentageDetails', 'CREATE TABLE `AttendancePercentageDetails` (\n  `AttendancePercentageDetailsId` int(11) NOT NULL AUTO_INCREMENT,\n  `Date` datetime NOT NULL,\n  `Percentage` double NOT NULL,\n  `SiteDetailsID` int(11) NOT NULL,\n  `UserDetailsID` int(11) NOT NULL,\n  PRIMARY KEY (`AttendancePercentageDetailsId`),\n  KEY `IX_SiteDetailsID` (`SiteDetailsID`) USING HASH,\n  KEY `IX_UserDetailsID` (`UserDetailsID`) USING HASH,\n  CONSTRAINT `FK_AttendancePercentageDetails_UserDetails_UserDetailsID` FOREIGN KEY (`UserDetailsID`) REFERENCES `UserDetails` (`UserDetailsID`) ON DELETE CASCADE ON UPDATE CASCADE,\n  CONSTRAINT `FK_AttendancePercentageDetails_SiteDetails_SiteDetailsID` FOREIGN KEY (`SiteDetailsID`) REFERENCES `SiteDetails` (`SiteDetailsId`) ON DELETE CASCADE ON UPDATE CASCADE\n) ENGINE=InnoDB AUTO_INCREMENT=812 DEFAULT CHARSET=latin1'

我正在尝试使用以下命令删除foreign key UserDetailsID

SET foreign_key_checks = 0;
ALTER TABLE dbclockit.AttendancePercentageDetails DROP FOREIGN KEY UserDetailsID;
SET foreign_key_checks = 1;

MySQL抛出错误说

Error Code: 1091. Can't DROP 'UserDetailsID'; check that column/key exists

我跑了

SELECT * FROM dbclockit.AttendancePercentageDetails;

,输出中包含UserDetailsID列。

1 个答案:

答案 0 :(得分:1)

这很有用。

ALTER TABLE dbclockit.AttendancePercentageDetails DROP foreign key FK_AttendancePercentageDetails_UserDetails_UserDetailsID;
ALTER TABLE dbclockit.AttendancePercentageDetails DROP COLUMN  UserDetailsID;