【问题标题】:ORA-04091 Error when creating triggerORA-04091 创建触发器时出错
【发布时间】:2016-12-07 00:22:04
【问题描述】:

我正在尝试创建一个触发器,它将获取为血库捐献的血液量并将其添加到该特定血型的当前数量中。我可以毫无问题地编译触发器。然而,当我运行一些条目时,我得到三个错误,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));

【问题讨论】:

    标签: sql database oracle triggers database-trigger


    【解决方案1】:

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

    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 约束。

    【讨论】:

      猜你喜欢
      • 2013-05-13
      • 2018-11-30
      • 2017-12-15
      • 1970-01-01
      • 2022-01-20
      • 2012-05-17
      • 2015-08-03
      • 1970-01-01
      • 2011-11-18
      相关资源
      最近更新 更多