MySQL:为什么使用VARCHAR(20)而不是VARCHAR(255)?

编程入门 行业动态 更新时间:2024-10-24 10:21:59
本文介绍了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)?

本文发布于:2023-10-28 00:58:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535027.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不是   VARCHAR   MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!