Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting a SQL row ignoring all foreign keys and constraints

I have a row in a table. This row has an ID column referenced in a few other tables with millions of rows. The SQL statement to delete the row always times out. From my design, I know the row I wish to delete is never referenced any where else. Hence I would like SQL to ignore having to check all other tables for a foreign key reference to this row and delete the row immediately. Is there a quick way to do this in SQL 2008? Perhaps something along the lines of:

DELETE FROM myTable where myTable.ID = 6850 IGNORE CONSTRAINTS

Or something along those lines.

like image 694
Matt Avatar asked Dec 07 '09 21:12

Matt


People also ask

How do I force a row to delete in SQL?

DELETE FROM table_name WHERE id = id_to_delete; -- Deleting a record from a table that has foreign key reference. SET FOREIGN_KEY_CHECKS = 1; -- Enabling foreign key checks after running the above query.

What if I delete a row containing a foreign key to another table?

Suppose there is a main table containing a primary key and there is another table which contains a foreign key to this main table. So if we delete the row of main table it will delete the child table also.


4 Answers

You can set the constraints on that table / column to not check temporarily, then re-enable the constraints. General form would be:

ALTER TABLE TableName NOCHECK CONSTRAINT ConstraintName 

Then re-enable all constraints with

ALTER TABLE TableName CHECK CONSTRAINT ConstraintName 

I assume that this would be temporary though? You obviously wouldn't want to do this consistently.

like image 109
Jeff Avatar answered Oct 01 '22 01:10

Jeff


Yes, simply run

DELETE FROM myTable where myTable.ID = 6850 

AND LET ENGINE VERIFY THE CONSTRAINTS.

If you're trying to be 'clever' and disable constraints, you'll pay a huge price: enabling back the constraints has to verify every row instead of the one you just deleted. There are internal flags SQL keeps to know that a constraint is 'trusted' or not. You're 'optimization' would result in either changing these flags to 'false' (meaning SQL no longer trusts the constraints) or it has to re-verify them from scratch.

See Guidelines for Disabling Indexes and Constraints and Non-trusted constraints and performance.

Unless you did some solid measurements that demonstrated that the constraint verification of the DELETE operation are a performance bottleneck, let the engine do its work.

like image 36
Remus Rusanu Avatar answered Oct 01 '22 03:10

Remus Rusanu


Do not under any circumstances disable the constraints. This is an extremely stupid practice. You cannot maintain data integrity if you do things like this. Data integrity is the first consideration of a database because without it, you have nothing.

The correct method is to delete from the child tables before trying to delete the parent record. You are probably timing out because you have set up cascading deltes which is another bad practice in a large database.

like image 29
HLGEM Avatar answered Oct 01 '22 02:10

HLGEM


I know this is an old thread, but I landed here when my row deletes were blocked by foreign key constraints. In my case, my table design permitted "NULL" values in the constrained column. In the rows to be deleted, I changed the constrained column value to "NULL" (which does not violate the Foreign Key Constraint) and then deleted all the rows.

like image 31
Tony Avatar answered Oct 01 '22 03:10

Tony