I have a database with many tables. Twelve of these tables start with the same prefix:
mystuff_table_1
mystuff_table_2
mystuff_table_3
mystuff_table_4
etc...
I don't want to type DROP TABLE mystuff_table_n CASCADE;
over and over again, especially since I have to repeatedly drop the tables. How can I make my life easier?
you can create a procedure to do this automatically:
Try:
Att
First of all, you can delete many tables in a single statement:
Next, you could put all of those tables into a separate schema. Add that schema to the default
search_path
of your user(s), so it's all transparent.Then all you need is:
If that's not short enough, create a function that executes the command.
A static SQL function:
Or a dynamic PL/pgSQL function:
Call:
For an even more dynamic statement:
How do I drop all tables in psql (PostgreSQL interactive terminal) that starts with a common word?