ORACLE:dbms_redefinition之后删除临时表时出错

时间:2011-10-17 16:32:08

标签: oracle plsql oracle11g dbms-redefinition

我正在研究Oracle11g,并试图用dbms_redefinition重新定义一个表。它工作正常,但在尝试删除临时表时会抛出ORA-02449: unique/primary keys in table referenced by foreign keys错误。

我在SO中找到了一个查找引用的查询,

select table_name, constraint_name, status, owner
from all_constraints
where r_owner = 'MYSCHEMA'
and constraint_type = 'R'
and r_constraint_name in
 (
   select constraint_name from all_constraints
   where constraint_type in ('P', 'U')
   and table_name = 'INTERIM_TABLE'
   and owner = 'MYSCHEMA'
 )
order by table_name, constraint_name

给出了

table_name  |constraint_name           |status   |owner
---------------------------------------------------------
anotherTable|TMP$$_anotherTable_JOB_ID0|DISABLED|MYSCHEMA

我认为这个约束是在重新定义过程中创建的,没关系,但我也希望它必须被同一个进程删除。这是错的吗?我说,这个约束没有被删除,是正常行为的一部分吗?

使用

删除约束是安全的
alter table anotherTable
   drop constraint TMP$$_anotherTable_JOB_ID0

没有丢失数据?

提前致谢。

- 编辑 - 在考虑了这个之后,我决定删除约束以让临时表被删除。

我已经修改了查询,以便几乎自动地删除指向我想要删除的表的其他表的约束。

DECLARE 
  my_table varchar2(100);
  my_constraint varchar2(100);
BEGIN
select table_name , constraint_name into my_table,my_constraint
from all_constraints
where r_owner = 'MYSCHEMA'
and constraint_type = 'R'
and r_constraint_name in
 (
   select constraint_name from all_constraints
   where constraint_type in ('P', 'U')
   and table_name = 'INTERIM_TABLE'
   and owner = 'MYSCHEMA'
 )
order by table_name, constraint_name;
execute immediate 'ALTER TABLE '||my_table||' DROP CONSTRAINT '|| my_constraint;
END;
/
DROP TABLE MYSCHEMA.INTERIM_TABLE; 

这对我有用,但我必须注意,在我的情况下,查询只抛出一行(只有一个依赖表),所以如果你认识某人,必须修改这个以通过循环或其他方法删除许多约束。 / p>

如果某人能够找出并解释为什么该过程本身不会删除该约束(或者这是正常行为),那将是一件好事。

2 个答案:

答案 0 :(得分:7)

强迫这一点很容易:

drop table INTERIM_TABLE cascade constraints;

答案 1 :(得分:2)

这是因为在运行SYNC和FINISH重新定义过程时,原始和新约束STATUS会发生变化,如下所示。您可以在禁用模式下从子表创建临时表的fkeys。当我们完成redef时,我们禁用旧的fkeys并启用新闻(无需验证)。考虑:

create table t NOLOGGING
as
select * from all_objects;


alter table t add constraint t_pk primary key(object_id);

create table t1( x references t );
create table t2( y references t );


insert into t1 select object_id from t where rownum <= 100;

100 rows created.


insert into t2 select object_id from t where rownum <= 100;
100 rows created.



create table t_interim similar to t table.

alter table t1 add constraint t1_new_fk foreign key(x) references t_interim disable;

alter table t2 add constraint t2_new_fk foreign key(y) references t_interim disable;

select constraint_name, status from user_constraints where constraint_type = 'R';

CONSTRAINT_NAME                STATUS
------------------------------ --------
SYS_C004733                    ENABLED     <<<== original constraint
T1_NEW_FK                      DISABLED
SYS_C004734                    ENABLED
T2_NEW_FK                      DISABLED


begin
dbms_redefinition.sync_interim_table( user, 'T', 'T_INTERIM' );
end;
/

PL/SQL procedure successfully completed.

begin
dbms_redefinition.finish_redef_table( user, 'T', 'T_INTERIM' );
end;
/

PL/SQL procedure successfully completed.


select constraint_name, status from user_constraints where constraint_type = 'R';

CONSTRAINT_NAME                STATUS
------------------------------ --------
SYS_C004733                    DISABLED       <<< flip flopped the status
T1_NEW_FK                      ENABLED
SYS_C004734                    DISABLED
T2_NEW_FK                      ENABLED

drop table t_interim cascade constraints;

select constraint_name, status from user_constraints where 
constraint_type = 'R';

CONSTRAINT_NAME                STATUS
------------------------------ --------
T1_NEW_FK                      ENABLED
T2_NEW_FK                      ENABLED