Skip to content

SQL Notes

MYSQL

Data Types

🔢 Numeric Types

TypeRange/Use Case
TINYINT-128 to 127 (or 0-255 unsigned). Ideal for booleans (0/1) or small enums.
SMALLINT-32,768 to 32,767 (0-65,535 unsigned). Small counters/IDs.
MEDIUMINT-8.3M to 8.3M (0-16M unsigned). Middle-ground integers.
INT-2.1B to 2.1B (0-4.2B unsigned). Default for most whole numbers (e.g., user IDs).
BIGINT±9.2 quintillion (0-18 quintillion unsigned). Large IDs (e.g., Twitter tweets).
FLOATApproximate decimals (7 digits). Scientific data, less precise.
DOUBLEApproximate decimals (15 digits). Larger/more precise than FLOAT.
DECIMAL(p,s)Exact decimals (e.g., DECIMAL(10,2) for currency). Precision = total digits, scale = decimals.

📅 Date & Time

TypeUse Case
DATEYYYY-MM-DD (no time). Birthdates, events.
TIMEHH:MM:SS (or -838:59:59 to 838:59:59). Durations.
DATETIMEYYYY-MM-DD HH:MM:SS (no timezone). Local events (e.g., user signup).
TIMESTAMPUnix epoch (1970-2038, timezone-aware). Auto-updates on row change.
YEAR1901-2155 (or 4-digit format). Rarely used.

📜 String Types

TypeMax Size/Use Case
CHAR(n)Fixed-length (1-255 chars). Fast for exact-length strings (e.g., country codes).
VARCHAR(n)Variable-length (1-65,535 chars). Default for most text (e.g., usernames, emails).
TINYTEXT255 chars. Short text (rarely needed over VARCHAR).
TEXT65KB. Articles, comments.
MEDIUMTEXT16MB. Large documents (e.g., JSON blobs).
LONGTEXT4GB. Massive text (e.g., logs, books).
BINARY(n)Fixed-length binary (e.g., hashes).
VARBINARY(n)Variable binary (e.g., encrypted data).
ENUMPredefined list (e.g., ENUM('red','green','blue')). Limited options.
SETMultiple values from a list (e.g., SET('a','b','c')). Rarely used.

🌀 Other Types

TypeUse Case
BOOLEANAlias for TINYINT(1) (0=false, 1=true).
JSONStore/query JSON data (MySQL 5.7+). Semi-structured data (e.g., configs).
BLOBBinary data (images, files). Prefer TEXT for UTF-8.

⚡ Quick Rules of Thumb

  • Integers: Use INT unless space/size demands otherwise.
  • Decimals: DECIMAL for money (exact), FLOAT/DOUBLE for scientific.
  • Strings: VARCHAR for most text, TEXT for large content.
  • Dates: DATETIME for general use, TIMESTAMP for auto-updates.

Handcrafted with 💜 in Bangalore, India