Mysql commonly used commands

 · 3 mins read

MySQL Command-Line

Connecting

mysql -u [username] -p

mysql -u [username] -p[password]

Importing

mysql -u[username] -p[password] < filename

Dumping

mysqldump -u[username] -p[password] database [tables] > filename

Basic MySQL Commands

SHOW DATABASES;

CREATE DATABASE database;

USE database;

DROP DATABASE database;

SHOW TABLES;

DESCRIBE table;

SHOW COLUMNS FROM table;

DROP TABLE table;


No doubt that, using mysql-workbench is more efficient and friendly to users!

click this to jump downloading page!


Built-in Functions

MySQL Mathematical Functions

WhatHow 
Count rows per groupCOUNT(column*)
Average value of groupAVG(column) 
Minumum value of groupMIN(column) 
Maximum value of groupMAX(column) 
Sum values in a groupSUM(column) 
Absolute valueabs(number) 
Rounding numbersround(number) 
Largest integer not greaterfloor(number) 
Smallest integer not smallerceiling(number) 
Square rootsqrt(number) 
nth powerpow(base,exponent) 
random number n, 0<n < 1rand() 
sin (similar cos, etc.)sin(number) 

MySQL String Functions

WhatHow
Compare stringsstrcmp(string1,string2)
Convert to lower caselower(string)
Convert to upper caseupper(string)
Left-trim whitespace (similar right)ltrim(string)
Substring of stringsubstring(string,index1,index2)
Encrypt passwordpassword(string)
Encode stringencode(string,key)
Decode stringdecode(string,key)
Get datecurdate()
Get timecurtime()
Extract day name from date stringdayname(string)
Extract day number from date stringdayofweek(string)
Extract month from date stringmonthname(string)

SQL

Create table

CREATE TABLE table (

column1 type [[NOT] NULL]

[AUTO_INCREMENT],

column2 type [[NOT] NULL]

[AUTO_INCREMENT],

other options,

PRIMARY KEY (column(s)) );

Insert data

  • INSERT INTO table VALUES (list of values);
  • INSERT INTO table SET column1=value1, column2=value2, … columnk=valuek;
  • INSERT INTO table (column1,column2,…) VALUES (value1,value2…);

Insert/Select

INSERT INTO table (column1,column2,…)

SELECT statement;

(See below)

Delete data

DELETE FROM table

[WHERE condition(s)];

Updating Data

UPDATE table SET

column1=value1,

column2=value2,

columnk=valuek

[WHERE condition(s)];

Insert column

ALTER TABLE table ADD COLUMN

column type options;

Delete column

ALTER TABLE table

DROP COLUMN column;

Querying

SELECT * FROM table;

SELECT column1,column2,… FROM table [WHERE condition(s)] [ORDER BY column(s) [DESC]];

No Repeats

SELECT [DISTINCT] column(s) FROM table;

Column Aliases

SELECT column1 [AS alias1], column2 [AS alias2], … FROM table1;

Grouping

SELECT column1,column2,…

FROM table

[GROUP BY column(s)];

Group Filtering

SELECT column1,column2,…

FROM table

[GROUP BY column(s)]

[HAVING condition(s)];

Everything

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]];