A summary of the string data types follows. For additional information about properties of the string types, see Section 10.4, “String Types”. Storage requirements are given in Section 10.5, “Data Type Storage Requirements”.
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 12.1.14.2, “Silent Column Specification Changes”.
MySQL interprets length specifications in character column
definitions in character units. This applies to
CHAR,
VARCHAR, and the
TEXT types.
Column definitions for many string data types can include
attributes that specify the character set or collation of the
column. These attributes apply to the
CHAR,
VARCHAR, the
TEXT types,
ENUM, and
SET data types:
The
CHARACTER SETattribute specifies the character set, and theCOLLATEattribute specifies a collation for the character set. 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
c1that has a character set ofutf8with the default collation for that character set, and a column namedc2that has a character set oflatin1and a case-sensitive collation.The rules for assigning the character set and collation when either or both of the
CHARACTER SETandCOLLATEattributes are missing are described in Section 9.1.3.4, “Column Character Set and Collation”.CHARSETis a synonym forCHARACTER SET.Specifying the
CHARACTER SET binaryattribute for a character data type causes the column to be created as the corresponding binary 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 );The
ASCIIattribute is shorthand forCHARACTER SET latin1.The
UNICODEattribute is shorthand forCHARACTER SET ucs2.The
BINARYattribute is shorthand for specifying the binary collation of the column character set. In this case, sorting and comparison are based on numeric character values.
Character column sorting and comparison are based on the
character set assigned to the column. For the
CHAR,
VARCHAR,
TEXT,
ENUM, and
SET data types, you can declare a
column with a binary collation or the BINARY
attribute to cause sorting and comparison to use the underlying
character code values rather than a lexical ordering.
Section 9.1, “Character Set Support”, provides additional information about use of character sets in MySQL.
[NATIONAL] CHAR[(M)] [CHARACTER SETcharset_name] [COLLATEcollation_name]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.NoteTrailing spaces are removed when
CHARvalues are retrieved unless thePAD_CHAR_TO_FULL_LENGTHSQL mode is enabled.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 4.1 and up usesutf8as this predefined character set. Section 9.1.3.6, “National Character Set”.The
CHAR BYTEdata type is an alias for theBINARYdata type. This is a compatibility feature.MySQL permits you to create a column of type
CHAR(0). This is useful primarily when you have to 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 SETcharset_name] [COLLATEcollation_name]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,utf8characters can require up to three bytes per character, so aVARCHARcolumn that uses theutf8character set can be declared to be a maximum of 21,844 characters.MySQL stores
VARCHARvalues as a one-byte or two-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 5.5 follows the standard SQL specification, and does not remove trailing spaces from
VARCHARvalues.VARCHARis shorthand forCHARACTER VARYING.NATIONAL VARCHARis the standard SQL way to define that aVARCHARcolumn should use some predefined character set. MySQL 4.1 and up usesutf8as this predefined character set. Section 9.1.3.6, “National Character Set”.NVARCHARis shorthand forNATIONAL VARCHAR.The
BINARYtype is similar to theCHARtype, but stores binary byte strings rather than nonbinary character strings.Mrepresents the column length in bytes.The
VARBINARYtype is similar to theVARCHARtype, but stores binary byte strings rather than nonbinary character strings.Mrepresents the maximum column length in bytes.A
BLOBcolumn with a maximum length of 255 (28 – 1) bytes. EachTINYBLOBvalue is stored using a one-byte length prefix that indicates the number of bytes in the value.TINYTEXT [CHARACTER SETcharset_name] [COLLATEcollation_name]A
TEXTcolumn with a maximum length of 255 (28 – 1) characters. The effective maximum length is less if the value contains multi-byte characters. EachTINYTEXTvalue is stored using a one-byte length prefix that indicates the number of bytes in the value.A
BLOBcolumn with a maximum length of 65,535 (216 – 1) bytes. EachBLOBvalue is stored using a two-byte length prefix that indicates the number of bytes in the value.An optional length
Mcan 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 SETcharset_name] [COLLATEcollation_name]A
TEXTcolumn with a maximum length of 65,535 (216 – 1) characters. The effective maximum length is less if the value contains multi-byte characters. EachTEXTvalue is stored using a two-byte length prefix that indicates the number of bytes in the value.An optional length
Mcan be given for this type. If this is done, MySQL creates the column as the smallestTEXTtype large enough to hold valuesMcharacters long.A
BLOBcolumn with a maximum length of 16,777,215 (224 – 1) bytes. EachMEDIUMBLOBvalue is stored using a three-byte length prefix that indicates the number of bytes in the value.MEDIUMTEXT [CHARACTER SETcharset_name] [COLLATEcollation_name]A
TEXTcolumn with a maximum length of 16,777,215 (224 – 1) characters. The effective maximum length is less if the value contains multi-byte characters. EachMEDIUMTEXTvalue is stored using a three-byte length prefix that indicates the number of bytes in the value.A
BLOBcolumn 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 four-byte length prefix that indicates the number of bytes in the value.LONGTEXT [CHARACTER SETcharset_name] [COLLATEcollation_name]A
TEXTcolumn with a maximum length of 4,294,967,295 or 4GB (232 – 1) characters. The effective maximum length is less if the value contains multi-byte 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 four-byte length prefix that indicates the number of bytes in the value.ENUM('value1','value2',...) [CHARACTER SETcharset_name] [COLLATEcollation_name]An enumeration. A string object that can have only one value, chosen from the list of values
',value1'',value2'...,NULLor the special''error value. AnENUMcolumn can have a maximum of 65,535 distinct values.ENUMvalues are represented internally as integers.SET('value1','value2',...) [CHARACTER SETcharset_name] [COLLATEcollation_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'...ASETcolumn can have a maximum of 64 members.SETvalues are represented internally as integers.