I'm trying to perform the stored package procedure call in Oracle 11g XE, but for some reason I get the error below:
Error report - ORA-02291: integrity constraint (ROOT.SYS_C007057) violated - parent key not found ORA-06512: at "ROOT.BOOKS_STORE", line 69 ORA-06512: at line 2
02291. 00000 - "integrity constraint (%s.%s) violated - parent key not found"
*Cause: A foreign key value has no matching primary key value.
*Action: Delete the foreign key or add a matching primary key.
when calling the following procedure:
begin
books_store.add_books_to_store(
'To Kill a Mockingbird', 21,
'test description', 5,
'https://test_img.jpg',
10, 6.99
);
end;
What the procedure does is insert the data into the books
table. Below is the procedure text (which is inside the books_store
package) and the description of the books
table.
procedure add_books_to_store(
book_name books.name%type, book_author_id books.author_id%type,
book_description books.description%type default null,
book_publisher_id books.publisher_id%type, book_cover_img books.cover_img%type,
books_count books.available_count%type, book_price books.price%type)
is
existing_books_count integer;
add_negative_or_zero_books exception;
begin
if books_count <= 0 then
raise add_negative_or_zero_books;
end if;
select count(*) into existing_books_count from books
where
name = book_name and author_id = book_author_id and
description = book_description and publisher_id = book_publisher_id and
price = book_price;
if existing_books_count = 0 then
insert into books values (books_seq.nextval, book_name, book_description,
book_cover_img, books_count, book_author_id, book_publisher_id, book_price);
else
update books set available_count = available_count + books_count
where
name = book_name and author_id = book_author_id and
description = book_description and publisher_id = book_publisher_id and
price = book_price;
end if;
exception
when add_negative_or_zero_books then
raise_application_error(-10003, 'You cannot add 0 or less books');
end add_books_to_store;
books
description:
DESC books;
Name Null? Type
------------------------------------------------------------
ID NOT NULL NUMBER(5)
NAME NOT NULL VARCHAR2(200)
DESCRIPTION VARCHAR2(2000)
COVER_IMG VARCHAR2(300)
AVAILABLE_COUNT NOT NULL NUMBER(4)
PRICE NUMBER(10,2)
AUTHOR_ID NUMBER(5)
PUBLISHER_ID NUMBER(5)
So, the error says that there's something wrong with my primary or foreign keys. Though, I cannot understand what exactly is wrong.
I thought that the problem was that I passed wrong author_id
and publisher_id
as arguments to the procedure, but they're correct. Here's the select *
calls for authors
and publishers
tables:
select * from authors;
ID FIRST_NAME LAST_NAME BIRTHDAY
--------------------------------------------------
21 Harper Lee 28-APR-26
select * from publishers;
ID NAME
---------------------------
5 Penguin Fiction
Could you help me to find out what's wrong with my code and how to make it work?