Subject Re: [firebird-support] limit rows in a table
Author Ivan Prenosil
"Martijn Tonies" wrote:
>> Could someone please suggest how I can limit the amount of rows a
>> user
>> can enter in a table, using a Stored Procedure or a trigger.
>>
>> I would like to limit the amount of rows to 1K, and all new inserts
>> would be stopped/cancelled from this point on.
>>
>> How should I accomplish this?
>
> CREATE EXCEPTION TOO_MANY_ROWS 'too many rows';
>
> CREATE TRIGGER check_for_too_many_rows ON <table> BEFORE INSERT
> AS
> DECLARE VARIABLE cnt INTEGER;
> BEGIN
> select count(*) from <table> INTO :cnt;
> if (cnt = 1024)
> then exception TOO_MANY_ROWS;
> end

This is not reliable - if there are 1023 rows and two users will insert
new rows simultaneously, the row count will be 1025, so the exception
will never raise. So there should be
if (cnt >= 1024)

Another possibility is to limit primary key range, e.g.
CHECK(PK BETWEEN 1 AND 1024)

Ivan