网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>数据库技术>>Oracle技术>>Oracle开发>查看文档  
  如何对系统默认的约束名和索引名重命名     
  文章作者:未知  文章来源:赛迪网技术社区  
  查看:122次  录入:管理员--2008-02-29  
 

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

解决方法:

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;

 
 
上一篇: 怎样才能删除所有表中的"procedure"    下一篇: 轻松掌握"lag"与"lead"分析函?的用法
  相关文档
轻松掌握"0racle-07445"错误的解决步骤 03-03
深入讲解数据库中user和schema的关系 04-08
讲解使用文字下标简化“pl/sql”的技巧 01-24
细化解析:时间再次停止运行的解决方法 11-15
在Windows系统下完全卸载Oracle的好方法 09-29
解析:如何正确的更改监听器日志文件名称 11-15
Oracle数据库SPFILE文件特点及具体操作 06-03
个人经验:怎样杀死"oracle"的死锁进程 02-01
Oracle Text 的体系结构及其使用 09-29
oracle利用传输表空间导出导入数据的步骤 (1) 03-19
轻松掌握"lag"与"lead"分析函?的用法 02-29
详细讲解oracle数据库中不同的恢复特性 04-11
在Oracle9i中Oracle DATA计算时间差 04-23
“顺序事务”与“只读事务”的实际应用 03-06
oracle failsafe与rac两者之间的不同之处 05-13
用VC++开发Oracle数据库应用程序详解 04-11
Oracle中有没有系统数据库? 09-29
初学者必读:行连接和行迁移的不同之处 07-30
如何使用oracle提供的字符扫描工具csscan 03-06
通过幸存的oracle文件修复oracle9i数据 03-17
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
厦门(总部):13616026886 福州:0591-87655121
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息