oracle怎麼刪除schema

2022-05-25 18:01:35

在oracle中,可以利用drop語句刪除schema,語法為「drop user username cascade;」;drop語句用於刪除表的結構,包括schema,schema是資料庫物件的集合,也可以理解為使用者。

本教學操作環境:Windows10系統、Oracle 11g版、Dell G3電腦。

oracle怎麼刪除schema

刪除schema語法如下:

drop user username cascade;

52.png

範例如下:

1)檢視使用者的預設表空間及臨時表空間

set lines 300
col username for a30
select username ,default_tablespace,TEMPORARY_TABLESPACE from dba_users where username='MESPRD';
-----------------------------------
USERNAME   DEFAULT_TABLESPACE     TEMPORAR    Y_TABLESPACE
MESPRD    HTTBS_MESPRD                        TEMP

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='MESPRD' );
-----------------------------------
PRIVILEGE
CREATE CLUSTER
CREATE INDEXTYPE
CREATE OPERATOR
CREATE PROCEDURE
CREATE SEQUENCE
CREATE SESSION
CREATE TABLE
CREATE TRIGGER
CREATE TYPE

已選擇9行。

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' ));

4)執行指令碼獲得刪除該schema下物件的指令碼 mesprd為要刪除的schema

connect mesprd/MESPRD
spool E:\app\Administrator\del_mesprd.sql;
select 'alter table '||table_name||' drop constraint '||constraint_name||' ;' from user_constraints where constraint_type='R';
select 'truncate table '||table_name ||';' from user_tables;
select 'drop table '||table_name ||' purge;' from user_tables;
select 'drop index '||index_name ||';' from user_indexes;
select 'drop view ' ||view_name||';' from user_views;
select 'drop sequence ' ||sequence_name||';' from user_sequences;
select 'drop function ' ||object_name||';'  from user_objects  where object_type='FUNCTION';
select 'drop procedure '||object_name||';' from user_objects  where object_type='PROCEDURE';
select 'drop package '|| object_name||';' from user_objects  where object_type='PACKAGE';
select 'drop database link '|| object_name||';' from user_objects  where object_type='DATABASE LINK';
spool off;

5)sqlplus連線到該schema下,執行如上獲得的指令碼

執行前檢視下該schema下的物件,執行後再次檢視下該schema下的物件

@?\E:\app\Administrator\del_mesprd.sql;
SQL> select object_type,count(*) from user_objects group by object_type;

6)kill掉連線資料庫的session

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

7)刪除該schema

drop user MESPRD cascade;
以上就是oracle怎麼刪除schema的詳細內容,更多請關注TW511.COM其它相關文章!