Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle - drop multiple table in a single query

Tags:

oracle

ddl

I have fifty tables in a database, in that I need only six tables.

How can I delete remaining tables by one single query?

like image 871
vim Avatar asked Nov 17 '14 09:11

vim


People also ask

Can we delete records from multiple tables in a single query in Oracle?

Multi-Table Deletes. You can specify multiple tables in a DELETE statement to delete rows from one or more tables depending on the condition in the WHERE clause. You cannot use ORDER BY or LIMIT in a multiple-table DELETE .

Can we drop multiple tables at a time in Oracle?

Simply click the tables (using shift and ctrl if you like), then click Drop.

Can you drop multiple tables in the same statement?

We can drop multiple tables together using a single DROP Table statement as well. Let's create three tables and later we will drop it.


Video Answer


2 Answers

Use something like this, as there is no direct command or way in oracle to do this

begin
  for rec in (select table_name 
              from   all_tables 
              where  table_name like '%ABC_%'
             )
  loop
    execute immediate 'drop table '||rec.table_name;
  end loop;             
end;
/
like image 168
Deepu Avatar answered Sep 18 '22 12:09

Deepu


You can generate a list of DROP TABLE commands with the query below:

SELECT 'DROP TABLE ' || table_name || ';' FROM user_tables;

After that you remove your six tables you want to keep and execute the other commands. Or you add a WHERE table_name NOT IN (...) clause to the query.

Hope it helps.

like image 43
DirkNM Avatar answered Sep 18 '22 12:09

DirkNM