MySQL is a popular relational database management system (RDBMS) known for its performance and flexibility. This cheat sheet provides essential commands and queries for interacting with MySQL databases efficiently.
Connecting to MySQL:
Command
Description
mysql -u [username] -p
Connect to MySQL server with a username and password.
CREATE DATABASE [database_name];
Create a new database.
USE [database_name];
Switch to a specific database.
Managing Tables:
Command
Description
CREATE TABLE [table_name] ([column_definitions]);
Create a new table with specified columns.
DESCRIBE [table_name];
Display the structure of a table.
SHOW TABLES;
Show all tables in the current database.
Inserting Data:
Command
Description
INSERT INTO [table_name] VALUES (value1, value2, ...);
Insert data into a table.
INSERT INTO [table_name] (column1, column2, ...) VALUES (value1, value2, ...);
Insert data with specified columns.
Querying Data:
Command
Description
SELECT * FROM [table_name];
Retrieve all columns from a table.
SELECT column1, column2 FROM [table_name] WHERE condition;
Retrieve specific columns with conditions.
UPDATE [table_name] SET column1 = value1 WHERE condition;
Update data in a table.
DELETE FROM [table_name] WHERE condition;
Delete data from a table.
Filtering and Sorting:
Command
Description
SELECT * FROM [table_name] WHERE condition ORDER BY column_name ASC/DESC;
Filter and sort query results.
LIMIT [number];
Limit the number of rows returned.
Aggregation Functions:
Command
Description
SELECT COUNT(column_name) FROM [table_name];
Count the number of rows in a table.
SELECT AVG(column_name) FROM [table_name];
Calculate the average value of a column.
SELECT MAX(column_name) FROM [table_name];
Retrieve the maximum value in a column.
SELECT MIN(column_name) FROM [table_name];
Retrieve the minimum value in a column.
SELECT SUM(column_name) FROM [table_name];
Calculate the sum of values in a column.
Conclusion
This MySQL cheat sheet provides a concise reference for essential commands to manage databases, tables, and data. Whether you are a beginner or an experienced user, these commands cover a wide range of tasks, from creating databases and tables to querying and modifying data. Customize these commands based on your specific use cases and database configurations.