ORA-04091 创建触发器时出错

ORA-04091 Error when creating trigger

我正在尝试创建一个触发器,它将获取为血库捐献的血液量,并将其添加到该特定血型的当前数量。我可以毫无问题地编译触发器。然而,当我 运行 一些条目时,我得到三个错误,ORA-04091、ORA-06512 和 ORA-04088。它说 table 正在发生变化,因此触发器无法访问它,但我在触发器中插入后使用,所以 table 不应该已经插入了吗?任何建议都很好,教授并没有真正涵盖触发器。

触发器:

create or replace trigger addBloodCount
after insert on Donations
for each row
when(new.donationID is not null AND new.donorID is not null AND    new.bloodCode is not null AND new.donationDate is not null AND new.amountDonated         is not null)
declare
add_amount INTEGER;
begin
select amountDonated into add_amount from Donations;
update Blood
set quantity = Blood.quantity + add_amount
where bloodCode =:new.bloodCode;
end;
/

表格:

CREATE TABLE Address (
addressID INTEGER NOT NULL,
street VARCHAR(50) NOT NULL,
city VARCHAR(40) NOT NULL,
state VARCHAR(30) NOT NULL,
zip INTEGER NOT NULL,
PRIMARY KEY (addressID));


CREATE TABLE Donor (
donorID INTEGER NOT NULL,
fname VARCHAR(50) NOT NULL,
lname VARCHAR(50) NOT NULL,
sex VARCHAR(10) NOT NULL,
addressID INTEGER NOT NULL,
DOB DATE NOT NULL,
phoneNo INTEGER NOT NULL,
PRIMARY KEY (donorID));


CREATE TABLE Donations (
donationID INTEGER NOT NULL,
donorID INTEGER NOT NULL,
bloodCode INTEGER NOT NULL,
donationDate DATE NOT NULL,
amountDonated INTEGER NOT NULL,
PRIMARY KEY (donationID));


CREATE TABLE Blood (
bloodCode INTEGER NOT NULL,
bloodType VARCHAR(50) NOT NULL,
bloodPrice DECIMAL NOT NULL,
quantity INTEGER NOT NULL,
PRIMARY KEY (bloodCode));


CREATE TABLE BloodOrder (
orderID INTEGER NOT NULL,
bloodCode INTEGER NOT NULL,
hospitalID INTEGER NOT NULL,
amountOrdered INTEGER NOT NULL,
PRIMARY KEY (orderID));


CREATE TABLE Hospital (
hospitalID INTEGER NOT NULL,
hospitalName VARCHAR(50) NOT NULL,
addressID INTEGER NOT NULL,
phoneNo INTEGER NOT NULL,
PRIMARY KEY (hospitalID));


alter table Donor
add (constraint addressID_fk foreign key (addressID)
references Address(addressID));

alter table Donations
add (constraint donorID_fk foreign key (donorID)
references Donor(donorID));

alter table Donations
add (constraint bloodCode_fk foreign key (bloodCode)
references Blood(bloodCode));

alter table bloodOrder
add (constraint bloodCode_fk2 foreign key (bloodCode)
references Blood(bloodCode));

alter table bloodOrder
add (constraint hospitalID_fk foreign key (hospitalID)
references Hospital(hospitalID));

alter table Hospital
add (constraint addressID_fk2 foreign key (addressID)
references Address(addressID));

此问题是由以下语句引起的:

select amountDonated into add_amount from Donations;

简而言之,您不能引用直接插入的值。您需要改用 :NEW 。按如下方式重写触发器应该有效:

create or replace trigger addBloodCount
after insert on Donations
for each row
when(new.donationID is not null AND new.donorID is not null AND new.bloodCode is not null AND new.donationDate is not null AND new.amountDonated is not null)
begin
update Blood
set quantity = quantity + :new.amountDonated
where bloodCode =:new.bloodCode;
end;
/

声明

when(new.donationID is not null AND new.donorID is not null AND new.bloodCode is not null AND new.donationDate is not null AND new.amountDonated is not null)

可能不需要,因为您已经在所有这些列中设置了 NOT NULL 约束。