Home  >  Article  >  Database  >  How to delete schema in oracle

How to delete schema in oracle

WBOY
WBOYOriginal
2022-05-25 17:47:254679browse

In Oracle, you can use the drop statement to delete the schema. The syntax is "drop user username cascade;"; the drop statement is used to delete the structure of the table, including the schema. The schema is a collection of database objects and can also be understood as user.

How to delete schema in oracle

The operating environment of this tutorial: Windows 10 system, Oracle 11g version, Dell G3 computer.

How to delete schema in oracle

The syntax for deleting schema is as follows:

drop user username cascade;

How to delete schema in oracle

The example is as follows:

1 )View the user's default table space and temporary table space

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)View the user's permissions and roles

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 rows have been selected.

3) Get the script to grant user permissions

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) Execute the script to get the script to delete the object under the schema mesprd is the schema to be deleted

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 Connect to the schema and execute the script obtained above

Check the objects under the schema before execution, and check the objects under the schema again after execution

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

6) Kill the connected database session

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

7) Delete the schema

drop user MESPRD cascade;

Recommended tutorial: "Oracle Video Tutorial"

The above is the detailed content of How to delete schema in oracle. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn