Is there a good reason I see VARCHAR(255) used so often (as opposed to another length)?

后端 未结 10 645
Happy的楠姐
Happy的楠姐 2020-12-12 10:38

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

10条回答
  •  眼角桃花
    2020-12-12 11:14

    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.

提交回复
热议问题