How to drop multiple tables having foreign keys in PostgreSQL?

Somnath Muluk picture Somnath Muluk · Mar 14, 2013 · Viewed 9.5k times · Source

I know syntax for deleting multiple tables is:

DROP TABLE foo, bar, baz;

But in my case 3 tables having foreign keys in between them and with other tables which are not to be deleted.

  • Table1: Foreign keys From Table2, Table3, 3 more tables in database.
  • Table2: Forign keys From Table3, 2 more tables in database.
  • Table3: Forign keys From 3 more tables in database.

So how can I drop these 3 tables? They are having data in tables. Will above syntax work ignoring foreign keys? There should not be any data inconsistency in other tables in database.

Answer

a_horse_with_no_name picture a_horse_with_no_name · Mar 14, 2013

You can tell Postgres to automatically drop all foreign keys referencing those tables by using the cascade keyword:

DROP TABLE foo, bar, baz CASCADE;