先转一段:
如果想要快速的删除一个Schema下的所有数据库对象,您会使用什么样的手段来完成呢?
如果您是DBA,可能更倾向于先删除用户然后再重新创建用户。
优点:删除的彻底,不留任何痕迹(这是DBA伟大和危险的真实体现)。
缺点:要求的操作权限比较高,往往需要DBA亲力亲为;有一定误操作的风险;不便于书写到自动化脚本中,因为在当前用户有session连接的情况下是无法实现用户删除的。
如果您是一名开发人员,并且是位“工具达人”,可能会倾向于使用开发工具(如Toad、PL/SQL Developer等)点选的方式完成清理工作。
优点:要求权限不高,如果工具功能掌握熟练可以较快的完成删除任务。
缺点:无法实现自动化和批量操作的目的,人工成本太高,效率极低;存在工具软件异常假死的现象,不推荐使用。
这里按DBA,并且被要求不能删除用户的情况下,步骤如下:
---- Check out the DB object types in this schema as sysdba
SQL> select owner,object_type,count(1) from dba_objects where owner like 'VERTEX%' group by owner,object_type order by 1,2;
OWNER OBJECT_TYPE COUNT(1)
------------------------------ ------------------- ----------
VERTEX_SALES INDEX 535
VERTEX_SALES SEQUENCE 12
VERTEX_SALES TABLE 326
VERTEX_SALES TRIGGER 12
VERTEX_SALES VIEW 7
因为这里INDEX和TRIGGER都是在TABLE上的,所以只需drop掉 TABLE/VIEW/SEQUENCE即可。
---- generate SQL scripts running SELECT DROP clause below on SQLPLUS.
SQL> select 'drop table '||owner||'.'||table_name||' cascade constraints;' from dba_tables where wner='VERTEX_SALES';
SQL> select 'drop sequence '||sequence_owner||'.'||sequence_name||';'||chr(13)||chr(10) from all_sequences where sequence_owner='VERTEX_SALES';
SQL> select 'drop view '||owner||'.'||view_name||' cascade constraints;'||chr(13)||chr(10) from all_views where wner='VERTEX_SALES';
---- execute the SQL script genetated on the top one by one on SQLPLUS
SQL> @/tmp/salesdt.sql
SQL> @/tmp/salesds.sql
SQL> @/tmp/salesdv.sql
注意:执行之前需要对sql脚本修改加工,比如去掉标题和结果等,也可以在生成之前在sqlplus里set verify off pagesize 0 linesize 150 echo off feedback off head off timing off
然后再执行第一步检查是否全部清空
----- import the dmp file exported the source env.
/export/home/oracle > imp "'/ as sysdba'" file=vertex.dmp fromuser=VERTEX_SALES touser=VERTEX_SALES
注意:导入之前可能需要设置导入字符集和添加角色等,那就先create role 。。。
---- See if the import has been completed running the first SQL clause.
完整的SELECT DROP语句如下:
select 'drop table '||table_name||' cascade constraints;' from all_tables where wner='TEST';
select 'drop view ' || view_name||' cascade constraints;'||chr(13)||chr(10) from all_views where wner='TEST';
select 'drop index ' || index_name||' cascade constraints;'||chr(13)||chr(10) from all_indexes where wner='TEST';
select 'drop sequence ' || sequence_name||';'||chr(13)||chr(10) from all_sequences where sequence_owner='TEST';
select 'drop procedure ' || object_name||';'||chr(13)||chr(10) from all_objects where object_type='PROCEDURE' and wner='TEST';
select 'drop trigger ' || object_name||';'||chr(13)||chr(10) from all_objects where object_type='TRIGGER' and wner='TEST';
select 'drop package ' || object_name||';'||chr(13)||chr(10) from all_objects where object_type='PACKAGE' and wner='TEST';
---- The end.