当date_returned列更新时,将执行此触发器。触发器应该计算罚款并更新借款人在借款人表中的未付罚款。每天晚上罚款5美元。
我的桌子:
DROP TABLE Loan_Transaction;
DROP TABLE Borrower;
CREATE TABLE Borrower (
Borr_ID NUMBER (4) PRIMARY KEY,
borr_name VARCHAR2 (25) not null,
borr_address VARCHAR2 (50) not null,
borr_city VARCHAR2 (15) not null,
phone VARCHAR2 (8),
borr_type VARCHAR2 (2),
books_on_loan NUMBER(1),
outstanding_fines NUMBER (6,2),
check (borr_type in('R','NR','S')),
check (books_on_loan <10)
);
CREATE TABLE Loan_Transaction (
loan_ID NUMBER (9) PRIMARY KEY,
Borr_ID NUMBER (7) REFERENCES Borrower,
Item_Copy_ID NUMBER (7),
loan_type VARCHAR2 (1),
loan_date DATE,
due_date DATE,
date_returned DATE,
check (loan_type in('O','R'))
);
到目前为止我所拥有的......
CREATE OR REPLACE TRIGGER fines
BEFORE UPDATE ON Loan_Transaction
FOR EACH ROW
DECLARE fine_amt NUMBER;
BEGIN
SET fine_amt = (loan_date - date_returned) * 5
UPDATE Borrower b
SET outstanding_fines = (outstanding_fines + fine_amt);
END;
/
这是错误:ORA-24344:编译错误成功
答案 0 :(得分:1)
您的代码有语法错误。运行脚本创建触发器后,需要运行sho错误列出错误。
此行无效SET fine_amt = (loan_date - date_returned) * 5
相反它应该是fine_amt := (:NEW.loan_date - :NEW.date_returned) * 5;
你的更新声明也没有任何意义。您需要让where子句仅更新与loan_transaction表中更新的贷款相关的行。这段代码不正确,似乎也有错误。
您需要学习语法并相应地实施。您还需要使用:OLD,:NEW来引用loan_transaction中触发的列。