Sei sulla pagina 1di 3

16/03/2015

MySQL Commands

MainMenu Blog About


__ __
____ ___ _
| \/ |_ _/ ___| / _ \| |
| |\/| | | | \___ \| | | | |
| | | | |_| |___) | |_| | |___
|_| |_|\__, |____/ \__\_\_____|
|___/

HandyMySQLCommands
Description
Tologin(fromunix
shell)usehonlyif
needed.
Createadatabaseon
thesqlserver.
Listalldatabaseson
thesqlserver.
Switchtoadatabase.
Toseeallthetablesin
thedb.
Toseedatabase'sfield
formats.
Todeleteadb.
Todeleteatable.
Showalldataina
table.
Returnsthecolumns
andcolumn
informationpertaining
tothedesignatedtable.

Command
[mysqldir]/bin/mysqlhhostnameurootp
createdatabase[databasename]
showdatabases
use[dbname]
showtables
describe[tablename]
dropdatabase[databasename]
droptable[tablename]
SELECT*FROM[tablename]

showcolumnsfrom[tablename]

Showcertainselected
rowswiththevalue
SELECT*FROM[tablename]WHERE[fieldname]="whatever"
"whatever".
Showallrecords
containingthename
SELECT*FROM[tablename]WHEREname="Bob"ANDphone_number=
"Bob"ANDthephone '3444444'
number'3444444'.
Showallrecordsnot
containingthename
"Bob"ANDthephone SELECT*FROM[tablename]WHEREname!="Bob"ANDphone_number=
number'3444444'
'3444444'orderbyphone_number
orderbythe
phone_numberfield.
Showallrecords
startingwiththeletters SELECT*FROM[tablename]WHEREnamelike"Bob%"ANDphone_number=
'bob'ANDthephone '3444444'
number'3444444'.
Usearegular
expressiontofind
records.Use
"REGEXPBINARY"
http://g2pc1.bu.edu/~qzpeng/manual/MySQL%20Commands.htm

1/3

16/03/2015

MySQL Commands

toforcecase
SELECT*FROM[tablename]WHERErecRLIKE"^a$"
sensitivity.Thisfinds
anyrecordbeginning
witha.
Showuniquerecords. SELECTDISTINCT[columnname]FROM[tablename]
Showselectedrecords
sortedinanascending
SELECT[col1],[col2]FROM[tablename]ORDERBY[col2]DESC
(asc)ordescending
(desc).
Countrows.

SELECTCOUNT(*)FROM[tablename]

selectlookup.illustrationid,lookup.personid,person.birthdayfromlookup
Jointablesoncommon
leftjoinpersononlookup.personid=person.personid=statementtojoinbirthdayin
columns.
persontablewithprimaryillustrationid
Switchtothemysql
INSERTINTO[tablename](Host,User,Password)
db.Createanewuser. VALUES('%','user',PASSWORD('password'))
Changeausers
password.(fromunix [mysqldir]/bin/mysqladminuroothhostname.blah.orgppassword'newpassword'
shell).
Changeausers
password.(from
MySQLprompt).

SETPASSWORDFOR'user'@'hostname'=PASSWORD('passwordhere')

Switchtomysql
db.Giveuser
privilagesforadb.

INSERTINTO[tablename]
(Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv)
VALUES('%','db','user','Y','Y','Y','Y','Y','N')

Toupdateinfoalready UPDATE[tablename]SETSelect_priv='Y',Insert_priv='Y',Update_priv='Y'where
inatable.
[fieldname]='user'
Deletearow(s)froma
DELETEfrom[tablename]where[fieldname]='whatever'
table.
Updatedatabase
FLUSHPRIVILEGES
permissions/privilages.
Deleteacolumn.
Addanewcolumnto
db.

altertable[tablename]dropcolumn[columnname]
altertable[tablename]addcolumn[newcolumnname]varchar(20)

Changecolumnname. altertable[tablename]change[oldcolumnname][newcolumnname]varchar(50)
Makeauniquecolumn
altertable[tablename]addunique([columnname])
soyougetnodupes.
Makeacolumn
altertable[tablename]modify[columnname]VARCHAR(3)
bigger.
Deleteuniquefrom
table.

altertable[tablename]dropindex[colmnname]

LoadaCSVfileintoa LOADDATAINFILE'/tmp/filename.csv'replaceINTOTABLE[tablename]FIELDS
table.
TERMINATEDBY','LINESTERMINATEDBY'\n'(field1,field2,field3)
Dumpalldatabasesfor
backup.Backupfileis
[mysqldir]/bin/mysqldumpurootppasswordopt>/tmp/alldatabases.sql
sqlcommandsto
recreatealldb's.
Dumponedatabase
forbackup.

[mysqldir]/bin/mysqldumpuusernameppassworddatabasesdatabasename
>/tmp/databasename.sql

Dumpatablefroma
database.

[mysqldir]/bin/mysqldumpcuusernameppassworddatabasenametablename>
/tmp/databasename.tablename.sql

Restoredatabase(or
http://g2pc1.bu.edu/~qzpeng/manual/MySQL%20Commands.htm

2/3

16/03/2015

databasetable)from
backup.

MySQL Commands

[mysqldir]/bin/mysqluusernameppassworddatabasename</tmp/databasename.sql

CREATETABLE[tablename](firstnameVARCHAR(20),middleinitial
VARCHAR(3),lastnameVARCHAR(35),suffixVARCHAR(3),
CreateTableExample
officeidVARCHAR(10),useridVARCHAR(15),usernameVARCHAR(8),email
1.
VARCHAR(35),phoneVARCHAR(25),groups
VARCHAR(15),datestampDATE,timestamptime,pgpemailVARCHAR(255))
CreateTableExample createtable[tablename](personidint(50)notnullauto_incrementprimary
2.
key,firstnamevarchar(35),middlenamevarchar(50),lastnamevarchar(50)default'bato')

http://g2pc1.bu.edu/~qzpeng/manual/MySQL%20Commands.htm

3/3