CheatSheets
MySQL CheatSheet
CheatSheet
Creating a Database: CREATE DATABASE
To create a database, use:
Example:
Output:
To show databases, use:
Output:
To use the created database "company", use:
Example:
Creating a Table and Showing its Fields (CREATE TABLE - SHOW TABLES - DESCRIBE - DROP TABLE)
A database stores its data in tables. This is a data structure that organizes data in columns and rows; each column is a field (or attribute) and each row, a record. The intersection of a column with a row contains a specific data value.
To create a table, use:
Example:
To describe the table, use:
Output:
To show all tables in the database, use:
Output:
Loading Records into a Table and Retrieving Them (INSERT INTO – SELECT)
To insert a record into a table, use:
Example:
To select all records from a table, use:
Output:
Data Types
Data Type | Description | Range / Size |
---|---|---|
CHAR | Fixed length string | 0 - 255 |
VARCHAR | Variable length string | 0 - 255 |
TINYTEXT | Short text string | 0 - 255 |
TEXT | Text string | 0 - 65,535 |
BLOB | Binary large object | 0 - 65,535 |
MEDIUMTEXT | Medium length text string | 0 - 16,777,215 |
MEDIUMBLOB | Medium binary large object | 0 - 16,777,215 |
LONGTEXT | Long text string | 0 - 4,294,967,295 |
LONGBLOB | Long binary large object | 0 - 4,294,967,295 |
TINYINT x | Integer | -128 to 127 |
SMALLINT x | Integer | -32,768 to 32,767 |
MEDIUMINT x | Integer | -8,388,608 to 8,388,607 |
INT x | Integer | -2,147,483,648 to 2,147,483,647 |
BIGINT x | Integer | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
FLOAT | Decimal (precise to 23 digits) | Precision up to 23 digits |
DOUBLE | Decimal (24 to 53 digits) | Precision up to 53 digits |
DECIMAL | Fixed-point decimal | Precision and scale defined manually |
DATE | Date | YYYY-MM-DD |
DATETIME | Date and time | YYYY-MM-DD HH:MM:SS |
TIMESTAMP | Timestamp | YYYYMMDDHHMMSS |
TIME | Time | HH:MM:SS |
ENUM | One of a set of predefined values | Predefined options |
SET | Multiple values from a set | Selection of predefined options |
Select Queries
Operation | Query |
---|---|
Select all columns | SELECT * FROM tbl; |
Select specific columns | SELECT col1, col2 FROM tbl; |
Select only unique records | SELECT DISTINCT col FROM tbl WHERE condition; |
Column alias | SELECT col AS newname FROM tbl; |
Order results | `SELECT * FROM tbl ORDER BY col [ASC |
Group results | SELECT col1, SUM(col2) FROM tbl GROUP BY col1; |
Creating and Modifying
Operation | Query |
---|---|
Create a database | CREATE DATABASE db_name; |
Select a database | USE db_name; |
List databases on the server | SHOW DATABASES; |
Show a table's fields | DESCRIBE tbl; |
Create a new table | CREATE TABLE tbl (field1 datatype, field2 datatype); |
Insert data into a table | INSERT INTO tbl VALUES ("val1", "val2"); |
Delete a row | DELETE FROM tbl WHERE condition; |
Add a column | ALTER TABLE tbl ADD COLUMN col datatype; |
Remove a column | ALTER TABLE tbl DROP COLUMN col; |
Make a column a primary key | ALTER TABLE tbl ADD PRIMARY KEY (col); |
Return only 1 row matching query | SELECT * FROM tbl LIMIT 1; |
Amend the values of a column | UPDATE tbl SET column1="val1" WHERE condition; |
Clear all values (leaving table structure) | TRUNCATE TABLE tbl; |
Delete the table | DROP TABLE tbl; |
Delete the database | DROP DATABASE db_name; |
Matching Data
Operation | Query |
---|---|
Matching data using LIKE | SELECT * FROM tbl WHERE col LIKE '%value%'; |
Matching data using REGEX | SELECT * FROM tbl WHERE col RLIKE 'regular_expression'; |
Joins
Join Type | Description | Query |
---|---|---|
INNER JOIN | Returns only matching rows from both tables | SELECT * FROM tbl1 INNER JOIN tbl2 ON tbl1.id = tbl2.id; |
OUTER JOIN | Returns all rows from both tables, with NULLs where there is no match | SELECT * FROM tbl1 OUTER JOIN tbl2 ON tbl1.id = tbl2.id; |
LEFT JOIN | Returns all rows from the left table, with NULLs where there is no match in the right table | SELECT * FROM tbl1 LEFT JOIN tbl2 ON tbl1.id = tbl2.id; |
RIGHT JOIN | Returns all rows from the right table, with NULLs where there is no match in the left table | SELECT * FROM tbl1 RIGHT JOIN tbl2 ON tbl1.id = tbl2.id; |
String Functions
Function | Description | Query |
---|---|---|
STRCMP | Compare two strings | STRCMP("str1", "str2"); |
LOWER | Convert string to lower case | LOWER("str"); |
UPPER | Convert string to upper case | UPPER("str"); |
LTRIM | Remove leading spaces | LTRIM("str"); |
SUBSTRING | Extract a substring | SUBSTRING("str", index1, index2); |
CONCAT | Concatenate two strings | CONCAT("str1", "str2"); |
MySQL Calculation Functions
Function | Description | Query |
---|---|---|
COUNT | Count the number of rows | COUNT(col); |
AVG | Calculate average value | AVG(col); |
MIN | Get the minimum value | MIN(col); |
MAX | Get the maximum value | MAX(col); |
SUM | Calculate the sum of values | SUM(col); |