简体   繁体   English

如何从Oracle中的架构删除表列表?

[英]How to drop list of table from a schema in Oracle?

My Oracle scott schema contains table list like that 我的Oracle scott模式包含这样的表列表

'prefix_A'
'prefix_B'
'prefix_C'
'A'
'B'
'C'

Now i want to drop list of tables ,containing table prefix like that 'Prefix_',But others table A ,B ,C will be remain same. 现在我想删除表列表,其中包含像“ Prefix_”这样的表前缀,但是其他表A,B,C将保持不变。

How it is possible ? 怎么可能?

Thanks in Advance. 提前致谢。

Use dynamic SQL driving off the data dictionary. 使用动态SQL删除数据字典。

begin
     for trec in ( select table_name
                   from user_tables
                   where table_name like 'PREFIX\_%' escape `\' )
     loop
         dbms_output.put_line('dropping table ' || trec.table_name);
         execute immediate 'drop table '||trec.table_name;
     end loop;
end;

It's a good idea to be precise with the LIKE clause; 准确地讲LIKE子句是个好主意。 using the escape keyword to ensure underscores aren't treated as wildcards. 使用escape关键字来确保下划线不会被视为通配符。 Alternatively use substr(table_name, 1, 7) = 'PREFIX_' . 或者使用substr(table_name, 1, 7) = 'PREFIX_'

Dropping the wrong table isn't a disaster provided you're working on 10g or later and the RECYCLE BIN is enabled , but it's still better not to. 如果您使用的是10g或更高版本,并且启用了RECYCLE BIN ,那么放错表并不是灾难,但是最好还是不要这么做。 Obviously you wouldn't run code like this in Production, but you would use the principle to generate a script of drop statements. 显然,您不会在Production中运行这样的代码,但是您将使用该原理来生成drop语句的脚本。

The above code doesn't handle dependencies. 上面的代码不处理依赖项。 If you have foreign keys referencing the prefixed tables and you want to force the dropping of the tables use this additional logic: 如果您具有引用前缀表的外键,并且想要强制删除表,请使用以下附加逻辑:

     execute immediate 'drop table '|| trec.table_name ||' cascade constraint';

This drops the foreign key constraints but leaves the (formerly) dependent tables. 这会删除外键约束,但会保留(以前)依赖表。

I used this to disable constraints, drop constraints and delete the tables. 我用它来禁用约束,删除约束并删除表。

 -- disable constraints on tables
BEGIN
  FOR c IN
  (SELECT c.owner, c.table_name, c.constraint_name
   FROM user_constraints c, user_tables t
   WHERE c.table_name = t.table_name 
   AND t.table_name like 'WF_%'
   AND c.status = 'ENABLED'
   ORDER BY c.constraint_type DESC)
  LOOP
    dbms_utility.exec_ddl_statement('alter table "' || c.owner || '"."' || c.table_name || '" disable constraint ' || c.constraint_name);
  END LOOP;
END;             

-- drop the constraints
begin
    for r in ( select table_name, constraint_name
               from user_constraints where
               table_name like 'WF_%' )
    loop
        execute immediate 'alter table '||r.table_name
                          ||' drop constraint '||r.constraint_name;
    end loop;
end loop;

-- drop the tables
begin
     for trec in ( select table_name
                   from user_tables
                   where table_name like 'WF_%' )
     loop
         execute immediate 'drop table '||trec.table_name|| ' purge';
     end loop;
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM