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

表已删除。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值