views:

385

answers:

4

I have a table that saves some account limits like users. For most rows this value will be an integer. However I will also need to support storing unlimited (infinite) as a value. This might seem silly but is there a common integer value that represents unlimited (infinite)? I'd like to keep storing the values as integers.

+10  A: 
  • use zero
  • use -1
  • use null

I prefer null, unless the field is not nullable, then I prefer zero, unless zero is a valid limit, then I prefer -1, unless -1 is a valid value, in which case you need another field (say, a bit field) to signify 'unlimited'.

Steven A. Lowe
Hm. Zero and -1 are members of the set of integers. And null (no data) may be a valid value already. Redefining well-known values to have new special meanings is confusing and generally not wise.
Tor Haugen
no, it can make sense. obviously the normal range he'd be talking about isn't the set of integers, it'd be the set of non-negative integers. since you obviously can't set a limit of -1 users, then it's obvious that it has a special value. this method is used in many programming languages, too.
nickf
@[fixme.myopenid.com]: agreed, but i would guess in this case that the full range of integer values is not used, since we're talking about account limits. In that case you would want a sentinel value (http://en.wikipedia.org/wiki/Sentinel_value), which is fine. As long as it is documented!
Steven A. Lowe
Unless there is a strong reason otherwise, I prefer the 'extra field' solution. The 'magic value' solution can lead to all sorts of unexpected problems. I'm not saying it's not common, just that it's a little time-bomb ticking.
Richard A
To me, NULL makes perfect sense. It represents the idea of there being no integer for the limit. What else would a NULL value in this column mean?
Ned Batchelder
+2  A: 

Well, if you're accessing your database from .NET, the integer types have constant members MaxValue and MinValue. We sometimes use them to represent infinity. Of course, these values have no special meaning in your RDBMS...

Tor Haugen
"Redefining well-known values to have new special meanings is confusing and generally not wise" You mean, like redefining MinValue and MaxValue? LOL! ;-)
Steven A. Lowe
I think what is meant is that MaxValue is really the practical limit anyway, so why not use it?
tvanfosson
+3  A: 

As an integer, infinity will be hard. A few options:

1) -1, with corresponding program logic that treats it as infinite (and assuming that negative values aren't actually valid).

2) An extra column holding a flag indicating finite/infinite.

Drew Hall
+1  A: 

You can't really have an infinite number of accounts since there aren't an infinite number of people. In fact, you really are limited in the number of accounts that you can have (if you choose to store the value in a single database column) since any finite storage mechanism for countable things has a limit. I'd suggest, with @Tor Haugen, that the best way to do this is to use the limit imposed by the system (whatever maximum integer or long your language provides). That way you won't have to worry about any special cases for your comparisons. Additionally, I'd use a database constraint to prevent storing negative values in the field. A 64-bit integer will certainly hold a number big enough to be practically unlimited for your application.

tvanfosson