The string data types are 字符串数据类型有CHAR, VARCHAR, BINARY, VARBINARY, BLOB, TEXT, ENUM, and SET.CHAR、VARCHAR、BINARY、VARBINARY、BLOB、TEXT、ENUM和SET。
In some cases, MySQL may change a string column to a type different from that given in a 在某些情况下,MySQL可能会将字符串列更改为与CREATE TABLE or ALTER TABLE statement. CREATE TABLE或ALTER TABLE语句中给定的类型不同的类型。See Section 13.1.20.7, “Silent Column Specification Changes”.请参阅第13.1.20.7节,“静默列规范变更”。
For definitions of character string columns (对于字符串列(CHAR, VARCHAR, and the TEXT types), MySQL interprets length specifications in character units. CHAR、VARCHAR和TEXT类型)的定义,MySQL以字符单位解释长度规范。For definitions of binary string columns (对于二进制字符串列(BINARY, VARBINARY, and the BLOB types), MySQL interprets length specifications in byte units.BINARY、VARBINARY和BLOB类型)的定义,MySQL以字节为单位解释长度规范。
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:CHAR、VARCHAR、TEXT类型、ENUM、SET和任何同义词)的列定义可以指定列字符集和排序规则:
CHARACTER SET specifies the character set. CHARACTER SET指定字符集。If desired, a collation for the character set can be specified with the 如果需要,可以使用COLLATE attribute, along with any other attributes. COLLATE属性以及任何其他属性指定字符集的排序规则。For example:例如:
CREATE TABLE t
(
c1 VARCHAR(20) CHARACTER SET utf8,
c2 TEXT CHARACTER SET latin1 COLLATE latin1_general_cs
);
This table definition creates a column named 此表定义创建一个名为c1 that has a character set of utf8 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.c1的列,该列的字符集为utf8,该字符集的默认排序规则为utf8;一个名为c2的列,该列的字符集为latin1,且排序规则区分大小写(_cs)。
The rules for assigning the character set and collation when either or both of 第10.3.5节“列字符集和排序规则”中描述了当字符集和排序规则属性中的一个或两个都丢失时分配字符集和排序规则的规则。CHARACTER SET and the COLLATE attribute are missing are described in Section 10.3.5, “Column Character Set and Collation”.
CHARSET is a synonym for CHARACTER SET.CHARSET是CHARACTER 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. CHARACTER SET binary属性会导致将列创建为相应的二进制字符串数据类型:CHAR变为BINARY,VARCHAR变为VARBINARY,TEXT变为BLOB。For the 对于ENUM and SET data types, this does not occur; they are created as declared. ENUM和SET数据类型,不会发生这种情况;它们是按声明创建的。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
);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). BINARY属性是一个非标准的MySQL扩展,它是指定列字符集(或表默认字符集,如果没有指定列字符集)的二进制(_bin)排序规则的缩写。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;
In MySQL 8.0, this nonstandard use of the 在MySQL 8.0中,BINARY attribute is ambiguous because the utf8mb4 character set has multiple _bin collations. BINARY属性的这种非标准用法是不明确的,因为utf8mb4字符集有多个_bin排序规则。As of MySQL 8.0.17, the 从MySQL 8.0.17开始,BINARY attribute is deprecated and you should expect support for it to be removed in a future version of MySQL. BINARY属性就不受欢迎了,您应该希望在将来的MySQL版本中删除对它的支持。Applications should be adjusted to use an explicit 应用程序应该调整为使用显式的_bin collation instead._bin排序规则。
The use of 使用BINARY to specify a data type or character set remains unchanged.BINARY指定数据类型或字符集保持不变。
The ASCII attribute is shorthand for CHARACTER SET latin1.ASCII属性是CHARACTER SET latin1的缩写。
The UNICODE attribute is shorthand for CHARACTER SET ucs2.UNICODE属性是CHARACTER SET ucs2的缩写。
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.CHAR、VARCHAR、TEXT、ENUM和SET数据类型,可以使用二进制(_bin)排序规则或binary属性声明列,以使比较和排序使用底层字符代码值,而不是词法排序。
For additional information about use of character sets in MySQL, see Chapter 10, Character Sets, Collations, Unicode.有关在MySQL中使用字符集的更多信息,请参阅第10章“字符集,排序规则,Unicode”。
[NATIONAL] CHAR[(M)] [CHARACTER SET charset_name] [COLLATE collation_name]
A fixed-length string that is always right-padded with spaces to the specified length when stored. 一种固定长度的字符串,在存储时总是用空格右填充到指定的长度。M represents the column length in characters. M表示以字符表示的列长度。The range of M is 0 to 255. M的范围是0到255。If 如果省略M is omitted, the length is 1.M,则长度为1。
Trailing spaces are removed when 检索CHAR values are retrieved unless the PAD_CHAR_TO_FULL_LENGTH SQL mode is enabled.CHAR值时,除非启用了PAD_CHAR_TO_FULL_LENGTHSQL模式,否则将删除尾随空格。
CHAR is shorthand for CHARACTER. CHAR是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. NATIONAL CHAR(或其等价的缩写,NCHAR)是标准的SQL方法,用于定义CHAR列应该使用一些预定义的字符集。MySQL uses MySQL使用utf8 as this predefined character set. utf8作为这个预定义的字符集。Section 10.3.7, “The National Character Set”.第10.3.7节,“国家字符集”。
The CHAR BYTE data type is an alias for the BINARY data type. CHAR BYTE数据类型是BINARY数据类型的别名。This is a compatibility feature.这是一个兼容性功能。
MySQL permits you to create a column of type MySQL允许您创建CHAR(0). 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).CHAR(0)也非常好:定义为CHAR(0) NULL的列只占用一位,并且只能接受NULL和''(空字符串)值。
[NATIONAL] VARCHAR(M) [CHARACTER SET charset_name] [COLLATE collation_name]
A variable-length string. 长度可变的字符串。M represents the maximum column length in characters. M表示最大列长度(字符)。The range of M is 0 to 65,535. M的范围是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字节,在所有列中共享)和使用的字符集。For example, 例如,utf8 characters can require up to three bytes per character, so a VARCHAR column that uses the utf8 character set can be declared to be a maximum of 21,844 characters. utf8字符可能需要每个字符最多3个字节,因此使用utf8字符集的VARCHAR列可以声明为最多21844个字符。See Section 8.4.7, “Limits on Table Column Count and Row Size”.请参阅第8.4.7节,“表列计数和行大小限制”。
MySQL stores MySQL将VARCHAR values as a 1-byte or 2-byte length prefix plus data. VARCHAR值存储为1字节或2字节长度的前缀加上数据。The length prefix indicates the number of bytes in the value. 长度前缀表示值中的字节数。A 如果值不需要超过255字节,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.VARCHAR列使用一个长度字节;如果值可能需要超过255字节,则使用两个长度字节。
MySQL follows the standard SQL specification, and does not remove trailing spaces from MySQL遵循标准SQL规范,并且不从VARCHAR values.VARCHAR值中删除尾随空格。
VARCHAR is shorthand for CHARACTER VARYING. VARCHAR是CHARACTER VARYING的缩写。NATIONAL VARCHAR is the standard SQL way to define that a VARCHAR column should use some predefined character set. NATIONAL VARCHAR是定义VARCHAR列应该使用一些预定义字符集的标准SQL方法。MySQL uses MySQL使用utf8作为这个预定义的字符集。utf8 as this predefined character set. Section 10.3.7, “The National Character Set”. 第10.3.7节,“国家字符集”。NVARCHAR is shorthand for NATIONAL VARCHAR.NVARCHAR是NATIONAL VARCHAR的缩写。
The BINARY type is similar to the CHAR type, but stores binary byte strings rather than nonbinary character strings. BINARY类型类似于CHAR类型,但是存储二进制字节字符串而不是非二进制字符串。An optional length 可选长度M represents the column length in bytes. M表示以字节为单位的列长度。If omitted, 如果省略,M defaults to 1.M默认为1。
The VARBINARY type is similar to the VARCHAR type, but stores binary byte strings rather than nonbinary character strings. VARBINARY类型类似于VARCHAR类型,但是存储二进制字节字符串而不是非二进制字符串。M represents the maximum column length in bytes.M表示最大列长度(字节)。
A 最大长度为255(28 − 1)字节的BLOB column with a maximum length of 255 (28 − 1) bytes. BLOB列。Each 每个TINYBLOB value is stored using a 1-byte length prefix that indicates the number of bytes in the value.TINYBLOB值都使用一个1字节长度的前缀来存储,该前缀指示值中的字节数。
TINYTEXT [CHARACTER SET charset_name] [COLLATE collation_name]
A 最大长度为255(28 − 1)个字符的TEXT column with a maximum length of 255 (28 − 1) characters. TEXT列。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.TINYTEXT值都使用一个1字节长度的前缀来存储,该前缀指示值中的字节数。
A 最大长度为65535(216 − 1)字节的BLOB column with a maximum length of 65,535 (216 − 1) bytes. BLOB列。Each 每个BLOB value is stored using a 2-byte length prefix that indicates the number of bytes in the value.BLOB值都使用一个2字节长度的前缀来存储,该前缀指示值中的字节数。
An optional length 此类型可提供可选长度M can be given for this type. M。If this is done, MySQL creates the column as the smallest 如果这样做了,MySQL会创建一个最小的BLOB type large enough to hold values M bytes long.BLOB类型,这个BLOB类型足够大,可以容纳M字节长的值。
TEXT[(M)] [CHARACTER SET charset_name] [COLLATE collation_name]
A 最大长度为65535(216 − 1)个字符的TEXT column with a maximum length of 65,535 (216 − 1) characters. TEXT列。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.TEXT值都使用2字节长度的前缀来存储,该前缀指示值中的字节数。
An optional length 此类型可提供可选长度M can be given for this type. M。If this is done, MySQL creates the column as the smallest 如果这样做了,MySQL会将列创建为足够大的最小文本类型,以容纳TEXT type large enough to hold values M characters long.M个字符长的值。
A 最大长度为16777215(224 − 1)字节的BLOB column with a maximum length of 16,777,215 (224 − 1) bytes. BLOB列。Each 每个MEDIUMBLOB value is stored using a 3-byte length prefix that indicates the number of bytes in the value.MEDIUMBLOB值都使用一个3字节长度的前缀来存储,该前缀指示值中的字节数。
MEDIUMTEXT [CHARACTER SET charset_name] [COLLATE collation_name]
A 最大长度为16777215(224 − 1)个字符的TEXT column with a maximum length of 16,777,215 (224 − 1) characters. TEXT列。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.MEDIUMTEXT值都使用一个3字节长度的前缀来存储,该前缀指示值中的字节数。
A 最大长度为4294967295或4GB(232 − 1)字节的BLOB column with a maximum length of 4,294,967,295 or 4GB (232 − 1) bytes. BLOB列。The effective maximum length of LONGBLOB columns depends on the configured maximum packet size in the client/server protocol and available memory. LONGBLOB列的有效最大长度取决于客户端/服务器协议中配置的最大数据包大小和可用内存。Each 每个LONGBLOB value is stored using a 4-byte length prefix that indicates the number of bytes in the value.LONGBLOB值都使用一个4字节长度的前缀来存储,该前缀指示值中的字节数。
LONGTEXT [CHARACTER SET charset_name] [COLLATE collation_name]
A 最大长度为4294967295或4GB(232 − 1)字符的TEXT column with a maximum length of 4,294,967,295 or 4GB (232 − 1) characters. TEXT列。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. LONGTEXT列的有效最大长度还取决于客户端/服务器协议中配置的最大数据包大小和可用内存。Each 每个LONGTEXT value is stored using a 4-byte length prefix that indicates the number of bytes in the value.LONGTEXT值都使用一个4字节长度的前缀来存储,该前缀指示值中的字节数。
ENUM('value1','value2',...) [CHARACTER SET charset_name] [COLLATE collation_name]
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. 'value1'、'value2'、……、'NULL'或特殊的''错误值列表中选择。ENUM values are represented internally as integers.ENUM值在内部表示为整数。
An ENUM column can have a maximum of 65,535 distinct elements.ENUM列最多可以有65535个不同的元素。
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.ENUM元素支持的最大长度为M<=255并且(M x w)<=1020,其中M是元素文字长度,w是字符集中最大长度字符所需的字节数。
SET('value1','value2',...) [CHARACTER SET charset_name] [COLLATE collation_name]
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.'value1'、'value2'……列表中选择,SET值在内部表示为整数。
A 一个SET column can have a maximum of 64 distinct members.SET列最多可以有64个不同的成员。
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.SET元素的最大支持长度为M<=255并且(M x w)<=1020,其中M是元素文字长度,w是字符集中最大长度字符所需的字节数。