下面是MySQL中VARCHAR类型的说明信息。
使用命令:mysql> ? varchar
Name: ‘VARCHAR’
Description:
[NATIONAL] VARCHAR(M) [CHARACTER SET charset_name] [COLLATE
collation_name]
A variable-length string. M represents the maximum column length in
characters. The range of M is 0 to 65,535. The effective maximum length
of a VARCHAR is subject to the maximum row size (65,535 bytes, which is
shared among all columns) and the character set used. For example, utf8
characters can require up to three bytes per character, so a VARCHAR
column that uses the utf8 character set can be declared to be a maximum
of 21,844 characters. See
https://dev.mysql.com/doc/refman/8.0/en/column-count-limit.html.
MySQL stores VARCHAR values as a 1-byte or 2-byte length prefix plus
data. The length prefix indicates the number of bytes in the value. A
VARCHAR column uses one length byte if values require no more than 255
bytes, two length bytes if values may require more than 255 bytes.
Note:
MySQL follows the standard SQL specification, and does not remove
trailing spaces from VARCHAR values.
VARCHAR is shorthand for CHARACTER VARYING. NATIONAL VARCHAR is the
standard SQL way to define that a VARCHAR column should use some
predefined character set. MySQL uses utf8 as this predefined character
set. https://dev.mysql.com/doc/refman/8.0/en/charset-national.html.
NVARCHAR is shorthand for NATIONAL VARCHAR.*
URL: https://dev.mysql.com/doc/refman/8.0/en/string-type-overview.html
VARCHAR是一个可变长的字符串, M表示的是字符串(字符而不是字节)的最大长度,长度区间为[0, 65535]。
有效的最大长度受限于最大长度65535和使用的字符集(注意,65535是指所有字段中varcha类型的总长度不能超过65535)。
utf8字符集做多会使用3个字节来表示一个字符。
MySQL额外使用一个或者两个字节来表示VARCHAR值的长度。
如果长度不超过255个字节,使用一个字节。
如果长度超过255字节,使用两个字节。
MySQL遵循标准的SQL规范,不会移除VARCHAR值中(首部和尾部)的空格字符。
本文详细解析了MySQL中VARCHAR类型的特性,包括其最大长度限制、字符集影响、存储方式及空格处理规则,帮助读者全面掌握VARCHAR的使用要点。
2108

被折叠的 条评论
为什么被折叠?



