:: DEVELOPER ZONE
A BLOB is a binary large object that can hold a
variable amount of data. The four BLOB types,
TINYBLOB, BLOB,
MEDIUMBLOB, and LONGBLOB,
differ only in the maximum length of the values they can hold.
See Section 11.5, “Column Type Storage Requirements”.
The four TEXT types, TINYTEXT,
TEXT, MEDIUMTEXT, and
LONGTEXT, correspond to the four
BLOB types and have the same maximum lengths and
storage requirements.
BLOB columns are treated as binary strings (byte
strings). TEXT columns are treated as non-binary
strings (character strings). BLOB columns have no
character set, and sorting and comparison is based on the numeric
values of the bytes in column values. TEXT
columns have a character set, and values are sorted and compared
based on the collation of the character set assigned to the column
as of MySQL 4.1. Before 4.1, TEXT sorting and
comparison are based on the collation of the server character set.
No lettercase conversion takes place during storage or retrieval.
If you assign a value to a BLOB or
TEXT column that exceeds the column type's
maximum length, the value is truncated to fit. If the truncated
characters are not spaces, a warning is generated. You can cause an
error to occur rather than an warning by using ``strict'' SQL mode.
See Section 5.3.2, “The Server SQL Mode”.
In most respects, you can regard a BLOB column as
a VARBINARY column that can be as big as you
like. Similarly, you can regard a TEXT column as
a VARCHAR column. BLOB and
TEXT differ from VARBINARY and
VARCHAR in the following ways::
There is no trailing-space removal for BLOB and
TEXT columns when values are stored or
retrieved. Before MySQL 5.0.3, this differs from
VARBINARY and VARCHAR, for
which trailing spaces are removed when values are stored.
Note that TEXT is on comparison space extended
to fit the compared object, exactly like CHAR
and VARCHAR.
You can have indexes on BLOB and
TEXT columns only as of MySQL 3.23.2 for
MyISAM tables or MySQL 4.0.14 for
InnoDB tables. Older versions of MySQL did not
support indexing these column types.
For indexes on BLOB and TEXT
columns, you must specify an index prefix length. For
CHAR and VARCHAR, a prefix
length is optional.
BLOB and TEXT columns cannot
have DEFAULT values.
From MySQL 4.1.0 on, LONG and LONG
VARCHAR map to the MEDIUMTEXT data
type. This is a compatibility feature. If you use the
BINARY attribute with a TEXT
column type, the column is assigned the binary collation of the
column character set.
MySQL Connector/ODBC defines BLOB values as
LONGVARBINARY and TEXT values
as LONGVARCHAR.
Because BLOB and TEXT values
may be extremely long, you may encounter some constraints in using
them:
Only the first max_sort_length bytes of the
column are used when sorting. The default value of
max_sort_length is 1024; this value can be
changed using the --max_sort_length option when
starting the mysqld server. See
Section 5.3.3, “Server System Variables”.
As of MySQL 4.0.3, you can make more bytes significant in sorting
or grouping by increasing the value of
max_sort_length at runtime. Any client can
change the value of its session max_sort_length
variable:
mysql> SET max_sort_length = 2000;
mysql> SELECT id, comment FROM tbl_name
-> ORDER BY comment;
Another way to use GROUP BY or ORDER
BY on a BLOB or
TEXT column containing long values when you want
more than max_sort_length bytes to be
significant is to convert the column value into a fixed-length
object. The standard way to do this is with the
SUBSTRING function. For example, the following
statement causes 2000 bytes of the comment
column to be taken into account for sorting:
mysql> SELECT id, SUBSTRING(comment,1,2000) FROM tbl_name
-> ORDER BY SUBSTRING(comment,1,2000);
Before MySQL 3.23.2, you can group on an expression involving
BLOB or TEXT values by using
a column alias or by specifying the column position:
mysql> SELECT id, SUBSTRING(comment,1,2000) AS b
-> FROM tbl_name GROUP BY b;
mysql> SELECT id, SUBSTRING(comment,1,2000)
-> FROM tbl_name GROUP BY 2;
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. 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. For example, both
mysql and mysqldump allow you
to change the client-side max_allowed_packet
value. See Section 7.5.2, “Tuning Server Parameters”,
Section 8.3, “mysql, the Command-Line Tool”, and Section 8.8, “The mysqldump Database Backup Program”.
Each BLOB or TEXT value is
represented internally by a separately allocated object. This is in
contrast to all other column types, for which storage is allocated
once per column when the table is opened.
© 1995-2005 MySQL AB. All rights reserved.

User Comments
Warning: query failed: Unknown column 'user.firstname' in 'field list' in /data0/sites/live/web-main/lib/mysql-cxn.php on line 69
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /data0/sites/live/web-main/lib/docbook.php on line 245
Add your own comment.