You are on page 1of 3

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 unnin! "ySQL Importin! How mys#l $uusername $ppassword mys#l $uusername $ppassword ( filename Example(s) mys#l $ucusack% & $pe!bdf's

mys#l $usome)* $pblah ( my+ew)*.s#l )umpin! mys#ldump $uusername $ppassword database .tables/ mys#ldump $ume $pblah my)* 0 ,Savin!0 filename "y.s#l mys#ldump $ume $pblah my)* table1 table% 0 my.s#l Common MySQL Column Types Purpose ata Type Example Inte!ers int,Mint,'2loatin!$point ,real- numbers float,M,Dfloat,1%,3)ouble$precision floatin!$point double,M,Ddouble,%4,3)ates and times timestamp,Mtimestamp,5- ,for 6666""))timestamp,1%- ,for 6666""))HH""SS2ixed$len!th strin!s char,Mchar,147ariable$len!th strin!s varchar,Mvarchar,%48 lar!e amount of text blob blob 7alues chosen from a list enum,9value19,value%9,...- enum,9apples9,9oran!es9,9bananas9M is maximum to display, and D is precision to the ri!ht of the decimal. MySQL Mathemati!al "un!tions MySQL Strin# "un!tions What How What How :ount rows per :&;+T,column < =:ompare strin!s strcmp,string1,string2!roup :onvert to lower case lower,string8vera!e value of 87>,column:onvert to upper case upper,string!roup Left$trim whitespace ltrim,string"inumum value of "I+,column,similar ri!ht!roup Substrin! of strin! substrin!,string,index1,index2"aximum value of "8?,column@ncrypt password password,string!roup @ncode strin! encode,string,keySum values in a S;",column)ecode strin! decode,string,key!roup >et date curdate,8bsolute value abs,number>et time curtime,oundin! numbers round,number@xtract day name from dayname,stringLar!est inte!er not floor,numberdate strin! !reater @xtract day number from dayofweek,stringSmallest inte!er not ceilin!,numberdate strin! smaller @xtract month from date monthname,stringS#uare root s#rt,numberstrin!

nth power pow,base,exponentrandom number n, rand,4(n ( 1 sin ,similar cos, etc.- sin,number$asi! MySQL Commands What How Example(s) List all databases SH&A )8T8*8S@SB SH&A )8T8*8S@SB :reate database : @8T@ )8T8*8S@ databaseB : @8T@ )8T8*8S@ Chone)*B ;se a database ;S@ databaseB ;S@ Chon)*B List tables in the database SH&A T8*L@SB SH&A T8*L@SB Show the structure of a table )@S: I*@ tableB )@S: I*@ 8nimalsB SH&A :&L;"+S 2 &" tableB SH&A :&L;"+S 2 &" 8nimalsB )elete a database ,Careful!- ) &C )8T8*8S@ databaseB ) &C )8T8*8S@ Chone)*B SQL Commands% Modi&yin# What How Example(s) :reate table : @8T@ T8*L@ table , : @8T@ T8*L@ Students , column1 type ..+&T/ +;LL/ Last+ame varchar,34- +&T +;LL, .8;T&DI+: @"@+T/, 2irst+ame varchar,34- +&T +;LL, column2 type ..+&T/ +;LL/ StudentI) int +&T +;LL, .8;T&DI+: @"@+T/, "aFor varchar,%4-, ... )orm varchar,%4-, other options, C I"8 6 E@6 ,StudentI)- -B C I"8 6 E@6 ,column(s)- -B Insert data I+S@ T I+T& table 78L;@S I+S@ T I+T& Students 78L;@S ,list of alues-B I+S@ T I+T& table S@T ,9Smith9,9Hohn9,1%3I'JK5L,9"ath9,9Selleck9-B column1G alue1, I+S@ T I+T& Students S@T column2G alue2, 2irst+ameG9Hohn9, ... Last+ameG9Smith9, columnkG aluekB StudentI)G1%3I'JK5L, I+S@ T "aForG9"ath9B I+T& table ,column1,column2,...I+S@ T I+T& Students 78L;@S , alue1, alue2...-B ,StudentI),2irst+ame,Last+ame78L;@S ,1%3I'JK5L,9Hohn9,9Smith9-B InsertMSelect I+S@ T I+S@ T I+T& Students I+T& table ,column1,column2,...,StudentI),2irst+ame,Last+ame!"#"C$ statementB S@L@:T ,See belowStudentI),2irst+ame,Last+ame 2 &" &therStudentTableB AH@ @ Last+ame like 9Nson9B )elete data )@L@T@ 2 &" table )@L@T@ 2 &" Students .AH@ @ condition(s)/B AH@ @ Last+ameG9Smith9B )@L@T@ 2 &" Students AH@ @ Last+ame like 9NSmithN9B 8+) 2irst+ameG9Hohn9B ,&mit WHE'E to delete all data)@L@T@ 2 &" StudentsB ;pdatin! )ata ;C)8T@ table S@T ;C)8T@ Students S@T column1G alue1, Last+ameG9Hones9 AH@ @ column2G alue2, StudentI)GL5KJ'I3%1B ... ;C)8T@ Students S@T columnkG aluek Last+ameG9Hones9, "aForG9Theatre9 .AH@ @ condition(s)/B AH@ @ StudentI)GL5KJ'I3%1 & ,"8H& G98rt9 8+) 2irst+ameG9Cete9-B

Insert column

)elete column )elete table ,Careful!-

8LT@ T8*L@ table 8)) :&L;"+ column type optionsB 8LT@ T8*L@ table ) &C :&L;"+ columnB ) &C T8*L@ .I2 @?ISTS/ tableB

8LT@ T8*L@ Students 8)) :&L;"+ Hometown varchar,%4-B 8LT@ T8*L@ Students ) &C :&L;"+ )ormB ) &C T8*L@ 8nimalsB

SQL Commands% Queryin# What How Example(s) 8ll columns S@L@:T = 2 &" tableB S@L@:T = 2 &" StudentsB Some columns S@L@:T column1,column2,... S@L@:T Last+ame, 2irst+ame 2 &" StudentsB 2 &" tableB Some rowsM S@L@:T column1,column2,... S@L@:T Last+ame,2irst+ame columns 2 &" table 2 &" Students .AH@ @ condition(s)/B AH@ @ StudentI) LIE@ 9N1%3N9B +o epeats S@L@:T .)ISTI+:T/ column(s) S@L@:T )ISTI+:T Last+ame 2 &" tableB 2 &" StudentsB &rderin! S@L@:T column1,column2,... S@L@:T Last+ame,2irst+ame 2 &" table 2 &" Students .& )@ *6 column(s) .)@S://B & )@ *6 Last+ame, 2irst+ame )@S:B :olumn S@L@:T column1 .8S alias1/, S@L@:T Last+ame,2irst+ame 8S 2irst 8liases column2 .8S alias2/, ... 2 &" StudentsB 2 &" table1B >roupin! S@L@:T column1,column2,... S@L@:T Last+ame,:&;+T,=2 &" table 2 &" Students .> &;C *6 column(s)/B > &;C *6 Last+ameB >roup S@L@:T column1,column2,... S@L@:T Last+ame,:&;+T,=2ilterin! 2 &" table 2 &" Students .> &;C *6 column(s)/ > &;C *6 Last+ame .H87I+> condition(s)/B H87I+> Last+ame like 9Nson9B Hoins S@L@:T column1,column2,... S@L@:T Last+ame,Coints 2 &" table1,table2,... 2 &" Students,8ssi!nments .AH@ @ condition(s)/B AH@ @ 8ssi!nmentI)G1% 8+) Students.StudentI)G8ssi!nments.StudentI)B S@L@:T column1,column2,... S@L@:T Last+ame,Coints 2 &" table1 .alias1/, 2 &" Students S,8ssi!nments 8 table2 .alias2/,... AH@ @ S.StudentI)G8.StudentI) 8+) .AH@ @ condition(s)/B 8.8ssi!nmentI)G1%B S@L@:T .)ISTI+:T/ S@L@:T Coints, :&;+T,=- 8S :nt column1 .8S alias1/, 2 &" Students S,8ssi!nments 8 column2 .8S alias2/, ... AH@ @ S.StudentI)G8.StudentI) 8+) 2 &" table1 .alias1/, 8.8ssi!nmentI)G1% table2 .alias2/,... > &;C *6 Coints .AH@ @ condition(s)/ H87I+> Coints 0 14 .> &;C *6 column(s)/ & )@ *6 :nt, Coints )@S:B .H87I+> condition(s)/ .& )@ *6 column(s) .)@S://B

Table 8liases

@verythin!

You might also like