Limit an sqlite Table's Maximum Number of Rows Limit an sqlite Table's Maximum Number of Rows sqlite sqlite

Limit an sqlite Table's Maximum Number of Rows


Another solution is to precreate 100 rows and instead of INSERT use UPDATE to update the oldest row.
Assuming that the table has a datetime field, the query

UPDATE ...WHERE datetime = (SELECT min(datetime) FROM logtable)

can do the job.

Edit: display the last 100 entries

SELECT * FROM logtableORDER BY datetime DESCLIMIT 100

Update: here is a way to create 130 "dummy" rows by using join operation:

CREATE TABLE logtable (time TIMESTAMP, msg TEXT);INSERT INTO logtable DEFAULT VALUES;INSERT INTO logtable DEFAULT VALUES;-- insert 2^7 = 128 rowsINSERT INTO logtable SELECT NULL, NULL FROM logtable, logtable, logtable,   logtable, logtable, logtable, logtable;UPDATE logtable SET time = DATETIME('now'); 


You could create a trigger that fires on INSERT, but a better way to approach this, might be to simply have a scheduled job that runs periodically (say once a week) and deletes records from the table.


There are a couple of ways to constrain a table to 100 rows. (For brevity, 5 rows in the code below.) Tested in SQLite version 3.7.9.

All this code relies on a kind of quirk in the way SQLite handles data type declarations. (It seems quirky to me, anyway.) SQLite lets you insert nonsense like 3.14159 and 'wibble' into a bare integer column. But it lets you insert only integers into a column declared integer primary key or integer primary key autoincrement.

FOREIGN KEY constraint

Use a foreign key constraint to a table of valid id numbers to guarantee that the id numbers are in the range you want. Foreign key constraints work even on autoincrementing columns.

pragma foreign_keys=on;create table row_numbers (n integer primary key);insert into row_numbers values (1);insert into row_numbers values (2);insert into row_numbers values (3);insert into row_numbers values (4);insert into row_numbers values (5);create table test_row_numbers (  row_id integer primary key autoincrement,  other_columns varchar(35) not null,  foreign key (row_id) references row_numbers (n));insert into test_row_numbers (other_columns) values ('s');insert into test_row_numbers (other_columns) values ('s');insert into test_row_numbers (other_columns) values ('s');insert into test_row_numbers (other_columns) values ('s');insert into test_row_numbers (other_columns) values ('s');

Sixth insert fails with "Error: foreign key constraint failed".

I don't think Using an autoincrement is entirely safe. On other platforms, a rollback would leave a gap in the sequence. If you don't use an autoincrement, you can safely insert rows by picking the id number out of "row_numbers".

insert into test_row_numbers values(  (select min(n)    from row_numbers    where n not in      (select row_id from test_row_numbers)),   's');

CHECK() constraint

The primary key constraint below guarantees the id numbers will be integers. The CHECK() constraint guarantees the integers will be in the right range. Your application might still have to deal with gaps caused by rollbacks.

create table test_row_numbers (  row_id integer primary key autoincrement,  other_columns varchar(35) not null,  check (row_id between 1 and 5));