MySQL 8: CHAR, VARCHAR, TEXT, and BLOB data types – explained with examples

Updated: January 26, 2024 By: Guest Contributor Post a comment

Introduction

Each column in a MySQL database is required to have a ‘data type’. This data type tells MySQL what kind of data it can expect — for example, whether it’s textual or numeric, and how much space it should allocate on disk. Data types are crucial since they dictate how data is stored, retrieved, and processed.

In the world of databases, data types are the fundamental attributes that define the kind of data you can store in your columns. MySQL 8, the latest offering from the open-source relational database management system, brings improvements and nuances to data types. Ranging from the efficient CHAR and VARCHAR to the more expansive TEXT and BLOB types, understanding these data types is crucial for database optimization and application development. In this article, we delve into CHAR, VARCHAR, TEXT, and BLOB data types in MySQL 8 and provide examples so you can understand their use and differences.

The CHAR Data Type

CHAR is a fixed-length data type used to store character strings. When you define a column as CHAR(N), it will always consume N bytes of storage, regardless of the actual length of the data stored in it. Here is a basic example:

CREATE TABLE demo_char (
    username CHAR(10)
);

If you insert ‘John’ into the username column, MySQL will pad it with six spaces to ensure that it’s exactly 10 characters long.

INSERT INTO demo_char (username) VALUES ('John');

The retrieval of the data is straightforward:

SELECT username FROM demo_char;
-- Output: 'John      '

CHAR is most suitable for data that has a known, fixed length — for instance, country codes, gender, and other short, fixed-length fields.

The VARCHAR Data Type

If the data entries have significant variations in length, VARCHAR is the preferred data type. It represents variable-length character strings. Unlike CHAR, it stores the actual length of the string plus one or two additional bytes for the length information — saving space when the data is less than the specified length. Here’s how you would use VARCHAR:

CREATE TABLE demo_varchar (
    description VARCHAR(255)
);

Storing a string in this case is a bit different from the CHAR:

INSERT INTO demo_varchar (description) VALUES ('MySQL data type tutorial');

Retrieval of the data ignores trailing spaces, showing only the text:

SELECT description FROM demo_varchar;
-- Output: 'MySQL data type tutorial'

Because VARCHAR fields only use as much space as needed, they’re efficient for storing strings such as names, addresses, or any value with variable and unpredictable length.

The TEXT Data Type

When you need to store large amounts of text, the TEXT data type is a suitable choice. MySQL stores TEXT data outside of the table it’s associated with, referencing it with a pointer. This storage method is more efficient for large blocks of text. TEXT columns come in four object sizes: TINYTEXT, TEXT, MEDIUMTEXT, and LONGTEXT. For example:

CREATE TABLE demo_text (
    article TEXT
);

Let’s insert and retrieve a piece of text:

INSERT INTO demo_text (article) VALUES ('This is a sample text that could quite possibly be much longer.');

SELECT article FROM demo_text;
-- Output: 'This is a sample text that could quite possibly be much longer.'

Remember that since TEXT data types do not store the text directly within the table’s row, performance might differ in comparison to VARCHAR types, especially for smaller texts. TEXT types are also not stored with the trailing spaces.

The BLOB Data Type

BLOB stands for Binary Large Object, and it’s used to store binary data like images, multimedia files, and other byte sequences. Like TEXT, BLOB comes in four sizes: TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB. Use BLOBs when you need to store files directly in the database. Here’s how to create a table for binary data:

CREATE TABLE demo_blob (
    image BLOB
);

Suppose we have an image file ‘photo.jpg’ and we want to store it in the database:

INSERT INTO demo_blob (image) VALUES (LOAD_FILE('/path_to/photo.jpg'));

To retrieve the image back from the database you’d use:

SELECT image FROM demo_blob;

The actual use of BLOB types transcends beyond just storing images, extending to any kind of binary data that needs to be stored in the database. Just remember that integrating large blobs within your database can significantly affect performance. Typical practices involve storing the files on file systems and maintaining references to them within the database.

Collations and Character Sets

Two other considerations while using textual data types in MySQL are collation and character sets. Collation refers to the set of rules for comparing characters in a character set; it determines how MySQL sorts and compares strings. A character set is a set of symbols and encodings. Here’s how they are set at the table-level:

CREATE TABLE demo_collation (
    content VARCHAR(100)
) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

Many applications now use utf8mb4 to fully support Unicode and have consistent data encoding across different languages.

Advanced Topics

In advanced uses of MySQL data types, you may encounter the need to convert between types. Cast functions like CAST() and CONVERT() provide ways to transform one data type into another:

SELECT CAST(article AS CHAR(255)) as excerpt FROM demo_text;

SELECT CONVERT(description USING latin1) from demo_varchar;

Indexing and full-text searches also come into play with a performance impact. Here’s an example implementing a full-text index on a text column:

ALTER TABLE demo_text ADD FULLTEXT(article);
SELECT * FROM demo_text WHERE MATCH(article) AGAINST('sample' IN NATURAL LANGUAGE MODE);

Remember that when using data types and selecting between CHAR, VARCHAR, TEXT, and BLOB, think about the nature of your data and how it will be used. The trade-offs between them mostly revolve around performance and storage efficiency.

Conclusion

In this tutorial, we covered the essential aspects of the CHAR, VARCHAR, TEXT, and BLOB data types in MySQL 8, along with examples of usage and considerations in terms of performance and storage optimization. The understanding of these data types is a foundational skill for developers and database administrators which contributes significantly to the effective and efficient management of MySQL databases.