我看到 VARCHAR(255) 经常使用(而不是其他长度)是否有充分的理由? [英] Is there a good reason I see VARCHAR(255) used so often (as opposed to another length)?

查看:38
本文介绍了我看到 VARCHAR(255) 经常使用(而不是其他长度)是否有充分的理由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在多个课程、书籍和工作中,我看到定义为 VARCHAR(255) 的文本字段是短"文本的默认值.除了 一个不错的整数?是否是过去某个时候有充分理由(无论今天是否适用)的坚持?

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),这可能表明您不知道最大长度,只知道它是一个较短"的字符串.

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) v tinyblob v tinytext),它表示 VARCHAR(n) 需要 n+1 个字节的存储空间来存储 n<=255, n+2 字节的存储空间,用于 n>255.这是唯一的原因吗?这似乎有点武断,因为与 VARCHAR(256) 相比,您只会节省两个字节,而且您可以通过将其声明为 VARCHAR(253) 来轻松地再节省两个字节.

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).

推荐答案

从历史上看,255 个字符通常是某些 DBMS 中 VARCHAR 的最大长度,有时它仍然是有效的如果您想使用 UTF-8 并为列建立索引(因为索引长度限制),则为最大值.

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).

这篇关于我看到 VARCHAR(255) 经常使用(而不是其他长度)是否有充分的理由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆