| Oracle FAQ | Your Portal to the Oracle Knowledge Grid | |
Home -> Community -> Usenet -> c.d.o.misc -> Re: FIFO table question
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
![]() |
![]() |