Wednesday, July 9, 2008

Table Rename - What Happens to the Indexes,constraints and select queries running against the Table

I was just doing some R&D to find out whatz happenning when we are renaming a table name from X to Y.
The following are my observations
1. I got a table called numbers_1 which got 1900002 rows as shown below
SQL> show user;
USER is "DEMO"
SQL>select count(*) from numbers_1;
COUNT(*)
----------
1900002
2. Started a select query from numbers_1 as shown below
SQL>select * from numbers_1;
THis query is running...
I logged-in in another window as demo user Renamed the numbers_1 to numbers.
The select query is still running.
This is captured in the following video.









3. The Constraints, Indexes built on the original table are also reflected with the new table name.
This is as shown below Before and after Rename of EMP table:
SQL> select constraint_name,table_name from user_constraints;
CONSTRAINT_NAME TABLE_NAME
------------------------------ ------------------------------
FK_DEPTNO EMP
PK_DEPT DEPT
PK_EMP EMP
SQL> select index_name,table_name from user_indexes;
INDEX_NAME TABLE_NAME
------------------------------ ------------------------------
PK_DEPT DEPT
IDX_NUM1 NUMBERS_1
PK_EMP EMP
SQL> alter table emp rename to NEW_EMP;
Table altered.
SQL>select constraint_name,table_name from user_constraints;
CONSTRAINT_NAME TABLE_NAME
------------------------------ ------------------------------
FK_DEPTNO NEW_EMP
PK_DEPT DEPT
PK_EMP NEW_EMP
SQL> select index_name,table_name from user_indexes;
INDEX_NAME TABLE_NAME
------------------------------ ------------------------
PK_DEPT DEPT
IDX_NUM1 NUMBERS_1
PK_EMP NEW_EMP

Note: But the PL/SQL packages which has the references to the old table name becomes invalid after the Table rename.

No comments: