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
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
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
SELECT CHAR_LENGTH('café') AS chars, LENGTH('café') AS bytes;
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
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
CREATE TABLE names (name VARCHAR(50)) COLLATE utf8mb4_general_ci;
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: