8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 97%

13.3.1 字符串数据类型语法13.3.1 String Data Type Syntax

字符串数据类型有 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 SET specifies the character set. If desired, a collation for the character set can be specified with the COLLATE attribute, 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 named c1 that has a character set of utf8mb4 with the default collation for that character set, and a column named c2 that has a character set of latin1 and a case-sensitive (_cs) collation.

    当 CHARACTER SET 或 COLLATE 属性缺失时,分配字符集和排序规则的规则在 第 12.3.5 节,“列的字符集和排序规则” 中有描述。The rules for assigning the character set and collation when either or both of CHARACTER SET and the COLLATE attribute are missing are described in Section 12.3.5, “Column Character Set and Collation”.

    CHARSET 是 CHARACTER SET 的同义词。CHARSET is a synonym for CHARACTER SET.

  • 为字符字符串数据类型指定 CHARACTER SET binary 属性会导致该列被创建为相应的二进制字符串数据类型:CHAR 变为 BINARY,VARCHAR 变为 VARBINARY,而 TEXT 变为 BLOB。对于 ENUM 和 SET 数据类型,则不会发生这种情况;它们将按声明创建。假设你使用以下定义来指定表:Specifying the CHARACTER SET binary attribute for a character string data type causes the column to be created as the corresponding binary string data type: CHAR becomes BINARY, VARCHAR becomes VARBINARY, and TEXT becomes BLOB. For the ENUM and SET data 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)排序规则的简写(如果未指定列字符集,则使用表的默认字符集)。在这种情况下,比较和排序是基于字符的数值编码进行的。假设你使用以下定义来创建一个表:The BINARY attribute 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, the BINARY attribute 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 _bin collation instead.

    使用 BINARY 来指定数据类型或字符集的方式保持不变。The use of BINARY to specify a data type or character set remains unchanged.

  • ASCII 属性是 CHARACTER SET latin1 的简写。在旧版本的 MySQL 中受支持,但 ASCII 已被弃用;请改用 CHARACTER SET。The ASCII attribute is shorthand for CHARACTER SET latin1. Supported in older MySQL releases, ASCII is deprecated; use CHARACTER SET instead.

  • UNICODE 属性是 CHARACTER SET ucs2 的简写。在旧版本的 MySQL 中受支持,但 UNICODE 已被弃用;请改用 CHARACTER SET。The UNICODE attribute is shorthand for CHARACTER SET ucs2. Supported in older MySQL releases, UNICODE is deprecated; use CHARACTER SET instead.

字符列的比较和排序是基于分配给该列的排序规则。对于 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 SET charset_name] [COLLATE collation_name]

    固定长度的字符串,存储时总是用空格在右侧填充到指定长度。M 表示列的字符长度。M 的范围是 0 到 255。如果省略 M,长度为 1。A fixed-length string that is always right-padded with spaces to the specified length when stored. M represents the column length in characters. The range of M is 0 to 255. If M is omitted, the length is 1.

    Note

    在检索 CHAR 值时,会去除尾随空格,除非启用了 PAD_CHAR_TO_FULL_LENGTH SQL 模式。Trailing spaces are removed when CHAR values are retrieved unless the PAD_CHAR_TO_FULL_LENGTH SQL mode is enabled.

    CHAR 是 CHARACTER 的缩写。NATIONAL CHAR(或其等效的简写形式 NCHAR)是 SQL 定义 CHAR 列应使用某个预定义字符集的标准方式。MySQL 使用 utf8mb3 作为此预定义字符集。第 12.3.7 节,“国家字符集”。CHAR is shorthand for CHARACTER. NATIONAL CHAR (or its equivalent short form, NCHAR) is the standard SQL way to define that a CHAR column should use some predefined character set. MySQL uses utf8mb3 as this predefined character set. Section 12.3.7, “The National Character Set”.

    CHAR BYTE 数据类型是 BINARY 数据类型的别名。这是一个兼容性特性。The CHAR BYTE data type is an alias for the BINARY data type. This is a compatibility feature.

    MySQL 允许你创建类型为 CHAR(0) 的列。当你必须兼容依赖列存在但实际上不使用其值的旧应用程序时,这非常有用。CHAR(0) 也非常适合在你需要一个只能取两个值的列时使用:定义为 CHAR(0) NULL 的列只占用一位,并且只能取值 NULL 和 ''(空字符串)。MySQL permits you to create a column of type CHAR(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 as CHAR(0) NULL occupies only one bit and can take only the values NULL and '' (the empty string).

  • [NATIONAL] VARCHAR(M) [CHARACTER SET charset_name] [COLLATE collation_name] [NATIONAL] VARCHAR(M) [CHARACTER SET charset_name] [COLLATE collation_name]

    一个可变长度的字符串。M 表示列的最大字符长度。M 的范围是 0 到 65,535。VARCHAR 的有效最大长度受最大行大小(65,535 字节,所有列共享)和使用的字符集影响。例如,utf8mb3 字符每个字符可能需要最多三字节,因此使用 utf8mb3 字符集的 VARCHAR 列最大可以声明为 21,844 个字符。参见 10.4.7 节,“表列数和行大小的限制”。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, utf8mb3 characters can require up to three bytes per character, so a VARCHAR column that uses the utf8mb3 character 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 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 遵循标准 SQL 规范,并且 不会 删除 VARCHAR 值末尾的空格。MySQL follows the standard SQL specification, and does not remove trailing spaces from VARCHAR values.

    VARCHAR 是 CHARACTER VARYING 的简写。NATIONAL VARCHAR 是标准 SQL 定义 VARCHAR 列应使用某些预定义字符集的方式。MySQL 使用 utf8mb3 作为此预定义字符集。第 12.3.7 节,“国家字符集”。NVARCHAR 是 NATIONAL VARCHAR 的简写。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 utf8mb3 as this predefined character set. Section 12.3.7, “The National Character Set”. NVARCHAR is shorthand for NATIONAL VARCHAR.

  • BINARY[(M)] BINARY[(M)]

    BINARY 类型类似于 CHAR 类型,但存储的是二进制字节字符串,而不是非二进制字符字符串。可选的长度 M 表示列的字节长度。如果省略,M 默认为 1。The BINARY type is similar to the CHAR type, but stores binary byte strings rather than nonbinary character strings. An optional length M represents the column length in bytes. If omitted, M defaults to 1.

  • VARBINARY(M) VARBINARY(M)

    VARBINARY 类型类似于 VARCHAR 类型,但存储的是二进制字节字符串,而不是非二进制字符字符串。M 表示列的最大长度(以字节为单位)。The VARBINARY type is similar to the VARCHAR type, but stores binary byte strings rather than nonbinary character strings. M represents the maximum column length in bytes.

  • TINYBLOB TINYBLOB

    一个最大长度为 255(28 − 1)字节的 BLOB 列。每个 TINYBLOB 值使用 1 字节长度前缀存储,该前缀表示值中的字节数。A BLOB column with a maximum length of 255 (28 − 1) bytes. Each TINYBLOB value 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 SET charset_name] [COLLATE collation_name]

    一个 TEXT 列,最大长度为 255(28 − 1)个字符。如果值包含多字节字符,有效最大长度会更小。每个 TINYTEXT 值使用 1 字节长度前缀存储,该前缀表示值中的字节数。A TEXT column with a maximum length of 255 (28 − 1) characters. The effective maximum length is less if the value contains multibyte characters. Each TINYTEXT value is stored using a 1-byte length prefix that indicates the number of bytes in the value.

  • BLOB[(M)] BLOB[(M)]

    一个最大长度为 65,535(216 − 1)字节的 BLOB 列。每个 BLOB 值使用 2 字节长度前缀存储,该前缀表示值的字节数。A BLOB column with a maximum length of 65,535 (216 − 1) bytes. Each BLOB value is stored using a 2-byte length prefix that indicates the number of bytes in the value.

    可为此类型提供可选长度 M。如果这样做,MySQL 会将列创建为最小的 BLOB 类型,以容纳长度为 M 字节的值。An optional length M can be given for this type. If this is done, MySQL creates the column as the smallest BLOB type large enough to hold values M bytes long.

  • TEXT[(M)] [CHARACTER SET charset_name] [COLLATE collation_name] TEXT[(M)] [CHARACTER SET charset_name] [COLLATE collation_name]

    一个 TEXT 列,最大长度为 65,535 (216 − 1) 字节。如果值包含多字节字符,有效最大长度会减少。每个 TEXT 值使用一个 2 字节长度前缀存储,该前缀表示值中的字节数。A TEXT column with a maximum length of 65,535 (216 − 1) bytes. The effective maximum length is less if the value contains multibyte characters. Each TEXT value is stored using a 2-byte length prefix that indicates the number of bytes in the value.

    此类型可以指定一个可选长度M。如果指定,MySQL 会创建一个最小的 TEXT 类型列,足够存放长度为 M 个字符的值。An optional length M can be given for this type. If this is done, MySQL creates the column as the smallest TEXT type large enough to hold values M characters long.

  • MEDIUMBLOB

    最大长度为 16,777,215(224 − 1)字节的 BLOB 列。每个 MEDIUMBLOB 值都使用 3 字节长度前缀存储,用于指示该值的字节数。A BLOB column with a maximum length of 16,777,215 (224 − 1) bytes. Each MEDIUMBLOB value 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 SET charset_name] [COLLATE collation_name]

    一个最大长度为 16,777,215 (224 − 1) 个字符的 TEXT 列。如果值包含多字节字符,则有效最大长度会更小。每个 MEDIUMTEXT 值都使用 3 字节长度前缀存储,该前缀指示值的字节数。A TEXT column with a maximum length of 16,777,215 (224 − 1) characters. The effective maximum length is less if the value contains multibyte characters. Each MEDIUMTEXT value is stored using a 3-byte length prefix that indicates the number of bytes in the value.

  • LONGBLOB LONGBLOB

    一个最大长度为 4,294,967,295 或 4GB (232 − 1) 字节的 BLOB 列。LONGBLOB 列的实际最大长度取决于客户端/服务器协议中配置的最大数据包大小和可用内存。每个 LONGBLOB 值都使用一个 4 字节的长度前缀存储,该前缀指示值的字节数。A BLOB column with a maximum length of 4,294,967,295 or 4GB (232 − 1) bytes. The effective maximum length of LONGBLOB columns depends on the configured maximum packet size in the client/server protocol and available memory. Each LONGBLOB value 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 SET charset_name] [COLLATE collation_name]

    一个 TEXT 列,最大长度为 4,294,967,295 或 4GB(232 − 1)个字符。如果值包含多字节字符,有效最大长度会更小。LONGTEXT 列的有效最大长度还取决于客户端/服务器协议中配置的最大数据包大小和可用内存。每个 LONGTEXT 值使用 4 字节长度前缀存储,该前缀指示值中的字节数。A TEXT column 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 of LONGTEXT columns also depends on the configured maximum packet size in the client/server protocol and available memory. Each LONGTEXT value 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 SET charset_name] [COLLATE collation_name]

    枚举。一个字符串对象,它只能有一个值,从值列表 'value1'、'value2'、...、NULL 或特殊的 '' 错误值中选择。ENUM 值在内部表示为整数。An enumeration. A string object that can have only one value, chosen from the list of values 'value1', 'value2', ..., NULL or the special '' error value. ENUM values are represented internally as integers.

    ENUM 列可以有最多 65,535 个不同的元素。An ENUM column can have a maximum of 65,535 distinct elements.

    单个 ENUM 元素支持的最大长度为 M <= 255 且 (M x w) <= 1020,其中 M 是元素字面长度,w 是字符集中最大长度字符所需的字节数。The maximum supported length of an individual ENUM element is M <= 255 and (M x w) <= 1020, where M is the element literal length and w is 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 SET charset_name] [COLLATE collation_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', ... SET values are represented internally as integers.

    一个 SET 列最多可以有 64 个不同的成员。A SET column can have a maximum of 64 distinct members.

    单个 SET 元素支持的最大长度为 M <= 255 且 (M x w) <= 1020,其中 M 为元素的字面长度,w 为字符集中最长字符所需的字节数。The maximum supported length of an individual SET element is M <= 255 and (M x w) <= 1020, where M is the element literal length and w is the number of bytes required for the maximum-length character in the character set.