Documentation Home
MySQL 9.2 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 40.8Mb
PDF (A4) - 40.9Mb
Man Pages (TGZ) - 259.7Kb
Man Pages (Zip) - 366.9Kb
Info (Gzip) - 4.1Mb
Info (Zip) - 4.1Mb


MySQL 9.2 Reference Manual  / ...  / Data Types  / String Data Types  /  The BLOB and TEXT Types

13.3.4 The BLOB and TEXT Types

ABLOB 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. SeeSection 13.7, “Data Type Storage Requirements”.

BLOB values are treated as binary strings (byte strings). They have thebinary character set and collation, and comparison and sorting 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 other thanbinary, 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 aBLOB 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. SeeSection 7.1.11, “Server SQL Modes”.

Truncation of excess trailing spaces from values to be inserted intoTEXT columns always generates a warning, regardless of the SQL mode.

ForTEXT andBLOB columns, there is no padding on insert and no bytes are stripped on select.

If aTEXT column is indexed, index entry comparisons are space-padded at the end. 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. This is not true forBLOB columns.

In most respects, you can regard aBLOB 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 theBINARY attribute with aTEXT data type, the column is assigned the binary (_bin) collation of the column character set.

LONG andLONG VARCHAR map to theMEDIUMTEXT data type. This is a compatibility feature.

MySQL Connector/ODBC definesBLOB values asLONGVARBINARY andTEXT values asLONGVARCHAR.

BecauseBLOB andTEXT values can be extremely long, you might encounter some constraints in using them:

EachBLOB 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 inBLOB orTEXT columns. You may find MySQL's string handling functions useful for working with such data. SeeSection 14.8, “String Functions and Operators”. For security and other reasons, it is usually preferable to do so using application code rather than giving application users theFILE privilege. You can discuss specifics for various languages and platforms in the MySQL Forums (http://forums.mysql.com/).

Note

Within themysql client, binary strings display using hexadecimal notation, depending on the value of the--binary-as-hex. For more information about that option, seeSection 6.5.1, “mysql — The MySQL Command-Line Client”.