2011-10-17 3 views
2

Oracle11g에서 작업 중이며 dbms_redefinition으로 테이블을 다시 정의하려고합니다. 그것은 잘 작동하지만 임시 테이블을 삭제하려고 할 때 ORA-02449: unique/primary keys in table referenced by foreign keys 오류가 발생합니다.ORACLE : dbms_redefinition 이후 중간 테이블 삭제 오류

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

을 제공

내가 바로 여기에 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 

내가이 제약은 재정의 과정에서 생성 된 것으로 가정, 그 괜찮아요,하지만 또한 동일한 프로세스에 의해 삭제되어야한다고 예상했다. 잘못인가? 내가 말하길,이 제약이 삭제되지 않은 정상적인 행동의 일부입니까?

그것은 단지 데이터의 손실없이

alter table anotherTable 
    drop constraint TMP$$_anotherTable_JOB_ID0 

와 제약 조건을 삭제하는 것이 안전?

미리 감사드립니다.

- EDIT - 이 문제를 생각한 후에 임시 테이블을 삭제하도록 제약 조건을 삭제하기로 결정했습니다.

거의 자동으로 삭제하려는 테이블을 가리키는 다른 테이블의 제약 조건을 삭제하도록 쿼리를 수정했습니다.

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; 

이 나를 위해 일했다,하지만 난 그게 내 경우에는 해당 쿼리가 하나의 행 (하나의 종속 테이블)을 발생주의해야한다, 그래서 이것은 루프 또는 다른 방법 당신이 경우에 의해 많은 제약을 드롭하도록 수정해야 누군가를 아십시오.

누군가가 해당 제약 조건이 프로세스 자체에서 삭제되지 않은 이유 (또는 정상적인 동작 인 경우)를 파악하고 설명 할 수 있다면 좋을 것입니다.

답변

7

그것은이를 강제로 쉽게 충분 :

drop table INTERIM_TABLE cascade constraints; 
+0

임시 임시 테이블을 가리키는 테이블의 행을 삭제합니다 (아니오). 그 행의 외래 키가 이제 내 재정의 테이블을 가리키기 때문에 내가 찾고있는 것이 아닙니다. – Keber

+1

아니요. 'CASCADE'는 climfing에 대해 interim_table –

+0

이 @a_horse_with_no_name을 감사하는 제약 조건을 삭제한다는 것을 의미합니다. – Keber

2

원본과 새로운 제약 조건의 상태 변화가 아래와 같이 SYNC 마무리 재정의 절차를 실행하는 동안 때문이다. 비활성화 된 모드에서 하위 테이블의 임시 테이블에 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