SQLite Forum

How to implement a table working like a ring buffer
Login
You mean like this:

```
create table t
(
    ID      integer primary key,
    GroupID integer not null,
    Ack     integer not null check (Ack in (0,1))
);
create unique index t_Deleteable_ID on t (ID, GroupID) where Ack != 1;
create index t_GroupID on t (GroupID) where Ack != 1;

create trigger t_bi_prune before insert on t
begin
  delete from t
   where ID = (
                 select ID
                   from t as o
                  where (select count(ID) from t where GroupID == o.GroupID and Ack != 1) > 1
                    and Ack != 1
               order by ID
                  limit 1
              )
     and (
          select count(ID)
            from t
         ) >= 6;
end;

insert into t values (1, 100, 0), (2, 200, 1), (3, 200, 0), (4, 300, 0), (5, 300, 0), (6, 400, 0), (7, 400, 0);
```

The question is that if there is the following pattern:

1 100 1  
2 100 0  
3 100 0

which row do you want to delete, row 2 or row 3?

And what about the following:

1 100 0  
2 100 1  
3 100 0

which row should be deleted, row 1 or row 3?

And also for:

1 100 0  
2 100 0  
3 100 1

which row should be deleted, row 1 or row 2?