MySQL-大小VARCHAR [英] MySql - size VARCHAR
问题描述
许多人对我说,设置VARCHAR(100)没有意义.和255一样.我想知道为什么...
Many people say to me that set VARCHAR(100) doesnt make sense. It the same as put 255. I'd like to know why...
推荐答案
那是垃圾.他们可能在谈论varchar
使用一个字节作为长度的事实,而不管最大长度是100还是255(超过该长度将使用两个字节,最大为〜64K),但是它们是区别对待.
That's rubbish. They may be talking about the fact that a varchar
uses one byte for the length regardless of whether the maximum length is 100 or 255 ( lengths above that will use two bytes, up to ~64K) but they are treated differently.
如果在前者中插入一个150个字符的字符串,它将被截断为100,对于后一种情况并非如此.
If you insert a 150-character string into the former, it will be truncated to 100, that's not so for the latter case.
您应该使用有意义的长度.如果您有一列不超过30个字符的列,请不要使用varchar(255)
.
You should use the length that makes sense. If you have a column that will never exceed 30 characters, don't use varchar(255)
.
有关类型的详细信息,请参见此处.
See here for the type details.
这篇关于MySQL-大小VARCHAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!