天天看點

drop user與drop user cascade的差別

文檔課題:drop user與drop user cascade的差別.
oracle資料庫删除使用者有drop user username和drop user username cascade指令,它們有何差別呢?
官方文檔:
Dropping a Database User: Example If user Sidney's schema contains no objects, then you can drop sidney by issuing the statement:
DROP USER sidney;
If Sidney's schema contains objects, then you must use the CASCADE clause to drop sidney and the objects:
DROP USER sidney CASCADE;

當使用者下沒有資料庫對象時,可以用drop user username删除使用者,但當該使用者有對象時,就要用drop user username cascade删除使用者.
測試如下:
資料庫:oracle 11.2.0.4 64位
系統:centos 7.9 64位
1、drop user測試
SQL> create user liujun identified by liujun;

User created.

SQL> select object_type,count(*) from all_objects where owner='LIUJUN' group by object_type;

no rows selected
SQL> drop user liujun;

User dropped.
說明:使用者LIUJUN無資料庫對象,drop user指令删除成功.
2、drop user cascade測試
SQL> create user liujun identified by liujun;

User created.

SQL> grant connect,resource,unlimited tablespace to liujun;

Grant succeeded.
SQL> conn liujun/liujun;
Connected.

SQL> select * from user_sys_privs;

USERNAME                       PRIVILEGE                                ADM
------------------------------ ---------------------------------------- ---
LIUJUN                         UNLIMITED TABLESPACE                     NO

SQL> select * from session_privs;

PRIVILEGE
----------------------------------------
CREATE SESSION
UNLIMITED TABLESPACE
CREATE TABLE
CREATE CLUSTER
CREATE SEQUENCE
CREATE PROCEDURE
CREATE TRIGGER
CREATE TYPE
CREATE OPERATOR
CREATE INDEXTYPE

10 rows selected.
SQL> create table test (id number(6,2),
  2                     name varchar2(20));

Table created.

SQL> begin
  2    for i in 1..1000 loop
  3    insert into test values (i,'a' || i);
  4    end loop;
  5  end;
  6  /

PL/SQL procedure successfully completed.

SQL> commit;

Commit complete.
SQL> conn / as sysdba
Connected.
SQL> select object_type,count(*) from all_objects where owner='LIUJUN' group by object_type;

OBJECT_TYPE           COUNT(*)
------------------- ----------
TABLE                        1

SQL> drop user liujun;
drop user liujun
*
ERROR at line 1:
ORA-01922: CASCADE must be specified to drop 'LIUJUN'


SQL> drop user liujun cascade;

User dropped.

說明:當使用者LIUJUN有資料庫對象時,删除該使用者就需要使用drop user cascade.      

繼續閱讀