探究MySQL中varchar的定义长度
UTF8字符集下:
SQL>createtabletest(idintauto_increment,namevarchar(10),primarykey(id)); SQL>insertintotestvalues(null,'1234567890'); QueryOK,1rowaffected(0.00sec) SQL>insertintotestvalues(null,'一二三四五六七八九十'); QueryOK,1rowaffected(0.00sec) SQL>insertintotestvalues(null,'abcdefghig'); QueryOK,1rowaffected(0.01sec) SQL>insertintotestvalues(null,12345678901); ERROR1406(22001):Datatoolongforcolumn'name'atrow1 SQL>insertintotestvalues(null,'一二三四五六七八九十1'); ERROR1406(22001):Datatoolongforcolumn'name'atrow1 SQL>insertintotestvalues(null,'一二三四五六七八九十一'); ERROR1406(22001):Datatoolongforcolumn'name'atrow1 SQL>selectid,name,length(name),char_length(name)fromtest; +----+--------------------------------+--------------+-------------------+ |id|name|length(name)|char_length(name)| +----+--------------------------------+--------------+-------------------+ |1|1234567890|10|10| |2|一二三四五六七八九十|30|10| |3|abcdefghig|10|10| +----+--------------------------------+--------------+-------------------+ 3rowsinset(0.00sec)
GBK字符集下:
SQL>createtabletest(idintauto_increment,namevarchar(10),primarykey(id)); SQL>insertintotestvalues(null,'1234567890'); QueryOK,1rowaffected(0.00sec) SQL>insertintotestvalues(null,'一二三四五六七八九十'); QueryOK,1rowaffected(0.00sec) SQL>insertintotestvalues(null,'abcdefghig'); QueryOK,1rowaffected(0.01sec) SQL>insertintotestvalues(null,12345678901); ERROR1406(22001):Datatoolongforcolumn'name'atrow1 SQL>insertintotestvalues(null,'一二三四五六七八九十1'); ERROR1406(22001):Datatoolongforcolumn'name'atrow1 SQL>insertintotestvalues(null,'一二三四五六七八九十一'); ERROR1406(22001):Datatoolongforcolumn'name'atrow1 SQL>selectid,name,length(name),char_length(name)fromtest; +----+----------------------+--------------+-------------------+ |id|name|length(name)|char_length(name)| +----+----------------------+--------------+-------------------+ |1|1234567890|10|10| |2|一二三四五六七八九十|20|10| |3|abcdefghig|10|10| +----+----------------------+--------------+-------------------+ 3rowsinset(0.00sec)
由此可见,varchar定义的长度的单位是字符,哪怕是1个多字节字符也是1个字符,如中文和英文字母都被当作1个字符来对待。
那么varchar能够定义的最大长度是多少呢?这个和你当前所使用的字符集有关。抛开字符,其最大长度为65535字节(这是最大行大小,由所有列共享),而放在不同的字符集下,能够定义的最大长度就会有所不同,如UTF8下是21845。据说MySQL5中varchar的长度也为字符,而MySQL4中的则为字节,未经证实,感兴趣的有环境可以自己测下。
顺便补充一下,char数据类型定义的长度也为字符,其最大长度为255。
SQL>createtabletest(idintauto_increment,namechar(5),primarykey(id)); QueryOK,0rowsaffected(0.09sec) SQL>insertintotestvalues(null,'123'); QueryOK,1rowaffected(0.00sec) SQL>insertintotestvalues(null,'12345'); QueryOK,1rowaffected(0.00sec) SQL>insertintotestvalues(null,'一二三'); QueryOK,1rowaffected(0.00sec) SQL>insertintotestvalues(null,'一二三四五'); QueryOK,1rowaffected(0.00sec) SQL>insertintotestvalues(null,123456); ERROR1406(22001):Datatoolongforcolumn'name'atrow1 SQL>insertintotestvalues(null,'一二三四五1'); ERROR1406(22001):Datatoolongforcolumn'name'atrow1 SQL>selectid,name,length(name),char_length(name)fromtest; +----+-----------------+--------------+-------------------+ |id|name|length(name)|char_length(name)| +----+-----------------+--------------+-------------------+ |1|123|3|3| |2|12345|5|5| |3|一二三|9|3| |4|一二三四五|15|5| +----+-----------------+--------------+-------------------+ 4rowsinset(0.00sec)