DROP Table in Oracle

The DROP command in the Oracle database is used to remove database objects (like the table, index, view e.t.c.) from the database. To drop a table in the Oracle database, we use the DROP command.

In all relational databases like Oracle, MySQL, PostgreSQL, and e.t.c, we are allowed to drop only one database object at a time. We can’t drop multiple database objects at a time.

Syntax to use drop command in the Oracle database:-

DROP objectype objectname;

Syntax to drop a table:-

DROP TABLE tablename;

Now let us see the example to create and drop a table. First, we will create a table and then drop that table. Create a table test1,

SQL> CREATE TABLE test1(num NUMBER(10));
Table created.

SQL> DESC test1;
Name        Null?    Type
----------- -------- ----------
NUM                  NUMBER(10)

SQL> DROP TABLE test1;
Table dropped.

SQL> DESC test1;
ERROR:
ORA-04043: object test1 does not exist

To drop the table, it must be in your schema or you must have the DROP ANY TABLE system privilege.

The DROP table drops all rows from the table, all table indexes and domain indexes are dropped, as well as any triggers defined on the table, regardless of who created them or whose schema contains them.

Before oracle 10g, when we drop a table then it removes the table permanently. But from oracle 10g Enterprise edition onwards, whenever we drop a table then it goes into the recycle bin. we can found all dropped database objects in the recycle bin. 

PrepBytes
Use Code KNOWPROGRAM10 to get FLAT 10% OFF on all Courses

Whenever we drop a database object then the recycle bin assigns a different name for that object. The recycle bin of Oracle database stores all dropped object information like original name of database object, recycle bin name of database object, object type (table, index, view, and e.t.c.), and the drop time.  Command to show recycle bin,

SHOW RECYCLEBIN;

Previously we have dropped the test1 table in the recycle bin. We can see it inside the recycle bin. Example:- 

SQL> SHOW RECYCLEBIN;
ORIGINAL NAME  RECYCLEBIN NAME                OBJECT TYPE  DROP TIME
-------------- ------------------------------ ------------ -------------------
TEST1          BIN$2+bfMyIRTEy6d8joZ+EGkg==$0 TABLE        2020-12-24:13:06:49

Get Back Table From Recyclebin

We can get dropped database objects from the recycle bin. Syntax to get back the table from recycle bin:-

FLASHBACK TABLE tablename TO BEFORE DROP;

Example to get back the test1 table from the recycle bin:-

SQL> FLASHBACK TABLE test1 TO BEFORE DROP;
Flashback complete.

SQL> DESC test1;
 Name     Null?    Type
 -------- -------- ----------
 NUM               NUMBER(10)

Now the table test1 is restored in the database and it is not available in the recycle bin. If we again try to flashback the table test2 then we get an error.

SQL> FLASHBACK TABLE test1 TO BEFORE DROP;
FLASHBACK TABLE test1 TO BEFORE DROP
*
ERROR at line 1:
ORA-38305: object not in RECYCLE BIN

Drop Table Permanently in Oracle

We can also drop the table permanently in the Oracle database. Syntax to drop the table permanently:-

DROP TABLE tablename PURGE

If we use the PURGE command, then the database does not place the table and its dependent objects into the recycle bin. It also provides enhanced security if we want to prevent sensitive material from appearing in the recycle bin.

Note:- We cannot roll back a DROP TABLE statement with the PURGE clause, nor we can recover the table if we have dropped it with the PURGE clause. 

For example, let us first create a table, and then we will drop it permanently. To check table is dropped permanently or not we will look in the recycle bin.

SQL> CREATE TABLE test2(num NUMBER(10));
Table created.

SQL> DROP TABLE test2 PURGE;
Table dropped.

SQL> SHOW RECYCLEBIN;

Now, this time the table is permanently deleted and it is not stored in the recycle bin. The recycle bin is empty now.

Note:- In the Oracle database, we can’t retrieve drop columns by using the flashback command because the recycle bin doesn’t store the dropped column.

Recycle bin

Oracle 10g Enterprise edition introduced recycle bin which stores dropped table. It works similar to the Windows recycle bin.

If you want to view recycle bin then we are using following syntax:-

SQL> show recyclebin;

It is also the same as the window recycle bin that’s why we can also drop tables from the recycle bin by using the “purge” command.

Syntax to drop the single table at a time from recycle bin:- 

PURGE TABLE tablename;

To drop all tables at a time from the recyclebin:-

PURGE RECYCLEBIN;

Practice Example to drop table in Oracle

Create four tables “t1”, “t2”, “t3”, and “t4”. Drop the first three tables “t1”, “t2”, and “t3” which will be stored to recycle bin, and drop the “t4” table permenently. Now, drop table “t1” from recycle bin, and then clean the recyclebin (it will remove remaining all dropped objects from recyclebin).

Solution:-

Create four tables “t1”, “t2”, “t3”, and “t4” with some column.

SQL> CREATE TABLE t1(sno NUMBER(10));
Table created.

SQL> CREATE TABLE t2(sno NUMBER(10));
Table created.

SQL> CREATE TABLE t3(sno NUMBER(10));
Table created.

SQL> CREATE TABLE t4(sno NUMBER(10));
Table created.

Drop “t1”, “t2”, and “t3” which will be stored to the recycle bin,

SQL> DROP TABLE t1;
Table dropped.

SQL> DROP TABLE t2;
Table dropped.

SQL> DROP TABLE t3;
Table dropped.

Drop “t4” table permenently,

SQL> DROP TABLE t4 PURGE;
Table dropped.

Check recycle bin,

SQL> SHOW RECYCLEBIN;
ORIGINAL NAME  RECYCLEBIN NAME                OBJECT TYPE  DROP TIME
-------------- ------------------------------ ------------ -------------------
T1             BIN$4gRO1cCMSRW/DIp8EEoung==$0 TABLE        2020-12-24:13:46:12
T2             BIN$E9EABPGlQDeMGFtmdx7qqg==$0 TABLE        2020-12-24:13:46:16
T3             BIN$B4Q7gTQZQmioPz9ZDU9Ohw==$0 TABLE        2020-12-24:13:46:19

Drop table “t1” from the recycle bin,

SQL> PURGE TABLE t1;
Table purged.

Now, again check recycle bin,

SQL> SHOW RECYCLEBIN;
ORIGINAL NAME  RECYCLEBIN NAME                OBJECT TYPE  DROP TIME
-------------- ------------------------------ ------------ -------------------
T2             BIN$E9EABPGlQDeMGFtmdx7qqg==$0 TABLE        2020-12-24:13:46:16
T3             BIN$B4Q7gTQZQmioPz9ZDU9Ohw==$0 TABLE        2020-12-24:13:46:19

Remove all dropped database objects from the recycle bin,

SQL> PURGE RECYCLEBIN;
Recyclebin purged.

Check recycle bin. Now, recycle bin is empty.

SQL> SHOW RECYCLEBIN;

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *