Summary: in this tutorial, you’ll learn how to use the Oracle DROP TABLE
statement to remove an existing table.
Introduction to Oracle DROP TABLE statement #
To drop a table and its data from the Oracle Database, you use the DROP TABLE
statement:
DROP TABLE table_name
[CASCADE CONSTRAINTS | PURGE];
Code language: SQL (Structured Query Language) (sql)
In this syntax:
- First, specify the name of the table you want to remove in the
DROP TABLE
clause. - Second, use the
CASCADE CONSTRAINTS
clause to remove all referential integrity constraints that refer to primary and unique keys in the table. If such referential integrity constraints exist and you don’t use theCASCADE CONSTRAINTS
clause, Oracle stops removing the table and issues an error. - Third, use the
PURGE
clause if you want to drop the table and release the space associated with it at once. By using thePURGE
clause, Oracle will not place the table and its dependent objects into the recycle bin.
Notice that the PURGE
clause does not allow you to roll back or recover the dropped table. Therefore, it can be useful when you don’t want the sensitive data to appear in the recycle bin.
Basic Oracle DROP TABLE statement example #
First, create a new table called purchase_orders
:
CREATE TABLE purchase_orders (
order_number NUMBER PRIMARY KEY,
purchase_date DATE NOT NULL
);
Code language: SQL (Structured Query Language) (sql)
Second, drop the purchase_orders
table:
DROP TABLE purchase_orders;
Code language: SQL (Structured Query Language) (sql)
Using IF EXISTS clause #
If you attempt to drop a table that does not exist, you’ll encounter an error.
To avoid the error, you can the IF EXISTS
clause in the DROP TABLE
statement:
DROP TABLE IF EXISTS table_name;
Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)
Noticce thatt IF EXISTS
clause has been supported since Oraccle 23c. If you earlier version, the statement will fail.
The IF EXISTS
option instructs Oracle to drop the table only if the table exists. If the table does not exist, Oracle won’t issue an error and do nothing.
For example, the following statement attempts to drop the purchase_orders
table that does not exist:
DROP TABLE IF EXISTS purcchase_orders;
Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)
Dropping tables with constraints #
First, create two new tables called brands
and cars
:
CREATE TABLE brands (
brand_id NUMBER PRIMARY KEY,
brand_name varchar2 (50)
);
CREATE TABLE cars (
car_id NUMBER PRIMARY KEY,
make VARCHAR(50) NOT NULL,
model VARCHAR(50) NOT NULL,
YEAR NUMBER NOT NULL,
plate_number VARCHAR(25),
brand_id NUMBER NOT NULL,
CONSTRAINT fk_brand FOREIGN KEY (brand_id) REFERENCES brands (brand_id) ON DELETE CASCADE
);
Code language: SQL (Structured Query Language) (sql)
In these tables, each brand has one or more cars while each car belongs to only one brand.
Second, drop the brands
table:
DROP TABLE brands;
Code language: SQL (Structured Query Language) (sql)
Oracle issued the following error:
ORA-02449: unique/primary keys in table referenced by foreign keys
Code language: SQL (Structured Query Language) (sql)
This is because the primary key of the brands
table is currently referenced by the brand_id
column in the cars
table.
To drop the brands
table, you must use the CASCADE CONSTRAINTS
clause.
Third, drop the brands
table:
DROP TABLE brands CASCADE CONSTRAINTS;
Code language: SQL (Structured Query Language) (sql)
This statement drops the brands
table and the foreign key constraint fk_brand
from the cars
table.
Oracle DROP TABLE PURGE Statement example #
The following statement drops the cars
table using the PURGE
clause:
DROP TABLE cars PURGE;
Code language: SQL (Structured Query Language) (sql)
Dropping multiple tables at once #
Oracle provides no direct way to drop multiple tables at once. However, you can use the following PL/SQL block to do it:
BEGIN
FOR rec IN
(
SELECT
table_name
FROM
all_tables
WHERE
WHERE table_name LIKE 'TEST!_%' ESCAPE '!'
)
LOOP
EXECUTE immediate 'DROP TABLE '|| rec.table_name || ' CASCADE CONSTRAINTS';
END LOOP;
END;
/
Code language: SQL (Structured Query Language) (sql)
This block deletes all tables whose names start with TEST_
.
To test this code, you can first create three tables: test_1
, test_2
and test_3
as follows:
CREATE TABLE test_1 (c1 VARCHAR2 (50));
CREATE TABLE test_2 (c1 VARCHAR2 (50));
CREATE TABLE test_3 (c1 VARCHAR2 (50));
Code language: SQL (Structured Query Language) (sql)
Then, execute the PL/SQL block above.
Summary #
- Use the
DROP TABLE
statement to drop a table from the database.