Cheat Sheet
1. Basic Commands
Connecting to MySQL
You'll be prompted to enter the password.
Show Databases
Create a Database
Use a Database
Show Tables
Describe a Table
Or
Drop Database
Drop Table
2. Data Types
Numeric Types
INT
– IntegerDECIMAL(p, s)
– Fixed-pointFLOAT
– Floating-pointDOUBLE
– Double precision
String Types
VARCHAR(n)
– Variable-length stringCHAR(n)
– Fixed-length stringTEXT
– Large textBLOB
– Binary Large Object
Date and Time Types
DATE
– 'YYYY-MM-DD'DATETIME
– 'YYYY-MM-DD HH:MM:SS'TIMESTAMP
– Automatic timestampTIME
– 'HH:MM:SS'YEAR
– 'YYYY'
3. CRUD Operations
Create (INSERT)
Example:
Read (SELECT)
Examples:
Select all columns:
Select with condition:
Ordering and limiting:
Update (UPDATE)
Example:
Delete (DELETE)
Example:
4. Advanced Queries
JOINs
INNER JOIN
Returns records with matching values in both tables.
LEFT JOIN (LEFT OUTER JOIN)
Returns all records from the left table and matched records from the right table.
RIGHT JOIN (RIGHT OUTER JOIN)
Returns all records from the right table and matched records from the left table.
FULL OUTER JOIN
MySQL doesn’t support FULL OUTER JOIN directly, but it can be simulated:
GROUP BY and HAVING
GROUP BY
Aggregates data based on one or more columns.
HAVING
Filters groups based on aggregate functions.
ORDER BY
Sorts the result set.
LIMIT
Restricts the number of rows returned.
Or
5. Indexes
Create an Index
Create a Unique Index
Drop an Index
View Indexes
6. Constraints
Primary Key
Foreign Key
Unique Constraint
Not Null Constraint
Default Value
7. Functions
Aggregate Functions
COUNT(column)
SUM(column)
AVG(column)
MIN(column)
MAX(column)
Example:
String Functions
CONCAT(str1, str2, ...)
– Concatenates stringsSUBSTRING(str, pos, length)
– Extracts a substringLENGTH(str)
– Returns the length of a stringUPPER(str)
– Converts to uppercaseLOWER(str)
– Converts to lowercase
Example:
Date Functions
NOW()
– Current date and timeCURDATE()
– Current dateDATE_ADD(date, INTERVAL value unit)
– Adds interval to dateDATEDIFF(date1, date2)
– Difference in days
Example:
Numeric Functions
ROUND(number, decimals)
– Rounds a numberCEIL(number)
– Rounds upFLOOR(number)
– Rounds down
Example:
8. Views
Create a View
Select from a View
Drop a View
9. Transactions
Start a Transaction
Or
Commit a Transaction
Rollback a Transaction
10. User Management
Create a User
Grant Privileges
Or specific privileges:
Flush Privileges
Revoke Privileges
Drop a User
11. Backup and Restore
Backup Database
Restore Database
12. Miscellaneous
Set Character Set
Change Password
Check Current Database
Check Current User
13. Common Shortcuts
Select all columns:
Insert multiple rows:
Update with condition:
Delete with condition:
14. Performance Tips
Use Indexes: Improve query performance by indexing columns used in WHERE, JOIN, and ORDER BY clauses.
*Avoid SELECT : Specify only the columns you need.
Use EXPLAIN: Analyze query execution plans.
Optimize Joins: Ensure joined columns are indexed.
Limit Results: Use LIMIT to reduce the amount of data processed.
15. Security Best Practices
Use Strong Passwords: Ensure all MySQL users have strong, unique passwords.
Least Privilege Principle: Grant only necessary permissions to users.
Regular Backups: Keep regular backups and store them securely.
Keep MySQL Updated: Regularly update to the latest stable version to patch vulnerabilities.
Use SSL/TLS: Encrypt connections between MySQL and clients.
Last updated