0% found this document useful (0 votes)
5 views

MySQL Cheat Sheet

This document is a MySQL cheat sheet that provides a concise overview of commonly used SQL commands, data types, mathematical and string functions, and basic SQL commands for database manipulation. It includes examples for running MySQL, importing, dumping databases, and modifying data. Additionally, it covers querying techniques, including selection, filtering, and joining tables, along with references for more detailed syntax and functions.

Uploaded by

Emily Aumiller
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

MySQL Cheat Sheet

This document is a MySQL cheat sheet that provides a concise overview of commonly used SQL commands, data types, mathematical and string functions, and basic SQL commands for database manipulation. It includes examples for running MySQL, importing, dumping databases, and modifying data. Additionally, it covers querying techniques, including selection, filtering, and joining tables, along with references for more detailed syntax and functions.

Uploaded by

Emily Aumiller
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

8/31/2018 MySQL Cheat Sheet

(My)SQL Cheat Sheet


Here are the most commonly used SQL commands and the most commonly used options for each. There are
many more commands and options than listed here. In other words, the syntaxes as I have listed them are far
from complete. See the links at the bottom for more complete syntaxes and more commands.

MySQL Command-Line
What How Example(s)
Running MySQL mysql -uusername -ppassword mysql -ucusack2RO -pegbdf5s
Importing mysql -uusername -ppassword < filename mysql -usomeDB -pblah < myNewDB.sql
Dumping mysqldump -uusername -ppassword database [tables] > filename mysqldump -ume -pblah myDB > My.sql
(Saving) mysqldump -ume -pblah myDB table1
table2 > my.sql

Common MySQL Column Types


Purpose Data Type Example
Integers int(M) int(5)
Floating-point (real) numbers float(M,D) float(12,3)
Double-precision floating-point double(M,D) double(20,3)
Dates and times timestamp(M) timestamp(8) (for YYYYMMDD)
timestamp(12) (for YYYYMMDDHHMMSS)
Fixed-length strings char(M) char(10)
Variable-length strings varchar(M) varchar(20)
A large amount of text blob blob
Values chosen from a list enum('value1',value2',...) enum('apples','oranges','bananas')

M is maximum to display, and D is precision to the right of the decimal.

MySQL Mathematical Functions MySQL String Functions


What How What How
Count rows per group COUNT(column | *) Compare strings strcmp(string1,string2)
Average value of group AVG(column) Convert to lower case lower(string)
Minumum value of group MIN(column) Convert to upper case upper(string)
Maximum value of group MAX(column) Left-trim whitespace (similar right) ltrim(string)
Sum values in a group SUM(column) Substring of string substring(string,index1,index2)
Absolute value abs(number) Encrypt password password(string)
Rounding numbers round(number) Encode string encode(string,key)
Largest integer not greater floor(number) Decode string decode(string,key)
Smallest integer not smaller ceiling(number) Get date curdate()
Square root sqrt(number) Get time curtime()
nth power pow(base,exponent) Extract day name from date string dayname(string)
random number n, 0<n < 1 rand() Extract day number from date string dayofweek(string)

http://cse.unl.edu/~sscott/ShowFiles/SQL/CheatSheet/SQLCheatSheet.html 1/4
8/31/2018 MySQL Cheat Sheet
sin (similar cos, etc.) sin(number) Extract month from date string monthname(string)

Basic MySQL Commands


What How Example(s)
List all databases SHOW DATABASES; SHOW DATABASES;
Create database CREATE DATABASE database; CREATE DATABASE PhoneDB;
Use a database USE database; USE PhonDB;
List tables in the database SHOW TABLES; SHOW TABLES;
Show the structure of a table DESCRIBE table; DESCRIBE Animals;
SHOW COLUMNS FROM table; SHOW COLUMNS FROM Animals;
Delete a database (Careful!) DROP DATABASE database; DROP DATABASE PhoneDB;

SQL Commands: Modifying


What How Example(s)
Create table CREATE TABLE table ( CREATE TABLE Students (
column1 type [[NOT] NULL] LastName varchar(30) NOT NULL,
[AUTO_INCREMENT], FirstName varchar(30) NOT NULL,
column2 type [[NOT] NULL] StudentID int NOT NULL,
[AUTO_INCREMENT], Major varchar(20),
... Dorm varchar(20),
other options, PRIMARY KEY (StudentID) );
PRIMARY KEY (column(s)) );
Insert data INSERT INTO table VALUES INSERT INTO Students VALUES
(list of values); ('Smith','John',123456789,'Math','Selleck');
INSERT INTO table SET INSERT INTO Students SET
column1=value1, FirstName='John',
column2=value2, LastName='Smith',
... StudentID=123456789,
columnk=valuek; Major='Math';
INSERT INTO table (column1,column2,...) INSERT INTO Students
VALUES (value1,value2...); (StudentID,FirstName,LastName)
VALUES (123456789,'John','Smith');
Insert/Select INSERT INTO table (column1,column2,...) INSERT INTO Students
SELECT statement; (StudentID,FirstName,LastName)
(See below) SELECT StudentID,FirstName,LastName
FROM OtherStudentTable;
WHERE LastName like '%son';
Delete data DELETE FROM table DELETE FROM Students
[WHERE condition(s)]; WHERE LastName='Smith';
DELETE FROM Students
WHERE LastName like '%Smith%';
AND FirstName='John';
(Omit WHERE to delete all data) DELETE FROM Students;
Updating Data UPDATE table SET UPDATE Students SET
column1=value1, LastName='Jones' WHERE
column2=value2, StudentID=987654321;
... UPDATE Students SET
columnk=valuek LastName='Jones', Major='Theatre'
[WHERE condition(s)]; WHERE StudentID=987654321 OR
(MAJOR='Art' AND FirstName='Pete');
Insert column ALTER TABLE table ADD COLUMN ALTER TABLE Students ADD COLUMN
column type options; Hometown varchar(20);
http://cse.unl.edu/~sscott/ShowFiles/SQL/CheatSheet/SQLCheatSheet.html 2/4
8/31/2018 MySQL Cheat Sheet

Delete column ALTER TABLE table ALTER TABLE Students


DROP COLUMN column; DROP COLUMN Dorm;
Delete table (Careful!) DROP TABLE [IF EXISTS] table; DROP TABLE Animals;

SQL Commands: Querying


What How Example(s)
All columns SELECT * FROM table; SELECT * FROM Students;
Some columns SELECT column1,column2,... FROM table; SELECT LastName, FirstName FROM Students;
Some rows/ SELECT column1,column2,... SELECT LastName,FirstName
columns FROM table FROM Students
[WHERE condition(s)]; WHERE StudentID LIKE '%123%';
No Repeats SELECT [DISTINCT] column(s) SELECT DISTINCT LastName
FROM table; FROM Students;
Ordering SELECT column1,column2,... SELECT LastName,FirstName
FROM table FROM Students
[ORDER BY column(s) [DESC]]; ORDER BY LastName, FirstName DESC;
Column SELECT column1 [AS alias1], SELECT LastName,FirstName AS First
Aliases column2 [AS alias2], ... FROM Students;
FROM table1;
Grouping SELECT column1,column2,... SELECT LastName,COUNT(*)
FROM table FROM Students
[GROUP BY column(s)]; GROUP BY LastName;
Group Filtering SELECT column1,column2,... SELECT LastName,COUNT(*)
FROM table FROM Students
[GROUP BY column(s)] GROUP BY LastName
[HAVING condition(s)]; HAVING LastName like '%son';
Joins SELECT column1,column2,... SELECT LastName,Points
FROM table1,table2,... FROM Students,Assignments
[WHERE condition(s)]; WHERE AssignmentID=12 AND
Students.StudentID=Assignments.StudentID;
Table SELECT column1,column2,... SELECT LastName,Points
Aliases FROM table1 [alias1], FROM Students S,Assignments A
table2 [alias2],... WHERE S.StudentID=A.StudentID AND
[WHERE condition(s)]; A.AssignmentID=12;
Everything SELECT [DISTINCT] SELECT Points, COUNT(*) AS Cnt
column1 [AS alias1], FROM Students S,Assignments A
column2 [AS alias2], ... WHERE S.StudentID=A.StudentID AND
FROM table1 [alias1], A.AssignmentID=12
table2 [alias2],... GROUP BY Points
[WHERE condition(s)] HAVING Points > 10
[GROUP BY column(s)] ORDER BY Cnt, Points DESC;
[HAVING condition(s)]
[ORDER BY column(s) [DESC]];

For more details, see the following pages from MySQL.com.

MySQL Reference Manual


MySQL Column Types
SHOW syntax
CREATE TABLE syntax
ALTER TABLE syntax
INSERT syntax
http://cse.unl.edu/~sscott/ShowFiles/SQL/CheatSheet/SQLCheatSheet.html 3/4
8/31/2018 MySQL Cheat Sheet

DELETE syntax
UPDATE syntax
SELECT syntax
INSERT ... SELECT syntax
MySQL Functions

http://cse.unl.edu/~sscott/ShowFiles/SQL/CheatSheet/SQLCheatSheet.html 4/4

You might also like