Oracle-在一个查询中删除多个表

Oracle-在一个查询中删除多个表,oracle,ddl,Oracle,Ddl,我在一个数据库中有50个表,因为我只需要6个表 如何通过一个查询删除剩余的表?使用类似的方法,因为oracle中没有直接的命令或方法来执行此操作 begin for rec in (select table_name from all_tables where table_name like '%ABC_%' ) loop execute immediate 'drop table '||

我在一个数据库中有50个表,因为我只需要6个表


如何通过一个查询删除剩余的表?

使用类似的方法,因为oracle中没有直接的命令或方法来执行此操作

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;
/

您可以使用以下查询生成下拉表命令列表:

SELECT 'DROP TABLE ' || table_name || ';' FROM user_tables;
然后删除要保留的六个表并执行其他命令。或者向查询中添加
WHERE table\u name NOT IN(…)
子句


希望有帮助。

首先使用要保留的表名运行此查询

SELECT CONCAT( 'DROP TABLE ', GROUP_CONCAT(table_name) , ';' ) 
AS statement FROM information_schema.tables 
WHERE table_schema = 'mydatabase' AND table_name not in ('table1', 'table2', 'table3');
此查询将为您提供DROP表查询

  • 从左侧列表中选择要删除的所有表。将它们拖放到工作表中。从弹出窗口中选择对象名称
  • 按编辑菜单并选择替换
  • 在查找字段中键入逗号符号
  • 在替换字段中键入以下文本
    \ndrop表格
    。请注意,单词表后面有一个空格。全部替换
  • 在第一张表格前键入
    放下表格
    ,然后单击
    在上一个之后
  • 你准备好放下你的桌子了
  • 为了进一步说明答案, 对于Oracle 10及更高版本,删除的表不会被永久删除,而是移动到回收站。要真正删除表,需要添加可选参数PURGE

    根据公认的答案展开:

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

    如果要保留的表是
    keep_tab1
    keep_tab2
    ,等等,或者以ASB开头
    一样使用
    regexp\u。这里仅显示一个类似于

    即使在删除其中一个表时发生错误,循环仍将继续。您可以 必须多次运行此脚本,因为在没有先删除引用表的情况下删除主表时可能会发生错误

    begin 
    for rec in (
               select table_name  from   user_tables 
               where  not   
               regexp_like(table_name, 'keep_tab1|keep_tab2|^ASB')  
               ) 
    loop  
        begin  
        execute immediate 'drop table '||rec.table_name;  
        exception when others then   
        dbms_output.put_line(rec.table_name||':'||sqlerrm);  
        end;  
    end loop;               
    end;  
    

    如果我是你,我将导出这六个表,删除所有内容,然后还原这六个表。你没有像SQL Developer或Toad这样的工具吗?只需单击表(如果愿意,可以使用shift和ctrl),然后单击“删除”。@ThorstenKettner我有sql developer,但问题是我可以选择多个表,但在选择多个表后,它并没有提供“删除”选项。哦,太糟糕了。对不起,我不知道。我以为所有这些工具都会提供这种简单的功能。我正在使用Toad。最好的解决方案是@Raptor:简单、快速、安全……我还将添加级联约束,并使用
    user\u tables
    而不是
    all\u tables
    。否则,可以在其他架构中删除具有相同名称的表(如果您有删除权限)。@Deepu-in-where子句中我们可以使用“not-in”运算符,然后我们将所有六个名称都放在那里?@我自己:好的,这是不可能的,因为在
    drop-table
    命令中没有架构名称。在最坏的情况下,它将导致一个错误,因为表已经被删除,但这并不是那么悲惨:)@vim yes它应该是可能的,它在我运行时给出了错误。SQL错误:ORA-00942:表或视图不存在。我使用此查询选择“DROP TABLE”| | TABLE| U name | |”来自pmd_dba;这里pmd_dba是数据库否,您必须使用
    user_表
    。它是您是所有者的所有表的视图。
    begin 
    for rec in (
               select table_name  from   user_tables 
               where  not   
               regexp_like(table_name, 'keep_tab1|keep_tab2|^ASB')  
               ) 
    loop  
        begin  
        execute immediate 'drop table '||rec.table_name;  
        exception when others then   
        dbms_output.put_line(rec.table_name||':'||sqlerrm);  
        end;  
    end loop;               
    end;