Quick reference

MySQL Common Commands Cheat Sheet

A complete MySQL command reference manual covering database operations, table management, query optimization, and more, helping you quickly master MySQL database development and management skills.

· Published · Updated

MySQL is the world's most popular open-source relational database management system. This cheat sheet organizes the most commonly used SQL statements, administration commands, query optimization hints, and permission controls.

Connection & Server Status

Command Description Example
mysql -u root -p Connect to MySQL server as user mysql -h localhost -u root -p
exit / quit Exit MySQL CLI exit
SHOW DATABASES; List all databases SHOW DATABASES;
USE <database>; Switch active database USE my_app;
STATUS; Display current server status & connection details STATUS;

Database Operations (DDL)

Command Description Example
CREATE DATABASE Create a new database CREATE DATABASE test_db;
DROP DATABASE Delete database and all tables DROP DATABASE test_db;
SHOW TABLES; List all tables in current database SHOW TABLES;
DESCRIBE <table>; Inspect table schema and column definitions DESCRIBE users;
CREATE TABLE Create new table CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50));
DROP TABLE Delete table permanently DROP TABLE old_logs;
TRUNCATE TABLE Empty all rows from table and reset auto-increment TRUNCATE TABLE temp_sessions;

CRUD Operations (DML)

Insert Data

INSERT INTO users (name, age, email) VALUES 
  ('Alice', 25, 'alice@example.com'),
  ('Bob', 30, 'bob@example.com');

Query Data

-- Filter, sort, and pagination
SELECT id, name, age FROM users 
WHERE age >= 21 AND status = 'active'
ORDER BY age DESC 
LIMIT 10 OFFSET 0;

-- Aggregate functions
SELECT COUNT(*), AVG(age) FROM users;

Update Data

UPDATE users 
SET age = 26, updated_at = NOW() 
WHERE name = 'Alice';

Delete Data

DELETE FROM users WHERE id = 10;

Table Joins (JOIN)

Join Type Description
INNER JOIN Returns rows when there is a match in both tables
LEFT JOIN Returns all rows from left table, and matching rows from right
RIGHT JOIN Returns all rows from right table, and matching rows from left
CROSS JOIN Produces cartesian product of both tables
SELECT u.name, o.order_number, o.total_amount
FROM users u
INNER JOIN orders o ON u.id = o.user_id
WHERE o.status = 'completed';

Index Management

Command Description
CREATE INDEX idx_name ON table(col); Create standard B-tree index
CREATE UNIQUE INDEX idx_email ON table(col); Create unique constraint index
SHOW INDEX FROM table; List indexes on table
DROP INDEX idx_name ON table; Drop index from table

Query Optimization & Performance

Command Description
EXPLAIN SELECT ... Display execution plan chosen by optimizer
ANALYZE TABLE <table>; Update key distribution statistics
OPTIMIZE TABLE <table>; Defragment table storage and reclaim unused space

User Management & Permissions (DCL)

Command Description
CREATE USER 'app'@'%' IDENTIFIED BY 'secret'; Create a new user account
GRANT SELECT, INSERT ON db.* TO 'app'@'%'; Grant specific privileges
GRANT ALL PRIVILEGES ON db.* TO 'admin'@'%'; Grant all privileges
REVOKE INSERT ON db.* FROM 'app'@'%'; Revoke specific privileges
FLUSH PRIVILEGES; Reload privilege tables
DROP USER 'app'@'%'; Remove user account

Backup & Recovery

Command Description
mysqldump -u root -p db_name > backup.sql Export database dump (terminal)
mysql -u root -p db_name < backup.sql Restore database from SQL dump

Official References

Related topics