MySQL Cheat Sheet

MySQL is a popular open-source relational database management system used for storing and managing data. This cheatsheet provides a quick reference guide for common MySQL commands and operations.

Basic Commands

CommandDescription
mysql -u [username] -pLog in to MySQL as a specific user
SHOW DATABASES;Display a list of available databases
USE [database];Switch to a specific database
SHOW TABLES;Show tables in the current database
DESCRIBE [table];Display the structure of a table

Data Definition Language (DDL)

CommandDescription
CREATE DATABASE [name];Create a new database
CREATE TABLE [table]...;Create a new table
ALTER TABLE [table]...;Modify the structure of a table
DROP DATABASE [name];Delete a database
DROP TABLE [table];Delete a table

Data Manipulation Language (DML)

CommandDescription
INSERT INTO [table]...;Insert new records into a table
UPDATE [table]...;Update existing records in a table
DELETE FROM [table]...;Delete records from a table
SELECT [columns] FROM [table];Retrieve data from a table

Data Querying

CommandDescription
SELECT * FROM [table];Retrieve all columns from a table
SELECT [columns] FROM [table] WHERE [condition];Filter data based on a condition
ORDER BY [column] [ASC/DESC];Sort the result set
LIMIT [number];Limit the number of rows returned

Data Filtering

CommandDescription
WHERE [condition];Filter records based on a condition
ANDCombine multiple conditions with AND
ORCombine multiple conditions with OR
IN ([values]);Filter records where a column is in a list

Joins

CommandDescription
JOIN [table] ON [condition];Combine rows from two or more tables
INNER JOIN [table] ON [condition];Return only matching rows
LEFT JOIN [table] ON [condition];Return all rows from the left table
RIGHT JOIN [table] ON [condition];Return all rows from the right table
FULL JOIN [table] ON [condition];Return all rows when there is a match

Indexing

CommandDescription
CREATE INDEX [index] ON [table] ([columns]);Create an index on one or more columns
DROP INDEX [index] ON [table];Remove an index from a table

Transactions

CommandDescription
START TRANSACTION;Begin a new transaction
COMMIT;Save the changes made during the transaction
ROLLBACK;Undo the changes made during the transaction

This cheatsheet covers a wide range of MySQL commands, from basic database interaction to advanced querying and transaction management. Use it as a quick reference guide to enhance your productivity when working with MySQL databases.