MySQL 删除一些外键
我有一个表,其主键在其他几个表中使用,并且有其他表的多个外键。
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):重命名时出错”
如何删除上面分配表中的列而不出现此错误?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
正如此处所解释的,似乎外键约束具有由约束名称而不是索引名称删除。
语法是:
As explained here, seems the foreign key constraint has to be dropped by constraint name and not the index name.
The syntax is:
外键用于确保数据完整性,因此只要列是外键的一部分,您就无法删除该列。 您需要先放下钥匙。
我认为以下查询可以做到这一点:
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:
正如上面大家所说,您可以轻松删除FK。 然而,我刚刚注意到,在某些时候可能有必要删除 KEY 本身。 如果您有任何错误消息来创建另一个像上一个索引一样的索引,我的意思是使用相同的名称,那么删除与该索引相关的所有内容将很有用。
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.
检查约束名称和外键名称是什么:
删除约束名称和外键名称:
希望这有帮助!
Check what's the CONSTRAINT name and the FOREIGN KEY name:
Remove both the CONSTRAINT name and the FOREIGN KEY name:
Hope this helps!
嘿,我遵循了上面的一些顺序,
并找到了一些解决方案。
您将获得 FK 约束名称,例如
现在您需要删除此约束。 通过 alter table commandd
然后删除表列,
Hey I followed some sequence above,
and found some solution.
You will get FK Constrain Name like
Now you need to remove this constraints. by alter table commantd
Then drop the table column,
这是一种删除外键约束的方法,它会起作用。
更改表
位置
。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
;如果您的表使用 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
首先需要通过此查询获取实际的约束名称
此查询将结果外键的约束名称,现在下面的查询将删除它。
上面约束名称中的最后一个数字取决于表中有多少个外键
first need to get actual constrain name by this query
This query will result constrain name of the foreign key, now below query will drop it.
last number in above constrain name depends how many foreign keys you have in table
您无法删除外键列,因为它是从表
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 constraintassignmentStuff.assignmentIDX
.A similar question has already been asked here. Check also here for more info.
尝试这个:
Try this:
步骤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.
就我而言,我对一个字段有一个唯一的约束,该字段也是外键。 我的目的是删除整个表,但
迁移
给我ValueError:发现错误的约束数量(0)
。所以我使用了你的不同答案来实现这一点:
执行此操作,并将
options={'unique_together': {(field1, field2)},}
行放入 0001_initial (我不保留迁移历史记录,但如果你这样做,我猜您必须在再次运行makemigrations
和migrate
之前将此选项或 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 meValueError: Found wrong number (0) of constraints
.So I used different pieces of yours answers for make that work :
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 againmakemigrations
andmigrate
.I got the
tablename_fieldname_fk_blabla_id
key_name with the given above command :Finally, no more constraint on my table, the migrate deleted the table !