Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
String data encompasses everything from customer names, emails, and postal codes to full-length blog posts, JSON strings, and product descriptions. MySQL provides several string and binary data types, each with distinct storage mechanics, indexing capabilities, and performance profiles.
CHAR vs VARCHAR: Fixed-Length vs Variable-LengthThe two most frequently used string types are CHAR and VARCHAR.
| Feature | CHAR(M) | VARCHAR(M) |
|---|---|---|
| Length Type | Fixed length (always allocates M characters). | Variable length (allocates only actual characters + length prefix). |
| Max Capacity | 0 to 255 characters. | 0 to 65,535 bytes (shared across the entire row). |
| Padding | Right-padded with spaces upon storage; stripped on retrieval. | No trailing padding. |
| Length Prefix | None (0 bytes). | 1 byte (if length <= 255), 2 bytes (if length > 255). |
| Ideal For | Fixed-length codes: Country code (IN, US), MD5/SHA256 hashes, PIN codes. | Variable-length strings: Names, email addresses, street addresses. |
TEXT FamilyWhen a string exceeds the capacity of VARCHAR or represents large prose (e.g., article content, product reviews), use the TEXT family:
| Type | Maximum Storage | Typical Use Case |
|---|---|---|
TINYTEXT | 255 bytes | Short notes, subtitles. |
TEXT | 65,535 bytes (~64 KB) | Blog comments, product descriptions. |
MEDIUMTEXT | 16,777,215 bytes (~16 MB) | Full articles, book chapters, long HTML pages. |
LONGTEXT | 4,294,967,295 bytes (~4 GB) | Massive legal contracts, raw log dumps. |
TEXT columns cannot have default values in older MySQL versions and cannot be fully indexed in their entirety without specifying a prefix length (e.g., INDEX (content(255))).BINARY, VARBINARY, and BLOBWhile CHAR and VARCHAR store text (subject to character sets and collations), binary types store raw byte sequences:
VARBINARY(M): Variable-length byte string (e.g., encrypted hashes, UUID binary blobs).BLOB (Binary Large Object): Stores raw binary data such as PDF documents, images, or audio clips. Divided into TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB.VARCHAR(500)) in the database!TEXT and BLOB columns store only a 24-byte pointer on-page, with the rest stored in off-page overflow tablespaces.VARCHAR(100) store a string containing 10 characters?LONGTEXT column in MySQL?VARCHAR and VARBINARY?MEDIUMBLOB directly in a relational table?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Numeric Data Types | Date & Time Types |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.