如果容量 (cap) 为 0,则此触发器应删除新插入的元组。我在这里做错了什么?这是代码

If the capacity (cap) is 0, this trigger should delete the newly inserted tuple. what did i do wrong here? here is the code

我正在 oracle apex 上申请。现在,如果容量 (cap) 为 0,此触发器应删除新插入的元组,因为 passenger_id 将无效。我在这里做错了什么?

这是代码

create or replace trigger Ticket_katbo
before insert on passenger 
for each row
declare 
newcap number;
cap number;
flightpk number;
begin
flightpk := :new.flight_fid;
select flight_capacity into cap from flight where fid = flightpk;
if (cap>0) THEN
newcap := cap-1;
update flight set flight_capacity = newcap where fid = flightpk ;
ELSIF (cap=0 or cap = NULL) THEN
Delete from passenger where passenger_id = :new.passenger_id;
END IF;
end;

您在插入之前执行此操作。因此,如果容量不存在,则引发错误:

create or replace trigger Ticket_katbo
before insert on passenger 
for each row
declare 
    v_capacity number;
begin
    select flight_capacity into v_capacity
    from flight
    where fid = :new.flight_fid;

    if (cap <= 0) then
        raise_application_error( -20001, 
                                 'No capacity for another passenger');
    end if;

    update flight
        set flight_capacity = flight_capacity - 1
        where fid = :new.flight_fid;
end;