Somnath Muluk
Somnath Muluk

Reputation: 57656

How to drop multiple tables having foreign keys in PostgreSQL?

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.

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.

Upvotes: 7

Views: 14260

Answers (2)

user330315
user330315

Reputation:

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

DROP TABLE foo, bar, baz CASCADE;

Upvotes: 23

Piyas De
Piyas De

Reputation: 1764

Usually,

You will have chance of inconsistency if Primary keys of Table1, Table2, and Table3 are referred as foreign key in any other tables in the database.

If you have so, The safest way to drop these tables are

first drop contraints in those tables by -

ALTER TABLE table_name DROP CONSTRAINT "table_name_id_fkey";

and then drop these tables one by one.

Upvotes: 0

Related Questions