1. 程式人生 > >Oracle中查詢索引名稱,批量修改索引名稱語句

Oracle中查詢索引名稱,批量修改索引名稱語句

   在Oralce資料庫資料優化過程中,對源資料表處理,原則上是做更名備份,作為被查或回退使用,所以,有修改資料表名後重新建表的操作,這樣,往往也需要修改索引、主鍵、外來鍵名稱,方便重建,為了方便、快速生成處理資料指令碼,採用批量處理方式,如第4、5段例句,拼接字串,生成批量處理指令碼。
一、依據DBA檢視查詢,涉及到的檢視有:user_ind_columns、user_indexes、user_constraints
1、查詢索引資訊例句
select t.*,i.index_type from user_ind_columns t,user_indexesi
where t.index_name = i.index_name and t.table_name = i.table_nameand
t.table_name in('WORKFLOW_INSTANCE','WORKFLOW_INSTANCE_TRANSLOG','PROCESS_INSTANCE','PROCESS_INSTANCE_DATA',
'PROCESS_ACTIVITY','MESSAGE','MESSAGE_TRACK','NOTIFICATION_SEARCH_DATA')
2、查詢主鍵資訊例句
select cu.* from user_cons_columns cu, user_constraints au
where cu.constraint_name = au.constraint_name andau.constraint_type = 'P' and
au.table_name in('WORKFLOW_INSTANCE','WORKFLOW_INSTANCE_TRANSLOG','PROCESS_INSTANCE','PROCESS_INSTANCE_DATA',
'PROCESS_ACTIVITY','MESSAGE','MESSAGE_TRACK','NOTIFICATION_SEARCH_DATA')
3、查詢外來鍵關係資訊例句
select * from user_constraints c where c.constraint_type = 'R'and
c.table_name in('WORKFLOW_INSTANCE','WORKFLOW_INSTANCE_TRANSLOG','PROCESS_INSTANCE','PROCESS_INSTANCE_DATA',
'PROCESS_ACTIVITY','MESSAGE','MESSAGE_TRACK','NOTIFICATION_SEARCH_DATA')
二、拼接字串,生成批量處理語句
4、批量修改索引名稱
   批量修改就是在索引名稱後面追加字元,用以區分。
select
'alter index '||i.index_name||' rename to'||substr(i.index_name,0,decode(sign(25-length(i.index_name)),'-1',25,length(i.index_name)))||'tmp1;',
i.table_name,i.tablespace_name
 from user_indexes i
where i.table_name in ('DOC_DOCMAIN','DOC_WF_OPINION')
5、批量修改主鍵名稱
select
'alter table '||c.table_name||' rename constraint'||c.constraint_name||' to'||substr(c.constraint_name,0,decode(sign(25-length(c.constraint_name)),'-1',25,length(c.constraint_name)))||'tmp1;',
c.table_name from user_constraints c where c.constraint_type in('R','P') and
c.table_name in('TASK_LIST','TASK_LIST_WAIT','WORKFLOW_INSTANCE_TRANSLOG')