gpt4 book ai didi

mysql - 无法删除 MySQL 中带有外键的列

转载 作者:行者123 更新时间:2023-11-29 12:19:07 25 4
gpt4 key购买 nike

我在 ASP.NET 中使用 Entityframework 创建了一个表。我的数据库是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'

我正在尝试使用以下命令删除外键 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列。

最佳答案

这有效。

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

关于mysql - 无法删除 MySQL 中带有外键的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29302933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com