Quick Reference · relational database & SQL

mysql cheat sheet

Every statement either defines structure (DDL), changes data (DML), reads data (DQL), or controls the server (DCL/TCL). Learn which bucket a keyword falls into and the syntax stops being 400 things to memorize.

connect / inspect structure (DDL) data (DML) query (DQL) admin (DCL/TCL) destructive most common

Distilled & cross-checked against: dev.mysql.com/doc/refman · mysqltutorial.org · learnsql.com · quickref.me · devhints.io · phoenixnap.com

Anatomy of a SELECT — the order you write it vs. the order MySQL runs it
YOU WRITE ↓ SELECT FROM / JOIN WHERE GROUP BY HAVING ORDER BY LIMIT written left → right MYSQL RUNS ↓ (this order decides what's valid & what's fast) 1· FROM + JOIN, builds row set 2· WHERE filters raw rows 3· GROUP BY buckets into groups 4· HAVING filters groups 5· SELECT picks columns 6· ORDER BY, then LIMIT Why this matters • WHERE can't reference a column alias defined in SELECT — SELECT hasn't run yet. • Filter with WHERE before grouping (cheap); filter with HAVING after grouping, on aggregates (expensive). • ORDER BY / LIMIT run last — MySQL can sort & page only once every earlier stage is resolved.
01Connect & Sessioncommand-line client
02DatabasesDDL
03Create & Inspect TablesDDL
04Alter Tablereshape existing structure
05Data Typeswhat a column can hold

Numeric

TINYINT1 byte · ±127 (0–255 unsigned)
SMALLINT / MEDIUMINT2 / 3 bytes
INT4 bytes · ±2.1 billion
BIGINT8 bytes · ±9.2×10¹⁸
DECIMAL(M,D)exact fixed-point — use for money
FLOAT / DOUBLEapproximate, floating-point

String

CHAR(M)fixed length, padded, ≤255
VARCHAR(M)variable length, ≤65,535 bytes/row share
TEXT / LONGTEXT64KB → 4GB of text
ENUM('a','b')one value from a fixed list
BLOB / LONGBLOBbinary data, same size tiers as TEXT

Date & Time

DATEYYYY-MM-DD
DATETIMEYYYY-MM-DD HH:MM:SS, no TZ conversion
TIMESTAMPlike DATETIME, UTC-stored, 1970–2038
TIME / YEARHH:MM:SS / YYYY

Other

BOOLEANalias for TINYINT(1)
JSONvalidated JSON document (5.7+)
GEOMETRY / POINTspatial types (needs SRID for GIS work)
06Insert DataDML
07Select — BasicsDQL
08Filter Rows — WHEREDQL
09Sort & PaginateDQL
10JoinsDQL · combine tables
11Group & AggregateDQL · roll rows up
12Update & DeleteDML · handle with care
13Keys & ConstraintsDDL · data integrity
14Indexes & EXPLAINDDL · speed up reads
15TransactionsTCL · InnoDB only
16Users & PrivilegesDCL · access control
17ViewsDDL · saved queries
18Backup, Restore & Importshell · not run inside mysql>
19Common Functionsstring / date / numeric
Operators & Wildcardsused throughout WHERE / LIKE

Join types at a glance

Same two tables, five different result sets. Shaded area = rows returned. Based on the standard join-Venn reference used across MySQL, PostgreSQL & SQL Server docs.

INNER JOIN

Only rows where the key exists on both sides.

a b

LEFT JOIN

All of the left table, matched right rows or NULL.

a b

RIGHT JOIN

All of the right table, matched left rows or NULL.

a b

FULL OUTER (via UNION)

Everything from both sides. MySQL has no native FULL JOIN — simulate with LEFT UNION RIGHT.

a b

CROSS JOIN

Cartesian product — every row of a paired with every row of b.

a1 a2 a3 b1 b2 × 3 × 2 = 6 result rows

Worth memorizing

DELETE ≠ TRUNCATEDELETE is row-logged & rollback-able; TRUNCATE resets the table instantly & can't
WHERE ≠ HAVINGWHERE filters rows before grouping; HAVING filters groups after
PRIMARY KEY ≠ UNIQUEPK forbids NULL, one per table; UNIQUE allows one NULL, many per table
InnoDB vs MyISAMInnoDB = transactions + foreign keys; MyISAM lacks both
= vs <=><=> is NULL-safe; plain = against NULL is always unknown
autocommitON by default — every statement commits itself unless in a transaction
LIMIT 40,20same as LIMIT 20 OFFSET 40 — offset first, count second
CHAR vs VARCHARCHAR is fixed-length & padded; VARCHAR stores only what you write