oracle rename table 相關index view constraint synonym等的變化
首先ORACLE官方文件對RENAME操作的說明如下:
Use theRENAME statement to rename a table, view, sequence, or private synonym.
-
Oracle Database automatically transfers integrity constraints, indexes, and grants on the old object to the new object.
-
Oracle Database invalidates all objects that depend on the renamed object, such as views, synonyms, and stored procedures and functions that refer to a renamed table.
[可以使用rename語句來rename一個表,檢視,序列或私有同義詞]。
.Oracle自動把舊的物件上的完整性約束,索引,和許可權遷移到新的物件上面。
.Oracle資料庫上涉及與命名後的物件有關的例如:檢視,同義詞和儲存過程和函式都會失效。
1.使用scott使用者建立一個測試表e1和測試索引e1_eno.
SQL> create table e1 as select * from emp;
Table created.
SQL> create index e1_eno on e1(empno);
Index created.
2.分析執行計劃。
SQL> explain plan for select * from e1 where empno>7500;
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 12 | 1044 | 2 (0)| 00:0
0:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| E1 | 12 | 1044 | 2 (0)| 00:0
0:01 |
|* 2 | INDEX RANGE SCAN | E1_ENO | 12 | | 1 (0)| 00:0
0:01 |
3.檢視index情況。
SQL> select index_name,table_owner,table_name,status from user_indexes where index_name='E1_ENO';
INDEX_NAME TABLE_OWNER
------------------------------ ------------------------------
TABLE_NAME STATUS
------------------------------ --------
E1_ENO SCOTT
E1 VALID
4.進行rename操作.
SQL> rename e1 to e2;
Table renamed.
5.查詢索引情況:
SQL> select index_name,table_owner,table_name,status from user_indexes where index_name='E1_ENO';
INDEX_NAME TABLE_OWNER
------------------------------ ------------------------------
TABLE_NAME STATUS
------------------------------ --------
E1_ENO SCOTT
E2 VALID
6.按照第二步進行分析,執行計劃如下:
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 12 | 1044 | 2 (0)| 00:0
0:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| E2 | 12 | 1044 | 2 (0)| 00:0
0:01 |
|* 2 | INDEX RANGE SCAN | E1_ENO | 12 | | 1 (0)| 00:0
發現更改表名後,索引也同時跟著變動,依舊有效。
7.再次更換為e1看看索引情況:
SQL> rename e2 to e1;
Table renamed.
SQL> select index_name,table_owner,table_name,status from user_indexes where index_name='E1_ENO';
INDEX_NAME TABLE_OWNER
------------------------------ ------------------------------
TABLE_NAME STATUS
------------------------------ --------
E1_ENO SCOTT
E1 VALID
發現索引再次隨著表的更改也進行了同步變動。
8.根據第2步再次檢視執行計劃:
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 12 | 1044 | 2 (0)| 00:0
0:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| E1 | 12 | 1044 | 2 (0)| 00:0
0:01 |
|* 2 | INDEX RANGE SCAN | E1_ENO | 12 | | 1 (0)| 00:0
0:01 |
--------------------------------------------------------------------------------
總結:由此可知:rename 表後,相應的索引會隨著表的變動同步變動。依舊是有效的。不需要我們手動修改。
我們也可以測試一下rename 表後,同義詞是否有效:
1.使用scott給e1表建立一個同義詞s_e.
create synonym s_e for e1;
2.查詢同義詞資訊
SQL> select synonym_name,table_owner,table_name from user_synonyms;
SYNONYM_NAME TABLE_OWNER
------------------------------ ------------------------------
TABLE_NAME
------------------------------
S_E SCOTT
E1
3.重新命名
SQL> rename e1 to e2;
Table renamed.
4.查詢s_e
SQL> select synonym_name,table_owner,table_name from user_synonyms;
SYNONYM_NAME TABLE_OWNER
------------------------------ ------------------------------
TABLE_NAME
------------------------------
S_E SCOTT
E1
SQL> select * from s_e;
select * from s_e
*
ERROR at line 1:
ORA-00980: synonym translation is no longer valid
5.重新更改為e1.
SQL> rename e2 to e1;
Table renamed.
SQL> select synonym_name,table_owner,table_name from user_synonyms;
SYNONYM_NAME TABLE_OWNER
------------------------------ ------------------------------
TABLE_NAME
------------------------------
S_E SCOTT
E1
6.查詢s_e
發現ok.
總結:rename表後,同義詞不會同步,因此rename表後,同義詞失效。
我們再測試一下rename表對檢視的影響:
1.在e1表的基礎上建立檢視v_e1
SQL> create view v_e1 as select empno,ename,sal from e1;
View created.
SQL> select * from v_e1;
EMPNO ENAME SAL
---------- ---------- ----------
7369 SMITH 800
7499 ALLEN 1600
7521 WARD 1250
2.rename 表
SQL> rename e1 to e2;
Table renamed.
3.驗證一下view是否可用。
EMPNO ENAME SAL
---------- ---------- ----------
7369 SMITH 800
7499 ALLEN 1600
7521 WARD 1250
EMPNO ENAME SAL
---------- ---------- ----------
7369 SMITH 800
7499 ALLEN 1600
7521 WARD 1250
SQL> select * from v_e1;
select * from v_e1
*
ERROR at line 1:
ORA-04063: view "SCOTT.V_E1" has errors
4.重新改為e1
SQL> rename e2 to e1;
Table renamed.
SQL> select * from v_e1;
EMPNO ENAME SAL
---------- ---------- ----------
7369 SMITH 800
7499 ALLEN 1600
7521 WARD 1250
5.再次驗證檢視的有效性
SQL> rename e2 to e1;
Table renamed.
SQL> select * from v_e1;
EMPNO ENAME SAL
---------- ---------- ----------
7369 SMITH 800
7499 ALLEN 1600
7521 WARD 1250
總結:rename後,view同同義詞一樣失效。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29551564/viewspace-2133675/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Oracle synonym 相關Oracle
- oracle10.2.0.4_create table_constraint_indexOracleAIIndex
- oracle index索引相關筆記OracleIndex索引筆記
- constraint和index的一點關係!AIIndex
- Oracle 10g SGA 相關的VIEWOracle 10gView
- 關於oracle synonym 的總結整理Oracle
- MySQL 的Rename Table語句MySql
- v$asm 相關的viewASMView
- Synonym_View_Materialized和Public物件ViewZed物件
- Oracle Index-organized table (IOT)概述OracleIndexZed
- MySQL rename table方法大全MySql
- Oracle模擬MySQL的show index from table命令OracleMySqlIndex
- 簡述Oracle IOT(Index Organized Table)(上)OracleIndexZed
- 簡述Oracle IOT(Index Organized Table)(下)OracleIndexZed
- 簡述Oracle IOT(Index Organized Table)(中)OracleIndexZed
- Dataguard (Standby) 相關的檢視(View)View
- VIEW和SYNONYM引起的執行計劃的異常View
- css中關於table的相關設定CSS
- oracle11g同義詞synonym建立及授權且刪除相關測試Oracle
- index table (IOT)Index
- reverse index 反轉索引相關Index索引
- 4.2.10.1 Oracle Restart 相關變數配置OracleREST變數
- Oracle - 約束、索引等相關常用操作語句Oracle索引
- Oracle中的同義詞SYNONYMOracle
- ORACLE建庫過程中自動生成的跟鎖相關的VIEWOracleView
- 關於move table和rebuild index批量操作的記錄RebuildIndex
- 影響ORACLE優化器的相關因素Oracle優化
- 關於under any table/view 許可權的解釋View
- oracle管理之 view synonym sequence(server.102 b14231)OracleViewServer
- partition table and partition indexIndex
- View工作流程-相關學習View
- 閃回之 回收站、Flashback Drop (table、index、trigger等)Index
- mysql 命令重新命名錶RENAME TABLE 句法MySql
- view the favorites table.View
- Oracle查詢優化器的相關引數Oracle優化
- Oracle優化相關的一些引數Oracle優化
- TABLE size (including table,index,lob,lobindex)Index
- 查詢使用表空間的TABLE,INDEX,INDEX SUBPARTITIONIndex