MySQL Cheat Sheet
MySQL Cheat Sheet
http://cse.unl.edu/~sscott/ShowFiles/SQL/CheatSheet/SQLCheatSheet.html
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 Importing Dumping (Saving) How mysql -uusername -ppassword < filename Example(s) mysql -ucusack2RO -pegbdf5s mysql -usomeDB -pblah < myNewDB.sql Running MySQL mysql -uusername -ppassword
mysqldump -uusername -ppassword database [tables] > filename mysqldump -ume -pblah myDB > My.sql mysqldump -ume -pblah myDB table1 table2 > my.sql
Common MySQL Column Types Purpose Integers Floating-point (real) numbers Dates and times Fixed-length strings Variable-length strings A large amount of text Values chosen from a list Data Type int(M) float(M,D) timestamp(M) char(M) varchar(M) blob Example int(5) float(12,3) double(20,3) timestamp(8) (for YYYYMMDD) timestamp(12) (for YYYYMMDDHHMMSS) char(10) varchar(20) blob
enum('value1',value2',...) enum('apples','oranges','bananas')
MySQL Mathematical Functions What Count rows per group Average value of group Minumum value of group Maximum value of group Sum values in a group Absolute value Rounding numbers Largest integer not greater Square root nth power How COUNT(column | *) AVG(column) MIN(column) MAX(column) SUM(column) abs(number) round(number) floor(number) sqrt(number) pow(base,exponent) What Compare strings
MySQL String Functions How strcmp(string1,string2) lower(string) upper(string) substring(string,index1,index2) password(string) encode(string,key) decode(string,key) curdate() curtime() dayname(string)
Convert to lower case Convert to upper case Substring of string Encrypt password Encode string Decode string Get date Get time Extract day name from date string
1 of 4
8/24/2011 6:53 AM
http://cse.unl.edu/~sscott/ShowFiles/SQL/CheatSheet/SQLCheatSheet.html
rand() sin(number)
Extract day number from date string dayofweek(string) Extract month from date string monthname(string)
Basic MySQL Commands What List all databases Create database Use a database List tables in the database How SHOW DATABASES; USE database; SHOW TABLES; Example(s) SHOW DATABASES; USE PhonDB; 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 Create table How CREATE TABLE table ( column1 type [[NOT] NULL] [AUTO_INCREMENT], column2 type [[NOT] NULL] [AUTO_INCREMENT], ... other options, PRIMARY KEY (column(s)) ); Example(s) CREATE TABLE Students ( LastName varchar(30) NOT NULL, FirstName varchar(30) NOT NULL, StudentID int NOT NULL, Major varchar(20), Dorm varchar(20), PRIMARY KEY (StudentID) );
Insert data
INSERT INTO table V ALUES INSERT INTO Students V ALUES ('Smith','John',123456789,'Math','Selleck'); (list of values); INSERT INTO Students SET INSERT INTO table SET FirstName='John', column1=value1, LastName='Smith', column2=value2, StudentID=123456789, ... Major='Math'; columnk=valuek; INSERT INTO table (column1,column2,...) INSERT INTO Students (StudentID,FirstName,LastName) V ALUES (value1,value2...); V ALUES (123456789,'John','Smith'); 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 FROM table [WHERE condition(s)]; DELETE FROM Students WHERE LastName='Smith'; DELETE FROM Students WHERE LastName like '%Smith%'; AND FirstName='John'; DELETE FROM Students; UPDATE Students SET LastName='Jones' WHERE StudentID=987654321; UPDATE Students SET LastName='Jones', Major='Theatre' WHERE StudentID=987654321 OR (MAJOR='Art' AND FirstName='Pete');
Insert/Select
Delete data
(Omit WHERE to delete all data) Updating Data UPDATE table SET column1=value1, column2=value2, ... columnk=valuek [WHERE condition(s)];
2 of 4
8/24/2011 6:53 AM
http://cse.unl.edu/~sscott/ShowFiles/SQL/CheatSheet/SQLCheatSheet.html
ALTER TABLE table ADD COLUMN column type options; ALTER TABLE table DROP COLUMN column;
ALTER TABLE Students ADD COLUMN Hometown varchar(20); ALTER TABLE Students DROP COLUMN Dorm; DROP TABLE Animals;
SQL Commands: Querying What All columns Some rows/ columns No Repeats Ordering How SELECT * FROM table; SELECT column1,column2,... FROM table [WHERE condition(s)]; SELECT [DISTINCT] column(s) FROM table; SELECT column1,column2,... FROM table [ORDER BY column(s) [DESC]]; SELECT column1 [AS alias1], column2 [AS alias2], ... FROM table1; SELECT column1,column2,... FROM table [GROUP BY column(s)]; Example(s) SELECT * FROM Students; SELECT LastName,FirstName FROM Students WHERE StudentID LIKE '%123%'; SELECT DISTINCT LastName FROM Students; SELECT LastName,FirstName FROM Students ORDER BY LastName, FirstName DESC; SELECT LastName,FirstName AS First FROM Students; SELECT LastName,COUNT(*) FROM Students GROUP BY LastName; SELECT LastName,COUNT(*) FROM Students GROUP BY LastName HAVING LastName like '%son'; SELECT LastName,Points FROM Students,Assignments WHERE AssignmentID=12 AND Students.StudentID=Assignments.StudentID; SELECT LastName,Points FROM Students S,Assignments A WHERE S.StudentID=A.StudentID AND A.AssignmentID=12; SELECT Points, COUNT(*) AS Cnt FROM Students S,Assignments A WHERE S.StudentID=A.StudentID AND A.AssignmentID=12 GROUP BY Points HAVING Points > 10 ORDER BY Cnt, Points DESC;
Some columns SELECT column1,column2,... FROM table; SELECT LastName, FirstName FROM Students;
Group Filtering SELECT column1,column2,... FROM table [GROUP BY column(s)] [HAVING condition(s)]; Joins SELECT column1,column2,... FROM table1,table2,... [WHERE condition(s)]; SELECT column1,column2,... FROM table1 [alias1], table2 [alias2],... [WHERE condition(s)]; SELECT [DISTINCT] column1 [AS alias1], column2 [AS alias2], ... FROM table1 [alias1], table2 [alias2],... [WHERE condition(s)] [GROUP BY column(s)] [HAVING condition(s)] [ORDER BY column(s) [DESC]];
Table Aliases
Everything
For more details, see the following pages from MySQL.com. MySQL Reference Manual MySQL Column Types SHOW syntax CREATE TABLE syntax
3 of 4
8/24/2011 6:53 AM
http://cse.unl.edu/~sscott/ShowFiles/SQL/CheatSheet/SQLCheatSheet.html
ALTER TABLE syntax INSERT syntax DELETE syntax UPDATE syntax SELECT syntax INSERT ... SELECT syntax MySQL Functions
4 of 4
8/24/2011 6:53 AM