This blog is mainly for analyzing dropping table commands for Oracle SQL. You might have seen the dropping table commands like below:
1.drop table table_name; 2.drop table table_name cascade constraints; 3.drop table table_name purge;
and wonder what would Oracle Database do behind the scenes for dropping table commands?
Basically, if there are no keywords specified as the first command shows, Oracle Database would perform the following operations:
All rows from the table are dropped.
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. If
tableis partitioned, then any corresponding local index partitions are also dropped.
All the storage tables of nested tables and LOBs of
When you drop a range-, hash-, or list-partitioned table, then the database drops all the table partitions. If you drop a composite-partitioned table, then all the partitions and subpartitions are also dropped.
The first question is Why am I getting the error: ORA-02449: unique/primary keys in table referenced by foreign keys
Answer: The Oracle error is caused by the attempt to drop a table with unique or primary keys referenced by foreign keys in another table, or in other word, the table that is referenced as a parent table by a foreign key constraint in a parent-child relationship that established between two tables through a foreign key. Oracle does not allow to drop tables referenced by foreign keys of other tables without specifying the CASCADE CONSTRAINTS option in the DROP TABLE statement, or to drop the parent table without first removing the foreign key.
The solution and workaround for the error when you want to drop tables referenced by child tables, is to use the CASCADE CONSTRAINTS option in the DROP TABLE statement.
That is why we use: 2. drop table table_name cascade constraints;
Now the question is What if I accidentally delete the table and want to recover it back?
Here comes the keyword PURGE that differentiates a normal dropping command and a purged one. See the table below for comparisons between the two statements.
|DROP TABLE WITHOUT PURGE||DROP TABLE WITH PURGE|
|Release the space||No||Yes|
Unless you specify the
PURGE clause, the
TABLE statement does not result in space being released back to the tablespace for use by other objects, and the space continues to count toward the user’s space quota. Additionally, the first command only results in the to-be-dropped table and its dependent objects being placed into the recycle bin. Specify PURGE if you want to drop the table and release the space associated with it in a single step.
Note: You cannot roll back a DROP TABLE statement with the PURGE keyword clause, nor you can recover the table if you have dropped it with PURGE clause. So be cautious and intentionally.
So how could we recover the tables in the recycle bin?
One feature of Oracle 10g is
recyclebin, which allows you to recover dropped tables.With
recyclebin, any tables you drop do not actually get deleted. Instead, Oracle renames the table and its associated objects to a system-generated name that begins with
Look at the following example to get a better understanding:
Let us first create a testing table called studentinfo that contains one row as below.
Now we want to drop this table and look into recycle bin to find it.
You could see the object name is changed because of the dropping action.
You could still query it as you would do before with this new object name. Because the table data is still there and if you want to undo the drop action, you could use the FLASHBACK command like below:
The next time when we drop the same table, oracle database would associate a new object name for the table. So you would understand that after a table is dropped, it is just renamed. It is still part of your tablespace, and this space counts against your user tablespace quota.This space would not release until you purge the table out of the recycbin.
So now goes my last question: Will the dropped tables stay in the recycle bin forever if there is no action made toward it later on?
The answer is NO. When your tablespace runs out of space or until you reach your user quota on the tablesapce. At that point, Oracle database would purge the objects at one time, starting with the ones dropped the longest time ago, until there is enough space for the current operation.