HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

MySQL String Data Types

String data types store text in MySQL — from a single character to an entire article. MySQL provides several string types, and picking the right one depends on how long the text is, whether its length varies, and whether the values must come from a fixed list. This lesson covers every important string type with clear examples.


CHAR — Fixed-Length Strings

CHAR(n) stores a string of exactly n characters. If you store a shorter value, MySQL pads it with spaces on the right to always fill the full length. The maximum value of n is 255.

country_code CHAR(2)

This column always stores exactly 2 characters — 'BD', 'US', 'GB'. Because every value is the same length, MySQL can locate rows very quickly, making CHAR slightly faster than VARCHAR for fixed-length data.

Best for: Country codes, currency codes, gender flags ('M' / 'F'), fixed-length hashes like MD5 (CHAR(32)), and postal codes where length never varies.

VARCHAR — Variable-Length Strings

VARCHAR(n) stores a string of up to n characters. Unlike CHAR, it only uses the space the actual value needs, plus 1–2 bytes to record the length. The maximum value of n is 65,535 (though this is also affected by the row size limit and character set).

email VARCHAR(150) NOT NULL,
full_name VARCHAR(100) NOT NULL

VARCHAR is the most widely used string type in MySQL because most real-world text — names, emails, titles, URLs — varies in length from row to row. Using VARCHAR avoids wasting space that CHAR would pad with.

Feature CHAR(n) VARCHAR(n)
Storage length Always exactly n characters Only as long as the actual value
Trailing spaces Pads with spaces Preserves trailing spaces
Performance Slightly faster for equal-length data Better for varying-length data
Maximum length 255 characters 65,535 characters
Best for Country codes, hashes, fixed codes Names, emails, titles, URLs

TEXT Types — Long Text Content

When text can be very long — like a blog post, product description, or user comment — the TEXT family is the right choice. MySQL offers four sizes depending on how much content you expect:

Type Maximum Length Typical Use
TINYTEXT 255 characters Short notes, brief descriptions
TEXT 65,535 characters (~64 KB) Comments, articles, product descriptions
MEDIUMTEXT 16,777,215 characters (~16 MB) Long articles, documentation, HTML content
LONGTEXT 4,294,967,295 characters (~4 GB) Entire books, large JSON exports, log files
Note: Unlike VARCHAR, you cannot specify a length inside the TEXT types — just write TEXT, not TEXT(500). Also, TEXT columns cannot have a DEFAULT value in MySQL.

ENUM — A Controlled List of Values

ENUM lets you define a column that accepts only one value from a predefined list. If someone tries to insert a value that is not in the list, MySQL either stores an empty string (in non-strict mode) or returns an error (in strict mode).

status ENUM('pending', 'active', 'suspended', 'deleted') NOT NULL DEFAULT 'pending'

Internally, MySQL stores each ENUM value as a small integer (1 byte for up to 255 values, 2 bytes for up to 65,535), so it is more space-efficient than storing the full string. It also makes your data cleaner by preventing typos — 'actve' or 'ACTIVE' would be rejected.

Best for: Status fields, role types, category labels, gender — any column where the complete set of valid values is known and small.

SET — Multiple Values from a List

SET is similar to ENUM, but a SET column can hold multiple values from the defined list at once, stored as a comma-separated string. A SET can contain up to 64 members.

permissions SET('read', 'write', 'delete', 'admin') NOT NULL DEFAULT 'read'

A row could store 'read,write' or 'read,write,delete' in this column. However, SET is rarely used in modern database design — a separate junction table with proper foreign keys is usually the cleaner approach for multi-value relationships.


Practical Example

Here is an articles table that uses a variety of string types appropriately:

CREATE TABLE articles (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    slug VARCHAR(200) NOT NULL UNIQUE,
    title VARCHAR(250) NOT NULL,
    summary VARCHAR(500),
    body MEDIUMTEXT NOT NULL,
    status ENUM('draft', 'published', 'archived') NOT NULL DEFAULT 'draft',
    language CHAR(5) NOT NULL DEFAULT 'en',
    PRIMARY KEY (id)
);
  • slug — URL-friendly identifier, variable length, must be unique.
  • title — article heading, up to 250 characters.
  • summary — optional short excerpt, up to 500 characters.
  • body — full article content, potentially several megabytes.
  • status — controlled list, only the three defined states allowed.
  • language — fixed short code like 'en' or 'bn'.

Key Points to Remember

  • Use VARCHAR(n) for most text columns where length varies — names, emails, titles, URLs.
  • Use CHAR(n) only for truly fixed-length values like country codes or hashes.
  • Use TEXT for long content like articles, comments, and descriptions — it has no length parameter.
  • Use MEDIUMTEXT or LONGTEXT only when you genuinely expect very large amounts of text.
  • Use ENUM when a column should only accept one value from a known, small list.
  • TEXT columns cannot have a DEFAULT value in MySQL.