MySQL:为什么使用VARCHAR(20)而不是VARCHAR(255)? [英] MySQL: Why use VARCHAR(20) instead of VARCHAR(255)?

查看:436
本文介绍了MySQL:为什么使用VARCHAR(20)而不是VARCHAR(255)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

对所有基于文本的字段使用通用varchar(255)有什么缺点吗?

在MYSQL中,您可以为VARCHAR字段类型选择长度。

In MYSQL you can choose a length for the VARCHAR field type. Possible values are 1-255.

但是,如果使用VARCHAR(255)而不是VARCHAR(20),它的优点是什么?据我所知,条目的大小只取决于插入字符串的实际长度。

But what are its advantages if you use VARCHAR(255) that is the maximum instead of VARCHAR(20)? As far as I know, the size of the entries depends only on the real length of the inserted string.

size(bytes)= length + 1

size (bytes) = length+1

因此,如果在VARCHAR(255)字段中有单词Example,它将有8个字节。如果你在VARCHAR(20)字段中有它,它也将有8个字节。有什么区别?

So if you have the word "Example" in a VARCHAR(255) field, it would have 8 bytes. If you have it in a VARCHAR(20) field, it would have 8 bytes, too. What is the difference?

我希望你能帮助我。提前感谢!

I hope you can help me. Thanks in advance!

推荐答案

签出: Varchar的参考资料

总之,没有太大的区别,除非你去您的VARCHAR中的大小为255,这将需要另一个字节作为长度前缀。

In short there isn't much difference unless you go over the size of 255 in your VARCHAR which will require another byte for the length prefix.

长度表示对存储在列中的数据的约束比其他任何事情更多。这固有地约束列的MAXIMUM存储大小。 IMHO,长度应该对数据有意义。如果你存储一个社会保障#,把长度设置为128是没有意义的,即使它不会花费你的存储空间,如果你实际存储的是一个SSN。

The length indicates more of a constraint on the data stored in the column than anything else. This inherently constrains the MAXIMUM storage size for the column as well. IMHO, the length should make sense with respect to the data. If your storing a Social Security # it makes no sense to set the length to 128 even though it doesn't cost you anything in storage if all you actually store is an SSN.

这篇关于MySQL:为什么使用VARCHAR(20)而不是VARCHAR(255)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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