【The CHAR and VARCHAR Types】
The CHAR
and VARCHAR
types are declared with a length that indicates the maximum number of characters you want to store. For example,CHAR(30)
can hold up to 30 characters.
括号中指定宽度。
The length of a CHAR
column is fixed to the length that you declare when you create the table. The length can be any value from 0 to 255. WhenCHAR
values are stored, they are right-padded with spaces to the specified length. When CHAR
values are retrieved, trailing spaces are removed unless the PAD_CHAR_TO_FULL_LENGTH
SQL mode is enabled.
CHAR长度为0-255,固定,取值时尾部空格忽略,除非PAD_CHAR_TO_FULL_LENGTH模式被启用。
Values in VARCHAR
columns are variable-length strings. The length can be specified as a value from 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.
VARCHAR长度为0-65535,VARCHAR与其它列共享最大长度65535。
In contrast to CHAR
, VARCHAR
values are stored as a 1-byte or 2-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.
VARCHAR将长度存储在头部,1-2字节。