正如标题所述,我需要重命名具有名称的主键约束 sys_ 000545,并且索引名称使用相同的名称。我如何在oracle pl / sql中实现这一点。这个名字是随机的。
我尝试将alter index与选择索引名称的子查询一起使用,但是oracle不允许这样做。
编辑 我需要将此重命名作为更新脚本的一部分
答案 0 :(得分:2)
SQL> create table test (id number primary key);
Table created.
SQL> select constraint_name from user_constraints where table_name = 'TEST';
CONSTRAINT_NAME
------------------------------
SYS_C009641
SQL> alter table test rename constraint sys_c009641 to pk_test;
Table altered.
SQL> select constraint_name from user_constraints where table_name = 'TEST';
CONSTRAINT_NAME
------------------------------
PK_TEST
SQL>
SQL> select index_name from user_indexes where table_name = 'TEST';
INDEX_NAME
------------------------------
SYS_C009641
SQL> alter index sys_c009641 rename to pk_test;
Index altered.
SQL> select index_name from user_indexes where table_name = 'TEST';
INDEX_NAME
------------------------------
PK_TEST
SQL>
[编辑:脚本需要动态SQL]
SQL> drop table test;
Table dropped.
SQL> create table test (id number primary key);
Table created.
SQL> select constraint_name from user_constraints where table_name = 'TEST';
CONSTRAINT_NAME
------------------------------
SYS_C009643
SQL> declare
2 l_table_name user_tables.table_name%type := 'TEST';
3 l_pk user_constraints.constraint_name%type;
4 begin
5 select constraint_name
6 into l_pk
7 from user_constraints
8 where table_name = l_table_name
9 and constraint_type = 'P';
10
11 execute immediate 'alter table ' || l_table_name || ' rename constraint ' ||
12 l_pk || ' to pk_' || l_table_name;
13 end;
14 /
PL/SQL procedure successfully completed.
SQL> select constraint_name from user_constraints where table_name = 'TEST';
CONSTRAINT_NAME
------------------------------
PK_TEST
SQL>