Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Oracle | How to delete records from a table when they match another table?

How would I delete records from a table where they match a delete table? As in, I have a table of record keys that say what need to be deleted from my main table. How would I write a delete to say "delete anything from my main table where this field matches a field in my delete table?"

like image 405
Johnson Gale Avatar asked Oct 24 '25 05:10

Johnson Gale


1 Answers

If it's a small delete table:

delete from TableA A
   where a.key in ( select key from deleteTable );

If it's a bigger table, you can try an EXISTs:

delete from TableA A
   where exists ( select * from deleteTable d
                     where d.key = A.key );

All this depends of course, on your indexes and sizes of tables ... etc ....

(and if it gets really big, you might want to consider another option .. ie partitioning, rebuild table, etc.)

like image 59
Ditto Avatar answered Oct 26 '25 20:10

Ditto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!