MySQL 删除一些外键

发布于 2024-07-18 08:53:43 字数 694 浏览 10 评论 0 原文

我有一个表,其主键在其他几个表中使用,并且有其他表的多个外键。

CREATE TABLE location (
   locationID INT NOT NULL AUTO_INCREMENT PRIMARY KEY
   ...
) ENGINE = InnoDB;

CREATE TABLE assignment (
   assignmentID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
   locationID INT NOT NULL,
   FOREIGN KEY locationIDX (locationID) REFERENCES location (locationID)
   ...
) ENGINE = InnoDB;

CREATE TABLE assignmentStuff (
   ...
   assignmentID INT NOT NULL,
   FOREIGN KEY assignmentIDX (assignmentID) REFERENCES assignment (assignmentID)
) ENGINE = InnoDB;

问题是,当我尝试删除外键列之一(即 locationIDX)时,它给了我一个错误。

“错误 1025 (HY000):重命名时出错”

如何删除上面分配表中的列而不出现此错误?

I have a table whose primary key is used in several other tables and has several foreign keys to other tables.

CREATE TABLE location (
   locationID INT NOT NULL AUTO_INCREMENT PRIMARY KEY
   ...
) ENGINE = InnoDB;

CREATE TABLE assignment (
   assignmentID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
   locationID INT NOT NULL,
   FOREIGN KEY locationIDX (locationID) REFERENCES location (locationID)
   ...
) ENGINE = InnoDB;

CREATE TABLE assignmentStuff (
   ...
   assignmentID INT NOT NULL,
   FOREIGN KEY assignmentIDX (assignmentID) REFERENCES assignment (assignmentID)
) ENGINE = InnoDB;

The problem is that when I'm trying to drop one of the foreign key columns (ie locationIDX) it gives me an error.

"ERROR 1025 (HY000): Error on rename"

How can I drop the column in the assignment table above without getting this error?

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

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

发布评论

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

评论(12

与往事干杯 2024-07-25 08:53:43

正如此处所解释的,似乎外键约束具有由约束名称而不是索引名称删除。

语法是:

ALTER TABLE footable DROP FOREIGN KEY fooconstraint;

As explained here, seems the foreign key constraint has to be dropped by constraint name and not the index name.

The syntax is:

ALTER TABLE footable DROP FOREIGN KEY fooconstraint;
凝望流年 2024-07-25 08:53:43

外键用于确保数据完整性,因此只要列是外键的一部分,您就无法删除该列。 您需要先放下钥匙。

我认为以下查询可以做到这一点:

ALTER TABLE assignmentStuff DROP FOREIGN KEY assignmentIDX;

The foreign keys are there to ensure data integrity, so you can't drop a column as long as it's part of a foreign key. You need to drop the key first.

I would think the following query would do it:

ALTER TABLE assignmentStuff DROP FOREIGN KEY assignmentIDX;
温柔女人霸气范 2024-07-25 08:53:43

正如上面大家所说,您可以轻松删除FK。 然而,我刚刚注意到,在某些时候可能有必要删除 KEY 本身。 如果您有任何错误消息来创建另一个像上一个索引一样的索引,我的意思是使用相同的名称,那么删除与该索引相关的所有内容将很有用。

ALTER TABLE your_table_with_fk
  drop FOREIGN KEY name_of_your_fk_from_show_create_table_command_result,
  drop KEY the_same_name_as_above

As everyone said above, you can easily delete a FK. However, I just noticed that it can be necessary to drop the KEY itself at some point. If you have any error message to create another index like the last one, I mean with the same name, it would be useful dropping everything related to that index.

ALTER TABLE your_table_with_fk
  drop FOREIGN KEY name_of_your_fk_from_show_create_table_command_result,
  drop KEY the_same_name_as_above
情话难免假 2024-07-25 08:53:43

检查约束名称和外键名称是什么:

SHOW CREATE TABLE table_name;

删除约束名称和外键名称:

ALTER TABLE table_name
  DROP FOREIGN KEY the_name_after_CONSTRAINT,
  DROP KEY the_name_after_FOREIGN_KEY;

希望这有帮助!

Check what's the CONSTRAINT name and the FOREIGN KEY name:

SHOW CREATE TABLE table_name;

Remove both the CONSTRAINT name and the FOREIGN KEY name:

ALTER TABLE table_name
  DROP FOREIGN KEY the_name_after_CONSTRAINT,
  DROP KEY the_name_after_FOREIGN_KEY;

Hope this helps!

从来不烧饼 2024-07-25 08:53:43

嘿,我遵循了上面的一些顺序,
并找到了一些解决方案。

SHOW CREATE TABLE footable;

您将获得 FK 约束名称,例如

ProjectsInfo_ibfk_1

现在您需要删除此约束。 通过 alter table commandd

alter table ProjectsInfo drop foreign key ProjectsInfo_ibfk_1;

然后删除表列,

alter table ProjectsInfo drop column clientId;

Hey I followed some sequence above,
and found some solution.

SHOW CREATE TABLE footable;

You will get FK Constrain Name like

ProjectsInfo_ibfk_1

Now you need to remove this constraints. by alter table commantd

alter table ProjectsInfo drop foreign key ProjectsInfo_ibfk_1;

Then drop the table column,

alter table ProjectsInfo drop column clientId;
一刻暧昧 2024-07-25 08:53:43

这是一种删除外键约束的方法,它会起作用。
更改表位置location_id
删除外键location_ibfk_1

Here's a way to drop foreign key constraint, it will work.
ALTER TABLE location.location_id
DROP FOREIGN KEY location_ibfk_1;

╰沐子 2024-07-25 08:53:43

如果您的表使用 InnoDB 引擎,您通常会收到此错误。 在这种情况下,您必须删除外键,然后执行更改表并删除列。

但棘手的部分是您不能使用列名删除外键,而是必须找到用于索引它的名称。 要找到这一点,请发出以下选择:

SHOW CREATE TABLE Region;
这应该显示一行,在左上角单击 + 选项,单击全文 raio 按钮,然后单击 go 。您将获得索引的名称,如下所示:

CONSTRAINT Region_ibfk_1 FOREIGN KEY (country_id) REFERENCES国家/地区 (id) 删除时不执行任何操作 更新时不执行任何操作
现在只需发出:

alter tableregion dropforeign keyregion_ibfk_1;

或者

更简单地输入:-
更改表 TableName 删除外键 TableName_ibfk_1;

请记住唯一的事情是在表名后添加 _ibfk_1 ,如下所示:- TableName_ibfk_1

You usually get this error if your tables use the InnoDB engine. In that case you would have to drop the foreign key, and then do the alter table and drop the column.

But the tricky part is that you can't drop the foreign key using the column name, but instead you would have to find the name used to index it. To find that, issue the following select:

SHOW CREATE TABLE region;
This should show you a row ,at left upper corner click the +option ,the click the full text raio button then click the go .there you will get the name of the index, something like this:

CONSTRAINT region_ibfk_1 FOREIGN KEY (country_id) REFERENCES country (id) ON DELETE NO ACTION ON UPDATE NO ACTION
Now simply issue an:

alter table region drop foreign key region_ibfk_1;

or

more simply just type:-
alter table TableName drop foreign key TableName_ibfk_1;

remember the only thing is to add _ibfk_1 after your tablename to make like this:- TableName_ibfk_1

老娘不死你永远是小三 2024-07-25 08:53:43

首先需要通过此查询获取实际的约束名称

SHOW CREATE TABLE TABLE_NAME

此查询将结果外键的约束名称,现在下面的查询将删除它。

ALTER TABLE TABLE_NAME DROP FOREIGN KEY COLUMN_NAME_ibfk_1

上面约束名称中的最后一个数字取决于表中有多少个外键

first need to get actual constrain name by this query

SHOW CREATE TABLE TABLE_NAME

This query will result constrain name of the foreign key, now below query will drop it.

ALTER TABLE TABLE_NAME DROP FOREIGN KEY COLUMN_NAME_ibfk_1

last number in above constrain name depends how many foreign keys you have in table

晌融 2024-07-25 08:53:43

您无法删除外键列,因为它是从表 assignmentStuff 中引用的。 因此,您应该首先删除外键约束assignmentStuff.assignmentIDX

已经提出了类似的问题此处了解更多信息。

You can not drop the foreign key column because it is being referenced from the table assignmentStuff. So you should first drop the foreign key constraint assignmentStuff.assignmentIDX.

A similar question has already been asked here. Check also here for more info.

与他有关 2024-07-25 08:53:43

尝试这个:

alter table Documents drop
  FK__Documents__Custo__2A4B4B5E

Try this:

alter table Documents drop
  FK__Documents__Custo__2A4B4B5E
黑寡妇 2024-07-25 08:53:43

步骤1:show create tablevendor_locations;

步骤2:ALTER TABLEvendor_locations drop外键vendor_locations_ibfk_1;

它对我有用。

step1: show create table vendor_locations;

step2: ALTER TABLE vendor_locations drop foreign key vendor_locations_ibfk_1;

it worked for me.

酸甜透明夹心 2024-07-25 08:53:43

就我而言,我对一个字段有一个唯一的约束,该字段也是外键。 我的目的是删除整个表,但迁移给我ValueError:发现错误的约束数量(0)

所以我使用了你的不同答案来实现这一点:

ALTER TABLE 表名
删除索引表名_字段名_fk_blabla_id
删除外键tablename_fieldname_fk_blabla_id;

执行此操作,并将 options={'unique_together': {(field1, field2)},} 行放入 0001_initial (我不保留迁移历史记录,但如果你这样做,我猜您必须在再次运行 makemigrationsmigrate 之前将此选项或 AlterUniqueTogether 编辑为 0002 或进一步迁移)。

我使用上面给出的命令获得了 tablename_fieldname_fk_blabla_id key_name :

从表名显示索引;

最后,我的表不再受约束,迁移删除了该表!

In my case, I have a unique constraint on a field which is also a foreign key. My purpose is to drop the entire table but the migrate give me ValueError: Found wrong number (0) of constraints.

So I used different pieces of yours answers for make that work :

ALTER TABLE tablename
DROP INDEX tablename_fieldname_fk_blabla_id,
DROP FOREIGN KEY tablename_fieldname_fk_blabla_id;

Executing this, and drop the options={'unique_together': {(field1, field2)},} line into the 0001_initial (I don't keep the migrations history, but if you do, I guess you have to edit this option or the AlterUniqueTogether into 0002 or further migrations) just before to run again makemigrations and migrate.

I got the tablename_fieldname_fk_blabla_id key_name with the given above command :

SHOW INDEX FROM tablename;

Finally, no more constraint on my table, the migrate deleted the table !

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