DML: 数据操作语言
INSERT, DELETE, UPDATE, SELECT 常用的语句,增、删、改、查;
INSERT:
一次插入一行或多行数据;
Syntax:
INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name [(col_name,...)]
{VALUES | VALUE} ({expr | DEFAULT},...),(...),...
[ ON DUPLICATE KEY UPDATE
col_name=expr
[, col_name=expr] ... ]
mysql> INSERT tbl_name [(col1,...)] VALUES (val1,...), (val21,...)
例如: mysql> INSERT INTO student (name,sex,register_date) VALUES (‘Jinjiao‘,‘M‘,‘2019-01-29‘);
Query OK, 1 row affected (0.01 sec)
mysql> SELECT * FROM student;
+----+----------+---------------+------+
| id | name | register_date | sex |
+----+----------+---------------+------+
| 1 | ZhanYang | 2018-06-20 | NULL |
| 4 | LiuJia | 2018-05-30 | NULL |
| 8 | JiaLiu | 2018-06-20 | NULL |
| 9 | JiaLiu | 2018-04-20 | NULL |
| 10 | gaoyf | 2018-04-20 | NULL |
| 11 | zhujh | 2018-04-20 | NULL |
| 12 | zhouha | 2018-04-20 | NULL |
| 13 | hanzb | 2018-04-21 | M |
| 16 | ZhanXing | 2018-05-21 | M |
| 17 | XingYan | 2018-05-21 | M |
| 18 | Jinjiao | 2019-01-29 | M |
+----+----------+---------------+------+
11 rows in set (0.00 sec)
Or:
INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name
SET col_name={expr | DEFAULT}, ...
[ ON DUPLICATE KEY UPDATE
col_name=expr
[, col_name=expr] ... ]
例如: mysql> INSERT INTO student SET name=‘Yinjiao‘,sex=‘F‘,register_date=‘2019-01-28‘;
Query OK, 1 row affected (0.01 sec)
Or:
INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name [(col_name,...)]
SELECT ...
[ ON DUPLICATE KEY UPDATE
col_name=expr
[, col_name=expr] ... ]
DELETE:
DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name
[WHERE where_condition] #如果不指where条件,该表会被清空
[ORDER BY ...]
[LIMIT row_count]
注意:一定要有限制条件,否则将清空表中的所有数据;
限制条件:
WHERE
LIMIT
UPDATE:
UPDATE [LOW_PRIORITY] [IGNORE] table_reference
SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]
mysql> UPDATE student SET age=100 WHERE id=19;
注意:一定要有限制条件,否则将修改所有行的指定字段;
限制条件:
WHERE
LIMIT
SELECT:
Query Cache #查询缓存,在查询结果确定时,才进行缓存。
mysql> SELECT NOW(); #显示当前系统时间。
查询执行路径中的组件:查询缓存、解析器、预处理器、优化器、查询执行引擎、存储引擎;
SELECT语句的执行流程:
FROM Clause --> WHERE Clause --> GROUP BY --> HAVING Clause --> ORDER BY --> SELECT --> LIMIT
单表查询:
SELECT
[ALL | DISTINCT | DISTINCTROW ]
[SQL_CACHE | SQL_NO_CACHE]
select_expr [, select_expr ...]
[FROM table_references
[WHERE where_condition]
[GROUP BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[HAVING where_condition]
[ORDER BY {col_name | expr | position}
[ASC | DESC], ...]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
[FOR UPDATE | LOCK IN SHARE MODE]
DISTINCT: 数据去重;
mysql> SELECT DISTINCT sex FROM student;
+------+
| sex |
+------+
| NULL |
| M |
| F |
+------+
3 rows in set (0.00 sec)
SQL_CACHE: 显式指定存储查询结果于缓存之中;
SQL_NO_CACHE: 显式查询结果不予缓存;
命中个数:mysql> SHOW GLOBAL VARIABLES LIKE ‘query%‘;
+------------------------------+---------+
| Variable_name | Value |
+------------------------------+---------+
| query_alloc_block_size | 8192 |
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 1048576 |
| query_cache_type | OFF |
| query_cache_wlock_invalidate | OFF |
| query_prealloc_size | 8192 |
+------------------------------+---------+
7 rows in set (0.00 sec)
query_cache_type的值为‘ON‘时,查询缓存功能打开;
SELECT的结果符合缓存条件即会缓存,否则,不予缓存;
显式指定SQL_NO_CACHE,不予缓存;
query_cache_type的值为‘DEMAND‘时,查询缓存功能按需进行;
显式指定SQL_CACHE的SELECT语句才会缓存;其它均不予缓存;
缓存命中率查询:mysql> SHOW GLOBAL STATUS LIKE ‘Qcache%‘;
+-------------------------+---------+
| Variable_name | Value |
+-------------------------+---------+
| Qcache_free_blocks | 1 |
| Qcache_free_memory | 1031352 |
| Qcache_hits | 0 |
| Qcache_inserts | 0 |
| Qcache_lowmem_prunes | 0 |
| Qcache_not_cached | 63 |
| Qcache_queries_in_cache | 0 |
| Qcache_total_blocks | 1 |
+-------------------------+---------+
8 rows in set (0.00 sec)
mysql> SHOW GLOBAL STATUS LIKE ‘Com_se%‘;
+----------------+-------+
| Variable_name | Value |
+----------------+-------+
| Com_select | 75 |
| Com_set_option | 0 |
+----------------+-------+
2 rows in set (0.00 sec)
字段显示可以使用别名:
col1 AS alias1, col2 AS alias2, ...
WHERE子句:指明过滤条件以实现“选择”的功能:
过滤条件:布尔型表达式;
算术操作符:+, -, *, /, %
比较操作符:=, !=, <>, <=>, >, >=, <, <=
mysql> SELECT name,age,id FROM student WHERE age >10;
mysql> SELECT name,age,id FROM student WHERE age+10 >10;
BETWEEN min_num AND max_num #取值区间
IN (element1, element2, ...) #等值或条件表达式
mysql> SELECT name,age,id FROM student WHERE age IN (18,100);
IS NULL #表达式取值为空
mysql> SELECT name,age,sex FROM student WHERE sex IS NULL;
IS NOT NULL #表达式取值不为空
LIKE:
%: 任意长度的任意字符;
_:任意单个字符;
RLIKE:
REGEXP:匹配字符串可用正则表达式书写模式;
逻辑操作符:
NOT #|
AND #&&
OR #|| 或
XOR #亦或,二者不同则为真,相同则为假
GROUP:根据指定的条件把查询结果进行“分组”以用于做“聚合”运算:
avg(), max(), min(), count(), sum()
mysql> SELECT * FROM student GROUP BY sex;
mysql> SELECT avg(age) FROM student GROUP BY sex;
HAVING: 对分组聚合运算后的结果指定过滤条件;
mysql> SELECT avg(age) as aage,sex FROM student GROUP BY sex HAVING aage>20;
#aage 定义的字段别名
mysql> SELECT count(id) AS NOS FROM student GROUP BY id;
mysql> SELECT count(id) AS NOS,id FROM student GROUP BY id;
mysql> SELECT count(id) AS NOS,id FROM student GROUP BY id HAVING NOS>2;
ORDER BY: 根据指定的字段对查询结果进行排序;
默认为升序
升序:ASC
降序:DESC
mysql> SELECT count(id) AS NOS,id FROM student GROUP BY id HAVING NOS>2 ORDER BY NOS;
mysql> SELECT count(id) AS NOS,id FROM student GROUP BY id HAVING NOS>2 ORDER BY age DESC;
LIMIT [[offset,]row_count]:对查询的结果进行输出行数数量限制;
#offset 偏移量 #row_count 显示多少行
mysql> SELECT count(id) AS NOS,id FROM student GROUP BY id LIMIT 10;
+-----+----+
| NOS | id |
+-----+----+
| 1 | 1 |
| 1 | 4 |
| 1 | 8 |
| 1 | 9 |
| 1 | 10 |
| 1 | 11 |
| 1 | 12 |
| 1 | 13 |
| 1 | 16 |
| 1 | 17 |
+-----+----+
10 rows in set (0.00 sec)
mysql> SELECT count(id) AS NOS,id FROM student GROUP BY id LIMIT 10,10;
+-----+----+
| NOS | id |
+-----+----+
| 1 | 18 |
| 1 | 19 |
| 1 | 20 |
+-----+----+
3 rows in set (0.00 sec)
对查询结果中的数据请求施加“锁”:
[FOR UPDATE | LOCK IN SHARE MODE]]
FOR UPDATE: 写锁,排他锁,独占锁;
LOCK IN SHARE MODE: 读锁,共享锁
多表查询:
交叉连接:笛卡尔乘积;
mysql> SELECT FROM student,study_record;
内连接:
等值连接:让表之间的字段以“等值”建立连接关系;
mysql> SELECT FROM students,teachers WHERE student,TeacherID=teachers.TID
不等值连接
自然连接
自连接
外连接:
左外连接:
FROM tb1 LEFT JOIN tb2 ON tb1.col=tb2.col
右外连接
FROM tb1 RIGHT JOIN tb2 ON tb1.col=tb2.col
子查询:在查询语句嵌套着查询语句
基于某语句的查询结果再次进行的查询
用在WHERE子句中的子查询:
(1) 用于比较表达式中的子查询;子查询仅能返回单个值;
mysql> SELECT Name,Age FROM students WHERE Age>(SELECT avg(Age) FROM students);
mysql> SELECT avg(age) FROM student WHERE age>(SELECT avg(age) FROM student);
(2) 用于IN中的子查询:子查询应该单键查询并返回一个或多个值从构成列表;
mysql> SELECT Name,Age FROM students WHERE Age IN (SELECT Age FROM teachers);
(3) 用于EXISTS;
用于FROM子句中的子查询;
使用格式:SELECT tb_alias.col1,... FROM (SELECT clause) AS tb_alias WHERE Clause;
示例:
mysql> SELECT s.aage,s.ClassID FROM (SELECT avg(Age) AS aage,ClassID FROM students WHERE ClassID IS NOT NULL GROUP BY ClassID) AS s WHERE s.aage>30;
联合查询:UNION
mysql> SELECT Name,Age FROM students UNION SELECT Name,Age FROM teachers;
原文地址:http://blog.51cto.com/zhanx/2348162
时间: 2024-10-29 19:05:58