DROP command
The DROP
command in Oracle Database is used to permanently delete database objects, such as tables, views, indexes, or even the entire database schema. Once a table or other object is dropped, it is removed from the database and cannot be recovered (unless you have enabled features like Oracle's Recycle Bin).
1. Purpose of DROP
Command
- Completely removes the specified object from the database.
- Deletes the structure and data of the object.
- Drops dependent objects like constraints, indexes, and triggers associated with the table.
2. Syntax
DROP TABLE table_name [CASCADE CONSTRAINTS] [PURGE];
3. Parameters
-
table_name
:- Specifies the name of the table to drop.
-
CASCADE CONSTRAINTS
:- Drops the table along with all constraints (e.g., foreign key constraints) referencing it.
-
PURGE
:- Removes the table from the Oracle Recycle Bin, making it unrecoverable.
4. Examples
A. Basic Drop Table
Deletes the table and all its data.
DROP TABLE EMP1;
B. Drop Table with Constraints
Deletes the table and also removes any constraints referencing it.
DROP TABLE EMP1 CASCADE CONSTRAINTS;
C. Drop Table Permanently
Deletes the table without moving it to the Recycle Bin.
DROP TABLE EMP1 PURGE;
5. Behavior and Notes
-
Dependent Objects:
- When you drop a table, all dependent objects such as constraints, triggers, and indexes are also dropped automatically.
-
Foreign Keys:
- If other tables reference the table being dropped (via foreign keys), you must use
CASCADE CONSTRAINTS
.
- If other tables reference the table being dropped (via foreign keys), you must use
-
Recycle Bin:
- By default, Oracle moves dropped tables to the Recycle Bin, allowing them to be restored using the
FLASHBACK TABLE
command. - Use the
PURGE
keyword to bypass the Recycle Bin.
- By default, Oracle moves dropped tables to the Recycle Bin, allowing them to be restored using the
-
Irreversible Action:
- Dropping a table with
PURGE
is irreversible. Use it cautiously.
- Dropping a table with
6. Verification
Check Table Existence
To confirm whether a table exists:
SELECT table_name FROM user_tables WHERE table_name = 'EMP1';
7. Recycle Bin
When you drop a table without PURGE
, it moves to the Recycle Bin. You can restore or manage it.
-
View Recycle Bin:
SHOW RECYCLEBIN;
-
Restore Table:
FLASHBACK TABLE EMP1 TO BEFORE DROP;
-
Permanently Remove from Recycle Bin:
PURGE TABLE EMP1;
8. Common Errors
-
ORA-02449: Unique/primary keys in table referenced by foreign keys
:- Occurs when the table being dropped is referenced by foreign keys in another table. Use
CASCADE CONSTRAINTS
to resolve this:DROP TABLE EMP1 CASCADE CONSTRAINTS;
- Occurs when the table being dropped is referenced by foreign keys in another table. Use
-
ORA-00942: Table or view does not exist
:- The specified table does not exist or the user does not have sufficient privileges.
9. Best Practices
- Backup: Always back up important data before dropping tables.
- Dependency Check: Ensure that no essential objects or processes depend on the table.
- Recycle Bin Usage: Avoid using
PURGE
unless absolutely necessary to maintain the ability to recover dropped tables.
Comments
Post a Comment