ORA-04091创建触发器时出错

时间:2016-12-07 00:22:04

标签: sql database oracle triggers database-trigger

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

触发:

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));

1 个答案:

答案 0 :(得分:0)

这个问题是由这个声明引起的:

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约束,因此可能不需要