0

Want to delete data from multiple tables. As i understood, there is no way to do it via join/using in Postresql (currently 9.4). It is possible to delete only from one table at a time. Trying like this, but data was deleted only from t1, obviously. Maybe there is a mistake in my query.

DELETE FROM 
    shema.table1 t1
USING
    shema.table2 t2,
    shema.table3 t3,
    shema.table4 t4
WHERE 
    t1.id = 111 AND
    t1.id = t2.t1_id AND
    t2.id = t3.t2_id AND
    t2.id = t4.t2_id

Question 1. There is no simple way to delete data from multiple tables in one query with some feature like join in Postgresql?

Question 2. Query/ies is suppose to be run in python with sqlalchemy as raw SQL statements. Is there a difference how multiple DELETE statements will be execute?

As separate queries. Probably execute all queries within transaction.

from sqlalchemy import text

query_1 = text('DELETE ...')
query_2 = text('DELETE ...')
query_3 = text('DELETE ...')

engine.execute(query_1)
engine.execute(query_2)
engine.execute(query_3)

Or as one long statement

from sqlalchemy import text

queries = text(
    'DELETE ...;'
    'DELETE ...;'
    'DELETE ...;'
)

engine.execute(queries)
3
  • 1
    If you want to delete from multiple tables, you need to run multiple DELETE statements Commented Nov 30, 2022 at 8:24
  • 1) Postgres 9.4 is coming up on 3 years past EOL, you should be thinking of moving on. 2) Do the tables have FOREIGN KEY relationships? Commented Nov 30, 2022 at 16:27
  • 1) sadly, i have no opportunity to change versions. 2) no, they don't. It seems that i have to use multiple delete queries Commented Dec 1, 2022 at 7:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.