Skip to Content

The Ultimate MySQL Cheat Sheet for Beginners

As a beginner, learning MySQL can be quite overwhelming, especially when it comes to remembering all the different commands and syntaxes. That's why we have compiled the ultimate MySQL cheat sheet to help you learn and remember the essential MySQL commands and functions.

Basic MySQL Commands

1. Connect to a MySQL server:

mysql -h host -u user -p

2. List all databases:

SHOW DATABASES;

3. Create a new database:

CREATE DATABASE database_name;

4. Use a database:

USE database_name;

5. List tables in the selected database:

SHOW TABLES;

6. Create a new table:

CREATE TABLE table_name (column1 datatype, column2 datatype, ...);

7. Describe a table's structure:

DESCRIBE table_name;

8. Drop a table:

DROP TABLE table_name;

CRUD (Create, Read, Update, Delete) Operations

1. Insert data into a table:

INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);

2. Select all data from a table:

SELECT * FROM table_name;

3. Select specific columns from a table:

SELECT column1, column2, ... FROM table_name;

4. Update data in a table:

UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;

5. Delete data from a table:

DELETE FROM table_name WHERE condition;

Filtering, Sorting, and Grouping

1. Filter data using a WHERE clause:

SELECT * FROM table_name WHERE condition;

2. Sort data using ORDER BY:

SELECT * FROM table_name ORDER BY column_name ASC|DESC;

3. Group data using GROUP BY:

SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name;

4. Filter groups using HAVING:

SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name HAVING condition;

Joins and Unions

1. Perform an INNER JOIN:

SELECT * FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name;

2. Perform a LEFT JOIN:

SELECT * FROM table1 LEFT JOIN table2 ON table1.column_name = table2.column_name;

3. Perform a RIGHT JOIN:

SELECT * FROM table1 RIGHT JOIN table2 ON table1.column_name = table2.column_name;

4. Perform a FULL OUTER JOIN (using a combination of LEFT JOIN and RIGHT JOIN, since MySQL does not support FULL OUTER JOIN directly):

SELECT * FROM table1 LEFT JOIN table2 ON table1.column_name = table2.column_name UNION SELECT * FROM table1 RIGHT JOIN table2 ON table1.column_name = table2.column_name;

5. Combine the results of two SELECT queries using UNION:

SELECT column_name(s) FROM table1 UNION SELECT column_name(s) FROM table2;

Indexes and Constraints

1. Create an index on a table:

CREATE INDEX index_name ON table_name (column_name);

2. Create a unique index on a table:

CREATE UNIQUE INDEX index_name ON table_name (column_name);

3. Add a primary key constraint to a table:

ALTER TABLE table_name ADD PRIMARY KEY (column_name);

4. Add a foreign key constraint to a table:

ALTER TABLE table_name ADD FOREIGN KEY (column_name) REFERENCES another_table (another_column);

5. Add a unique constraint to a table:

ALTER TABLE table_name ADD UNIQUE (column_name);

6. Add a CHECK constraint to a table:

ALTER TABLE table_name ADD CHECK (condition);

7. Remove a constraint from a table:

ALTER TABLE table_name DROP CONSTRAINT constraint_name;

Conclusion

This MySQL cheat sheet is an excellent resource for beginners and can serve as a quick reference for experienced users as well. It covers the basic commands, CRUD operations, filtering, sorting, grouping, joins, unions, and constraints. Remember to keep this cheat sheet handy while working with MySQL databases and continue learning and practicing new commands and techniques to master this powerful database management system.

Powered by PHPKB Knowledge Base Software