In multiple courses, books, and jobs, I have seen text fields defined as VARCHAR(255) as kind of the default for \"shortish\" text. Is there any good reason that a length o
An unsigned 1 byte number can contain the range [0-255] inclusive. So when you see 255, it is mostly because programmers think in base 10
(get the joke?) :)
Actually, for a while, 255 was the largest size you could give a VARCHAR in MySQL, and there are advantages to using VARCHAR over TEXT with indexing and other issues.
When you say 2^8
you get 256
, but the numbers in computers terms begins from the number 0
. So, then you got the 255
, you can probe it in a internet mask for the IP or in the IP itself.
255
is the maximum value of a 8 bit integer : 11111111 = 255
Does that help?
255 is used because it's the largest number of characters that can be counted with an 8-bit number. It maximizes the use of the 8-bit count, without frivolously requiring another whole byte to count the characters above 255.
When used this way, VarChar only uses the number of bytes + 1 to store your text, so you might as well set it to 255, unless you want a hard limit (like 50) on the number of characters in the field.
0000 0000 -> this is an 8-bit binary number. A digit represents a bit.
You count like so:
0000 0000 → (0)
0000 0001 → (1)
0000 0010 → (2)
0000 0011 → (3)
Each bit can be one of two values: on or off. The total highest number can be represented by multiplication:
2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 - 1 = 255
Or
2^8 - 1.
We subtract one because the first number is 0.
255 can hold quite a bit (no pun intended) of values.
As we use more bits the max value goes up exponentially. Therefore for many purposes, adding more bits is overkill.
Probably because both SQL Server and Sybase (to name two I am familiar with) used to have a 255 character maximum in the number of characters in a VARCHAR
column. For SQL Server, this changed in version 7 in 1996/1997 or so... but old habits sometimes die hard.
In many applications, like MsOffice (until version 2000 or 2002), the maximum number of characters per cell was 255. Moving data from programs able of handling more than 255 characters per field to/from those applications was a nightmare. Currently, the limit is less and less hindering.