可能重复: 对所有基于文本的字段使用通用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)?
发布评论