首页|资讯|互联网|电信|硬件|软件|情报|产经|博客|家庭|商用电脑|游戏|评测|学院|下载|网络通信|方案应用|搜索
移动计算|商用软件|外包|开源|中间件|企业|IT经理|发烧友|程序员|IT女性|学生|老板|笔记本|手机|台式机|数码|论坛
程序员

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

2008-02-29 10:32 作者:陈雅诗 来源:赛迪网
[摘要] 如何对系统默认的约束名和索引名重命名?
[关键字] 约束名 索引名 重命名
  问题:如何对系统默认的约束名和索引名重命名?

  解决方法:

  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;

【责任编辑 陈东方】

关键词: 约束名, 索引名, 重命名,
  • 我要留言
关于eNet | 广告服务 | 版权声明 | 加入eNet | 联系我们 | 建议/投诉 | 网站导航 | 加入收藏

网站合作、内容监督、商务咨询、投诉建议:010-65245588
合作建议:hezuo@mail.enet.com.cn
Copyright © 1998--2008 硅谷动力公司版权所有 京ICP证000044号

京ICP证000044号