日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

oracle添加表的索引,oracle批量添加指定表前缀的索引SQL语句

發(fā)布時(shí)間:2023/12/2 数据库 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 oracle添加表的索引,oracle批量添加指定表前缀的索引SQL语句 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

oracle批量添加指定表前綴的索引SQL語(yǔ)句

需要輸入用戶名,表空間,索引個(gè)數(shù),表名前綴,過(guò)濾的表名后綴

##需要輸入用戶名,表空間,索引個(gè)數(shù),表名前綴,過(guò)濾的表名后綴

declare

--用戶名

userName varchar2(50) :='apps';

--表空間

tablespaceName varchar2(50) :='APPS_TS_TX_IDX';

--索引個(gè)數(shù)

rowN number :=3;

--過(guò)濾表名后綴,該表不添加索引,例如臨時(shí)表

tableLastName varchar2(10) :=upper('_temp');

tableLastName1 varchar2(10) :=upper('_te');

--表名前綴

cursor vdata is select t.table_name from user_tables t where t.TABLE_NAME like upper('SRM_POS_%')

and t.TABLE_NAME not like '%'||tableLastName

and t.TABLE_NAME not like '%'||tableLastName1

order by t.TABLE_NAME;

--聲明變量,記錄一行數(shù)

v vdata%rowtype;

n number;

--索引名,限制最大程度為30字符

indexName varchar2(30);

temp varchar2(27);

k number;

begin

--打開(kāi)游標(biāo)

open vdata;

--循環(huán)遍歷取數(shù)據(jù)

loop

fetch vdata into v;

exit when vdata%NOTFOUND;

select count(*) into n from user_ind_columns t where t.TABLE_NAME=upper(v.table_name);

if n

k := 1;

--dbms_output.put_line('表名:-----'||v.table_name);

select length(v.table_name) into n from dual;

if n>27 then

temp := substr(v.table_name,0,27);

else

temp := v.table_name;

end if;

indexName := temp||'_N'||k;

for cur in (select t.column_name,t.TABLE_NAME from user_tab_columns t where t.table_name = upper(v.table_name) ---表名

and instr(lower(t.column_name), 'attribute') = 0

and instr(lower(t.COLUMN_NAME),'last_update') = 0

and instr(lower(t.COLUMN_NAME),'version_num') = 0

and instr(lower(t.COLUMN_NAME),'created_by') = 0

and instr(lower(t.COLUMN_NAME),'creation_date') = 0

and t.COLUMN_ID<=rowN) loop

--判斷該字段是否已經(jīng)創(chuàng)建索引

select count(*) into n from user_ind_columns t where t.TABLE_NAME=upper(v.table_name) and t.COLUMN_NAME=upper(cur.column_name);

--未創(chuàng)建索引

if n<1 then

--判斷索引名是否存在

select count(*) into n from user_ind_columns t where t.INDEX_NAME=upper(indexName);

while n>0 loop

k := k+1;

indexName := temp||'_N'||k;

select count(*) into n from user_ind_columns t where t.INDEX_NAME=upper(indexName);

end loop;

--打印創(chuàng)建索引語(yǔ)句

dbms_output.put_line('Create Index '||userName||'.'||indexName||' ON '||userName||'.'||cur.table_name||'('||cur.column_name||')'||' tablespace '||tablespaceName||';');

k := k+1;

indexName := temp||'_N'||k;

end if;

end loop;

end if;

end loop;

--關(guān)閉游標(biāo)

close vdata;

end;

總結(jié)

以上是生活随笔為你收集整理的oracle添加表的索引,oracle批量添加指定表前缀的索引SQL语句的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。