2016-12-07 3 views
0

J'essaie de créer un déclencheur qui prendra la quantité de sang donnée pour une banque de sang et l'ajoutera à la quantité actuelle pour ce groupe sanguin spécifique. Je peux compiler le déclencheur sans aucun problème. Cependant quand je cours quelques entrées, j'obtiens trois erreurs, ORA-04091, ORA-06512 et ORA-04088. Il dit que la table est en train d'être muté, donc le trigger ne peut pas y accéder mais je l'utilise après l'insertion dans le trigger, alors la table ne devrait-elle pas déjà être insérée? Toutes les suggestions seraient grandes, le professeur n'a pas vraiment couvert les déclencheurs beaucoup.ORA-04091 Erreur lors de la création du déclencheur

Trigger:

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

Tables:

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

Répondre

0

La question est causée par cette déclaration:

select amountDonated into add_amount from Donations; 

En bref, vous ne pouvez pas faire référence à des valeurs étant insérées directement. Vous devez utiliser: NEW à la place. Réécrire le déclencheur comme suit devrait fonctionner:

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

Déclaration

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) 

est probablement pas nécessaire puisque vous avez déjà contraintes NOT NULL définies dans toutes ces colonnes.

+0

merci beaucoup qui a fait l'affaire! – Christian