The CHAR
and VARCHAR
types are similar, but differ in the way they are stored and retrieved. CHAR
和VARCHAR
类型相似,但在存储和检索方式上有所不同。They also differ in maximum length and in whether trailing spaces are retained.它们在最大长度和是否保留尾随空格方面也有所不同。
The CHAR
and VARCHAR
types are declared with a length that indicates the maximum number of characters you want to store. CHAR
和VARCHAR
类型的声明长度指示要存储的最大字符数。For example, 例如,CHAR(30)
can hold up to 30 characters.CHAR(30)
最多可以容纳30个字符。
The length of a CHAR
column is fixed to the length that you declare when you create the table. CHAR
列的长度固定为创建表时声明的长度。The length can be any value from 0 to 255. 长度可以是0到255之间的任意值。When 当存储CHAR
values are stored, they are right-padded with spaces to the specified length. CHAR
值时,它们用空格右填充到指定的长度。When 检索字符值时,除非启用了CHAR
values are retrieved, trailing spaces are removed unless the PAD_CHAR_TO_FULL_LENGTH
SQL mode is enabled.PAD_CHAR_TO_FULL_LENGTH
SQL模式,否则将删除尾随空格。
Values in VARCHAR
columns are variable-length strings. VARCHAR
列中的值是可变长度的字符串。The length can be specified as a value from 0 to 65,535. 长度可以指定为0到65535之间的值。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
的有效最大长度取决于最大行大小(65535字节,在所有列中共享)和使用的字符集。See Section 8.4.7, “Limits on Table Column Count and Row Size”.请参阅第8.4.7节,“表列计数和行大小限制”。
In contrast to 与CHAR
, VARCHAR
values are stored as a 1-byte or 2-byte length prefix plus data. CHAR
不同,VARCHAR
值存储为1字节或2字节长度的前缀加上数据。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.如果值不需要超过255字节,则列使用一个长度字节;如果值可能需要超过255字节,则使用两个长度字节。
If strict SQL mode is not enabled and you assign a value to a 如果未启用严格SQL模式,并且为CHAR
or VARCHAR
column that exceeds the column's maximum length, the value is truncated to fit and a warning is generated. CHAR
或VARCHAR
列指定的值超过了该列的最大长度,则会截断该值以适合该列,并生成警告。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. 对于非空格字符的截断,可以导致出现错误(而不是警告),并通过使用严格的SQL模式禁止插入值。See Section 5.1.11, “Server SQL Modes”.请参阅第5.1.11节“服务器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. VARCHAR
列,超过列长度的尾随空格在插入之前会被截断,并生成警告,而不管使用的是哪种SQL模式。For 对于CHAR
columns, truncation of excess trailing spaces from inserted values is performed silently regardless of the SQL mode.CHAR
列,不管SQL模式如何,从插入的值中截取多余的尾随空格都将以静默方式执行。
VARCHAR
values are not padded when they are stored. VARCHAR
值在存储时不填充。Trailing spaces are retained when values are stored and retrieved, in conformance with standard SQL.根据标准SQL,在存储和检索值时保留尾随空格。
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
).CHAR(4)
和VARCHAR(4)
列(假设该列使用单字节字符集,如latin1
)的结果来说明CHAR
和VARCHAR
之间的差异。
Value | CHAR(4) | VARCHAR(4) | ||
---|---|---|---|---|
'' | ' ' | 4 bytes | '' | 1 byte |
'ab' | 'ab ' | 4 bytes | 'ab' | 3 bytes |
'abcd' | 'abcd' | 4 bytes | 'abcd' | 5 bytes |
'abcdefgh' | 'abcd' | 4 bytes | 'abcd' | 5 bytes |
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.表的最后一行中存储的值仅在不使用严格SQL模式时适用;如果启用严格模式,则不会存储超过列长度的值,并会导致错误。
InnoDB
encodes fixed-length fields greater than or equal to 768 bytes in length as variable-length fields, which can be stored off-page. InnoDB
将长度大于或等于768字节的固定长度字段编码为可变长度字段,可以在页外存储。For example, a 例如,如果字符集的最大字节长度大于3,则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(255)
列可以超过768字节,就像utf8mb4
一样。
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. CHAR(4)
和VARCHAR(4)
列中,则从这些列检索到的值并不总是相同的,因为在检索时会从CHAR
列中删除尾随空格。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)
Values in CHAR
, VARCHAR
, and TEXT
columns are sorted and compared according to the character set collation assigned to the column.CHAR
、VARCHAR
和TEXT
列中的值根据分配给该列的字符集排序规则进行排序和比较。
MySQL collations have a pad attribute of MySQL排序规则的pad属性为PAD SPACE
, other than Unicode collations based on UCA 9.0.0 and higher, which have a pad attribute of NO PAD
. PAD SPACE
,而基于UCA 9.0.0及更高版本的Unicode排序规则的pad属性为NO PAD
。(see Section 10.10.1, “Unicode Character Sets”).(见第10.10.1节“Unicode字符集”)。
To determine the pad attribute for a collation, use the 要确定排序规则的pad属性,请使用INFORMATION_SCHEMA
COLLATIONS
table, which has a PAD_ATTRIBUTE
column.INFORMATION_SCHEMA
COLLATIONS
表,该表具有PAD_ATTRIBUTE
列。
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. CHAR
、VARCHAR
和TEXT
值),字符串排序规则pad属性确定在比较字符串末尾的尾随空格时的处理方式。与任何其他字符一样,NO PAD
collations treat trailing spaces as significant in comparisons, like any other character. NO PaD
排序规则在比较中将尾随空格视为重要字符。PAD SPACE
collations treat trailing spaces as insignificant in comparisons; strings are compared without regard to trailing spaces. PAD SPACE
排序规则将尾部空间视为在比较中不重要的空间;比较字符串时不考虑尾随空格。See Trailing Space Handling in Comparisons. 请参阅比较中的尾随空间处理。The server SQL mode has no effect on comparison behavior with respect to trailing spaces.服务器SQL模式对尾部空格的比较行为没有影响。
For more information about MySQL character sets and collations, see Chapter 10, Character Sets, Collations, Unicode. 有关MySQL字符集和排序规则的更多信息,请参阅第10章,“字符集,排序规则,Unicode”。For additional information about storage requirements, see Section 11.7, “Data Type Storage Requirements”.有关存储要求的更多信息,请参阅第11.7节“数据类型存储要求”。
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.'a'
,则尝试存储'a '
,导致重复密钥错误。