首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ORACLE:在dbms_redefinition之后删除临时表时出错

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

Stack Overflow用户
提问于 2011-10-18 00:32:08
回答 2查看 3.5K关注 0票数 3

我正在开发Oracle11g,并尝试用dbms_redefinition重新定义一个表。它工作得很好,但是当尝试删除临时表时,它抛出了一个ORA-02449: unique/primary keys in table referenced by foreign keys错误。

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

代码语言:javascript
复制
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

这给了我们

代码语言:javascript
复制
table_name  |constraint_name           |status   |owner
---------------------------------------------------------
anotherTable|TMP$$_anotherTable_JOB_ID0|DISABLED|MYSCHEMA

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

使用以下命令删除约束是安全的

代码语言:javascript
复制
alter table anotherTable
   drop constraint TMP$$_anotherTable_JOB_ID0

而不会丢失数据?

提前谢谢。

-- EDIT --在考虑了这个问题之后,我决定删除这个约束,以便删除临时表。

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

代码语言:javascript
复制
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; 

这对我很有效,但我必须注意,在我的示例中,该查询只抛出一行(只有一个依赖表),因此如果您认识某人,则必须通过循环或另一种方法对其进行修改以删除许多约束。

如果有人能找出并解释为什么该约束没有被进程本身删除(或者如果这是正常行为),那就更好了。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-10-18 03:42:18

强制执行以下操作非常简单:

代码语言:javascript
复制
drop table INTERIM_TABLE cascade constraints;
票数 7
EN

Stack Overflow用户

发布于 2013-11-15 05:57:28

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

代码语言:javascript
复制
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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7796743

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档