0

I have an application that should delete orders from the order table and insert the details of the deleted order into two tables: One contains the details of the order and the second one contains the details of the odetails. The problem comes from the table DELETE_ORDERS_D as it throws the following exception:

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

Orders

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

Order details

     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));

Parts

     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);

Sample data

     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);

Table generates the error is bellow

    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));

Constraint optimization problem

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


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

Could you please help me to solve this!

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • Can you show actually the delete command you use? – Jorge Campos Jan 09 '16 at 01:37
  • I pass the order number to a procedure that deletes orders and orders' details and insert their details into two different tables and one of them is Delete_orders_D. The procedure is too long to post it with a question! –  Jan 09 '16 at 01:42
  • 2
    Well without the code that actually raises this error no one can give an answer other than a guess. Although the error is pretty straightforward, somewhere in your procedure you are trying to delete a registry on orders or parts that is on DELETE_ORDERS_D, – Jorge Campos Jan 09 '16 at 01:46
  • Hi Jorge and I appreciate your help, would it be good if we can open a chat then I can send you the procedure? However, If I post the procedure it won't look good as it is too long! –  Jan 09 '16 at 11:49
  • No need for that (your comment on Bob's answer gave a clue). I would suggest you to run a select command on the DELETE_ORDERS_D table where ONO in (1020,1021,1022,1023) and see if there is registries there. If yes this is your problem, you have to look into your procedure and do this check before delete on order table. Note that i suggested those IDs because of your sample data, it MAY be others. I suggest you to look into your procedure and get the lines where the delete on orders table happens and add it here on your question, with few lines before and few after. No need the entire code. – Jorge Campos Jan 09 '16 at 12:06
  • Thank you for getting back to me, I did that in the procedure, Inserting into two tables one of which is DELETE_ORDERS_D and then did the delete command on ORDERS and ODETAILS respectively. –  Jan 09 '16 at 12:10
  • Then there is your problem, you INSERTED lines into DELETE_ORDERS_D that is related woth orders table. The database won't let you just delete that (related registry) from orders because there is a child (to be more clear) of it on the DELETE_ORDERS_D. That is what a constraint is for, they avoid you to have inconsistent data. I will try to find some especific text on it on the web and post the link so you can understand better. – Jorge Campos Jan 09 '16 at 12:17
  • Here is a good one: http://dba.stackexchange.com/questions/44956/good-explanation-of-cascade-on-delete-update-behavior – Jorge Campos Jan 09 '16 at 12:21
  • Good resource! I think I solved the issue according to Bob, but need to do more tests to check all procedures! –  Jan 09 '16 at 12:30

3 Answers3

1

History tables such as DELETE_ORDERS_D can't have foreign key constraints which reference the actual data tables (ORDERS and PARTS, in this case) because when you delete the corresponding rows in ORDERS and PARTS it will cause precisely the error you're seeing. Drop the foreign key constraints on DELETE_ORDERS_D.ONO and PNO.

Best of luck.

  • Thank you for your post. The delete will be only on ORDERS and ODETAILS tables . I will make sure to alter the table and delete the FKs as you suggested. However, Do I need to keep the primary keys as (ono, pno) for the DELETE_ORDERS_D? –  Jan 09 '16 at 11:52
  • I would not suggest such a thing, if the table was designed to have that constraint it would be to maintain consistancy, deleting it will leave inconsistent data into the database. – Jorge Campos Jan 09 '16 at 12:05
  • I am so sorry I misunderstood what you said! Could you please show me practically what do you cause I am lost ! –  Jan 09 '16 at 12:07
  • If (ONO, PNO) will always be unique then keep the primary key of (ONO, PNO). @JorgeCampos - my understanding of the situation is the OP is using the DELETE_ORDERS_D table to keep track of rows which have been deleted from the ORDERS, PARTS, and ODETAILS tables. Since the purpose of the DELETE_ORDERS_D table is to keep track of data which no longer exists in the main tables, you can't very well have a foreign key constraint back to ORDERS and PARTS since the purpose of DELETE_ORDERS_D is to record items which have been removed from those tables. Thanks. – Bob Jarvis - Слава Україні Jan 09 '16 at 13:25
  • @BobJarvis You are in fact absolutelly right, I missed that point about the delete record tracking, I was focused on the error message itselft. I will Upvote your answer also. A good thing to this would be adding the alter table delete constraint command so he can understand and accept this as a proper answer. – Jorge Campos Jan 09 '16 at 14:23
  • 1
    @JorgeCampos - because the constraints are not named in the `CREATE TABLE` statements shown in the question, and because I don't happen to have an Oracle instance handy to test on, I don't feel confident that I could create an `ALTER TABLE` which would actually drop the constraints correctly. I know I could query `ALL_CONSTRAINTS` and `ALL_CONS_COLUMNS` to determine the constraint name, but as I say without being able to test I doubt I'd get it right. Thanks. – Bob Jarvis - Слава Україні Jan 09 '16 at 14:46
  • Good point. @Dodi look at [here](http://stackoverflow.com/a/11880294/460557) to see how to find out a constraint name. – Jorge Campos Jan 09 '16 at 17:03
  • Thank you guys for time and efforts of explain and clarifying the about this issue. I have removed the FK from the DELETE_ORDERS_D and the procedure worked! However, I have another issue with another table called ORDERS_RECORDS and this time an error with the primay key and that is the ONO for this table! So shall I remove the primary key (ono)? –  Jan 09 '16 at 20:37
  • That depends. Is ONO guaranteed to be unique on this table? If so, then it should be the primary key, and there's apparently a logical problem causing multiple rows to be inserted. If a single ONO can appear multiple times in the table then ONO can't be the primary key. You may need to create an artificial key using a sequence in this case. Best of luck. – Bob Jarvis - Слава Україні Jan 10 '16 at 02:34
  • Thank you very much for your reply. ONO is a unique on this table and it is a Primary key. I made sure that there weren't any duplications on the inserted ONO ! The table accept the insert command on values for some procedures, but it throws errors for some even though I leave the table empty to avoid replications/duplications! I am really confused and had to work hours and hours last night but couldn't figure out this behaviour! –  Jan 10 '16 at 13:24
  • Hi guys, just a quick question! How can I create a sequence to replace the ONO on DELETED_ORDERS_D ? –  Jan 11 '16 at 19:39
  • Hi, Still struggling with this problem! However, I have generated an entity-relationship (ER) diagram within Oracle developer as it looks like the Star wars movie to me! Is it ok If I can send you an image of this? –  Jan 12 '16 at 00:21
1

If you are getting such errors then find the table which has to be deleted first by using below query:

 SELECT * FROM dba_constraints
 WHERE constraint_name = 'FK_WMINVN_LOCNHDR'; 
Just code
  • 13,553
  • 10
  • 51
  • 93
Arun
  • 11
  • 5
0

As said previously by bob jarvis history tables shouldnt be having constriants referencing to the actual data.

However to resolve you issue you can write on delete cascade along with your foreign key constraints. Whenever you will issue a delete a record on the parent table this will automatically delete all the child record associated with it

Alter the foreign key constraints on the odetails. But for that you need to drop the existing foreign key constraints in odetails table.

alter table odetails add constraint odetails_ono_fk
foreign key (ono) references orders(ono) on delete cascade;
Indiecoder
  • 186
  • 3
  • 17
  • Thank you for your answer. I can't alter the table Odetails as I added the DELETE_ORDERS_D prior to the schema created! This will change the modelling relationships among the table ! –  Jan 09 '16 at 11:45
  • @Dawn: if I'm reading the question correctly the purpose of DELETE_ORDERS_D is to maintain a record of the orders and parts which have been deleted, in which case a delete cascade would delete the records OP wishes to save. Perhaps I'm misinterpreting this, but that's my understanding. Thanks. – Bob Jarvis - Слава Україні Jan 09 '16 at 13:32