国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

對系統默認的約束名和索引名進行重命名

2024-07-21 02:41:54
字體:
來源:轉載
供稿:網友

對系統默認的約束名和索引名進行重命名的存儲過程示例:


create or replace PRocedure proc_rename_constraint
as
--查找用戶所有表的游標
cursor cur_table is select table_name from user_tables;

--查找某張表所有約束的游標
cursor cur_cons (c_table varchar2) is select
c.constraint_name,c.constraint_type,
c.search_condition from user_constraints c
where c.table_name=c_table
and substr(c.constraint_name,2,2) <> 'K_';

--查找某個約束所有字段的游標
cursor cur_columns(c_cons varchar2) is select
column_name from user_cons_columns
where constraint_name=c_cons;

--存儲修改后的約束名
v_new_cons_name varchar2(100);

--存儲修改約束名的SQL語句
v_sql varchar2(150);

--存儲重名的個數
cnt number :=1;

--存儲新索引名的個數
n_idx number;

--存儲新約束名的個數
n_con number;

begin
--循環取表名
for cur_ltable in cur_table loop

--循環取約束名
for cur_lcons in cur_cons(cur_ltable.table_name) loop

v_new_cons_name :=null;

--循環取字段名
for cur_lcolumns in cur_columns(cur_lcons.constraint_name) loop
v_new_cons_name := v_new_cons_name || cur_lcolumns.column_name;
end loop;
v_new_cons_name := replace(v_new_cons_name,'_','');
v_new_cons_name := cur_ltable.table_name ||'_' || v_new_cons_name;

if cur_lcons.constraint_type='P' then
v_new_cons_name := 'PK_' || v_new_cons_name;
elsif cur_lcons.constraint_type='R' then
v_new_cons_name := 'FK_' || v_new_cons_name;
elsif cur_lcons.constraint_type='U' then
v_new_cons_name := 'UK_' || v_new_cons_name;
elsif cur_lcons.constraint_type='C'
and instr(cur_lcons.search_condition,'IS NOT NULL') > 0 then
v_new_cons_name := 'CK_' || v_new_cons_name || 'NOTNULL' ;
elsif cur_lcons.constraint_type='C'
and instr(cur_lcons.search_condition,'IS NOT NULL') = 0
and cur_lcons.search_condition is not null then
v_new_cons_name := 'CK_' || v_new_cons_name;
end if;

--約束名如果超過30個字符的處理
if length(v_new_cons_name) > 29 then
v_new_cons_name := substr(v_new_cons_name,1,15)
|| substr(v_new_cons_name,-14);
end if;

--查找系統里是否有新的約束名
select count(*) into n_con from user_constraints
where constraint_name=v_new_cons_name;
select count(*) into n_idx from user_indexes where
index_name=v_new_cons_name;
if n_con > 0 or n_idx > 0 then
v_new_cons_name := v_new_cons_name || to_char(cnt);
cnt := cnt +1;
end if;


--對由主鍵和唯一鍵創建的索引進行改名,改后的名字為新的約束名
if cur_lcons.constraint_type='P' or cur_lcons.constraint_type='U' then
select count(*) into n_con from user_indexes where
index_name=cur_lcons.constraint_name;
if n_con = 1 then
v_sql := 'alter index ' || cur_lcons.constraint_name
|| ' rename to ' || v_new_cons_name;
execute immediate v_sql;
end if;
end if;

--對約束改名
v_sql := 'alter table ' || cur_ltable.table_name || ' rename constraint ';
v_sql := v_sql || cur_lcons.constraint_name || ' to ' || v_new_cons_name;
execute immediate v_sql;

end loop;

end loop;

end;


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 祥云县| 尼玛县| 甘肃省| 汝南县| 长宁县| 罗山县| 电白县| 阿拉善右旗| 府谷县| 乡城县| 珲春市| 灌南县| 改则县| 定兴县| 依兰县| 安溪县| 武冈市| 关岭| 隆化县| 吉木萨尔县| 蓬莱市| 永川市| 布尔津县| 铜川市| 齐齐哈尔市| 邵阳县| 精河县| 运城市| 吴川市| 乌鲁木齐市| 马公市| 乡城县| 伊宁市| 色达县| 荣昌县| 闽侯县| 肃南| 大冶市| 六枝特区| 航空| 双峰县|