| Lesson 8 | Deleting a table |
| Objective | Explain the process involved with deleting a database table, including the impact on dependent objects and modern Oracle 23c safety features such as the Recycle Bin and Flashback. |
In Oracle 23c, deleting a table is more than just removing data. When you drop a table, you remove its structure, its indexes, and many related objects from the database. Because dropping a table is a potentially destructive operation, Oracle provides safety features such as the Recycle Bin and Flashback, as well as options to control how constraints and dependencies are handled.
To remove a table and all of its rows from the database, use the
DROP TABLE statement:
DROP TABLE table_name;
DROP TABLE – Required keywords that begin the DDL statement to remove
the table definition from the database.
table_name – The name of the existing table you want to drop. If you
omit a schema qualifier, Oracle assumes the table is in your current schema.
A more complete syntax, including constraint and safety options, is:
DROP TABLE [schema.]table_name
[CASCADE CONSTRAINTS]
[PURGE];
schema. – Optional schema that owns the table. If
omitted, Oracle uses your current schema.
CASCADE CONSTRAINTS – Drops all referential integrity
constraints in other tables that reference this table’s primary or unique keys. If
you omit this clause and such constraints exist, Oracle raises an error and does
not drop the table.
PURGE – Permanently drops the table and bypasses the
Recycle Bin. Use this only when you are certain you will not need to restore the
table with Flashback.
You must own the table or have appropriate system privileges (for example,
DROP ANY TABLE) to execute this statement.
Dropping a table has several important effects:
Other database objects that reference the table are not physically removed, but they are affected:
CASCADE CONSTRAINTS; otherwise, they prevent the drop.
In Oracle 23c, DROP TABLE is a DDL statement and performs an implicit
commit before and after execution. You cannot roll back a successfully executed
DROP TABLE with ROLLBACK; instead, you rely on the Recycle
Bin and Flashback (if enabled) or traditional backups.
In most non-system tablespaces, when you drop a table without PURGE,
Oracle moves the table to the Recycle Bin instead of immediately releasing its
storage. The table is renamed internally (for example, to a name starting with
BIN$), but you can still restore it.
To restore a dropped table, use FLASHBACK TABLE:
FLASHBACK TABLE table_name TO BEFORE DROP;
This statement recovers the table (and, if possible, its dependent indexes) from the
Recycle Bin. If you are certain a table should never be recovered—for example, for
regulatory or privacy reasons—use PURGE when dropping it:
DROP TABLE table_name PURGE;
Alternatively, you can purge specific Recycle Bin objects later using:
PURGE TABLE table_name;
-- or
PURGE RECYCLEBIN;
Dropping a table is different from removing its data. Oracle provides three main mechanisms, each with distinct effects and performance characteristics:
ROLLBACK.
Use TRUNCATE TABLE to remove all rows but keep the table structure:
TRUNCATE TABLE table_name;
Use DELETE when you want transactional control over row removal:
DELETE FROM table_name;
WHERE clause.In summary, choose:
Before issuing DROP TABLE in an Oracle 23c environment, follow this
checklist:
DBA_DEPENDENCIES, ALL_CONSTRAINTS, and
ALL_VIEWS to find views, PL/SQL units, and foreign keys that reference
the table.
CASCADE CONSTRAINTS or adjust
foreign keys manually.
Understanding how DROP TABLE interacts with constraints, dependencies, and the Recycle Bin is essential for safely managing schema changes in Oracle 23c. In the next lesson, you will review the module and reinforce these concepts with examples and a quiz.