静下心来总结了下mysql中char和varchar的区别:
1、字符长度:char(255字符 ),varchar(65535字节 ,如果是UTF则最大65535/3 - 1个字符,因为要预留空间存放该字段的字符数目,手册里这么说:In contrast to CHAR
,VARCHAR
values are stored as a one-byte or two-byte length prefix plus data. The length prefix indicates the number of bytes in the value. A column uses one length byte if values require no more than 255 bytes, two length bytes if values may require more than 255 bytes.)
2、存储方式:char(M)会在后面自动补齐空格到M长度,在查询的时候会把后面的空格trim掉。varchar(M)不会在后面补空格,只是会多一个或者两个字节来存储字段字符数。
参考资料:mysql manual