Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8350923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:28:10+00:00 2026-06-09T08:28:10+00:00

DECLARE cursor curs is select * from starting; appleId number; bananaId number; BEGIN for

  • 0
DECLARE    
 cursor curs is select * from starting;  
 appleId number;      
 bananaId number;    
BEGIN  
    for foo in curs  
    LOOP     
        insert into apple (id, weight)  
        values(1,1)  
        returning id into appleId;  
        insert into banana(id,weight)  
        values(1,3)  
        returning id into bananaId;  
        insert into apple_banana_lookup  
        values(1,appleId,bananaId);  
    END LOOP;  
    COMMIT;   
END;  

The above code results in a foreign key constraint violation. Claiming that the ID field in Apple does not yet exist. My question is how do I make this code function above and have the apple_banana_lookup table successfully persist the keys referenced in appleId and bananaId . As an added I want to avoid having to commit after every insert into apple and banana on account of there will be ~200 millions records in a given cursor.

UPDATE

Schema declaration:

    create table apple  
    ( 
        id number(20,0) not null, 
        weight number (20,0)   
    );

    create table banana  
    (   
         id number(20,0) not null, 
         weight number(20,0)  
    )  ;

    create table apple_banana_lookup  
( 
      id number(20,0) not null,
      appleId number(20,0) not null,
      bananaId number(20,0) not null   
      CONSTRAINT "apple_fk" foreign key ("appleId")  
      REFERENCES "apple" ("id"),  
     CONSTRAINT "banana_fk" foreign key ("bananaId")  
      REFERENCES "banana" ("id"),
);  

ERROR MESSAGE:

ORA-02291: integrity constraint  
parent key not found
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T08:28:12+00:00Added an answer on June 9, 2026 at 8:28 am

    Something appears to be left out of your explanation– your code appears to work correctly when the tables are created correctly

    I’m assuming that this is how the apple, banana, and apple_banana_lookup tables are defined (note that since you don’t specify the column list on your insert into apple_banana_lookup, I’m assuming that the columns in the table are ordered as your PL/SQL block seems to expect them to be).

    SQL> create table apple(
      2    id number primary key,
      3    weight number
      4  );
    
    Table created.
    
    SQL> create table banana(
      2    id number primary key,
      3    weight number
      4  );
    
    Table created.
    
    SQL> create table apple_banana_lookup (
      2    id number primary key,
      3    appleID number references apple(id),
      4    bananaID number references banana(id)
      5  );
    
    Table created.
    

    Just to avoid making any changes to your code, I created a starting table with 1 row

    SQL> create table starting( id number );
    
    Table created.
    
    SQL> insert into starting values( 1 );
    
    1 row created.
    

    Now I run your code exactly as you posted it. No errors are generated and one row is inserted into each table.

    SQL> DECLARE
      2   cursor curs is select * from starting;
      3   appleId number;
      4   bananaId number;
      5  BEGIN
      6      for foo in curs
      7      LOOP
      8          insert into apple (id, weight)
      9          values(1,1)
     10          returning id into appleId;
     11          insert into banana(id,weight)
     12          values(1,3)
     13          returning id into bananaId;
     14          insert into apple_banana_lookup
     15          values(1,appleId,bananaId);
     16      END LOOP;
     17      COMMIT;
     18  END;
     19  /
    
    PL/SQL procedure successfully completed.
    
    SQL> select * from apple_banana_lookup;
    
            ID    APPLEID   BANANAID
    ---------- ---------- ----------
             1          1          1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a stored procedure DECLARE cursor FOR SELECT [FooData] From [FooTable]; OPEN cursor
DECLARE @table table(XYZ VARCHAR(8) , id int) INSERT INTO @table SELECT '4000', 1 UNION
I have written a cursor: DECLARE CURSOR_SendMail CURSOR FAST_FORWARD FOR SELECT AlertId,AlertDetailsId,AlertDescription ,AlertTarget,ProjectDetailId,Subject FROM
Basically I'm doing this in a procedure: DECLARE CURSOR r_cursor is SELECT * from
Given following snippet (MS SQL): DECLARE UpdateList CURSOR FOR SELECT MyColumn FROM MyTable OPEN
I've this cursor declaration: DECLARE CursorArticulo CURSOR FOR SELECT HstAt.IdArticulo, SUM(HstAt.Cantidad) As SumaCantidad, HstAt.Precio
This is my cursor procedure: DECLARE C1 CURSOR LOCAL FOR SELECT DISTINCT PPTR_MATCH_REF_NO, PPTR_LDGR_CODE,
How can i count number of loops in cursor declare counter int; set counter
I have query that looks like this declare @tmpTable Table(id int) insert into @tmpTable(id)
I have a query that looks like this: DECLARE Match_Cursor CURSOR FOR SELECT ID,UserKey,TypeCode

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.