删除主键 - 外键约束未正确形成

发布于 2025-01-29 19:48:41 字数 1120 浏览 4 评论 0原文

我似乎无法在表中删除主键。 所有参考文献(FKS)已被删除,但仍然不允许我删除它。

我要做的是:删除添加新键的旧主键 - 但请保留旧的列和数据(只需删除PK属性)即可。

有什么问题?

表:

CREATE TABLE `employee` (
  `User` int(10) unsigned NOT NULL,
  `Company` int(10) unsigned NOT NULL,
  --unrelated boolean fields
  PRIMARY KEY (`User`,`Company`),
  KEY `FK_Employee_Company_idx` (`Company`),
  CONSTRAINT `FK_Employee_Company` FOREIGN KEY (`Company`) REFERENCES `company` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `FK_Employee_User` FOREIGN KEY (`User`) REFERENCES `user` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

尝试删除:

alter table Employee
drop primary key;

问题:

错误1025:'。\ db_name#sql-3640_4'to'。 /code>。<代码>员工删除主键

没有任何引用此表的引用。我还通过语句检查,这些语句从Information_schema.key_column_usage中选择,但不会产生任何结果。

浪费了Google上的最后几个小时,但似乎无法弄清楚。


,如果这样可以添加新列:

alter table Employee
add column ID int unsigned not null auto_increment primary key;

I cannot seem to be able to delete primary keys in a table.
All references (FKs) have been removed but it still doesn't let me delete it.

What I'm trying to do is: delete old primary keys to add a new one - but keep the old columns and data (just remove the PK attribute).

What is wrong ?

Table:

CREATE TABLE `employee` (
  `User` int(10) unsigned NOT NULL,
  `Company` int(10) unsigned NOT NULL,
  --unrelated boolean fields
  PRIMARY KEY (`User`,`Company`),
  KEY `FK_Employee_Company_idx` (`Company`),
  CONSTRAINT `FK_Employee_Company` FOREIGN KEY (`Company`) REFERENCES `company` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `FK_Employee_User` FOREIGN KEY (`User`) REFERENCES `user` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Trying to delete:

alter table Employee
drop primary key;

Issue:

Error 1025: Error on rename of '.\DB_NAME#sql-3640_4' to '.\DB_NAME\employee' (errno: 150 "Foreign key constraint is incorrectly formed") SQL Statement: ALTER TABLE DB_NAME.employee DROP PRIMARY KEY

Nothing references this table anymore. I also checked via statements which select from information_schema.key_column_usage but yields no results.

Wasted the last hours on Google but can't seem to figure it out.


And if that would work, adding a new column:

alter table Employee
add column ID int unsigned not null auto_increment primary key;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残疾 2025-02-05 19:48:41

现有FK约束仍然需要索引。

添加以下索引(第一个)应满足该要求:

CREATE INDEX xxx ON employee (User, Company);

The index is still needed for the existing FK constraints.

Adding the following index (first) should satisfy that requirement:

CREATE INDEX xxx ON employee (User, Company);

Test case

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文