Anda di halaman 1dari 4

MySQL Cheat Sheet http://cse.unl.edu/~sscott/ShowFiles/SQL/CheatS...

(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 -uusername -ppassword mysql -ucusack2RO -pegbdf5s
MySQL
Importing mysql -uusername -ppassword < filename mysql -usomeDB -pblah <
myNewDB.sql
Dumping mysqldump -uusername -ppassword mysqldump -ume -pblah myDB
(Saving) database [tables] > filename > My.sql
mysqldump -ume -pblah myDB
table1
table2 > my.sql

Common MySQL Column Types


Purpose Data Type Example
Integers int(M) int(5)
Floating-point (real) float(M,D) float(12,3)
numbers
Double-precision double(M,D) double(20,3)
floating-point
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 COUNT(column | *) Compare strings strcmp(string1,string2)
group
Convert to lower lower(string)
Average value AVG(column) case
of group
Convert to upper upper(string)
Minumum value MIN(column) case
of group

1 of 4 21/03/17 15:55
MySQL Cheat Sheet http://cse.unl.edu/~sscott/ShowFiles/SQL/CheatS...

Maximum value MAX(column)


of group Left-trim ltrim(string)
whitespace (similar
Sum values in a SUM(column)
right)
group
Substring of string substring(string,index1,index2)
Absolute value abs(number)
Encrypt password password(string)
Rounding round(number)
numbers Encode string encode(string,key)
Largest integer floor(number) Decode string decode(string,key)
not greater
Get date curdate()
Smallest ceiling(number)
Get time curtime()
integer not
smaller Extract day name dayname(string)
from date string
Square root sqrt(number)
Extract day dayofweek(string)
nth power pow(base,exponent)
number from date
random number rand() string
n, 0<n < 1
Extract month from monthname(string)
sin (similar cos, sin(number) date string
etc.)

Basic MySQL Commands


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

SQL Commands: Modifying


What How Example(s)
Create table CREATE TABLE table ( CREATE TABLE Students (
column1 type [[NOT] LastName varchar(30) NOT NULL,
NULL] FirstName varchar(30) NOT NULL,
StudentID int NOT NULL,
[AUTO_INCREMENT], Major varchar(20),
column2 type [[NOT] Dorm varchar(20),
NULL] PRIMARY KEY (StudentID) );

[AUTO_INCREMENT],
...
other options,
PRIMARY KEY
(column(s)) );

2 of 4 21/03/17 15:55
MySQL Cheat Sheet http://cse.unl.edu/~sscott/ShowFiles/SQL/CheatS...

Insert data INSERT INTO table VALUES INSERT INTO Students VALUES
(list of values);
INSERT INTO table SET ('Smith','John',123456789,'Math','Selleck');
column1=value1, INSERT INTO Students SET
column2=value2, FirstName='John',
... LastName='Smith',
columnk=valuek; StudentID=123456789,
INSERT INTO table Major='Math';
(column1,column2,...) INSERT INTO Students
VALUES (StudentID,FirstName,LastName)
(value1,value2...); VALUES (123456789,'John','Smith');
Insert/Select INSERT INTO table INSERT INTO Students
(column1,column2,...) (StudentID,FirstName,LastName)
SELECT statement; SELECT
(See below) 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 DELETE FROM Students;
data)
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 ALTER TABLE Students ADD COLUMN
COLUMN Hometown varchar(20);
column type options;
Delete column ALTER TABLE table ALTER TABLE Students
DROP COLUMN DROP COLUMN Dorm;
column;
Delete table DROP TABLE [IF EXISTS] DROP TABLE Animals;
(Careful!) table;

SQL Commands: Querying


What How Example(s)
All columns SELECT * FROM table; SELECT * FROM Students;
Some SELECT column1,column2,... SELECT LastName, FirstName FROM
columns FROM table; 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] SELECT DISTINCT LastName
column(s) FROM Students;
FROM table;
Ordering SELECT column1,column2,... SELECT LastName,FirstName
FROM table FROM Students
[ORDER BY column(s) ORDER BY LastName, FirstName DESC;

3 of 4 21/03/17 15:55
MySQL Cheat Sheet http://cse.unl.edu/~sscott/ShowFiles/SQL/CheatS...

[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 SELECT column1,column2,... SELECT LastName,COUNT(*)
Filtering 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
DELETE syntax
UPDATE syntax
SELECT syntax
INSERT ... SELECT syntax
MySQL Functions

4 of 4 21/03/17 15:55

Anda mungkin juga menyukai