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
类型是TINYBLOB
、BLOB
、MEDIUMBLOB
和LONGBLOB
。These differ only in the maximum length of the values they can hold. 它们仅在所能容纳的值的最大长度上不同。The four 这四种TEXT
types are TINYTEXT
, TEXT
, MEDIUMTEXT
, and LONGTEXT
. TEXT
类型是TINYTEXT
、TEXT
、MEDIUMTEXT
和LONGTEXT
。These 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 如果未启用严格SQL模式,并且为BLOB
or TEXT
column that exceeds the column's maximum length, the value is truncated to fit and a warning is generated. BLOB
或TEXT
列指定的值超过了该列的最大长度,则会截断该值以适合该列,并生成警告。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 无论SQL模式如何,从要插入到TEXT
columns always generates a warning, regardless of the SQL mode.TEXT
列的值中截断多余的尾随空格都会生成警告。
For 对于TEXT
and BLOB
columns, there is no padding on insert and no bytes are stripped on select.TEXT
和BLOB
列,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:BLOB
和TEXT
与VARBINARY
和VARCHAR
的区别如下:
For indexes on 对于BLOB
and TEXT
columns, you must specify an index prefix length. BLOB
和TEXT
列上的索引,必须指定索引前缀长度。For 对于CHAR
and VARCHAR
, a prefix length is optional. CHAR
和VARCHAR
,前缀长度是可选的。See Section 8.3.5, “Column Indexes”.请参阅第8.3.5节,“列索引”。
BLOB
and TEXT
columns cannot have DEFAULT
values.BLOB
和TEXT
列不能具有默认值。
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. LONG
和LONG VARCHAR
映射到MEDIUMTEXT
数据类型。This is a compatibility feature.这是一个兼容性功能。
MySQL Connector/ODBC defines MySQL连接器/ODBC将BLOB值定义为BLOB
values as LONGVARBINARY
and TEXT
values as LONGVARCHAR
.LONGVARBINARY
,将文本值定义为LONGVARCHAR
。
Because 由于BLOB
and TEXT
values can be extremely long, you might encounter some constraints in using them:BLOB
和TEXT
值可能非常长,因此在使用它们时可能会遇到一些限制:
Only the first 排序时仅使用列的第一个max_sort_length
bytes of the column are used when sorting. max_sort_length
字节。The default value of max_sort_length
is 1024. max_sort_length
的默认值是1024。You can make more bytes significant in sorting or grouping by increasing the value of 通过在服务器启动或运行时增加max_sort_length
at server startup or runtime. max_sort_length
的值,可以使更多字节在排序或分组时具有重要意义。Any client can change the value of its session 任何客户端都可以更改其会话max_sort_length
variable:max_sort_length
变量的值:
mysql>SET max_sort_length = 2000;
mysql>SELECT id, comment FROM t
->ORDER BY comment;
Instances of 使用临时表处理的查询结果中的BLOB
or TEXT
columns in the result of a query that is processed using a temporary table causes the server to use a table on disk rather than in memory because the MEMORY
storage engine does not support those data types (see Section 8.4.4, “Internal Temporary Table Use in MySQL”). BLOB
或TEXT
列实例会导致服务器使用磁盘上的表而不是内存中的表,因为MEMORY
存储引擎不支持这些数据类型(请参阅第8.4.4节“MySQL中的内部临时表使用”)。Use of disk incurs a performance penalty, so include 使用磁盘会导致性能下降,因此只有在确实需要时才在查询结果中包含BLOB
or TEXT
columns in the query result only if they are really needed. BLOB
或TEXT
列。For example, avoid using 例如,避免使用SELECT *
, which selects all columns.SELECT *
,它选择所有列。
The maximum size of a BLOB
or TEXT
object is determined by its type, but the largest value you actually can transmit between the client and server is determined by the amount of available memory and the size of the communications buffers. BLOB
或TEXT
对象的最大大小由其类型决定,但实际上可以在客户端和服务器之间传输的最大值由可用内存量和通信缓冲区的大小决定。You can change the message buffer size by changing the value of the 您可以通过更改max_allowed_packet
variable, but you must do so for both the server and your client program. max_allowed_packet
变量的值来更改消息缓冲区大小,但必须同时对服务器和客户端程序执行此操作。For example, both mysql and mysqldump enable you to change the client-side 例如,mysql和mysqldump都允许您更改客户端max_allowed_packet
value. max_allowed_packet
值。See Section 5.1.1, “Configuring the Server”, Section 4.5.1, “mysql — The MySQL Command-Line Client”, and Section 4.5.4, “mysqldump — A Database Backup Program”. 请参阅第5.1.1节,“配置服务器”、第4.5.1节,“mysql命令行客户端”和第4.5.4节,“mysqldump A数据库备份程序”。You may also want to compare the packet sizes and the size of the data objects you are storing with the storage requirements, see Section 11.7, “Data Type Storage Requirements”您可能还希望将数据包大小和存储的数据对象大小与存储要求进行比较,请参阅第11.7节,“数据类型存储要求”。
Each 每个BLOB
or TEXT
value is represented internally by a separately allocated object. BLOB
或TEXT
值在内部由一个单独分配的对象表示。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. BLOB
或TEXT
列中存储二进制数据,例如媒体文件。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/)。
Within the mysql client, binary strings display using hexadecimal notation, depending on the value of the 在mysql客户机中,二进制字符串使用十六进制表示法显示,具体取决于--binary-as-hex
. --binary-as-hex
的值。For more information about that option, see Section 4.5.1, “mysql — The MySQL Command-Line Client”.有关该选项的更多信息,请参阅第4.5.1节,“mysql命令行客户端”。