env.dev

MySQL Cheat Sheet — Commands & Queries Quick Reference

MySQL quick reference: data types, CRUD operations, JOINs, indexes, user management, backup/restore, and common functions.

By env.dev Updated

A quick reference for MySQL commands and syntax. Covers data types, CRUD operations, JOINs, indexes, user management, backup/restore, common functions, and schema changes. MySQL 8.0 reached end of life on 30 April 2026 — 8.4 LTS (supported to 2032) is the upgrade target, with 9.7 (April 2026) opening the next LTS series.

Data Types

TypeDescription
INT / BIGINTInteger (4 bytes / 8 bytes)
DECIMAL(10,2)Exact numeric with precision
FLOAT / DOUBLEApproximate floating-point
VARCHAR(255)Variable-length string (max 255)
TEXT / LONGTEXTLarge text (64KB / 4GB max)
BOOLEANAlias for TINYINT(1)
DATE / DATETIMEDate / date and time
TIMESTAMPUTC timestamp, auto-converts timezone — range ends 2038-01-19
JSONNative JSON data type
ENUM("a","b","c")Predefined set of string values

CRUD Operations

CommandDescription
SELECT * FROM users WHERE id = 1Read rows matching condition
SELECT name, email FROM users ORDER BY nameSelect specific columns, sorted
SELECT COUNT(*) FROM usersCount rows
INSERT INTO users (name, email) VALUES ("Jo", "jo@x.com")Insert a row
INSERT INTO users (name) VALUES ("A"), ("B")Insert multiple rows
UPDATE users SET name = "Jo" WHERE id = 1Update matching rows
DELETE FROM users WHERE id = 1Delete matching rows
SELECT DISTINCT status FROM ordersSelect unique values

JOINs

SyntaxDescription
INNER JOIN t2 ON t1.id = t2.fkRows matching in both tables
LEFT JOIN t2 ON t1.id = t2.fkAll rows from left, matched from right
RIGHT JOIN t2 ON t1.id = t2.fkAll rows from right, matched from left
CROSS JOIN t2Cartesian product of both tables
SELECT ... FROM t1 JOIN t2 USING (id)Join using same-named column
SELECT * FROM t1 NATURAL JOIN t2Join on all same-named columns
LEFT JOIN t2 ON ... WHERE t2.id IS NULLRows in t1 with no match in t2

Indexes

CommandDescription
CREATE INDEX idx_name ON users (name)Create an index
CREATE UNIQUE INDEX idx_email ON users (email)Create a unique index
CREATE INDEX idx_comp ON t (a, b)Create a composite index
DROP INDEX idx_name ON usersDrop an index
SHOW INDEX FROM usersShow indexes on a table
EXPLAIN SELECT * FROM users WHERE name = "Jo"Show query execution plan
CREATE FULLTEXT INDEX idx_ft ON posts (body)Create full-text search index

User Management

CommandDescription
CREATE USER 'app'@'%' IDENTIFIED BY 'pass'Create user (any host)
GRANT ALL ON mydb.* TO 'app'@'%'Grant all privileges on database
GRANT SELECT, INSERT ON mydb.users TO 'app'@'%'Grant specific privileges
REVOKE INSERT ON mydb.users FROM 'app'@'%'Revoke a privilege
SHOW GRANTS FOR CURRENT_USERShow current user privileges
DROP USER 'app'@'%'Delete a user
ALTER USER 'app'@'%' IDENTIFIED BY 'newpass'Change user password
FLUSH PRIVILEGESReload privilege tables

Backup & Restore

CommandDescription
mysqldump -u root -p mydb > backup.sqlDump entire database
mysqldump -u root -p mydb users > users.sqlDump a single table
mysqldump --all-databases > all.sqlDump all databases
mysqldump --single-transaction mydb > backup.sqlConsistent dump without locking tables (InnoDB)
mysql -u root -p mydb < backup.sqlRestore from dump file
mysqldump --no-data mydb > schema.sqlDump schema only (no data)
mysqldump --routines --triggers mydb > full.sqlInclude routines and triggers

Common Functions

FunctionDescription
NOW()Current date and time
CURDATE() / CURTIME()Current date / current time
CONCAT(a, b)Concatenate strings
IFNULL(expr, default)Return default if expr is NULL
COALESCE(a, b, c)Return first non-NULL argument
GROUP_CONCAT(col SEPARATOR ",")Aggregate values into a string
DATE_FORMAT(dt, "%Y-%m-%d")Format a date value
SUBSTRING(str, pos, len)Extract part of a string

ALTER TABLE

CommandDescription
ALTER TABLE t ADD COLUMN age INTAdd a column
ALTER TABLE t DROP COLUMN ageRemove a column
ALTER TABLE t MODIFY COLUMN name VARCHAR(100)Change column type
ALTER TABLE t RENAME COLUMN old TO newRename a column
ALTER TABLE t ADD PRIMARY KEY (id)Add primary key
ALTER TABLE t ADD FOREIGN KEY (uid) REFERENCES users(id)Add foreign key
ALTER TABLE t RENAME TO new_tableRename a table
ALTER TABLE t ENGINE = InnoDBChange storage engine

Common gotchas

  • MySQL 8.0 hit end of life on 30 April 2026 — no more security patches. AWS RDS auto-enrolls remaining 8.0 instances into paid Extended Support from August 2026. Upgrade to 8.4 LTS; skipping an LTS series (8.4 → 10.x) is not a supported path.
  • In 8.4, mysql_native_password is disabled by default — old clients and connectors fail at auth after the upgrade. Migrate accounts to caching_sha2_password first.
  • utf8 is an alias for the 3-byte utf8mb3 — emoji and many CJK characters fail to insert. Always use utf8mb4 (the default since 8.0).
  • TIMESTAMP converts to the session time zone on read and dies at 2038; DATETIME stores what you wrote, no conversion. Pick one convention per schema and document it.
  • Plain mysqldump takes table locks for the whole dump on busy databases. Use --single-transaction for InnoDB — and remember TRUNCATE and DDL commit implicitly, so they cannot be rolled back.

Dialect-agnostic syntax lives in the SQL cheat sheet; comparing engines? See the PostgreSQL cheat sheet. Format long queries with the SQL formatter.

Was this helpful?

Frequently Asked Questions

What is the difference between MySQL and MariaDB?

MariaDB is a fork of MySQL created by its original developers. They are largely compatible with similar syntax and features. MariaDB adds some extra storage engines and features not in MySQL.

How do I backup a MySQL database?

Use mysqldump -u user -p database > backup.sql for a logical backup. For large databases, use mysqldump with --single-transaction for InnoDB tables. Use mysql < backup.sql to restore.

What is the difference between InnoDB and MyISAM?

InnoDB supports transactions, foreign keys, and row-level locking — use it for most applications. MyISAM is faster for read-heavy workloads but lacks transactions and crash recovery. InnoDB is the default since MySQL 5.5.