procedure : First delete expired data from table seckill, then insert new data
createorreplaceprocedure seckill_procedure
iscursor c isselecttemp.id from(select id from product where id notin(select productid from seckill)orderby dbms_random.value)tempwhere rownum <=16;-- 16 records
countnum number;begin-- When we restart the Oracle services, all of data may be deleted from table seckilldeletefrom seckill where endtime < sysdate;commit;-- Attention the commitselectcount(*)into countnum from seckill;for v_tem in c loop-- trigger `seckill_trig`insertinto seckill (productid)values(v_tem.id);endloop;commit;-- Attention the commit-- if 0, we should insert another 16 records into table seckill to make sure -- there are always 32 records in table seckillif(countnum =0)then
seckill_procedure;endif;commit;-- Attention the commitend;
trigger : Before insert new data into table seckill, we need to set the id , starttime and endtime of each record
createorreplacetrigger seckill_trig
before inserton seckill
for each rowdeclare
id number;
seckilltime date;
seckillhour number;
countnum number;
PRAGMA AUTONOMOUS_TRANSACTION;-- Attention: when the trigger is being executed, the table seckill will be locked.beginselectcount(*)into countnum from seckill;-- check if there are records in table seckill.-- if no, "seckilltime" will be assigned with current time.-- if yes, "seckilltime" will be assigned with the time which is 2 hours later.if(countnum =0)then
seckilltime := sysdate;else
seckilltime := sysdate +2/24;endif;
:new.id := seckillseq.nextval;
seckillhour := to_number(to_char(seckilltime,'hh24'));-- check whether "seckillhour " is odd or evenif(mod(seckillhour,2)=0)then
:new.starttime := trunc(seckilltime,'hh');
:new.endtime := trunc(seckilltime +2/24,'hh');else
:new.starttime := trunc(seckilltime -1/24,'hh');
:new.endtime := trunc(seckilltime +1/24,'hh');endif;end;