违反完整性约束 - 调用存储过程时找不到父键
Integrity constraint violated - parent key not found when calling stored procedure
我试图在 Oracle 11g XE 中执行存储包过程调用,但由于某种原因,我收到以下错误:
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.
调用以下过程时:
begin
books_store.add_books_to_store(
'To Kill a Mockingbird', 21,
'test description', 5,
'https://test_img.jpg',
10, 6.99
);
end;
该过程所做的是将数据插入 books
table。下面是程序文本(在 books_store
包内)和 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
描述:
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)
所以,错误说我的主键或外键有问题。虽然,我不明白到底出了什么问题。
我认为问题是我传递了错误的 author_id
和 publisher_id
作为过程的参数,但它们是正确的。这是 select *
对 authors
和 publishers
table 的调用:
select * from authors;
ID FIRST_NAME LAST_NAME BIRTHDAY
--------------------------------------------------
21 Harper Lee 28-APR-26
select * from publishers;
ID NAME
---------------------------
5 Penguin Fiction
你能帮我找出我的代码有什么问题以及如何让它工作吗?
PS:这是我的 ER 图:
我想,问题可能出在这里:
insert into books values (books_seq.nextval, book_name, book_description,
book_cover_img, books_count, book_author_id, book_publisher_id, book_price);
因为在 table 描述栏中有另一个顺序:
PRICE NUMBER(10,2)
AUTHOR_ID NUMBER(5)
PUBLISHER_ID NUMBER(5)
尝试明确指定列名:
insert into books (ID, NAME, DESCRIPTION, COVER_IMG, AVAILABLE_COUNT, PRICE, AUTHOR_ID, PUBLISHER_ID)
values (books_seq.nextval, book_name, book_description,
book_cover_img, books_count, book_price, book_author_id, book_publisher_id);
现在看起来您正在将 AUTHOR_ID
值插入 PRICE
列,将 PUBLISHER_ID
插入 AUTHOR_ID
并将 PRICE
插入 PUBLISHER_ID
。
我试图在 Oracle 11g XE 中执行存储包过程调用,但由于某种原因,我收到以下错误:
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.
调用以下过程时:
begin
books_store.add_books_to_store(
'To Kill a Mockingbird', 21,
'test description', 5,
'https://test_img.jpg',
10, 6.99
);
end;
该过程所做的是将数据插入 books
table。下面是程序文本(在 books_store
包内)和 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
描述:
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)
所以,错误说我的主键或外键有问题。虽然,我不明白到底出了什么问题。
我认为问题是我传递了错误的 author_id
和 publisher_id
作为过程的参数,但它们是正确的。这是 select *
对 authors
和 publishers
table 的调用:
select * from authors;
ID FIRST_NAME LAST_NAME BIRTHDAY
--------------------------------------------------
21 Harper Lee 28-APR-26
select * from publishers;
ID NAME
---------------------------
5 Penguin Fiction
你能帮我找出我的代码有什么问题以及如何让它工作吗?
PS:这是我的 ER 图:
我想,问题可能出在这里:
insert into books values (books_seq.nextval, book_name, book_description,
book_cover_img, books_count, book_author_id, book_publisher_id, book_price);
因为在 table 描述栏中有另一个顺序:
PRICE NUMBER(10,2)
AUTHOR_ID NUMBER(5)
PUBLISHER_ID NUMBER(5)
尝试明确指定列名:
insert into books (ID, NAME, DESCRIPTION, COVER_IMG, AVAILABLE_COUNT, PRICE, AUTHOR_ID, PUBLISHER_ID)
values (books_seq.nextval, book_name, book_description,
book_cover_img, books_count, book_price, book_author_id, book_publisher_id);
现在看起来您正在将 AUTHOR_ID
值插入 PRICE
列,将 PUBLISHER_ID
插入 AUTHOR_ID
并将 PRICE
插入 PUBLISHER_ID
。