暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

ORA-02449 unique-primary keys in table referenced by foreign keys

原创 不吃草的牛_Nick 2023-04-14
619
[oracle@db02 ~]$ oerr ora 02449
02449, 00000, "unique/primary keys in table referenced by foreign keys"
// *Cause: An attempt was made to drop a table with unique or
//         primary keys referenced by foreign keys in another table.
// *Action: Before performing the above operations the table, drop the
//          foreign key constraints in other tables. You can see what
//          constraints are referencing a table by issuing the following
//          command:
//          SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME = "tabnam";

1.检查哪些表的外键引用了要删除的表的唯一/主键.B是父, A是子

select A.*
  from user_constraints A, user_constraints B
 WHERE b.table_name = upper('TEST')
   and a.constraint_type = 'R'
   and a.r_constraint_name = b.constraint_name;

2.生成所有引用要删除表的外键的创建语句.
select 'select dbms_metadata.get_ddl(''REF_CONSTRAINT'',''' ||
       A.CONSTRAINT_NAME || ''') FROM DUAL;'
  from user_constraints A, user_constraints B
 WHERE b.table_name = upper('TEST')
   and a.constraint_type = 'R'
   and a.r_constraint_name = b.constraint_name;

3.执行步骤2中产生的SQL语句,获取外键定义.
set line 122
set pagesize 9999
set long 99999
select dbms_metadata.get_ddl('REF_CONSTRAINT','FK_RF_TEST') FROM DUAL;

4.删除表.
drop table test cascade constraints;

5.删除并重建表.

6.建立因创建该表所删除的外键约束.
执行步骤3中获取到的sql语句.

7.检查各外键是否得到恢复,系统是否正常.
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论