Data Definition Language (DDL)
Database
SHOW DATABASES;
USE <name of database>;
CREATE DATABASE <name of database>;
DROP DATABASE <name of database>;
Table
SHOW TABLES
;Sql CREATE TABLE <name of table>( <column name1> <column type1> <extra>, <column name2> <column type2> <extra>, PRIMARY KEY(<a column name>) );
DROP TABLE <name of table>;
ALTER TABLE <name of table> ADD <column name> <column type> AFTER <another column name>;
ALTER TABLE <name of table> DROP <column name>;
ALTER TABLE <name of table> MODIFY <column name> <column type>;
ALTER TABLE <name of table> CHANGE <old column name> <new column name> <type of new column>;
ALTER TABLE <name of table> RENAME (TO) <new name of table>;
An Example
1 | ##################### DATABASE ##################### |
Data Manipulation Language (DML)
Insert New Data into Table
INSERT INTO <name of table> (<column1>, <column2>, <column3>) VALUES (<value1>, <value2>, <value3>);
INSERT INTO <name of table> VALUES (<value1>, <value2>, <value3>);
This will insert a data with values in order with columns.
Update Data in Table
UPDATE <name of table> SET <column1>=<value1>, <column2>=<value2> WHERE <conditions>;
UPDATE <name of table> SET <column1>=<value1>, <column2>=<value2> ;
This will update the columns of all data in the table.
Delete Data from Table
DELETE FROM <name of table> WHERE <conditions>;
DELETE FROM <name of table>;
WARNING: This would clear all the data of the table, but the id won’t be clear.
TRUNCATE <name of table>;
WARNING: This would clear all the data of the table, with id also cleared.
An Example
1 | # show the contents of the table user |
Data Control Language (DCL)
Change Password
1 | # change password |
Create User
CREATE USER <user name>@<user IP> IDENTIFIED BY <user passoword>;
Grant Privileges to User
GRANT <privilege1>, <privilege2> ON <database>.<table> TO <user>@<user IP>;
- Granting all privileges uses
ALL
orALL PRIVILEGES
instead of<privilege1>, <privilege2>
. - Granting all tables of a database uses
<database>.*
instead of<database>.<table>
. - Granting all databases uses
*.*
instead of<database>.<table>
.
- Granting all privileges uses
Combining ‘create user’ and ‘grant privileges’ uses:
GRANT <privilege1>, <privilege2> ON <database>.<table> TO <new username>@<new user IP> IDENTIFIED BY 'new user password';
Revoke Privileges from User
REVOKE <privilege1>, <privilege2> ON <database>.<table> FROM <user>@<user IP>;
- Revoking privileges from all IP uses
<user>@'%'
instead of<user>@<user IP>
.
- Revoking privileges from all IP uses
Show Privileges for User
SHOW GRANTS FOR
@ ;
Delete User
DROP USER
@ ;
Data Query Language (DQL)
Basic Query Display
SELECT <column1> (AS <shorthand1>), <column2> (AS <shorthand2>), <column3> (AS <shorthand3>) FROM <table> WHERE <conditions>;
WHERE <conditions>
could be omitted.Listing all the data in the table uses:
SELECT * FROM <table>;
An Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33# list user_name, user_email, mobile_phone of all data from user table.
SELECT user_name, user_email, mobile_phone FROM user;
/*
+-----------+----------------+--------------+
| user_name | user_email | mobile_phone |
+-----------+----------------+--------------+
| jack | jack@163.com | 12555555555 |
| mike | mike@gmail.com | 13888888888 |
| 马克 | haha@gmail.com | 13888888888 |
+-----------+----------------+--------------+
*/
# list the item where user_name is 'jack'.
SELECT user_name, user_emial, mobile_phone FROM user WHERE user_name='jack';
/*
+-----------+--------------+--------------+
| user_name | user_email | mobile_phone |
+-----------+--------------+--------------+
| jack | jack@163.com | 12555555555 |
+-----------+--------------+--------------+
*/
# using shorthands
SELECT user_name as name, user_email as email FROM user;
/*
+------+----------------+
| name | email |
+------+----------------+
| jack | jack@163.com |
| mike | mike@gmail.com |
| 马克 | haha@gmail.com |
+------+----------------+
*/
Query Filtered Repeated Data
SELECT DISTINCT <only one column> FROM <table>;
Query Concatenated Columns
SELECT CONCAT(<column1>, <column2>, .........., <columnN>) AS <concated_name> FROM <table>;
SELECT CONCAT_WS(<seperator>, <column1>, <column2>, ..., <columnN>) AS <concated_name> FROM <table>
;
An Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22SELECT CONCAT(user_name, user_emial, mobile_phone) AS 'user_name_email_phone' FROM user;
/*
+-------------------------------+
| user_name_email_phone |
+-------------------------------+
| jackjack@163.com12555555555 |
| mikemike@gmail.com13888888888 |
| 马克haha@gmail.com13888888888 |
+-------------------------------+
*/
# Add a seperator.
SELECT CONCAT_WS('::', user_name, user_emial, mobile_phone) AS 'user_name_email_phone' FROM user;
/*
+-----------------------------------+
| user_name_email_phone |
+-----------------------------------+
| jack::jack@163.com::12555555555 |
| mike::mike@gmail.com::13888888888 |
| 马克::haha@gmail.com::13888888888 |
+-----------------------------------+
*/
Query by Fuzzy Search
SELECT <column1>, <column2> FROM <table> WHERE <one column> LIKE <%something%>;
Where <one column> LIKE <%something%>
could only be used when the size of the dataset is not so large. If the dataset is so large that we don’t useLIKE
command which would costs several seconds, one way is using Sphinx.An Example
1
2
3
4
5
6
7
8
9SELECT user_name, user_email FROM user WHERE user_email like '%gmail%';
/*
+-----------+----------------+
| user_name | user_email |
+-----------+----------------+
| mike | mike@gmail.com |
| 马克 | haha@gmail.com |
+-----------+----------------+
*/
Query by Sorting Data
Ascending:
SELECT <column1>, <column2> FROM <table> ORDER BY <one column> (ASC);
Descending:
SELECT <column1>, <column2> FROM <table> ORDER BY <one column> DESC;
Query by using Aggregate Function
- Count:
SELECT COUNT(<column>) FROM <table>;
- Sum:
SELECT SUM(<column>) FROM <table>;
- Average:
SELECT AVG(<column>) FROM <table>;
- Maximum:
SELECT MAX(<column>) FROM <table>;
- Minimum:
SELECT MIN(<column>) FROM <table>;
Group Query
SELECT COUNT(*) FROM <table> GROUP BY <one column>;
1
2
3
4
5
6
7
8
9
10# Group by 'mobile_phone'.
SELECT mobile_phone, COUNT(*) as totals FROM user GROUP BY mobile_phone;
/*
+--------------+--------+
| mobile_phone | totals |
+--------------+--------+
| 12555555555 | 1 |
| 13888888888 | 2 |
+--------------+--------+
*/
SELECT <one column> FROM <table> GROUP BY <one column> HAVING COUNT(*)<conditions> ;
1 | # Group by age and display the age(s) that are counted to be bigger than 2. |
Query by connecting two tables
Inner join:
SELECT <table 1>.<one column>, <table 2>.<one column> from <table 1>, <table 2> where <conditions>;
- another form:
SELECT <table 1>.<one column>, <table 2>.<one column> from <table 1> INNER JOIN <table 2> where <conditions>;
- another form:
Left join:
SELECT <table 1>.<one column>, <table 2>.<one column> from <table 1> LEFT JOIN <table 2> where <conditions>;
Left join means to show the whole selected column of the left table (table 1), and then join the selected column of the right table (table 2).
Right join:
SELECT <table 1>.<one column>, <table 2>.<one column> from <table 1> RIGHT JOIN <table 2> where <conditions>;
Right join is just the opposite with the left join, which is to show the whole selected column from the right table (table 2), and then join the selected column from the left table (table 1).
Union all:
SELECT <column> FROM <table 1> UNION ALL SELECT <column> FROM <table 2>;
Sub query:
SELECT <column 1> FROM <table 1> where <column 2> in (SELECT <column 2> FROM <table 2>);
Limit Query
SELECT <column> FROM <table> LIMIT <number>;
1
2
3
4
5
6
7
8
9SELECT id, name, age FROM student LIMIT 3;
/*
| id | name | age |
+----+------+-----+
| 1 | wang | 16 |
| 2 | hong | 17 |
| 3 | li | 17 |
+----+------+-----+
*/Getting the length of the table could use:
SELECT id FROM student ORDER BY id DESC LIMIT 1;
instead of:
SELECT count(*) FROM student;
Because the former one is far more efficient.
Transaction
- Start transaction:
START TRANSACTION;
- Roll back:
ROLLBACK;
- Commit the transaction:
COMMIT;
An example
1 | # Using transaction to achieve a transfer service. |
View
View is used as a query table.
- Create a view:
CREATE VIEW <name of view> (column1, column2, ...) AS <SELECT commands>;
- Show the creating information:
SHOW CREATE VIEW <name of view>;
- Alter a view:
ALTER VIEW <name of view> (column1, column2, ...) AS <SELECT commands>;
- Query the view:
SELECT * FROM <name of view>;
- Delete a view:
DROP VIEW <name of view>;
An example
1 | # Create a view named 'test_view' |
Trigger
Trigger a series of operations before or after some operations (insert, update, delete) on a table.
- Create a trigger:
1 | CREATE TRIGGER <name of trigger> AFTER|BEFORE INSERT|UPDATE|DELETE ON <name of table> FOR EACH ROW |
- Show the creating information:
SHOW CREATE TRIGGER <name of trigger>;
- Delete the trigger:
DROP TRIGGER <name of trigger>;
An example
1 | # The total_num table shows the numbers of rows of each table. |
Import and export file
Export SQL file:
$ mysqldump -u <username> -p <password> <database> <table name> > <filename.sql>
- Using navicat:
Dump SQL File
Import SQL file:
$ mysql -u <username> -p <password> <database> < <filename.sql>
- Using navicat:
Execute SQL File
Optimization of MySQL
- Explain the query:
explain <query command>;
1 | # use shop |
- Profiles of the query commands:
- Open the
profiling
:set profiling=ON;
- Show profiles:
show profiles;
- Show the detailed duration of a query:
show profile for query <Query_ID>;
- Open the
1 | show variables like "%profil%"; |
If there is one status that costs too much time, perhaps it’s where the problem exists, and then you could come up with some solutions to it.