ORACLE下刪除當前用戶下所有對象的SQL

ORACLE下刪除當前用戶下所有對象的SQL,這個是轉別人的,但是,做了修改和註釋
 

Sql代碼
  1. --刪除某個用戶下的對象   
  2. set heading off;   
  3. set feedback off;   
  4. spool c:\dropobj.sql;   
  5.   prompt --Drop constraint   
  6.  select 'alter table '||table_name||' drop constraint '||constraint_name||' ;' from user_constraints where constraint_type='R';   
  7.  prompt --Drop tables   
  8.  select 'drop table '||table_name ||';' from user_tables;    
  9.     
  10.  prompt --Drop view   
  11.  select 'drop view ' ||view_name||';' from user_views;   
  12.     
  13.  prompt --Drop sequence   
  14.  select 'drop sequence ' ||sequence_name||';' from user_sequences;    
  15.     
  16.  prompt --Drop function   
  17.  select 'drop function ' ||object_name||';'  from user_objects  where object_type='FUNCTION';   
  18.   
  19.  prompt --Drop procedure   
  20.  select 'drop procedure '||object_name||';' from user_objects  where object_type='PROCEDURE';   
  21.     
  22.  prompt --Drop package   
  23.  prompt --Drop package body   
  24.  select 'drop package '|| object_name||';' from user_objects  where object_type='PACKAGE';   
  25.   
  26.  prompt --Drop database link   
  27.  select 'drop database link '|| object_name||';' from user_objects  where object_type='DATABASE LINK';   
  28.     
  29. spool off;   
  30. set heading on;   
  31. set feedback on;   
  32.   
  33. @@c:\dropobj.sql;   
  34. host del c:\dropobj.sql;  



註釋:
1.上面這個語句,在pl/sql裏面是放在命令裏面執行的。
2.set heading off; 意思就是關閉表頭。如果不關閉,寫入dropobj.sql文件中就會帶有結果集的表頭如:
'DROPTABLE'||TABLE_NAME||';'
------------------------------------------
drop table TEACHER;
實際上我們需要的是“drop table TEACHER;”,“'DROPTABLE'||TABLE_NAME||';'
”就是表頭。
3.set feedback off; 意思就是關閉回顯。如果不關閉,寫入dropobj.sql文件中就會帶有返回結果集的大小等信息,如:"137 rows selected"
4.spool c:\dropobj.sql; 把結果集寫入這個文件。spool off; 結束寫入。
5.@@c:\dropobj.sql; 執行這個sql
6.host del c:\dropobj.sql; 刪除主機上這文件。
7.CONSTRAINT_TYPE 就是鍵的類型:
 

Sql代碼
  1. C (check constraint on a table)    
  2. P (primary key)    
  3. U (unique key)   
  4. R (referential integrity)   
  5. V (with check optionon a view)   
  6. O (with read onlyon a view)  


8.當執行'drop package ………… '這句時,package body會被同時刪除。

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