create or replace trigger trigger_update_rental after insert on rental for each row declare integrity_error exception; errno integer; errmsg char(200); dummy integer; found boolean; begin if inserting then SELECT count(1) into dummy FROM rental a where a.特定DVD字段='特定DVD'; if mod(dummy,4)='0' then SYS.DBMS_OUTPUT.PUT_LINE('此DVD需被清洁'); end if; exception when integrity_error then SYS.DBMS_OUTPUT.PUT_LINE(errno, errmsg); end;
你的copy表里是所有的DVD吗?rental是借出记录吗?
如果是这样的话,copy表里加一个字段,标记清洁后借出了几次.
rental有记录进入时,更新对应copy表里的这个字段的值+1,
然后前台程序,再查copy表,如果是特定DVD,看这个字段的值是不是4,如果是4,就输出提示.
清洁后,这个字段的值更新为0.
希望能给一些语句,便于我理解trigger,THX!!!!
这个靠谱,查询的时候用for update锁住,如果小于4,则更新+1,大于4,输出提示,这个不用前台来查,可以直接由触发器判断
只是一个作业,可以这么实现,但如果是正式环境,并发量大的话,这种设计可能会有点问题
on rental for each row
declare
integrity_error exception;
errno integer;
errmsg char(200);
dummy integer;
found boolean;
begin
if inserting then
SELECT count(1) into dummy FROM rental a where a.特定DVD字段='特定DVD';
if mod(dummy,4)='0' then
SYS.DBMS_OUTPUT.PUT_LINE('此DVD需被清洁');
end if;
exception
when integrity_error then
SYS.DBMS_OUTPUT.PUT_LINE(errno, errmsg);
end;