7

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.

2
  • How could you imagine to drop referenced tables without losing data consistency?! Commented Mar 14, 2013 at 7:35
  • @KouberSaparev: I mean After droping these 3 tables they should not affect other tables in database as there will be no any relation kept now. Commented Mar 14, 2013 at 7:43

2 Answers 2

23

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

DROP TABLE foo, bar, baz CASCADE;
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

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.