ORACLE:在dbms_重新定义后删除临时表时出错
我正在开发Oracle11g,并试图用它重新定义一个表。它工作正常,但当尝试删除临时表时,抛出ORA-02449:外键引用的表中的唯一/主键错误 我在这里找到了一个查询来查找引用ORACLE:在dbms_重新定义后删除临时表时出错,oracle,plsql,oracle11g,dbms-redefinition,Oracle,Plsql,Oracle11g,Dbms Redefinition,我正在开发Oracle11g,并试图用它重新定义一个表。它工作正常,但当尝试删除临时表时,抛出ORA-02449:外键引用的表中的唯一/主键错误 我在这里找到了一个查询来查找引用 select table_name, constraint_name, status, owner from all_constraints where r_owner = 'MYSCHEMA' and constraint_type = 'R' and r_constraint_name in ( selec
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;
这对我来说很有效,但我必须注意,在我的例子中,查询只抛出一行,只抛出一个依赖表,所以必须对其进行修改,以便通过循环或其他方法(如果您认识某个人)删除许多约束
如果有人能够找出并解释为什么该约束没有被流程本身删除,或者这是一种正常行为,那就太好了。强制执行这一点很容易:
drop table INTERIM_TABLE cascade constraints;
这是因为在运行同步并完成重新定义过程时,原始约束和新约束状态会发生更改,如下所示。您可以在禁用模式下从子表创建临时表的FKEY。当我们完成redef时,我们禁用旧FKEY并启用新闻,而无需验证。考虑:
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
这将删除那些指向我的临时临时表的表中的行,不是吗?这不是我要找的,因为这些行的外键现在也指向我重新定义的表。级联意味着它将删除引用临时表的任何约束