简体   繁体   English

MySQL:删除一行忽略外键约束

[英]MySQL: delete a row ignoring foreign key constraint

so I am working on a few tables and there are some data inconsistency between them... One or two tables have a foreign key constraint on a particular table (call it table X), but that table has multiple rows with the foreign key column.所以我正在处理几个表,并且它们之间存在一些数据不一致......一个或两个表在特定表上具有外键约束(称为表 X),但该表具有多行与外键列.

What I want to do is to remove the duplicated rows in table X, but the foreign key constraint is preventing me from doing this.我想要做的是删除表 X 中的重复行,但外键约束阻止我这样做。 Is there a way to force delete the rows while ignoring the foreign key constraint since I know what I'm doing?有没有办法在忽略外键约束的同时强制删除行,因为我知道我在做什么?

SET foreign_key_checks = 0;

That will prevent MySQL from checking foreign keys.这将阻止 MySQL 检查外键。 Make sure to set it back to 1 when you are done though.但是,请确保在完成后将其设置回 1。

Also, you could always drop the foreign key and then add it later if you wanted to only affect a singular key此外,如果您只想影响单个键,您可以随时删除外键,然后再添加它

ALTER TABLE tableName DROP FOREIGN KEY fk;

Simply execute as follows:只需执行如下:

  1. Disable foreign key check禁用外键检查

    SET foreign_key_checks = 0; SET foreign_key_checks = 0;

  2. Delete your records删除您的记录

    DELETE FROM table_name WHERE {conditions}; DELETE FROM table_name WHERE {conditions};

  3. Enable foreign key check启用外键检查

    SET foreign_key_checks = 1; SET foreign_key_checks = 1;

Credit: https://www.knowledgewalls.com/johnpeter/books/mysql/how-to-ignore-constraints-while-insertupdate-or-delete-records-in-mysql信用: https : //www.knowledgewalls.com/johnpeter/books/mysql/how-to-ignore-constraints-while-insertupdate-or-delete-records-in-mysql

As some people already pointed out, ignoring a restricting foreign key leaves you with database inconsistencies.正如一些人已经指出的那样,忽略限制性外键会导致数据库不一致。 Preventing DELETE s is something you want in such cases.在这种情况下,您需要防止DELETE

You should better delete depending rows prior to the main query:您最好在主查询之前删除相关行:

DELETE FROM cities WHERE country_id=3;
-- Afterwards you delete rows from the parent table without error:
DELETE FROM countries WHERE country_id=3;

Or, even better, change the foreign key once, so it does the deletion automatically (cascading):或者,更好的是,更改外键一次,这样它就会自动删除(级联):

ALTER TABLE cities DROP FOREIGN KEY `fk.cities.country_id`;
ALTER TABLE cities ADD CONSTRAINT `fk.cities.country_id` FOREIGN KEY (country_id)
    REFERENCES countries (id) ON UPDATE CASCADE ON DELETE CASCADE;
-- From now on, just delete from the parent table:
DELETE FROM countries WHERE country_id=3;

To expand on the accepted answer, you have to specify the constraint name after DROP FOREIGN KEY要扩展已接受的答案,您必须在DROP FOREIGN KEY之后指定约束名称

You can check the constraint name by issuing SHOW CREATE TABLE .您可以通过发出SHOW CREATE TABLE来检查约束名称。

> SHOW CREATE TABLE tbl_name

Create Table: CREATE TABLE `tbl_name` (
  `id` int(11) DEFAULT NULL,
  `foo_id` int(11) DEFAULT NULL,
  CONSTRAINT `foo_ibfk_1` FOREIGN KEY (`foo_id`)
)

In this case, "foo_ibfk_1" is the constraint name.在这种情况下,“foo_ibfk_1”是约束名称。 So you can write:所以你可以写:

ALTER TABLE tableName DROP FOREIGN KEY foo_ibfk_1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM