Search code examples
sqlitecreate-table

how to set maximum rows in create table for sqlite


In MySQL, I can use MAX_ROWS option in CREATE TABLE statement to limit rows number, how might I do this in sqlite? thanks


Solution

  • SQLite has no explicit mechanism for this, but you could implement it with a trigger:

    CREATE TRIGGER MyTable_row_count
    BEFORE INSERT ON MyTable
    WHEN (SELECT COUNT(*) FROM MyTable) >= 10
    BEGIN
        SELECT RAISE(FAIL, 'too many rows');
    END;