我正在尝试创建一个名为" card reservation" (学校)与APEX。它应该像在电影院一样工作,有几个节目,用户可以为特别节目预留座位。
现在我想制作一个触发器,验证一个房间内同时没有2个节目,每个节目之间应该是15分钟。
目前看起来像这样:
create or replace trigger tg_roomoccupancy
before insert or update on show
for each row
declare
sDate date;
sDuration number(4);
newDuration number(4);
begin
select datum into sDate from show where room = :new.room and to_char(datum, 'DD-MM-YYYY') = to_char(:new.datum, 'DD-MM-YYYY');
select dauer into vDuration from film f, show s where to_char(datum, 'DD-MM-YYYY') = to_char(:new.datum, 'DD-MM-YYYY') AND s.filmId = f.filmId;
select dauer into newDuration from film where film.filmId = :new.filmId;
if((((:new.datum - sDate)*1440) < (sDuration + 15)) AND (((:new.datum - vDate)*1440) > 0))then
raise_application_error(-20001, 'There is an other show running in this room!');
elsif((((sDate - :new.datum)*1440) < (newDuration + 15)) AND ((sDate - :new.datum)*1440) > 0) then
raise_application_error(-20002, 'The show lasts too long, an other show will start!');
elsif((sDate - :new.datum) = 0) then
raise_application_error(-20003, 'Two shows cannot start at the same time!');
end if;
end;+
你可以看到ifs正在做什么,我用1440倍增,因为两个日期之间的减法给出了一个以天为单位的数字,我将它与* 24 * 60相乘以得到这个分钟的差异,因为电影也在几分钟内完成。
触发器会根据需要引发错误,但我的问题是:
当我想在一天中插入节目时,没有其他节目正在运行,我得到一个&#34; NO_DATA_FOUND&#34; -Exception,我无法插入新节目。如何插入新节目?
感谢您的帮助,对不好的英语抱歉: - )
答案 0 :(得分:0)
您可以在下面的触发器中应用与此类似的逻辑。
使用此代码创建的表。你可以在show.filmId上添加约束来检查电影表中的电影存在, 但这里是由触发器完成的。我还将两个错误(2002和2003)加入到一个并抛出错误 &#34;另一个在这个房间或间隔时间太短的节目&#34;,但这取决于你。
create table show (datum date not null, room number not null, filmId number not null);
create table film (filmId number not null unique, dauer number not null);
insert into film values (1, 120);
测试:
insert into show (datum, room, filmId) values
(to_date('2015-01-01 15:00:00', 'yyyy-MM-dd HH24:mi:ss'), 1, 1);
=> OK
insert into show (datum, room, filmId) values
(to_date('2015-01-01 15:00:00', 'yyyy-MM-dd HH24:mi:ss'), 1, 2);
=> Incorrect movie ID.
insert into show (datum, room, filmId) values
(to_date('2015-01-01 16:00:00', 'yyyy-MM-dd HH24:mi:ss'), 1, 1);
=> Another show running in this room.
insert into show (datum, room, filmId) values
(to_date('2015-01-01 17:10:00', 'yyyy-MM-dd HH24:mi:ss'), 1, 1);
=> Interval between shows too short.
insert into show (datum, room, filmId) values
(to_date('2015-01-01 17:20:00', 'yyyy-MM-dd HH24:mi:ss'), 1, 1);
=> OK.
insert into show (datum, room, filmId) values
(to_date('2015-01-01 17:20:00', 'yyyy-MM-dd HH24:mi:ss'), 2, 1);
=> OK. (Show is in another room.)
触发码:
create or replace trigger tg_roomoccupancy
before insert or update on show
for each row
declare
v_duration number;
v_cnt number;
begin
begin
select dauer into v_duration
from film f where filmId = :new.filmId;
exception when no_data_found then
raise_application_error(-20001, 'Incorrect movie ID.');
end;
select count(1) into v_cnt from show join film f using (filmId)
where room = :new.room and
((:new.datum between datum and datum + f.dauer/1440)
or (:new.datum+v_duration/1440 between datum and datum + f.dauer/1440));
if v_cnt > 0 then
raise_application_error(-20002, 'Another show running in this room.');
end if;
select count(1) into v_cnt from show join film f using (filmId)
where room = :new.room and
((:new.datum between datum - 15/1440 and datum + (f.dauer + 15)/1440)
or (:new.datum + v_duration/1440
between datum - 15/1440 and datum + (f.dauer+15)/1440));
if v_cnt > 0 then
raise_application_error(-20003, 'Interval between shows too short.');
end if;
end;