oracle快速刪除schema/username

 1.問題:

oracle該schema下的對象很多,3萬個以上,直接執行刪除用戶命令要很長時間,半小時未刪除完成

 2.刪除前的準備工作

  一般刪除用戶都是爲了重新導入該用戶數據(不刪除表空間),涉及到刪除該schema的重建,故刪除用戶前收集下該schema信息

 1)查看用戶的默認表空間及臨時表空間
 set lines 300
 col username for a30
 select username ,default_tablespace,TEMPORARY_TABLESPACE from dba_users where username='TEST';

 2)查看該用戶的權限和角色

   select privilege from dba_sys_privs where grantee='SYSADM'
 union
 select privilege from dba_sys_privs where grantee in (select granted_role from dba_role_privs where grantee='TEST' );

 3)獲取獲得授予用戶權限的腳本

 select 'grant '||privilege||' to SYSADM;' from (select privilege from dba_sys_privs where grantee='SYSADM'
 union
select privilege from dba_sys_privs where grantee in (select granted_role from dba_role_privs where grantee='SYSADM' ));


 3.快速刪除用戶辦法

 1)停止連接該數據庫的應用服務

 (大部分的應用都用重連機制,不關閉應用服務的話應用會反覆的連接數據庫,即使使用數據庫kill session的命令也無法結束,會話會一直存在)

 2)執行腳本獲得刪除該schema下對象的腳本

  test爲要刪除的schema

  connect test/test
spool /home/oracle/del_test.sql;  
prompt --Drop constraint  
select 'alter table '||table_name||' drop constraint '||constraint_name||' ;' from user_constraints where constraint_type='R';
 
prompt --truncate table  
select 'truncate table '||table_name ||';' from user_tables;

prompt --Drop tables  
select 'drop table '||table_name ||' purge;' from user_tables;  

prompt --Drop indexes  
select 'drop index '||index_name ||';' from user_indexes;
    
prompt --Drop view  
select 'drop view ' ||view_name||';' from user_views;
 
prompt --Drop sequence  
select 'drop sequence ' ||sequence_name||';' from user_sequences;  
 
prompt --Drop function  
select 'drop function ' ||object_name||';'  from user_objects  where object_type='FUNCTION';
 
prompt --Drop procedure  
select 'drop procedure '||object_name||';' from user_objects  where object_type='PROCEDURE';
 
prompt --Drop package  
prompt --Drop package body  
select 'drop package '|| object_name||';' from user_objects  where object_type='PACKAGE';
 
prompt --Drop database link  
select 'drop database link '|| object_name||';' from user_objects  where object_type='DATABASE LINK';  
   
spool off; 

  3)sqlplus連接到該schema下,執行如上獲得的腳本

   執行前查看下該schema下的對象,執行後再次查看下該schema下的對象

  SQL> select object_type,count(*) from user_objects group by object_type;

  4)kill掉連接數據庫的session

   select 'alter system kill session '''||sid||','||serial#||''' immediate;' from v$session where username='TEST'; 

  5)刪除該schema

   drop user test cascade;

  

     


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章