CHAR 和 VARCHAR 类型类似,但在存储和检索方式上有所不同。它们在最大长度以及是否保留尾随空格方面也有不同。The CHAR and VARCHAR types
are similar, but differ in the way they are stored and
retrieved. They also differ in maximum length and in whether
trailing spaces are retained.
CHAR 和 VARCHAR 类型声明时会指定长度,该长度表示你希望存储的最大字符数。例如,CHAR(30) 可以容纳最多 30 个字符。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.
CHAR 列的长度固定为创建表时声明的长度。长度可以是 0 到 255 之间的任意值。当存储 CHAR 值时,它们会在右侧用空格填充到指定长度。当检索 CHAR 值时,除非启用了 PAD_CHAR_TO_FULL_LENGTH SQL 模式,否则尾随空格会被移除。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. When CHAR
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.
VARCHAR 列中的值是可变长度的字符串。长度可以指定为 0 到 65,535 之间的值。VARCHAR 的有效最大长度取决于最大行大小(65,535 字节,这在所有列之间共享)以及所使用的字符集。请参阅 第 10.4.7 节,“表列数和行大小的限制”。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. See Section 10.4.7, “Limits on Table Column Count and Row Size”.
与 CHAR 相比,VARCHAR 值存储为 1 字节或 2 字节的长度前缀加上数据。长度前缀指示值中的字节数。如果值不超过 255 字节,列使用一个长度字节;如果值可能超过 255 字节,则使用两个长度字节。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.
如果没有启用严格 SQL 模式,并且您向 CHAR 或 VARCHAR 列分配的值超过了该列的最大长度,则该值会被截断以适应列长度,并生成一个警告。对于非空格字符的截断,您可以通过使用严格 SQL 模式来导致错误发生(而不是警告)并阻止值的插入。参见 第 7.1.11 节 “服务器 SQL 模式”。If strict SQL mode is not enabled and you assign a value to a
CHAR or VARCHAR column
that exceeds the column's maximum length, the value is truncated
to fit and a warning is generated. For truncation of nonspace
characters, you can cause an error to occur (rather than a
warning) and suppress insertion of the value by using strict SQL
mode. See Section 7.1.11, “Server SQL Modes”.
对于 VARCHAR 列,插入前超过列长度的尾随空格会被截断并生成警告,无论使用的是哪种 SQL 模式。对于 CHAR 列,插入值中过多的尾随空格会被静默截断,无论 SQL 模式如何。For VARCHAR columns, trailing spaces in
excess of the column length are truncated prior to insertion and
a warning is generated, regardless of the SQL mode in use. For
CHAR columns, truncation of excess trailing
spaces from inserted values is performed silently regardless of
the SQL mode.
VARCHAR 值在存储时不会被填充。存储和检索值时会保留尾随空格,这符合标准 SQL 的规定。VARCHAR values are not padded when they are
stored. Trailing spaces are retained when values are stored and
retrieved, in conformance with standard SQL.
下面的表格通过展示将各种字符串值存储到 CHAR(4) 和 VARCHAR(4) 列中的结果,说明了 CHAR 和 VARCHAR 之间的差异(假设该列使用单字节字符集,如 latin1)。The following table illustrates the differences between
CHAR and VARCHAR by
showing the result of storing various string values into
CHAR(4) and VARCHAR(4)
columns (assuming that the column uses a single-byte character
set such as latin1).
| 价值Value | CHAR(4) |
所需存储Storage Required | VARCHAR(4) |
所需存储Storage Required |
|---|---|---|---|---|
'' |
' ' |
4 字节4 bytes | '' |
1 字节1 byte |
'ab' |
'ab ' |
4 字节4 bytes | 'ab' |
3 字节3 bytes |
'abcd' |
'abcd' |
4 字节4 bytes | 'abcd' |
5 字节5 bytes |
'abcdefgh' |
'abcd' |
4 字节4 bytes | 'abcd' |
5 字节5 bytes |
表中最后一行所示的存储值仅适用于不使用严格 SQL 模式的情况;如果启用了严格模式,超过列长度的值将不会被存储,并会产生错误。The values shown as stored in the last row of the table apply only when not using strict SQL mode; if strict mode is enabled, values that exceed the column length are not stored, and an error results.
InnoDB 将固定长度字段中大于等于 768 字节的字段编码为可变长度字段,这些字段可以存储在页外。例如,如果字符集的最大字节长度大于 3(如 utf8mb4),则 CHAR(255) 列可以超过 768 字节。InnoDB encodes fixed-length fields greater
than or equal to 768 bytes in length as variable-length fields,
which can be stored off-page. For example, a
CHAR(255) column can exceed 768 bytes if the
maximum byte length of the character set is greater than 3, as
it is with utf8mb4.
如果将某个值存储到 CHAR(4) 和 VARCHAR(4) 列中,从这些列中检索到的值并不总是相同的,因为 CHAR 列在检索时会去掉尾随空格。以下示例说明了这种差异:If a given value is stored into the CHAR(4)
and VARCHAR(4) columns, the values retrieved
from the columns are not always the same because trailing spaces
are removed from CHAR columns upon retrieval.
The following example illustrates this difference:
mysql>CREATE TABLE vc (v VARCHAR(4), c CHAR(4));Query OK, 0 rows affected (0.01 sec) mysql>INSERT INTO vc VALUES ('ab ', 'ab ');Query OK, 1 row affected (0.00 sec) mysql>SELECT CONCAT('(', v, ')'), CONCAT('(', c, ')') FROM vc;+---------------------+---------------------+ | CONCAT('(', v, ')') | CONCAT('(', c, ')') | +---------------------+---------------------+ | (ab ) | (ab) | +---------------------+---------------------+ 1 row in set (0.06 sec)
CHAR、VARCHAR 和 TEXT 列中的值会根据分配给该列的字符集排序规则进行排序和比较。Values in CHAR, VARCHAR,
and TEXT columns are sorted and compared
according to the character set collation assigned to the column.
MySQL 排序规则具有 PAD
SPACE 的填充属性,除了基于 UCA 9.0.0 及更高版本的 Unicode 排序规则,它们具有 NO
PAD 的填充属性。(参见 12.10.1 节,“Unicode 字符集”)MySQL collations have a pad attribute of PAD
SPACE, other than Unicode collations based on UCA
9.0.0 and higher, which have a pad attribute of NO
PAD. (see Section 12.10.1, “Unicode Character Sets”).
要确定排序规则的 pad 属性,请使用 INFORMATION_SCHEMA COLLATIONS 表,该表具有一个 PAD_ATTRIBUTE 列。To determine the pad attribute for a collation, use the
INFORMATION_SCHEMA
COLLATIONS table, which has a
PAD_ATTRIBUTE column.
对于非二进制字符串(CHAR、VARCHAR 和 TEXT 值),字符串排序规则的填充属性决定了在比较字符串末尾的尾随空格时的处理方式。NO PAD 排序规则在比较中将尾随空格视为重要字符,就像其他任何字符一样。PAD SPACE 排序规则在比较中忽略尾随空格;字符串比较时不考虑尾随空格。请参阅 比较中的尾随空格处理。服务器的 SQL 模式对尾随空格的比较行为没有影响。For nonbinary strings (CHAR,
VARCHAR, and TEXT values),
the string collation pad attribute determines treatment in
comparisons of trailing spaces at the end of strings.
NO PAD collations treat trailing spaces as
significant in comparisons, like any other character.
PAD SPACE collations treat trailing spaces as
insignificant in comparisons; strings are compared without
regard to trailing spaces. See
Trailing Space Handling in Comparisons.
The server SQL mode has no effect on comparison behavior with
respect to trailing spaces.
有关 MySQL 字符集和排序规则的更多信息,请参见 第 12 章,字符集、排序规则、Unicode。有关存储需求的更多信息,请参见 第 13.7 节,“数据类型存储需求”。For more information about MySQL character sets and collations, see Chapter 12, Character Sets, Collations, Unicode. For additional information about storage requirements, see Section 13.7, “Data Type Storage Requirements”.
对于那些去掉尾部填充字符或比较时忽略它们的情况,如果某列有要求唯一值的索引,则向该列插入仅在尾部填充字符数量上不同的值会导致重复键错误。例如,如果一个表包含 'a',尝试存储 'a ' 会导致重复键错误。For those cases where trailing pad characters are stripped or
comparisons ignore them, if a column has an index that requires
unique values, inserting into the column values that differ only
in number of trailing pad characters results in a duplicate-key
error. For example, if a table contains 'a',
an attempt to store 'a ' causes a
duplicate-key error.