MySQL

MySQL Cheatsheet #

MySQL is a widely used relational database management system known for its speed, reliability, and ease of use. It is commonly used for managing databases in various applications.

General Commands #

Command/OptionExampleDescription
mysql -u <user> -pmysql -u root -pConnect to MySQL server with username and password
mysql -u <user> -p <database>mysql -u root -p mydbConnect to a specific database
exitexitExit the MySQL command-line client

Database Management #

Command/OptionExampleDescription
CREATE DATABASE <db_name>;CREATE DATABASE mydb;Create a new database
DROP DATABASE <db_name>;DROP DATABASE mydb;Delete a database
SHOW DATABASES;SHOW DATABASES;List all databases

Table Management #

Command/OptionExampleDescription
USE <db_name>;USE mydb;Switch to a specific database
CREATE TABLE <table_name> (column_definitions);CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100));Create a new table
DROP TABLE <table_name>;DROP TABLE users;Delete a table
SHOW TABLES;SHOW TABLES;List all tables in the current database

Data Manipulation #

Command/OptionExampleDescription
INSERT INTO <table> (columns) VALUES (values);INSERT INTO users (name) VALUES ('John Doe');Insert new data into a table
SELECT * FROM <table>;SELECT * FROM users;Retrieve all data from a table
UPDATE <table> SET column = value WHERE condition;UPDATE users SET name = 'Jane Doe' WHERE id = 1;Update existing data in a table
DELETE FROM <table> WHERE condition;DELETE FROM users WHERE id = 1;Delete data from a table

Query Filtering and Sorting #

Command/OptionExampleDescription
SELECT column1, column2 FROM <table> WHERE condition;SELECT name FROM users WHERE id = 1;Retrieve specific data based on a condition
`SELECT * FROM ORDER BY column [ASCDESC];`SELECT * FROM users ORDER BY name ASC;

Indexing #

Command/OptionExampleDescription
CREATE INDEX <index_name> ON <table> (column);CREATE INDEX idx_name ON users (name);Create an index on a column
DROP INDEX <index_name> ON <table>;DROP INDEX idx_name ON users;Delete an index

User Management #

Command/OptionExampleDescription
CREATE USER '<username>'@'<host>' IDENTIFIED BY '<password>';CREATE USER 'user'@'localhost' IDENTIFIED BY 'password';Create a new MySQL user
DROP USER '<username>'@'<host>';DROP USER 'user'@'localhost';Delete a MySQL user
GRANT ALL PRIVILEGES ON <db_name>.* TO '<username>'@'<host>';GRANT ALL PRIVILEGES ON mydb.* TO 'user'@'localhost';Grant privileges to a user
REVOKE ALL PRIVILEGES ON <db_name>.* FROM '<username>'@'<host>';REVOKE ALL PRIVILEGES ON mydb.* FROM 'user'@'localhost';Revoke privileges from a user

Backup and Restore #

Command/OptionExampleDescription
mysqldump -u <user> -p <db_name> > <file.sql>mysqldump -u root -p mydb > mydb_backup.sqlBackup a database to a SQL file
mysql -u <user> -p <db_name> < <file.sql>mysql -u root -p mydb < mydb_backup.sqlRestore a database from a SQL file

Server Status and Configuration #

Command/OptionExampleDescription
SHOW STATUS;SHOW STATUS;Display server status variables
SHOW VARIABLES;SHOW VARIABLES;Display server system variables
SHOW PROCESSLIST;SHOW PROCESSLIST;Show currently running threads

This cheatsheet covers the most commonly used MySQL commands and options, helping you manage databases, tables, data, indexing, user permissions, backups, and server status efficiently.

MySQL

Explore our comprehensive cheatsheets to enhance your knowledge and efficiency. Each cheatsheet provides detailed command options, examples, and descriptions to help you master various tools and technologies.