Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: FIFO table question

Re: FIFO table question

From: Ken Johnson <kjohnson_at_ryback.com>
Date: 1997/10/17
Message-ID: <6286rd$eej$1@opus.anet-stl.com>#1/1

 Alex Tait wrote in article <01bcd948$c67106a0$b481209a_at_f12-internet-pc>...

>Is it possible to set up a table whereby, once a predefined number of
 rows
>has been added, the first row is deleted?
>There are no relations to the table, it's simply an event log.
>--
>Alex Tait
>Force12 Solutions Ltd
>ajt_at_force12.com
>

Yes, here is an example (sorry, for some reason my newsreader isn't indenting)

CREATE SEQUENCE log_id_seq;

CREATE TABLE test_log
(

    logid number,
message varchar2(2000)
);

create or replace trigger test_log_fifo
before insert
on test_log
for each row
declare

   low_id number;
new_id number;
count_ids number;
begin
-- first we assign an id # to this transaction select log_id_seq
into new_id
from dual;

--
-- now lets get the lowest id #, and the number of items in the log
select min(logid), count(logid)
into low_id, count_ids
from test_log
;
--
-- now (if necessary) delete the earliest item
if (count_ids >= 10) then
delete from test_log where logid = low_id;
end if;
end;
/

insert into test_log (message) values ('This is a test. Test #1');
insert into test_log (message) values ('This is a test. Test #2');
...
insert into test_log (message) values ('This is a test. Test #12');

This will limit the test_log table to a maximum of 10 entries.  When the
11th entry is inserted, then the 1st will be deleted, etc.

-------
Ken Johnson
Received on Fri Oct 17 1997 - 00:00:00 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US