Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete from two tables with circular foreign keys

Tags:

sql

oracle

I have two tables

TableOne (TABLE_ONE_ID (pk), TABLE_TWO_ID (fk), ...(something else) )
TableTwo (TABLE_TWO_ID (pk), TABLE_ONE_ID (fk), ...(something else) )

How can I delete records from these tables?

P.S. I think that it is bad design, but this is not my fault and I don't have permission to change the database structure. I just need to know how to delete records from these tables.

like image 640
TarasLviv Avatar asked Aug 31 '12 09:08

TarasLviv


People also ask

Can you delete from two tables at once?

The syntax also supports deleting rows from multiple tables at once. To delete rows from both tables where there are matching id values, name them both after the DELETE keyword: DELETE t1, t2 FROM t1 INNER JOIN t2 ON t1.id = t2.id; What if you want to delete nonmatching rows?

Is it possible to link two tables using two foreign keys?

The FOREIGN KEY constraint differs from the PRIMARY KEY constraint in that, you can create only one PRIMARY KEY per each table, with the ability to create multiple FOREIGN KEY constraints in each table by referencing multiple parent table.

Can we delete a row with foreign key?

Here, ON DELETE CASCADE is added because when any row is deleted in one table the same gets deleted in the foreign referenced tables that are referencing the primary key in that table.

Are there any good foreign key related table manipulation tips?

There are many good foreign key related table manipulation tips, please check out the following: Jeffrey Yao is a senior SQL Server consultant, striving to automate DBA work as much as possible to have more time for family, life and more automation. has anyone found a way to get around the error?

What is foreign key in SQL?

In Foreign key, when an attribute in one table which is a non-primary key references the same attribute which is the primary key in another table then the non-prime key is called the foreign key. We use the foreign key references in order to make the links between the tables and whatever action is done in any of the tables. Attention reader!

How do you handle foreign keys?

One of our requirements is that they run without flaw, for obvious reasons. The first option (and not one I’d chose) is to either disable or drop the foreign key. Then afterwards we add them back.

Should you delete the children of a foreign key?

-- Always delete the children first. Again, this is a pretty simple example, but try thinking about it when you have a whole series of foreign keys. Say half a dozen, or even more, of them. All of a sudden, if you aren’t familiar with the structure of your database it could take some time to get that order right.


2 Answers

You can disable the foreign key with:

alter table TableOne disable constraint fk_table_two_id;

After that, you should be able to delete the rows.

Per DaveCosta's comment, you could defer the constraint checks. That way the constraints are checked over the entire transaction, not each individual SQL statement. For example:

begin transaction;
set constraints all deferred; 
delete from TableTwo;
delete from TableOne;
commit transaction;
like image 109
Andomar Avatar answered Nov 10 '22 14:11

Andomar


Either defer one of the foreign keys, or assign NULL (assuming it is NULL-able) to break the cycle.

BTW, how are you inserting the data? You must have done something to break the cycle there too.

NOTE: Disabling or deleting the FK may be OK if just one carefully controlled session is modifying the database, but would leave you wide open for data corruption in a concurrent environment where other clients may not be aware that the FK they expect is no longer enforced.

like image 30
Branko Dimitrijevic Avatar answered Nov 10 '22 14:11

Branko Dimitrijevic