database 我看到 VARCHAR(255) 经常使用(而不是其他长度)是否有充分的理由?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1217466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a good reason I see VARCHAR(255) used so often (as opposed to another length)?
提问by Kip
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 of 255 is chosen so often, other than being a nice round number? Is it a holdout from some time in the past when there was a good reason (whether or not it applies today)?
在多个课程、书籍和工作中,我看到定义为 VARCHAR(255) 的文本字段是“短”文本的默认类型。除了是一个不错的整数之外,是否有任何充分的理由经常选择 255 的长度?它是否是过去某个时候有充分理由的坚持(无论今天是否适用)?
I realize, of course, that a tighter limit would be more ideal, if you somehow know the maximum length of the string. But if you are using VARCHAR(255) that probably indicates that you don't know the max length, only that it is a "shortish" string.
我当然意识到,如果您以某种方式知道字符串的最大长度,那么更严格的限制会更理想。但是,如果您使用的 VARCHAR(255) 可能表明您不知道最大长度,只是它是一个“较短”的字符串。
Note: I found this question (varchar(255) v tinyblob v tinytext), which says that VARCHAR(n) requires n+1 bytes of storage for n<=255, n+2 bytes of storage for n>255. Is this the only reason? That seems kind of arbitrary, since you would only be saving two bytes compared to VARCHAR(256), and you could just as easily save another two bytes by declaring it VARCHAR(253).
注意:我发现了这个问题(varchar(255) v tinyblob v tinytext),它说 VARCHAR( n) 需要n+1 个字节的存储空间用于n<=255,n+2 个字节的存储空间用于n>255。这是唯一的原因吗?这似乎有点武断,因为与 VARCHAR(256) 相比,您只会节省两个字节,并且您可以通过将其声明为 VARCHAR(253) 来轻松地节省另外两个字节。
采纳答案by chaos
Historically, 255 characters has often been the maximum length of a VARCHAR
in some DBMSes, and it sometimes still winds up being the effective maximum if you want to use UTF-8 and have the column indexed (because of index length limitations).
从历史上看,255 个字符通常是VARCHAR
某些 DBMS中 a 的最大长度,如果您想使用 UTF-8 并对列进行索引(因为索引长度限制),它有时仍然是有效的最大值。
回答by Robert Harvey
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.
使用 255 是因为它是可以用 8 位数字计算的最大字符数。它最大限度地利用了 8 位计数,而无需轻率地要求另一个完整字节来计算 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.
以这种方式使用时,VarChar 仅使用字节数 + 1 来存储您的文本,因此您最好将其设置为 255,除非您希望对字段中的字符数进行硬限制(如 50)。
回答by Charles Bretana
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.
可能是因为 SQL Server 和 Sybase(举出我熟悉的两个)过去在VARCHAR
列中的字符数中最多有 255 个字符。对于 SQL Server,这在 1996/1997 年左右的第 7 版中发生了变化……但旧习惯有时很难改掉。
回答by MusiGenesis
I'm going to answer the literal question: no, there isn't a good reason you see VARCHAR(255) used so often (there are indeed reasons, as discussed in the other answers, just not good ones). You won't find many examples of projects that have failed catastrophically because the architect chose VARCHAR(300) instead of VARCHAR(255). This would be an issue of near-total insignificance even if you were talking about CHAR instead of VARCHAR.
我将回答字面问题: 不,您没有充分理由看到 VARCHAR(255) 经常使用(确实有原因,正如其他答案中所讨论的,只是不是很好的原因)。您不会找到许多灾难性失败的项目示例,因为架构师选择了 VARCHAR(300) 而不是 VARCHAR(255)。即使您谈论的是 CHAR 而不是 VARCHAR,这也将是一个几乎完全无关紧要的问题。
回答by exec.-
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.
当你说2^8
你得到时256
,但计算机术语中的数字是从数字开始的0
。所以,然后你得到了255
,你可以在 IP 的互联网掩码中或在 IP 本身中探测它。
255
is the maximum value of a 8 bit integer : 11111111 = 255
255
是 8 位整数的最大值: 11111111 = 255
Does that help?
这有帮助吗?
回答by Stefano Borini
Note: I found this question (varchar(255) v tinyblob v tinytext), which says that VARCHAR(n) requires n+1 bytes of storage for n<=255, n+2 bytes of storage for n>255. Is this the only reason? That seems kind of arbitrary, since you would only be saving two bytes compared to VARCHAR(256), and you could just as easily save another two bytes by declaring it VARCHAR(253).
注意:我发现了这个问题(varchar(255) v tinyblob v tinytext),它说 VARCHAR( n) 需要n+1 个字节的存储空间用于n<=255,n+2 个字节的存储空间用于n>255。这是唯一的原因吗?这似乎有点武断,因为与 VARCHAR(256) 相比,您只会节省两个字节,并且您可以通过将其声明为 VARCHAR(253) 来轻松地节省另外两个字节。
No. you don't save two bytes by declaring 253. The implementation of the varchar is most likely a length counter and a variable length, nonterminated array. This means that if you store "hello" in a varchar(255) you will occupy 6 bytes: one byte for the length (the number 5) and 5 bytes for the five letters.
不,你不会通过声明 253 来节省两个字节。 varchar 的实现很可能是一个长度计数器和一个可变长度的非终止数组。这意味着如果您将“hello”存储在 varchar(255) 中,您将占用 6 个字节:1 个字节用于长度(数字 5),5 个字节用于五个字母。
回答by Joaquin Montes
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.
在许多应用程序中,例如 MsOffice(直到版本 2000 或 2002),每个单元格的最大字符数为 255。将数据从能够处理每个字段超过 255 个字符的程序移入/移出这些应用程序是一场噩梦。目前,限制越来越少。
回答by gahooa
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?) :)
一个无符号的 1 字节数可以包含 [0-255] 范围内的值。所以当你看到 255 时,主要是因为程序员在基础上思考10
(开玩笑?):)
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.
实际上,有一段时间,255 是您可以在 MySQL 中提供 VARCHAR 的最大大小,并且在索引和其他问题上使用 VARCHAR 优于 TEXT。
回答by Booji Boy
Another reason may be that in very old data access libraries on Windows such as RDO and ADO (COM version not ADO.NET) you had to call a special method, GetChunk, to get data from a column with more than 255 chars. If you limited a varchar column to 255, this extra code wasn't necessary.
另一个原因可能是,在 Windows 上非常旧的数据访问库中,例如 RDO 和 ADO(COM 版本不是 ADO.NET),您必须调用特殊方法 GetChunk,才能从超过 255 个字符的列中获取数据。如果将 varchar 列限制为 255,则不需要此额外代码。
回答by ScottyBlades
0000 0000-> this is an 8-bit binary number. A digit represents a bit.
0000 0000-> 这是一个 8 位二进制数。一个数字代表一个位。
You count like so:
你是这样算的:
0000 0000→ (0)
0000 0000→ (0)
0000 0001→ (1)
0000 0001→ (1)
0000 0010→ (2)
0000 0010→ (2)
0000 0011→ (3)
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.
我们减去一个,因为第一个数字是 0。
255 can hold quite a bit (no pun intended) of values.
255 可以容纳相当多的值(没有双关语意)。
As we use more bits the max value goes up exponentially. Therefore for many purposes, adding more bits is overkill.
随着我们使用更多位,最大值呈指数上升。因此,出于许多目的,添加更多位是过大的。