字符串数据类型有 CHAR、VARCHAR、BINARY、VARBINARY、BLOB、TEXT、ENUM 和 SET。The string data types are CHAR,
VARCHAR,
BINARY,
VARBINARY,
BLOB,
TEXT,
ENUM, and
SET.
在某些情况下,MySQL 可能会将字符串列更改为与 CREATE
TABLE 或 ALTER TABLE 语句中指定的类型不同的类型。请参见 第 15.1.20.7 节,“静默列规范更改”。In some cases, MySQL may change a string column to a type
different from that given in a CREATE
TABLE or ALTER TABLE
statement. See Section 15.1.20.7, “Silent Column Specification Changes”.
对于字符字符串列的定义(CHAR、VARCHAR 和 TEXT 类型),MySQL 以字符单位解释长度规格。对于二进制字符串列的定义(BINARY、VARBINARY 和 BLOB 类型),MySQL 以字节单位解释长度规格。For definitions of character string columns
(CHAR,
VARCHAR, and the
TEXT types), MySQL interprets
length specifications in character units. For definitions of
binary string columns (BINARY,
VARBINARY, and the
BLOB types), MySQL interprets
length specifications in byte units.
字符字符串数据类型 CHAR、VARCHAR、TEXT 类型、ENUM、SET 以及任何同义词的列定义,可以指定列的字符集和排序规则:Column definitions for character string data types
CHAR,
VARCHAR, the
TEXT types,
ENUM,
SET, and any synonyms) can
specify the column character set and collation:
CHARACTER SET用于指定字符集。如果需要,可以使用COLLATE属性指定字符集的排序规则,以及其他任何属性。例如:CHARACTER SETspecifies the character set. If desired, a collation for the character set can be specified with theCOLLATEattribute, along with any other attributes. For example:CREATE TABLE t ( c1 VARCHAR(20) CHARACTER SET utf8mb4, c2 TEXT CHARACTER SET latin1 COLLATE latin1_general_cs );此表定义创建了一个名为
c1的列,其字符集为utf8mb4,使用该字符集的默认排序规则;以及一个名为c2的列,其字符集为latin1,并且使用区分大小写的排序规则(_cs)。This table definition creates a column namedc1that has a character set ofutf8mb4with the default collation for that character set, and a column namedc2that has a character set oflatin1and a case-sensitive (_cs) collation.当
CHARACTER SET或COLLATE属性缺失时,分配字符集和排序规则的规则在 第 12.3.5 节,“列的字符集和排序规则” 中有描述。The rules for assigning the character set and collation when either or both ofCHARACTER SETand theCOLLATEattribute are missing are described in Section 12.3.5, “Column Character Set and Collation”.CHARSET是CHARACTER SET的同义词。CHARSETis a synonym forCHARACTER SET.为字符字符串数据类型指定
CHARACTER SET binary属性会导致该列被创建为相应的二进制字符串数据类型:CHAR变为BINARY,VARCHAR变为VARBINARY,而TEXT变为BLOB。对于ENUM和SET数据类型,则不会发生这种情况;它们将按声明创建。假设你使用以下定义来指定表:Specifying theCHARACTER SET binaryattribute for a character string data type causes the column to be created as the corresponding binary string data type:CHARbecomesBINARY,VARCHARbecomesVARBINARY, andTEXTbecomesBLOB. For theENUMandSETdata types, this does not occur; they are created as declared. Suppose that you specify a table using this definition:CREATE TABLE t ( c1 VARCHAR(10) CHARACTER SET binary, c2 TEXT CHARACTER SET binary, c3 ENUM('a','b','c') CHARACTER SET binary );生成的表具有以下定义:The resulting table has this definition:
CREATE TABLE t ( c1 VARBINARY(10), c2 BLOB, c3 ENUM('a','b','c') CHARACTER SET binary );BINARY属性是一个非标准的 MySQL 扩展,它是指定列字符集的二进制(_bin)排序规则的简写(如果未指定列字符集,则使用表的默认字符集)。在这种情况下,比较和排序是基于字符的数值编码进行的。假设你使用以下定义来创建一个表:TheBINARYattribute is a nonstandard MySQL extension that is shorthand for specifying the binary (_bin) collation of the column character set (or of the table default character set if no column character set is specified). In this case, comparison and sorting are based on numeric character code values. Suppose that you specify a table using this definition:CREATE TABLE t ( c1 VARCHAR(10) CHARACTER SET latin1 BINARY, c2 TEXT BINARY ) CHARACTER SET utf8mb4;
生成的表具有以下定义:The resulting table has this definition:
CREATE TABLE t ( c1 VARCHAR(10) CHARACTER SET latin1 COLLATE latin1_bin, c2 TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin ) CHARACTER SET utf8mb4;
在 MySQL 8.4 中,
BINARY属性已被弃用,并且你应当预期在未来版本的 MySQL 中它将不再受支持。应用程序应调整为使用显式的_bin排序规则。In MySQL 8.4, theBINARYattribute is deprecated and you should expect support for it to be removed in a future version of MySQL. Applications should be adjusted to use an explicit_bincollation instead.使用
BINARY来指定数据类型或字符集的方式保持不变。The use ofBINARYto specify a data type or character set remains unchanged.ASCII属性是CHARACTER SET latin1的简写。在旧版本的 MySQL 中受支持,但ASCII已被弃用;请改用CHARACTER SET。TheASCIIattribute is shorthand forCHARACTER SET latin1. Supported in older MySQL releases,ASCIIis deprecated; useCHARACTER SETinstead.UNICODE属性是CHARACTER SET ucs2的简写。在旧版本的 MySQL 中受支持,但UNICODE已被弃用;请改用CHARACTER SET。TheUNICODEattribute is shorthand forCHARACTER SET ucs2. Supported in older MySQL releases,UNICODEis deprecated; useCHARACTER SETinstead.
字符列的比较和排序是基于分配给该列的排序规则。对于 CHAR、VARCHAR、TEXT、ENUM 和 SET 数据类型,你可以使用二进制 (_bin) 排序规则或 BINARY 属性来声明列,从而使比较和排序使用底层字符编码值而不是词汇顺序。Character column comparison and sorting are based on the
collation assigned to the column. For the
CHAR,
VARCHAR,
TEXT,
ENUM, and
SET data types, you can declare a
column with a binary (_bin) collation or the
BINARY attribute to cause comparison and
sorting to use the underlying character code values rather than
a lexical ordering.
有关在 MySQL 中使用字符集的更多信息,请参见 第 12 章,字符集、排序规则、Unicode。For additional information about use of character sets in MySQL, see Chapter 12, Character Sets, Collations, Unicode.
[NATIONAL] CHAR[(M)] [CHARACTER SETcharset_name] [COLLATEcollation_name]固定长度的字符串,存储时总是用空格在右侧填充到指定长度。
M表示列的字符长度。M的范围是 0 到 255。如果省略M,长度为 1。A fixed-length string that is always right-padded with spaces to the specified length when stored.Mrepresents the column length in characters. The range ofMis 0 to 255. IfMis omitted, the length is 1.Note在检索
CHAR值时,会去除尾随空格,除非启用了PAD_CHAR_TO_FULL_LENGTHSQL 模式。Trailing spaces are removed whenCHARvalues are retrieved unless thePAD_CHAR_TO_FULL_LENGTHSQL mode is enabled.CHAR是CHARACTER的缩写。NATIONAL CHAR(或其等效的简写形式NCHAR)是 SQL 定义CHAR列应使用某个预定义字符集的标准方式。MySQL 使用utf8mb3作为此预定义字符集。第 12.3.7 节,“国家字符集”。CHARis shorthand forCHARACTER.NATIONAL CHAR(or its equivalent short form,NCHAR) is the standard SQL way to define that aCHARcolumn should use some predefined character set. MySQL usesutf8mb3as this predefined character set. Section 12.3.7, “The National Character Set”.CHAR BYTE数据类型是BINARY数据类型的别名。这是一个兼容性特性。TheCHAR BYTEdata type is an alias for theBINARYdata type. This is a compatibility feature.MySQL 允许你创建类型为
CHAR(0)的列。当你必须兼容依赖列存在但实际上不使用其值的旧应用程序时,这非常有用。CHAR(0)也非常适合在你需要一个只能取两个值的列时使用:定义为CHAR(0) NULL的列只占用一位,并且只能取值NULL和''(空字符串)。MySQL permits you to create a column of typeCHAR(0). This is useful primarily when you must be compliant with old applications that depend on the existence of a column but that do not actually use its value.CHAR(0)is also quite nice when you need a column that can take only two values: A column that is defined asCHAR(0) NULLoccupies only one bit and can take only the valuesNULLand''(the empty string).[NATIONAL] VARCHAR(M) [CHARACTER SET charset_name] [COLLATE collation_name][NATIONAL] VARCHAR(M) [CHARACTER SETcharset_name] [COLLATEcollation_name]一个可变长度的字符串。
M表示列的最大字符长度。M的范围是 0 到 65,535。VARCHAR的有效最大长度受最大行大小(65,535 字节,所有列共享)和使用的字符集影响。例如,utf8mb3字符每个字符可能需要最多三字节,因此使用utf8mb3字符集的VARCHAR列最大可以声明为 21,844 个字符。参见 10.4.7 节,“表列数和行大小的限制”。A variable-length string.Mrepresents the maximum column length in characters. The range ofMis 0 to 65,535. The effective maximum length of aVARCHARis subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used. For example,utf8mb3characters can require up to three bytes per character, so aVARCHARcolumn that uses theutf8mb3character set can be declared to be a maximum of 21,844 characters. See Section 10.4.7, “Limits on Table Column Count and Row Size”.MySQL 将
VARCHAR值存储为 1 字节或 2 字节的长度前缀加数据。长度前缀表示值中的字节数。如果值不超过 255 字节,VARCHAR列使用一个长度字节;如果值可能超过 255 字节,则使用两个长度字节。MySQL storesVARCHARvalues as a 1-byte or 2-byte length prefix plus data. The length prefix indicates the number of bytes in the value. AVARCHARcolumn uses one length byte if values require no more than 255 bytes, two length bytes if values may require more than 255 bytes.NoteMySQL 遵循标准 SQL 规范,并且 不会 删除
VARCHAR值末尾的空格。MySQL follows the standard SQL specification, and does not remove trailing spaces fromVARCHARvalues.VARCHAR是CHARACTER VARYING的简写。NATIONAL VARCHAR是标准 SQL 定义VARCHAR列应使用某些预定义字符集的方式。MySQL 使用utf8mb3作为此预定义字符集。第 12.3.7 节,“国家字符集”。NVARCHAR是NATIONAL VARCHAR的简写。VARCHARis shorthand forCHARACTER VARYING.NATIONAL VARCHARis the standard SQL way to define that aVARCHARcolumn should use some predefined character set. MySQL usesutf8mb3as this predefined character set. Section 12.3.7, “The National Character Set”.NVARCHARis shorthand forNATIONAL VARCHAR.BINARY类型类似于CHAR类型,但存储的是二进制字节字符串,而不是非二进制字符字符串。可选的长度M表示列的字节长度。如果省略,M默认为 1。TheBINARYtype is similar to theCHARtype, but stores binary byte strings rather than nonbinary character strings. An optional lengthMrepresents the column length in bytes. If omitted,Mdefaults to 1.VARBINARY类型类似于VARCHAR类型,但存储的是二进制字节字符串,而不是非二进制字符字符串。M表示列的最大长度(以字节为单位)。TheVARBINARYtype is similar to theVARCHARtype, but stores binary byte strings rather than nonbinary character strings.Mrepresents the maximum column length in bytes.一个最大长度为 255(28 − 1)字节的
BLOB列。每个TINYBLOB值使用 1 字节长度前缀存储,该前缀表示值中的字节数。ABLOBcolumn with a maximum length of 255 (28 − 1) bytes. EachTINYBLOBvalue is stored using a 1-byte length prefix that indicates the number of bytes in the value.TINYTEXT [CHARACTER SET charset_name] [COLLATE collation_name]TINYTEXT [CHARACTER SETcharset_name] [COLLATEcollation_name]一个
TEXT列,最大长度为 255(28 − 1)个字符。如果值包含多字节字符,有效最大长度会更小。每个TINYTEXT值使用 1 字节长度前缀存储,该前缀表示值中的字节数。ATEXTcolumn with a maximum length of 255 (28 − 1) characters. The effective maximum length is less if the value contains multibyte characters. EachTINYTEXTvalue is stored using a 1-byte length prefix that indicates the number of bytes in the value.一个最大长度为 65,535(216 − 1)字节的
BLOB列。每个BLOB值使用 2 字节长度前缀存储,该前缀表示值的字节数。ABLOBcolumn with a maximum length of 65,535 (216 − 1) bytes. EachBLOBvalue is stored using a 2-byte length prefix that indicates the number of bytes in the value.可为此类型提供可选长度
M。如果这样做,MySQL 会将列创建为最小的BLOB类型,以容纳长度为M字节的值。An optional lengthMcan be given for this type. If this is done, MySQL creates the column as the smallestBLOBtype large enough to hold valuesMbytes long.TEXT[(M)] [CHARACTER SET charset_name] [COLLATE collation_name]TEXT[(M)] [CHARACTER SETcharset_name] [COLLATEcollation_name]一个
TEXT列,最大长度为 65,535 (216 − 1) 字节。如果值包含多字节字符,有效最大长度会减少。每个TEXT值使用一个 2 字节长度前缀存储,该前缀表示值中的字节数。ATEXTcolumn with a maximum length of 65,535 (216 − 1) bytes. The effective maximum length is less if the value contains multibyte characters. EachTEXTvalue is stored using a 2-byte length prefix that indicates the number of bytes in the value.此类型可以指定一个可选长度
M。如果指定,MySQL 会创建一个最小的TEXT类型列,足够存放长度为M个字符的值。An optional lengthMcan be given for this type. If this is done, MySQL creates the column as the smallestTEXTtype large enough to hold valuesMcharacters long.最大长度为 16,777,215(224 − 1)字节的
BLOB列。每个MEDIUMBLOB值都使用 3 字节长度前缀存储,用于指示该值的字节数。ABLOBcolumn with a maximum length of 16,777,215 (224 − 1) bytes. EachMEDIUMBLOBvalue is stored using a 3-byte length prefix that indicates the number of bytes in the value.MEDIUMTEXT [CHARACTER SET charset_name] [COLLATE collation_name]MEDIUMTEXT [CHARACTER SETcharset_name] [COLLATEcollation_name]一个最大长度为 16,777,215 (224 − 1) 个字符的
TEXT列。如果值包含多字节字符,则有效最大长度会更小。每个MEDIUMTEXT值都使用 3 字节长度前缀存储,该前缀指示值的字节数。ATEXTcolumn with a maximum length of 16,777,215 (224 − 1) characters. The effective maximum length is less if the value contains multibyte characters. EachMEDIUMTEXTvalue is stored using a 3-byte length prefix that indicates the number of bytes in the value.一个最大长度为 4,294,967,295 或 4GB (232 − 1) 字节的
BLOB列。LONGBLOB列的实际最大长度取决于客户端/服务器协议中配置的最大数据包大小和可用内存。每个LONGBLOB值都使用一个 4 字节的长度前缀存储,该前缀指示值的字节数。ABLOBcolumn with a maximum length of 4,294,967,295 or 4GB (232 − 1) bytes. The effective maximum length ofLONGBLOBcolumns depends on the configured maximum packet size in the client/server protocol and available memory. EachLONGBLOBvalue is stored using a 4-byte length prefix that indicates the number of bytes in the value.LONGTEXT [CHARACTER SET charset_name] [COLLATE collation_name]LONGTEXT [CHARACTER SETcharset_name] [COLLATEcollation_name]一个
TEXT列,最大长度为 4,294,967,295 或 4GB(232 − 1)个字符。如果值包含多字节字符,有效最大长度会更小。LONGTEXT列的有效最大长度还取决于客户端/服务器协议中配置的最大数据包大小和可用内存。每个LONGTEXT值使用 4 字节长度前缀存储,该前缀指示值中的字节数。ATEXTcolumn with a maximum length of 4,294,967,295 or 4GB (232 − 1) characters. The effective maximum length is less if the value contains multibyte characters. The effective maximum length ofLONGTEXTcolumns also depends on the configured maximum packet size in the client/server protocol and available memory. EachLONGTEXTvalue is stored using a 4-byte length prefix that indicates the number of bytes in the value.ENUM('value1','value2',...) [CHARACTER SET charset_name] [COLLATE collation_name]ENUM('value1','value2',...) [CHARACTER SETcharset_name] [COLLATEcollation_name]枚举。一个字符串对象,它只能有一个值,从值列表
'、value1''、value2'...、NULL或特殊的''错误值中选择。ENUM值在内部表示为整数。An enumeration. A string object that can have only one value, chosen from the list of values',value1'',value2'...,NULLor the special''error value.ENUMvalues are represented internally as integers.ENUM列可以有最多 65,535 个不同的元素。AnENUMcolumn can have a maximum of 65,535 distinct elements.单个
ENUM元素支持的最大长度为M<= 255 且 (Mxw) <= 1020,其中M是元素字面长度,w是字符集中最大长度字符所需的字节数。The maximum supported length of an individualENUMelement isM<= 255 and (Mxw) <= 1020, whereMis the element literal length andwis the number of bytes required for the maximum-length character in the character set.SET('value1','value2',...) [CHARACTER SET charset_name] [COLLATE collation_name]SET('value1','value2',...) [CHARACTER SETcharset_name] [COLLATEcollation_name]集合。一个字符串对象,可以有零个或多个值,每个值必须从值列表
',value1'',value2'...中选择。SET的值在内部表示为整数。A set. A string object that can have zero or more values, each of which must be chosen from the list of values',value1'',value2'...SETvalues are represented internally as integers.一个
SET列最多可以有 64 个不同的成员。ASETcolumn can have a maximum of 64 distinct members.单个
SET元素支持的最大长度为M<= 255 且 (Mxw) <= 1020,其中M为元素的字面长度,w为字符集中最长字符所需的字节数。The maximum supported length of an individualSETelement isM<= 255 and (Mxw) <= 1020, whereMis the element literal length andwis the number of bytes required for the maximum-length character in the character set.