ORA-02292“违反完整性约束 -

时间:2016-01-09 01:33:41

标签: sql oracle oracle11g

我有一个应用程序应该从订单表中删除订单,并将删除订单的详细信息插入到两个表中:一个包含订单的详细信息,第二个包含详细信息。问题来自表DELETE_ORDERS_D,因为它抛出以下异常:

        02292. 00000 - "integrity constraint (%s.%s) violated - child record  
                        found"
        *Cause:    attempted to delete a parent key value that had a foreign
                        dependency.

订单

      create table orders (
       ono      number(5) not null primary key,
       cno      number(5) references customers,
       eno      number(4) references employees,
       received date,
       shipped  date);

订单详情

     create table odetails (
     ono      number(5) not null references orders,
     pno      number(5) not null references parts,
     qty      integer check(qty > 0),
     primary key (ono,pno));

零件

     create table parts(
     pno      number(5) not null primary key,
     pname    varchar2(30),
     qoh      integer check(qoh >= 0),
     price    number(6,2) check(price >= 0.0),
     olevel   integer);

示例数据

     insert into orders values
       (1020,1111,1000,'10-DEC-11','12-DEC-11');
     insert into orders values
       (1021,1111,1000,'12-JAN-12','15-JAN-12');
     insert into orders values
       (1022,2222,1001,'13-FEB-12','20-FEB-12');
     insert into orders values
       (1023,3333,1000,'12-MAR-12',null);
     insert into odetails values
       (1020,10506,1);
     insert into odetails values
       (1020,10507,1);
     insert into odetails values
       (1020,10508,2);
     insert into odetails values
       (1020,10509,3);
     insert into odetails values
       (1021,10601,4);
     insert into odetails values
       (1022,10601,1);
     insert into odetails values
       (1022,10701,1);
     insert into odetails values
       (1023,10800,1);
     insert into odetails values
       (1023,10900,1);
     insert into parts values
       (10506,'Land Before Time I',200,19.99,20);
    insert into parts values
       (10507,'Land Before Time II',156,19.99,20);
    insert into parts values
       (10508,'Land Before Time III',190,19.99,20); 
    insert into parts values
       (10509,'Land Before Time IV',60,19.99,20);
    insert into parts values
       (10601,'Sleeping Beauty',300,24.99,20);
    insert into parts values
       (10701,'When Harry Met Sally',120,19.99,30);
    insert into parts values
       (10800,'Dirty Harry',140,14.99,30);
    insert into parts values
       (10900,'Dr. Zhivago',100,24.99,30);

表格生成错误

    CREATE TABLE DELETE_ORDERS_D (
       ONO      number(5) not null references orders,
       PNO      number(5) not null references parts,
       QTY     integer check(qty > 0),
       CONSTRAINT DEL_ODETAILS_pk PRIMARY KEY (ONO,PNO));

约束优化问题

select * from all_constraints
     where owner = 'SCOTT'
     and constraint_name = 'SYS_C007539';


          CONSTRAINT_TYPE           TABLE_NAME                
        ------------------        -----------------
                R                  DELETE_ORDERS_D

请你帮我解决一下!

3 个答案:

答案 0 :(得分:1)

诸如DELETE_ORDERS_D之类的历史表不能具有引用实际数据表的外键约束(在本例中为ORDERS和PARTS),因为当您删除ORDERS和PARTS中的相应行时,它将导致错误看到。删除DELETE_ORDERS_D.ONO和PNO上的外键约束。

祝你好运。

答案 1 :(得分:0)

如果您收到此类错误,请使用以下查询找到必须先删除的表格:

 SELECT * FROM dba_constraints
 WHERE constraint_name = 'FK_WMINVN_LOCNHDR'; 

答案 2 :(得分:0)

正如之前所说的bob jarvis历史表不应该让反对者参考实际数据。

但是要解决问题,您可以编写on delete cascade以及外键约束。每当您在父表上发出删除记录时,这将自动删除与之关联的所有子记录

更改odetails上的外键约束。但为此,您需要删除odetails表中的现有外键约束。

alter table odetails add constraint odetails_ono_fk
foreign key (ono) references orders(ono) on delete cascade;