萤火虫网络
0531-85551080
3G通信资讯3G中国 3G国际 3G终端 3G前沿 4G技术   3G市场动态3G话题 3G项目 3G数据 3G资料 3G技术
3G人才培训3G培训 3G认证 3G人才 3G企业 3G专家   3G网络媒体3G会展 3G增值 3G网络 3G论坛 3G媒体
  欢迎您来到3G中国资讯网!
     首页 > 3G市场动态 > 3G数据  3G论坛 3G认证   

如何对系统默认的约束名和索引名重命名
( http://3g.yhc360.com   2008-2-29 10:42:53 )
 

问题:如何对系统默认的约束名和索引名重命名?

解决方法:

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;

Google
 
  最新资讯排行
顶级智能新机HTCTouchPro
500万像素渐普及LG推出800万
山寨手机入侵PPC琦基i9高配智能
江宁打造中国首个"无线谷"核心圈层
3G市场竞争门槛提高价格战不再是有
互联网巨头布局“无线搜索”迎3G时
Wi-Fi联盟让无线语音成为可能
中东和北非地区的运营商实现基础设施
伊朗手机生产厂家欢迎降低手机进口关
中国电信将自建终端公司负责移动终端
  最新培训信息
基础课程在线培训1-1
基础课程在线培训1-2
基础课程在线培训1-4
基础课程在线培训3-2
基础课程在线培训1-3
基础课程在线培训2-1
基础课程在线培训4-1
基础课程在线培训2-2
基础课程在线培训3-1

您在本站有任何疑难问题请联系贝贝QQ:329683229 晶晶QQ:292701913 欢欢QQ:531069058 迎迎QQ:627254210我们的客服人员会即时为您解!
CopyRight © 2007 3G.yhc360.com, All Rights Reserved
鲁ICP备07021078号
萤火虫旗下网站