Oracle DROP USER

Summary: in this tutorial, you will learn how to use the Oracle DROP USER to delete a user from the database.

Introduction to Oracle DROP USER statement #

The DROP USER statement allows you to delete a user from the Oracle Database. If the user has schema objects, the DROP USER statement also can remove all the user’s schema objects along with the user.

Here’s the basic syntax of the DROP USER statement:

DROP USER username [CASCADE];Code language: SQL (Structured Query Language) (sql)

In this syntax, you need to specify the user that you want to drop after the DROP USER keywords.

If the user whose schemas contain objects such as views and tables, you need to delete all schema objects of the user first and then drop the user.

Deleting all schema objects of the users first before removing the user is quite tedious. Therefore, Oracle provides you with the CASCADE option.

If you specify the CASCADE option, Oracle will remove all schema objects of the user before deleting the user.

If the schema objects of the dropped user are referenced by objects in other schemas, Oracle will invalidate these objects after deleting the user.

If a table of the dropped user is referenced by materialized views in other schemas, Oracle will not drop these materialized views. However, the materialized views can no longer be refreshed because the base table doesn’t exist anymore.

Note that Oracle does not drop roles created by the user even after it deletes the user.

Oracle DROP USER statement examples #

Let’s take some examples of removing a user from the database.

Using Oracle DROP USER to remove a user that has no schema object #

First, log in to the Oracle database using the user ot .

Second, create a new user called joe:

CREATE USER joe IDENTIFIED BY oracle;Code language: SQL (Structured Query Language) (sql)

Third, drop the user joe using the DROP USER statement:

DROP USER joe;Code language: SQL (Structured Query Language) (sql)

You should see the following message:

User dropped.Code language: SQL (Structured Query Language) (sql)

Since the user joe has no schema objects, you could delete it without specifying the CASCADE option.

Using Oracle DROP USER to delete a user that has schema objects #

First, create a new user called maria and grant the CREATE SESSION and CREATE TABLE system privileges to the user:

CREATE USER maria
    IDENTIFIED BY oracle
    QUOTA 5m ON users;

GRANT 
    CREATE SESSION,
    CREATE TABLE
TO maria;Code language: SQL (Structured Query Language) (sql)

Second, use the user maria to log in to the Oracle database.

Third, create a new table named t1 in the maria user’s schema:

CREATE TABLE t1(
    id NUMBER PRIMARY KEY,
    v VARCHAR2(100) NOT NULL
);

INSERT INTO t1(id,v) 
VALUES(1,'A');Code language: SQL (Structured Query Language) (sql)

Fourth, go back to the session of the user ot and drop user bar:

DROP USER maria;Code language: SQL (Structured Query Language) (sql)

Oracle issued the following error:

ORA-01940: cannot drop a user that is currently connectedCode language: SQL (Structured Query Language) (sql)

Fifth, end the user bar’s session first using the exit command:

exitCode language: SQL (Structured Query Language) (sql)

And issue the DROP USER statement again in the user ot session:

DROP USER maria;Code language: SQL (Structured Query Language) (sql)

Oracle issued the following message:

ORA-01922: CASCADE must be specified to drop 'MARIA'Code language: SQL (Structured Query Language) (sql)

You could not delete the user maria without specifying CASCADE because the user maria has the table t1 as a schema object.

Seventh, use the DROP USER statement with CASCADE option to delete the user maria:

DROP USER maria CASCADE;Code language: SQL (Structured Query Language) (sql)

Oracle could delete the user maria and also the table t1.

Summary #

  • Use the Oracle DROP USER statement to delete a user and optionally remove all objects in the user’s schema from the Oracle database.
Was this tutorial helpful?