question

Blackhawk-17 avatar image
Blackhawk-17 asked

Can I limit the Size of a Table?

SEEDER QUESTION:

For one reason or another I need to limit the maximum storage a table can consume whilst allowing other tables in the dB to carry on their merry way. Perhaps it is to rein in the DEV team or to prevent a runaway scenario. Okay, a little contrived, but a good thought experiment.

How would I achieve this in SQL 2005?

database-designseeder-question
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

James Moore avatar image James Moore commented ·
What do you want to happen with new rows? For the oldest row to be evicted or just to stop inserts after a certain number of rows? Do you want the limit based on data size or row count?
0 Likes 0 ·
Blackhawk-17 avatar image Blackhawk-17 commented ·
Originally I said storage - so data size - but why not answer for both scenarios as Gianluca has done, size on disk and FIFO after a pre-determined number of rows have populated the table. We'll assume the math has been done to calculate how many rows fit in the storage limitation.
0 Likes 0 ·
Gianluca Sartori avatar image
Gianluca Sartori answered

Move the table in a separate data file and turn off autogrow for that file if you simply want the inserts to fail.

Another possible strategy is adding a trigger instead of insert and ignore new inserts if the table row count exceeds your predetermined value.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

KenJ avatar image
KenJ answered

Since we're assuming the math has been done to know how many rows fit in the storage limitation, you can use a check constraint on an identity column

   alter table <your-table> 
      add constraint <your-constraint> 
          check (your-identity-column < the-number-of-rows-you-want)

If you aren't using an identity column, or the developers were able to modify your identity definition (say, reseed at 0 and have it count by -1), you would want to have the check constraint reference a scalar function that you write that returns the row count of the table and makes sure it is less than the-number-of-rows-you-want.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.