当前位置:数据库 > Oracle >>

oracle在操作主、外键需要注意的ORA-02449、ORA-02298

oracle在操作主、外键需要注意的ORA-02449、ORA-02298解决
 
      做的项目很少加外键,今天碰到了有外键的系统刷数据有两个问题,特记录一下。
 
SQL> drop table test1 purge;
 
表已删除。
SQL> drop table test2 purge;
表已删除。
 
SQL> create table test1 as select * from dba_objects where object_id is not null;
表已创建。
SQL> alter table test1
      add constraint pk_t1_object_id primary key(OBJECT_ID);
表已更改。
SQL> create table test2 as select * from test1;
表已创建。
SQL> alter table TEST2
  2    add constraint fk_t2_object_id foreign key (OBJECT_ID)
  3    references test1 (OBJECT_ID);
表已更改。
SQL> drop table test1;
drop table test1
           *
第 1 行出现错误:
ORA-02449: 表中的唯一/主键被外键引用
 
SQL> drop table test1 cascade  constraint;
 
 
 
 
 
SQL> drop table test1 purge;
drop table test1 purge
           *
第 1 行出现错误:
ORA-00942: 表或视图不存在
SQL> drop table test2 purge;
表已删除。
SQL> create table test1 as select * from dba_objects where object_id is not null;
表已创建。
 
SQL> alter table test1
      add constraint pk_t1_object_id primary key(OBJECT_ID);
表已更改。
SQL> create table test2 as select * from dba_objects;
表已创建。
 
SQL> update test2 set object_id = 99999 where object_id is null;
已更新 1 行。
SQL> commit;
提交完成。
 
SQL> alter table TEST2
      add constraint fk_t2_object_id foreign key (OBJECT_ID)
      references test1 (OBJECT_ID);
  add constraint fk_t2_object_id foreign key (OBJECT_ID)
                 *
第 2 行出现错误:
ORA-02298: 无法验证 (TEST.FK_T2_OBJECT_ID) - 未找到父项关键字
 
找出从表的记录不在主键中
SQL> select count(*) from test2 t2
  2   where not exists
  3   (select 1 from test1 t1 where t1.object_id = t2.object_id);
  COUNT(*)
----------
         3
 
表已删除。
Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,