Why historically do people use 255 not 256 for database field magnitudes?

前端 未结 12 730
北荒
北荒 2020-12-07 07:17

You often see database fields set to have a magnitude of 255 characters, what is the traditional / historic reason why? I assume it\'s something to do with paging / memory l

相关标签:
12条回答
  • 2020-12-07 08:17

    I think this might answer your question. Looks like it was the max limit of varchar in earlier systems. I took it off another stackoverflow question.

    It's hard to know what the longest postal address is, of course, which is why many people choose a long VARCHAR that is certainly longer than any address. And 255 is customary because it may have been the maximum length of a VARCHAR in some databases in the dawn of time (as well as PostgreSQL until more recently).

    Are there disadvantages to using a generic varchar(255) for all text-based fields?

    0 讨论(0)
  • 2020-12-07 08:19

    255 is the maximum value of a 8 bit integer : 11111111 = 255.

    0 讨论(0)
  • 2020-12-07 08:19

    A maximum length of 255 allows the database engine to use only 1 byte to store the length of each field. You are correct that 1 byte of space allows you to store 2^8=256 distinct values for the length of the string.

    But if you allow the field to store zero-length text strings, you need to be able to store zero in the length. So you can allow 256 distinct length values, starting at zero: 0-255.

    0 讨论(0)
  • 2020-12-07 08:19

    It used to be that all strings required a NUL terminator, or "backslash-zero". Updated databases don't have that. It was "255 characters of text" with a "\0" added automatically at the end so the system knew where the string ended. If you said VARCHAR(256), it would end up being 257 and then you'd be in the next register for one character. Wasteful. That's why everything was VARCHAR(255) and VARCHAR(31). Out of habit the 255 seems to have stuck around but the 31's became 32's and the 511's became 512's. That part is weird. It's hard to make myself write VARCHAR(256).

    0 讨论(0)
  • 2020-12-07 08:20

    255 was the varchar limit in mySQL4 and earlier.

    Also 255 chars + Null terminator = 256

    Or 1 byte length descriptor gives a possible range 0-255 chars

    0 讨论(0)
  • 2020-12-07 08:20

    8 bits unsigned = 256 bytes

    255 characters + byte 0 for length

    0 讨论(0)
提交回复
热议问题