Catalog / MySQL Cheat Sheet
MySQL Cheat Sheet
A quick reference guide for commonly used MySQL commands and syntax, covering data definition, data manipulation, user management, and more.
Basic SQL Commands
Data Definition Language (DDL)
CREATE DATABASE: Creates a new database.
|
DROP DATABASE: Deletes an existing database.
|
CREATE TABLE: Creates a new table.
|
ALTER TABLE: Modifies an existing table structure.
|
DROP TABLE: Deletes a table.
|
TRUNCATE TABLE: Removes all rows from a table.
|
Data Manipulation Language (DML)
SELECT: Retrieves data from one or more tables.
|
INSERT: Adds new rows to a table.
|
UPDATE: Modifies existing data in a table.
|
DELETE: Removes rows from a table.
|
REPLACE: Deletes and inserts new rows, if a row with the same primary key or unique index exists.
|
Data Control Language (DCL)
GRANT: Grants privileges to users.
|
REVOKE: Revokes privileges from users.
|
Common SQL Clauses and Operators
WHERE Clause
Filters records based on a condition.
|
Common operators: |
LIKE: Pattern matching.
|
BETWEEN: Specifies a range.
|
IN: Specifies a set of values.
|
ORDER BY Clause
Sorts the result set.
|
GROUP BY Clause
Groups rows that have the same values into summary rows.
|
Often used with aggregate functions like |
HAVING: Filters groups based on a condition.
|
LIMIT Clause
Limits the number of rows returned.
|
Joins and Subqueries
JOIN Operations
INNER JOIN: Returns rows when there is a match in both tables.
|
LEFT JOIN: Returns all rows from the left table, and the matched rows from the right table. If there is no match, the result is NULL on the right side.
|
RIGHT JOIN: Returns all rows from the right table, and the matched rows from the left table. If there is no match, the result is NULL on the left side.
|
FULL OUTER JOIN: Returns all rows when there is a match in one of the tables. Note: MySQL does not directly support
|
CROSS JOIN: Returns the Cartesian product of the tables. Each row from the first table is combined with each row from the second table.
|
Subqueries
A query nested inside another query.
|
Can be used in |
Types: Scalar, Column, Row, Table subqueries. |
User Management
User Account Management
CREATE USER: Creates a new MySQL user.
|
DROP USER: Deletes a MySQL user.
|
RENAME USER: Renames a MySQL user.
|
SET PASSWORD: Sets or changes the password for a MySQL user.
|
SHOW GRANTS: Displays the privileges granted to a MySQL user.
|
Privilege Management
GRANT: Grants privileges to a user.
|
REVOKE: Revokes privileges from a user.
|
FLUSH PRIVILEGES: Reloads the grant tables after making changes to privileges.
|