参考 https://www.mysqltutorial.org/mysql-cheat-sheet.aspx
Access mysql server
mysql -u [username] -p;
mysql -u [username] -p [database]
# 逻辑备份
mysqldump -u [username] -p [database] > data_backup.sql;
Working with database
CREATE DATABASE [IF NOT EXISTS] database_name;
USE database_name;
DROP DATABASE [IF EXISTS] database_name;
show databases;
Working with table
show tables;
DESCRIBE table [column]
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] table(
key type(size) NOT NULL PRIMARY KEY AUTO_INCREMENT,
c1 type(size) NOT NULL,
c2 type(size) NULL,
...
);
insert into table_name values (),();
insert into table_name values(columns) select (same columns) from other_table_name --将数据一次性导入表,避免逐条insert
ALTER TABLE table ADD|DROP [COLUMN];
ALTER TABLE table ADD PRIMARY KEY (column,...)
ALTER TABLE table ADD INDEX [name](column, ...);
UPDATE table_name SET column = 'new value' WHERE id = xxx;
DELETE from table_name WHERE id = '';
CONCATENATE and Trim
CONCATENATE: select concat(id,'(',info,')') from test;
RTrim: select concat(RTrim(id),'(',RTrim(info),')') from test; (RTrim删除数据右侧空格)
select concat(id,'(',info,')') AS id_info from test; (use AS to create Alias)
select id, quantity, price, quantity*price AS amount from products;
Querying Data
show columns from table_name;= describe table_name;
select distinct vendor_id from products;
select distinct column_name1, column_name2, column_name3 from table_name;(1*2*3)
select * from table_name limit 50,50
select * from table_name order by column_name1, column_name2(先按1排序,再按2排序,对应的场景比如对姓名相同,名字不同的员工姓名排序)
select * from table_name order by product_price DESC, product_name;(price降序排序,name还是默认的升序排序)
最大值 select product_price from products order by product_price DESC LIMIT 1;
最小值 select product_price from products order by product_price LIMIT 1;
Filtering Data
select * from table_name where conditions;
select * from products where product_price between 5 and 10;
select * from products where id = 1003 and price >10;
select * from products (where id = 1003 or id = 1004) and price>10;
IN operator: select * from products where product_price NOT IN (1003,1004);
Wildcard and Regular Expression
LIKE 'word%' start with word (% 表示任意字符出现任意次数)
LIKE '%word' end with word
LIKE '%word%' contain word
LIKE 's%b' start with s and end with b
LIKE '_word' (_ only match single char)
REGEXP '^...
匹配特殊字符 \\. \\- \\n \\r (转义 escaping)
匹配字符类 '[[:digit:]]{4}'
select 'hello' REGEXP '[a-z]'
Function
length() 返回串的长度
locate() 找出串的一个子串
substring() 返回子串的字符
select * from table_name where Date(order_date) BETWEEN '2016-08-01' AND '2016-08-30' -- 8月的订单
select * from table_name where YEAR(order_data)=2016 AND MONTH(order_data)=9
AVG(DISTICT column_name)
COUNT()
SUM()
Group by and Having
select id,count(*) from table_name WHERE price>10 GOURP BY id HAVING count(*) >2 ORDER BY count(*) LIMIT 5
select id from table_name GROUP BY id 相当于对id去重
select id from orders where order_num IN (select order_num from table_name where ...)
select name, (select count(*) from orders where orders.id = customers.id) from customers
JOINing tables
select vendor_name,product_name, product_price from vendors, products
where vendors.id = products.id (等值联结)
select vendor_name,product_name, product_price from vendors INNER JOIN products
ON vendors.id = products.id (内部联结)
select p1.product_id, p1.product_name (自联结)
LEFT | RIGHT JOIN
UNION
组合查询,每个查询都要包含相同的列,表达式,聚集函数
select ... UNION (ALL) select... ORDER BY
VIEW
视图是一段SQL,不是数据。优点是方便重用SQL,保护数据
CREATE VIEW view_name AS (select ...)
STORED PROCEDURES
mysql> DELIMITER //
mysql> CREATE PROCEDURE proc1(OUT s int)
-> BEGIN
-> SELECT COUNT(*) INTO s FROM user;
-> END
-> //
mysql> DELIMITER ;
CALL proc1();//
CURSORS游标只能用于存储过程
Trigger
CREATE TRIGGER printMessage AFTER INSERT ON products
FOR EACH ROW SELECT 'product added'
每个表最多支持6个触发器(INSERT,DELETE,UPDATE前后)
TRANSACTION PROCESS
ROLLBACK and COMMIT, SAVEPOINT
事务
start transaction;
select * from table_name;
delete from table_name;
select * from table_name;
rollback;
select * from table_name;
--set autocommit=0
start transaction;
delete * from table_name;
commit;
Managing Security
-- show users
use mysql;
select * from user
create user xavizhao identified by 'password';
grant select on 'database'.* to user_name;
set password for user_name = Password('xxx')