我的项目表是。
create table book_rec(book_name char(30),author char(30),total_copies number(15),available_copies number(5),primary key(book_name));
create table membership_rec(mem_id number(20),mem_name char(30),no_of_books_taken number(4),primary key(mem_id));
create table book_details(book_id number(30),book_name char(30),mem_id number(5),primary key(book_id),foreign key(book_name)references book_rec(book_name));
create table circulation_rec(book_id number(20),mem_id number(20),issue_date date,return_date date,foreign key (book_id)references book_details(book_id),foreign key(mem_id)references membership_rec(mem_id));
我想创建一个触发器,根据circulation_date记录自动增加和减少来自book_rec的no_of_books_taken和return_date.But程序显示错误。如何解决错误。你可以帮助任何人吗?< / p>
这是我的触发器代码。
create or replace trigger t_trigger
After insert or update on circulation_rec
for each row
begin
if inserting then
update book_rec
set available_copies = available_copies - 1
where book_name = :NEW.book_name;
update membership_rec
set no_of_books_taken = no_of_books_taken +1
where mem_id = :NEW.mem_id;
elsif updating then
update book_rec
set available_copies = available_copies - 1
where book_name = :old.book_name;
update membership_rec
set no_of_books_taken = no_of_books_taken - 1
where mem_id = :New.mem_id;
end if;
end;
/
答案 0 :(得分:1)
如果您执行SHOW ERRORS
或运行此查询,
SELECT NAME, TYPE, line, text FROM user_errors;
您将了解完整的错误消息
NAME, TYPE LINE TEXT
---- --- --- ----
T_TRIGGER TRIGGER 6 PLS-00049: bad bind variable 'NEW.BOOK_NAME'
T_TRIGGER TRIGGER 13 PLS-00049: bad bind variable 'OLD.BOOK_NAME'
现在,导致此错误是因为触发器的所有者 - circulation_rec
表没有book_name
列。因此,Oracle无法理解:NEW.book_name
和:OLD.book_name
的内容意味着在这种情况下,因此它会抛出错误的绑定变量错误。
现在,要解决此问题,您可以添加IN
条件以从book_id
表中获取book_details
,因为circulation_rec
确实有book_id
。
像这样。
CREATE OR replace TRIGGER t_trigger
AFTER INSERT OR UPDATE ON circulation_rec
FOR EACH ROW
BEGIN
IF inserting THEN
UPDATE book_rec
SET available_copies = available_copies - 1
WHERE book_name IN (SELECT book_name
FROM book_details
WHERE book_id = :NEW.book_id);
UPDATE membership_rec
SET no_of_books_taken = no_of_books_taken + 1
WHERE mem_id = :NEW.mem_id;
ELSIF updating THEN
UPDATE book_rec
SET available_copies = available_copies - 1
WHERE book_name IN (SELECT book_name
FROM book_details
WHERE book_id = :OLD.book_id);
UPDATE membership_rec
SET no_of_books_taken = no_of_books_taken - 1
WHERE mem_id = :New.mem_id;
END IF;
END;
/
这是一个dbfiddle demo