- MySQL 5.1 Reference Manual :: 10 Data Types :: 10.4 String Types :: 10.4.3 The BLOB and TEXT Types
-
- MySQL 5.1 Reference Manual
- Preface, Notes, Licenses
- 1 General Information
- 2 Installing and Upgrading MySQL
- 3 Tutorial
- 4 MySQL Programs
- 5 MySQL Server Administration
- 6 Backup and Recovery
- 7 Optimization
- 8 Language Structure
- 9 Internationalization and Localization
- 10 Data Types
- 11 Functions and Operators
- 12 SQL Statement Syntax
- 13 Storage Engines
- 14 High Availability and Scalability
- 15 MySQL Enterprise Monitor
- 16 Replication
- 17 MySQL Cluster NDB 6.X/7.X
- 18 Partitioning
- 19 Stored Programs and Views
- 20 INFORMATION_SCHEMA Tables
- 21 Connectors and APIs
- 22 Extending MySQL
- A MySQL 5.1 Frequently Asked Questions
- B Errors, Error Codes, and Common Problems
- C MySQL Change History
- D Restrictions and Limits
- Index
- Standard Index
- C Function Index
- Command Index
- Function Index
- INFORMATION_SCHEMA Index
- Transaction Isolation Level Index
- JOIN Types Index
- Operator Index
- Option Index
- Privileges Index
- SQL Modes Index
- Status Variable Index
- Statement/Syntax Index
- System Variable Index
A
BLOB
is a binary large object that can hold a variable amount of data. The fourBLOB
types areTINYBLOB
,BLOB
,MEDIUMBLOB
, andLONGBLOB
. These differ only in the maximum length of the values they can hold. The fourTEXT
types areTINYTEXT
,TEXT
,MEDIUMTEXT
, andLONGTEXT
. These correspond to the fourBLOB
types and have the same maximum lengths and storage requirements. See Section 10.5, “Data Type Storage Requirements”.BLOB
values are treated as binary strings (byte strings). They have no character set, and sorting and comparison are based on the numeric values of the bytes in column values.TEXT
values are treated as nonbinary strings (character strings). They have a character set, and values are sorted and compared based on the collation of the character set.If strict SQL mode is not enabled and you assign a value to a
BLOB
orTEXT
column that exceeds the column's maximum length, the value is truncated to fit and a warning is generated. 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. See Section 5.1.7, “Server SQL Modes”.Beginning with MySQL 5.1.24, truncation of excess trailing spaces from values to be inserted into
TEXT
columns always generates a warning, regardless of the SQL mode.If a
TEXT
column is indexed, index entry comparisons are space-padded at the end. This means that, if the index requires unique values, duplicate-key errors will 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. This is not true forBLOB
columns.In most respects, you can regard a
BLOB
column as aVARBINARY
column that can be as large as you like. Similarly, you can regard aTEXT
column as aVARCHAR
column.BLOB
andTEXT
differ fromVARBINARY
andVARCHAR
in the following ways:If you use the
BINARY
attribute with aTEXT
data type, the column is assigned the binary collation of the column character set.LONG
andLONG VARCHAR
map to theMEDIUMTEXT
data type. This is a compatibility feature.MySQL Connector/ODBC defines
BLOB
values asLONGVARBINARY
andTEXT
values asLONGVARCHAR
.Because
BLOB
andTEXT
values can be extremely long, you might encounter some constraints in using them:-
Only the first
max_sort_length
bytes of the column are used when sorting. The default value ofmax_sort_length
is 1024. You can make more bytes significant in sorting or grouping by increasing the value ofmax_sort_length
at server startup or runtime. Any client can change the value of its sessionmax_sort_length
variable:mysql>
SET max_sort_length = 2000;
mysql>SELECT id, comment FROM t
->ORDER BY comment;
Another way to use
GROUP BY
orORDER BY
on aBLOB
orTEXT
column containing long values when you want more thanmax_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 theSUBSTRING()
function. For example, the following statement causes 2000 bytes of thecomment
column to be taken into account for sorting:mysql>
SELECT id, SUBSTRING(comment,1,2000) FROM t
->ORDER BY SUBSTRING(comment,1,2000);
Instances of
BLOB
orTEXT
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 theMEMORY
storage engine does not support those data types (see Section 7.5.10, “How MySQL Uses Internal Temporary Tables”). Use of disk incurs a performance penalty, so includeBLOB
orTEXT
columns in the query result only if they are really needed. For example, avoid usingSELECT *
, which selects all columns.The maximum size of a
BLOB
orTEXT
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 themax_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-sidemax_allowed_packet
value. See Section 7.5.3, “Tuning Server Parameters”, Section 4.5.1, “mysql — The MySQL Command-Line Tool”, and Section 4.5.4, “mysqldump — A Database Backup Program”. 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 10.5, “Data Type Storage Requirements”
Each
BLOB
orTEXT
value is represented internally by a separately allocated object. 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
orTEXT
columns. You may find MySQL's string handling functions useful for working with such data. See Section 11.4, “String Functions”. For security and other reasons, it is usually preferable to do so using application code rather than allowing application users theFILE
privilege. You can discuss specifics for various languages and platforms in the MySQL Forums (http://forums.mysql.com/).