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
Command | Description |
---|
mysql -u [username] -p | Log 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)
Command | Description |
---|
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)
Command | Description |
---|
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
Command | Description |
---|
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
Command | Description |
---|
WHERE [condition]; | Filter records based on a condition |
AND | Combine multiple conditions with AND |
OR | Combine multiple conditions with OR |
IN ([values]); | Filter records where a column is in a list |
Joins
Command | Description |
---|
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
Command | Description |
---|
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
Command | Description |
---|
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.