简体   繁体   English

如何在不检查外键约束的情况下删除

[英]How to delete with no check foreign key constraints

I have a table with some bad data in SQL.我在 SQL 中有一个包含一些错误数据的表。 this table has a lot of relationship with other tables and other tables have a lot of data so when I want to delete bad data it's very slowly and take lots of time to do.这个表与其他表有很多关系,其他表有很多数据,所以当我想删除坏数据时,速度非常慢,需要很多时间。 I think the cause of this problem is foreign key constraints.我认为这个问题的原因是外键约束。 the main problem is how can disable all of the foreign key constraints from one table.主要问题是如何禁用一张表中的所有外键约束。

You have to disable check constraint:您必须禁用检查约束:

SET FOREIGN_KEY_CHECKS=0;

Make sure to turn it back after your commands:确保在您发出命令后将其转回:

SET FOREIGN_KEY_CHECKS=1;

Disable constraint for one table:禁用一张表的约束:

alter table
   table_name
DISABLE constraint
   constraint_name;

Here is an example:下面是一个例子:

select 'alter table '||table_name||' disable constraint '||constraint_name||';' from user_constraints;

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

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