How to delete multiple rows from multiple tables using Where clause?

MiketheCalamity

Using an Oracle DB, I need to select all the IDs from a table where a condition exists, then delete the rows from multiple tables where that ID exists. The pseudocode would be something like:

SELECT ID FROM TABLE1 WHERE AGE > ?
DELETE FROM TABLE1 WHERE ID = <all IDs received from SELECT>
DELETE FROM TABLE2 WHERE ID = <all IDs received from SELECT>
DELETE FROM TABLE3 WHERE ID = <all IDs received from SELECT>

What is the best and most efficient way to do this?

I was thinking something like the following, but wanted to know if there was a better way.

PreparedStatement selectStmt = conn.prepareStatment("SELECT ID FROM TABLE1 WHERE AGE > ?");
selectStmt.setInt(1, age);
ResultSet rs = selectStmt.executeQuery():

PreparedStatement delStmt1 = conn.prepareStatment("DELETE FROM TABLE1 WHERE ID = ?");
PreparedStatement delStmt2 = conn.prepareStatment("DELETE FROM TABLE2 WHERE ID = ?");
PreparedStatement delStmt3 = conn.prepareStatment("DELETE FROM TABLE3 WHERE ID = ?");

while(rs.next())
{
    String id = rs.getString("ID");

    delStmt1.setString(1, id);
    delStmt1.addBatch();

    delStmt2.setString(1, id);
    delStmt2.addBatch();

    delStmt3.setString(1, id);
    delStmt3.addBatch();
}

delStmt1.executeBatch();
delStmt2.executeBatch();
delStmt3.executeBatch();

Is there a better/more efficient way?

ivanzg

You could do it with one DELETE statement if two of your 3 tables (for example "table2" and "table3") are child tables of the parent table (for example "table1") that have a "ON DELETE CASCADE" option.

This means that the two child tables have a column (example column "id" of "table2" and "table3") that has a foreign key constraint with "ON DELETE CASCADE" option that references the primary key column of the parent table (example column "id" of "table1"). This way only deleting from the parent table would automatically delete associated rows in the child tables.

Check out this in more detail : http://www.techonthenet.com/oracle/foreign_keys/foreign_delete.php

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to fetch data from multiple tables using Join and where clause

From Dev

How to delete rows in multiple tables using pdo

From Dev

Delete multiple rows from multiple tables in mysql using single value

From Dev

Postgresql delete multiple rows from multiple tables

From Dev

How to update multiple rows using single where clause

From Dev

how to compare multiple rows in where clause

From Dev

Delete rows from multiple tables as one query

From Dev

Delete rows from multiple tables in a database

From Dev

DELETE rows from multiple tables with JOIN in Mysql

From Dev

How to delete multiple rows in diferent tables?

From Dev

MySQL multiple tables in the WHERE clause

From Dev

How to get rows of Data from Multiple Tables using LinQ To Entities

From Dev

MySQL - Delete from multiple tables using a UNION?

From Dev

How can i delete from the database multiple rows using a checkbox?

From Dev

Is it possible to delete multiple rows in MySQL if target table is in from clause?

From Dev

Android SQLiteDatabase update multiple rows using where clause with IN or OR

From Dev

Deleting multiple rows based on where clause using FluentMigrator

From Dev

MySQL delete multiple rows in multiple tables

From Dev

Stored procedure: delete rows from multiple tables with output for every row

From Dev

SQL Select rows and delete/update from dynamic multiple tables

From Dev

mysql where clause using 'IN" or multiple 'OR'

From Dev

Using multiple AND operators in where clause

From Dev

mysqldump with multiple tables with or without where clause

From Dev

SQL JOIN with WHERE clause on multiple tables slowdown

From Dev

How to DELETE using multiple WHERE clauses [SQLITE]

From Dev

How to concatenate Multiple Rows from Multiple Tables Efficiently

From Dev

Multiple join clause using suqbqueries with multiple where

From Dev

sqlite3 delete multiple rows using "WHERE column IN (?)"

From Dev

Update multiple rows with where clause in codeigniter

Related Related

  1. 1

    How to fetch data from multiple tables using Join and where clause

  2. 2

    How to delete rows in multiple tables using pdo

  3. 3

    Delete multiple rows from multiple tables in mysql using single value

  4. 4

    Postgresql delete multiple rows from multiple tables

  5. 5

    How to update multiple rows using single where clause

  6. 6

    how to compare multiple rows in where clause

  7. 7

    Delete rows from multiple tables as one query

  8. 8

    Delete rows from multiple tables in a database

  9. 9

    DELETE rows from multiple tables with JOIN in Mysql

  10. 10

    How to delete multiple rows in diferent tables?

  11. 11

    MySQL multiple tables in the WHERE clause

  12. 12

    How to get rows of Data from Multiple Tables using LinQ To Entities

  13. 13

    MySQL - Delete from multiple tables using a UNION?

  14. 14

    How can i delete from the database multiple rows using a checkbox?

  15. 15

    Is it possible to delete multiple rows in MySQL if target table is in from clause?

  16. 16

    Android SQLiteDatabase update multiple rows using where clause with IN or OR

  17. 17

    Deleting multiple rows based on where clause using FluentMigrator

  18. 18

    MySQL delete multiple rows in multiple tables

  19. 19

    Stored procedure: delete rows from multiple tables with output for every row

  20. 20

    SQL Select rows and delete/update from dynamic multiple tables

  21. 21

    mysql where clause using 'IN" or multiple 'OR'

  22. 22

    Using multiple AND operators in where clause

  23. 23

    mysqldump with multiple tables with or without where clause

  24. 24

    SQL JOIN with WHERE clause on multiple tables slowdown

  25. 25

    How to DELETE using multiple WHERE clauses [SQLITE]

  26. 26

    How to concatenate Multiple Rows from Multiple Tables Efficiently

  27. 27

    Multiple join clause using suqbqueries with multiple where

  28. 28

    sqlite3 delete multiple rows using "WHERE column IN (?)"

  29. 29

    Update multiple rows with where clause in codeigniter

HotTag

Archive