11.3.4 The BLOB and TEXT TypesBLOB类型和TEXT类型

A BLOB is a binary large object that can hold a variable amount of data. BLOB是一个二进制大对象,它可以保存可变数量的数据。The four BLOB types are TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB. 这四种BLOB类型是TINYBLOBBLOBMEDIUMBLOBLONGBLOBThese differ only in the maximum length of the values they can hold. 它们仅在所能容纳的值的最大长度上不同。The four TEXT types are TINYTEXT, TEXT, MEDIUMTEXT, and LONGTEXT. 这四种TEXT类型是TINYTEXTTEXTMEDIUMTEXTLONGTEXTThese correspond to the four BLOB types and have the same maximum lengths and storage requirements. 它们对应于四种BLOB类型,具有相同的最大长度和存储要求。See Section 11.7, “Data Type Storage Requirements”.请参阅第11.7节,“数据类型存储要求”

BLOB values are treated as binary strings (byte strings). BLOB值被视为二进制字符串(字节字符串)。They have the binary character set and collation, and comparison and sorting are based on the numeric values of the bytes in column values. 它们具有binary字符集和排序规则,比较和排序基于列值中字节的数值。TEXT values are treated as nonbinary strings (character strings). TEXT值被视为非二进制字符串(字符串)。They have a character set other than binary, and values are sorted and compared based on the collation of the character set.它们有一个非binary的字符集,并且根据字符集的排序规则对值进行排序和比较。

If strict SQL mode is not enabled and you assign a value to a BLOB or TEXT column that exceeds the column's maximum length, the value is truncated to fit and a warning is generated. 如果未启用严格SQL模式,并且为BLOBTEXT列指定的值超过了该列的最大长度,则会截断该值以适合该列,并生成警告。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模式”

Truncation of excess trailing spaces from values to be inserted into TEXT columns always generates a warning, regardless of the SQL mode.无论SQL模式如何,从要插入到TEXT列的值中截断多余的尾随空格都会生成警告。

For TEXT and BLOB columns, there is no padding on insert and no bytes are stripped on select.对于TEXTBLOB列,insert时没有填充,select时也没有剥离字节。

If a TEXT column is indexed, index entry comparisons are space-padded at the end. 如果对TEXT列编制了索引,则索引项比较将在末尾填充空格。This means that, if the index requires unique values, duplicate-key errors occur for values that differ only in the number of trailing spaces. 这意味着,如果索引需要唯一的值,则仅在尾随空格数上不同的值会出现重复键错误。For example, if a table contains 'a', an attempt to store 'a ' causes a duplicate-key error. 例如,如果表包含'a',则尝试存储'a '导致重复键错误。This is not true for BLOB columns.对于BLOB列,情况并非如此。

In most respects, you can regard a BLOB column as a VARBINARY column that can be as large as you like. 在大多数情况下,可以将BLOB列视为VARBINARY列,它可以任意大。Similarly, you can regard a TEXT column as a VARCHAR column. 类似地,可以将文本列视为VARCHAR列。BLOB and TEXT differ from VARBINARY and VARCHAR in the following ways:BLOBTEXTVARBINARYVARCHAR的区别如下:

If you use the BINARY attribute with a TEXT data type, the column is assigned the binary (_bin) collation of the column character set.如果将BINARY属性与TEXT数据类型一起使用,则会为列指定列字符集的二进制(_bin)排序规则。

LONG and LONG VARCHAR map to the MEDIUMTEXT data type. LONGLONG VARCHAR映射到MEDIUMTEXT数据类型。This is a compatibility feature.这是一个兼容性功能。

MySQL Connector/ODBC defines BLOB values as LONGVARBINARY and TEXT values as LONGVARCHAR.MySQL连接器/ODBC将BLOB值定义为LONGVARBINARY,将文本值定义为LONGVARCHAR

Because BLOB and TEXT values can be extremely long, you might encounter some constraints in using them:由于BLOBTEXT值可能非常长,因此在使用它们时可能会遇到一些限制:

Each BLOB or TEXT value is represented internally by a separately allocated object. 每个BLOBTEXT值在内部由一个单独分配的对象表示。This is in contrast to all other data types, for which storage is allocated once per column when the table is opened.这与所有其他数据类型不同,在打开表时,每列只分配一次存储。

In some cases, it may be desirable to store binary data such as media files in BLOB or TEXT columns. 在某些情况下,可能需要在BLOBTEXT列中存储二进制数据,例如媒体文件。You may find MySQL's string handling functions useful for working with such data. 您可能会发现MySQL的字符串处理函数对于处理此类数据非常有用。See Section 12.8, “String Functions and Operators”. 请参阅第12.8节,“字符串函数和运算符”For security and other reasons, it is usually preferable to do so using application code rather than giving application users the FILE privilege. 出于安全和其他原因,通常最好使用应用程序代码,而不是授予应用程序用户FILE权限。You can discuss specifics for various languages and platforms in the MySQL Forums (http://forums.mysql.com/).您可以在MySQL论坛中讨论各种语言和平台的细节(http://forums.mysql.com/)。

Note注意

Within the mysql client, binary strings display using hexadecimal notation, depending on the value of the --binary-as-hex. mysql客户机中,二进制字符串使用十六进制表示法显示,具体取决于--binary-as-hex的值。For more information about that option, see Section 4.5.1, “mysql — The MySQL Command-Line Client”.有关该选项的更多信息,请参阅第4.5.1节,“mysql命令行客户端”