← Back to MySQL Course | Chapter 3: Data Types | Lesson 2 of 8

String Data Types

CHAR vs VARCHAR

CHAR always reserves the same fixed number of bytes per row and pads shorter values with spaces, while VARCHAR only uses as much space as the actual string needs — VARCHAR is the better default for names, emails, and other variable-length text.

Example: CHAR vs VARCHAR

sql
CREATE TABLE users (
  code CHAR(5),
  name VARCHAR(50)
);

TEXT Type

TEXT columns are built for large, unbounded blocks of content like articles or product descriptions, and unlike VARCHAR they're stored separately from the main row data, which affects how efficiently you can index them.

Example: TEXT Type

sql
CREATE TABLE articles (
  title VARCHAR(100),
  body TEXT
);

String Length Functions

Functions like CHAR_LENGTH() and LENGTH() report a string's size in characters versus bytes respectively — they differ once multi-byte Unicode characters like emoji are involved.

Example: String Length Functions

sql
SELECT CHAR_LENGTH('café') AS chars, LENGTH('café') AS bytes;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Binary Strings

BINARY and VARBINARY store raw byte sequences rather than human-readable text, which makes them appropriate for hashes, encrypted blobs, or fixed-format binary identifiers rather than ordinary strings.

Example: Binary Strings

sql
CREATE TABLE files (
  hash BINARY(32),
  data VARBINARY(255)
);

String Collation

Collation settings control whether string comparisons treat apple and Apple as equal or distinct, and whether sorting follows locale-specific alphabetical rules — get this wrong and search features can feel broken.

Example: String Collation

sql
CREATE TABLE names (name VARCHAR(50)) COLLATE utf8mb4_general_ci;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.