320 x 50 Mobile Ad
Cheat Sheets

MySQL Cheat Sheet

Transform, format, and optimize your data instantly with this free client-side tool.

CLI & Admin

mysql -u root -p

Connect to MySQL via terminal

mysql -h host -u user -p

Connect to a remote MySQL host

mysqldump -u user -p db > db.sql

Backup database to a SQL file

mysql -u user -p db < db.sql

Restore database from a SQL file

SHOW PROCESSLIST;

List currently running threads/queries

KILL process_id;

Kill a specific thread by ID

SHOW VARIABLES LIKE 'max_connections';

Show server system variables

Databases

CREATE DATABASE db_name;

Create a new database

DROP DATABASE db_name;

Delete a database

SHOW DATABASES;

List all databases on the server

USE db_name;

Select a database to use

Tables

SHOW TABLES;

List all tables in current database

DESCRIBE table_name;

Show table structure (columns and data types)

CREATE TABLE t (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50));

Create a new table

DROP TABLE table_name;

Delete a table

TRUNCATE TABLE table_name;

Empty all data from a table, reset auto increments

ALTER TABLE t ADD col INT;

Add a column to an existing table

ALTER TABLE t DROP COLUMN col;

Remove a column from a table

ALTER TABLE t MODIFY col VARCHAR(100);

Change column data type

RENAME TABLE old_name TO new_name;

Rename an existing table

CRUD

SELECT * FROM t;

Retrieve all rows and columns

INSERT INTO t (col1, col2) VALUES ('val1', 'val2');

Insert a new row

UPDATE t SET col1 = 'new' WHERE id = 1;

Update existing rows

DELETE FROM t WHERE id = 1;

Delete rows matching a condition

INSERT INTO t (id, col) VALUES (1, 'val') ON DUPLICATE KEY UPDATE col = 'val';

Insert, or update if duplicate key exists

Queries

SELECT col1, col2 FROM t;

Retrieve specific columns

SELECT DISTINCT col FROM t;

Retrieve only unique values

SELECT * FROM t WHERE col = 'val';

Filter with WHERE clause

SELECT * FROM t WHERE col IN ('val1', 'val2');

Match any value in list

SELECT * FROM t WHERE col LIKE '%pattern%';

Pattern matching with wildcards

SELECT * FROM t ORDER BY col DESC;

Sort results in descending order

SELECT * FROM t LIMIT 10 OFFSET 20;

Paginate (limit num rows, offset start)

Joins

SELECT * FROM t1 INNER JOIN t2 ON t1.id = t2.t1_id;

Inner join (matching rows in both)

SELECT * FROM t1 LEFT JOIN t2 ON t1.id = t2.t1_id;

Left join (all t1, matching t2)

SELECT * FROM t1 RIGHT JOIN t2 ON t1.id = t2.t1_id;

Right join (all t2, matching t1)

Aggregation

SELECT COUNT(*) FROM t;

Count total rows

SELECT col, COUNT(*) FROM t GROUP BY col;

Group rows and count occurrences

SELECT SUM(col) FROM t;

Calculate sum of a numeric column

SELECT AVG(col) FROM t;

Calculate average of a numeric column

SELECT col, COUNT(*) FROM t GROUP BY col HAVING COUNT(*) > 5;

Filter grouped results with HAVING

Users & Privileges

CREATE USER 'user'@'localhost' IDENTIFIED BY 'password';

Create a new database user

GRANT ALL PRIVILEGES ON db_name.* TO 'user'@'localhost';

Grant all permissions on a database

GRANT SELECT, INSERT ON db.* TO 'user'@'%';

Grant specific permissions globally

SHOW GRANTS FOR 'user'@'localhost';

List assigned permissions for a user

REVOKE ALL PRIVILEGES ON db.* FROM 'user'@'localhost';

Remove permissions

DROP USER 'user'@'localhost';

Delete a user

FLUSH PRIVILEGES;

Reload privileges from grant tables

Performance

CREATE INDEX idx_name ON t (col);

Create an index on a column

CREATE UNIQUE INDEX idx_name ON t (col);

Create a unique index

DROP INDEX idx_name ON t;

Delete an index

EXPLAIN SELECT * FROM t WHERE col = 'val';

Analyze query execution plan

Advanced

Advanced Command Snippet

Explore advanced configurations for Mysql

System Optimization

Performance tuning best practices for Mysql

Security Audit

Run security checks and validation for Mysql

320 x 50 Mobile Ad

Frequently Asked Questions

Got questions? We've got answers.

The Mysql Cheatsheet is a quick-reference guide that provides developers with the most essential and frequently used Mysql commands, syntax, and snippets in one centralized place.
This reference is built for both beginners who are just learning Mysql and need a quick syntax lookup, as well as seasoned professionals who need to jog their memory on complex commands.
You can use the real-time search bar at the top of the cheatsheet. Simply type a keyword (like &#39;delete&#39; or &#39;file&#39;) and the list will instantly filter to show only matching Mysql commands.
Yes! Every command block features a one-click copy button. Just hover over the command and click the copy icon to instantly send the snippet to your clipboard.
The cheatsheet is divided into logical categories such as Basics, Network, Operations, and specific Mysql features. You can click on the category filters at the top to isolate specific groups of commands.
Absolutely. This Mysql reference guide is 100% free, requiring no sign-ups or subscriptions, and is always available when you need it.
While these are standard Mysql operations, you should always understand what a command does before running it, especially if it involves system operations or destructive actions.
Yes, we have recently expanded this cheatsheet to include advanced snippets, best practices, and edge-case syntax that go beyond basic introductory commands.
ADVERTISEMENT
Boost Your Business Online