英语单词

Create      创建
Retrieve    读取
Update      更新
Delete      删除
duplicate   复制
updata      更新数据
replace     替换
constraint  限制
crud        增删查改(doge)
exam        考试
result      结果
distinct    不同(用于去重)
ascending order   升序
descending order  降序
limit             限度

表的增删查改

6.1 Create

6.1.1 单行数据 + 全列插入

MariaDB [table_crud]> system clear
MariaDB [table_crud]> create table if not exists students(-> id int unsigned primary key auto_increment comment '用户的主键',  -> sn int not null unique comment '学生的学号',                      -> name varchar(20) not null,-> qq varchar(20) unique-> );
Query OK, 0 rows affected (0.01 sec)MariaDB [table_crud]> desc students;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| sn    | int(11)          | NO   | UNI | NULL    |                |
| name  | varchar(20)      | NO   |     | NULL    |                |
| qq    | varchar(20)      | YES  | UNI | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)#单行全列插入可以省略前面的提示代码
MariaDB [table_crud]> insert into students (id, sn, name, qq) values (1, 100, '张三', '123@qq.com');
Query OK, 1 row affected (0.00 sec)#单行局部性插入不能省略提示代码
MariaDB [table_crud]> insert into students (id, sn, name, qq) values (2, 101, '李四', '1234@qq.com');
Query OK, 1 row affected (0.01 sec)MariaDB [table_crud]> insert into students (sn, name) values (102, '王五');
Query OK, 1 row affected (0.00 sec)MariaDB [table_crud]> select * from students;
+----+-----+--------+-------------+
| id | sn  | name   | qq          |
+----+-----+--------+-------------+
|  1 | 100 | 张三   | 123@qq.com  |
|  2 | 101 | 李四   | 1234@qq.com |
|  3 | 102 | 王五   | NULL        |
+----+-----+--------+-------------+
3 rows in set (0.00 sec)

6.1.2 多行数据 + 指定列插入

#多行插入
MariaDB [table_crud]> insert into students (sn, name, qq) values (103, '赵六', '12345@qq.com') , (104, '宋七', '123456@qq.com');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0
MariaDB [table_crud]> select * from students;
+----+-----+--------+---------------+
| id | sn  | name   | qq            |
+----+-----+--------+---------------+
|  1 | 100 | 张三   | 123@qq.com    |
|  2 | 101 | 李四   | 1234@qq.com   |
|  3 | 102 | 王五   | NULL          |
|  4 | 103 | 赵六   | 12345@qq.com  |
|  5 | 104 | 宋七   | 123456@qq.com |
+----+-----+--------+---------------+
5 rows in set (0.00 sec)

6.1.3 插入否则更新

由于主键或者唯一键对应的值已经存在而导致插入失败

MariaDB [table_crud]> insert into students (id, sn, name, qq) values (1, 105, '老八', '1234567@qq.com');
#主键冲突报错
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
MariaDB [table_crud]> insert into students (id, sn, name, qq) values (6, 100, '老八', '1234567@qq.com');
#键冲突报错
ERROR 1062 (23000): Duplicate entry '100' for key 'sn'

若存在则更新,若不存在则插入

MariaDB [table_crud]> insert into students (id, sn, name, qq) values (3, 102, '王五','1234567@qq.com') on duplicate key update qq='12345678@qq.com', name='老八';
MariaDB [table_crud]> select * from students;
+----+-----+--------+---------------+
| id | sn  | name   | qq            |
+----+-----+--------+---------------+
|  1 | 100 | 张三   | 123@qq.com    |
|  2 | 101 | 李四   | 1234@qq.com   |
|  3 | 102 | 王五   | NULL          |
|  4 | 103 | 赵六   | 12345@qq.com  |
|  5 | 104 | 宋七   | 123456@qq.com |
+----+-----+--------+---------------+
5 rows in set (0.00 sec)#因为id = 3 或 sn = 102已经存在了(发生键冲突),则更新内容
MariaDB [table_crud]> insert into students (id, sn, name, qq) values (3, 102, '王五', '1234567@qq.com') on duplicate key update qq='12345678@qq.com', name='老八';
Query OK, 2 rows affected (0.01 sec)MariaDB [table_crud]> select * from students;
+----+-----+--------+-----------------+
| id | sn  | name   | qq              |
+----+-----+--------+-----------------+
|  1 | 100 | 张三   | 123@qq.com      |
|  2 | 101 | 李四   | 1234@qq.com     |
|  3 | 102 | 老八   | 12345678@qq.com |
|  4 | 103 | 赵六   | 12345@qq.com    |
|  5 | 104 | 宋七   | 123456@qq.com   |
+----+-----+--------+-----------------+
5 rows in set (0.00 sec)#唯一键冲突也会更新
MariaDB [table_crud]> insert into students (id, sn, name, qq) values (6, 102, '王五', '1234567@qq.com') on duplicate key update qq='123456789@qq.com', name='老八';
Query OK, 2 rows affected (0.01 sec)MariaDB [table_crud]> select * from students;
+----+-----+--------+------------------+
| id | sn  | name   | qq               |
+----+-----+--------+------------------+
|  1 | 100 | 张三   | 123@qq.com       |
|  2 | 101 | 李四   | 1234@qq.com      |
|  3 | 102 | 老八   | 123456789@qq.com |
|  4 | 103 | 赵六   | 12345@qq.com     |
|  5 | 104 | 宋七   | 123456@qq.com    |
+----+-----+--------+------------------+
5 rows in set (0.00 sec)

6.1.4 替换

#主键或者唯一键没有冲突,则直接插入;
#主键或者唯一键若有冲突,则删除后进行插入
#若存在主键和唯一键都冲突的情况,会将冲突的记录全部删掉后插入
MariaDB [table_crud]> replace into students (id, sn, name, qq) values (3, 102, '老九', '12.qq.com');
Query OK, 2 rows affected (0.01 sec)MariaDB [table_crud]> select * from students;
+----+-----+--------+---------------+
| id | sn  | name   | qq            |
+----+-----+--------+---------------+
|  1 | 100 | 张三   | 123@qq.com    |
|  2 | 101 | 李四   | 1234@qq.com   |
|  3 | 102 | 老九   | 12.qq.com     |
|  4 | 103 | 赵六   | 12345@qq.com  |
|  5 | 104 | 宋七   | 123456@qq.com |
+----+-----+--------+---------------+
5 rows in set (0.00 sec)
MariaDB [table_crud]> replace into students (sn, name, qq) values (100, '老十', '1.qq.com');
Query OK, 2 rows affected (0.00 sec)MariaDB [table_crud]> select * from students;
+----+-----+--------+---------------+
| id | sn  | name   | qq            |
+----+-----+--------+---------------+
|  2 | 101 | 李四   | 1234@qq.com   |
|  3 | 102 | 老九   | 12.qq.com     |
|  4 | 103 | 赵六   | 12345@qq.com  |
|  5 | 104 | 宋七   | 123456@qq.com |
|  6 | 100 | 老十   | 1.qq.com      |
+----+-----+--------+---------------+
5 rows in set (0.00 sec)
MariaDB [table_crud]> replace into students (id, sn, name, qq) values (1, 105, '老一', 'a.qq.com');
Query OK, 1 row affected (0.00 sec)MariaDB [table_crud]> insert  into students (sn, name, qq) values (106, '老二', 'ab.qq.com');
Query OK, 1 row affected (0.00 sec)MariaDB [table_crud]> select * from students;
+----+-----+--------+---------------+
| id | sn  | name   | qq            |
+----+-----+--------+---------------+
|  1 | 105 | 老一   | a.qq.com      |
|  2 | 101 | 李四   | 1234@qq.com   |
|  3 | 102 | 老九   | 12.qq.com     |
|  4 | 103 | 赵六   | 12345@qq.com  |
|  5 | 104 | 宋七   | 123456@qq.com |
|  6 | 100 | 老十   | 1.qq.com      |
|  7 | 106 | 老二   | ab.qq.com     |
+----+-----+--------+---------------+
7 rows in set (0.00 sec)
#若存在主键和唯一键都冲突的情况,会将冲突的记录全部删掉后插入(情况三)
MariaDB [table_crud]> replace into students (sn, name, qq) values (105, '十一', '1.qq.com');
Query OK, 3 rows affected (0.03 sec)# sn和qq都存在冲突,所以id=1和id=6的记录都被删除,自增插入8MariaDB [table_crud]> select * from students;
+----+-----+--------+---------------+
| id | sn  | name   | qq            |
+----+-----+--------+---------------+
|  2 | 101 | 李四   | 1234@qq.com   |
|  3 | 102 | 老九   | 12.qq.com     |
|  4 | 103 | 赵六   | 12345@qq.com  |
|  5 | 104 | 宋七   | 123456@qq.com |
|  7 | 106 | 老二   | ab.qq.com     |
|  8 | 105 | 十一   | 1.qq.com      |
+----+-----+--------+---------------+
6 rows in set (0.00 sec)

6.2 Retrieve

6.2.0 准备工作

首先先创建一个测试案例,并插入一定量的测试数据

MariaDB [table_crud]> create table if not exists exam_result(-> id int unsigned primary key auto_increment,-> name varchar(20) not null comment '学生姓名',-> chinese float default 0.0 comment '语文成绩',-> math float default 0.0 comment '数学成绩',-> english float default 0.0 comment '英语成绩');
Query OK, 0 rows affected (0.01 sec)MariaDB [table_crud]> insert into exam_result (name, chinese, math, english) values -> ('唐三藏', 67, 98, 56),-> ('孙悟空', 87, 78, 77),-> ('猪悟能', 88, 98, 90),-> ('曹孟德', 82, 84, 67),-> ('刘玄德', 55, 85, 45),-> ('孙权', 70, 73, 78),-> ('宋公明', 75, 65, 30);
Query OK, 7 rows affected (0.00 sec)
Records: 7  Duplicates: 0  Warnings: 0MariaDB [table_crud]> select * from exam_result;
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  1 | 唐三藏    |      67 |   98 |      56 |
|  2 | 孙悟空    |      87 |   78 |      77 |
|  3 | 猪悟能    |      88 |   98 |      90 |
|  4 | 曹孟德    |      82 |   84 |      67 |
|  5 | 刘玄德    |      55 |   85 |      45 |
|  6 | 孙权      |      70 |   73 |      78 |
|  7 | 宋公明    |      75 |   65 |      30 |
+----+-----------+---------+------+---------+
7 rows in set (0.00 sec)

6.2.1 全列查询

通常情况下不建议使用*进行全列查询

  • 查询的列越多,意味着需要传输的数据量越大
  • 可能会影响到索引的使用 (索引部分会在后面部分讲解)
# 指定列查询
MariaDB [table_crud]> select id,name,math from exam_result;
+----+-----------+------+
| id | name      | math |
+----+-----------+------+
|  1 | 唐三藏    |   98 |
|  2 | 孙悟空    |   78 |
|  3 | 猪悟能    |   98 |
|  4 | 曹孟德    |   84 |
|  5 | 刘玄德    |   85 |
|  6 | 孙权      |   73 |
|  7 | 宋公明    |   65 |
+----+-----------+------+
7 rows in set (0.00 sec)# 指定列查询的基础上,可以手动添加列(可以不存在,比如:表达式、值)
MariaDB [table_crud]> select id, name, math, 10 + 10 from exam_result;
+----+-----------+------+---------+
| id | name      | math | 10 + 10 |
+----+-----------+------+---------+
|  1 | 唐三藏    |   98 |      20 |
|  2 | 孙悟空    |   78 |      20 |
|  3 | 猪悟能    |   98 |      20 |
|  4 | 曹孟德    |   84 |      20 |
|  5 | 刘玄德    |   85 |      20 |
|  6 | 孙权      |   73 |      20 |
|  7 | 宋公明    |   65 |      20 |
+----+-----------+------+---------+
7 rows in set (0.00 sec)# 列查询修改显示数据
MariaDB [table_crud]> select id, name, math+10 from exam_result;
+----+-----------+---------+
| id | name      | math+10 |
+----+-----------+---------+
|  1 | 唐三藏    |     108 |
|  2 | 孙悟空    |      88 |
|  3 | 猪悟能    |     108 |
|  4 | 曹孟德    |      94 |
|  5 | 刘玄德    |      95 |
|  6 | 孙权      |      83 |
|  7 | 宋公明    |      75 |
+----+-----------+---------+
7 rows in set (0.00 sec)# 列显示总分
MariaDB [table_crud]> select id, name, chinese + math + english from exam_result;
+----+-----------+--------------------------+
| id | name      | chinese + math + english |
+----+-----------+--------------------------+
|  1 | 唐三藏    |                      221 |
|  2 | 孙悟空    |                      242 |
|  3 | 猪悟能    |                      276 |
|  4 | 曹孟德    |                      233 |
|  5 | 刘玄德    |                      185 |
|  6 | 孙权      |                      221 |
|  7 | 宋公明    |                      170 |
+----+-----------+--------------------------+
7 rows in set (0.00 sec)# 表达式的列显示可以使用`as`进行重命名
MariaDB [table_crud]> select id, name, chinese + math + english as 总分 from exam_result;
+----+-----------+--------+
| id | name      | 总分   |
+----+-----------+--------+
|  1 | 唐三藏    |    221 |
|  2 | 孙悟空    |    242 |
|  3 | 猪悟能    |    276 |
|  4 | 曹孟德    |    233 |
|  5 | 刘玄德    |    185 |
|  6 | 孙权      |    221 |
|  7 | 宋公明    |    170 |
+----+-----------+--------+
7 rows in set (0.00 sec)# 对搜索结果进行去重
MariaDB [table_crud]> select math from exam_result;
+------+
| math |
+------+
|   98 |
|   78 |
|   98 |
|   84 |
|   85 |
|   73 |
|   65 |
+------+
7 rows in set (0.00 sec)
# 可以观察到math中是存在重复分数98
MariaDB [table_crud]> select distinct math from exam_result; # 使用distinct
+------+
| math |
+------+
|   98 |
|   78 |
|   84 |
|   85 |
|   73 |
|   65 |
+------+
6 rows in set (0.01 sec)MariaDB [table_crud]> select distinct math, id from exam_result;
# 可以看到distinct要求整行相同才可以去重
+------+----+
| math | id |
+------+----+
|   98 |  1 |
|   78 |  2 |
|   98 |  3 |
|   84 |  4 |
|   85 |  5 |
|   73 |  6 |
|   65 |  7 |
+------+----+
7 rows in set (0.00 sec)# order by + 列名 + asc(升序)/desc(降序) 根据特定字段进行排序
MariaDB [table_crud]> select id, name, chinese + math + english as 总分 from exam_result order by 总分;
# 默认升序排列
+----+-----------+--------+
| id | name      | 总分   |
+----+-----------+--------+
|  7 | 宋公明    |    170 |
|  5 | 刘玄德    |    185 |
|  1 | 唐三藏    |    221 |
|  6 | 孙权      |    221 |
|  4 | 曹孟德    |    233 |
|  2 | 孙悟空    |    242 |
|  3 | 猪悟能    |    276 |
+----+-----------+--------+
7 rows in set (0.00 sec)MariaDB [table_crud]> select id, name, chinese + math + english as 总分 from exam_result order by 总分 desc;
# 使用desc指定降序排列
+----+-----------+--------+
| id | name      | 总分   |
+----+-----------+--------+
|  3 | 猪悟能    |    276 |
|  2 | 孙悟空    |    242 |
|  4 | 曹孟德    |    233 |
|  1 | 唐三藏    |    221 |
|  6 | 孙权      |    221 |
|  5 | 刘玄德    |    185 |
|  7 | 宋公明    |    170 |
+----+-----------+--------+
7 rows in set (0.00 sec)

6.2.2 WHERE条件

比较运算符

运算符 说明
>, >=, <, <= 大于,大于等于,小于,小于等于
= 等于, NULL不安全,例如: NULL = NULL的结构是NULL
<=> 等于, NULL安全, 例如:NULL <=> NULL 的结果是True
!=, <> 不等于
between x and y 范围匹配,[x, y], 如果x <= value <= y, 返回True
IN(option, …) 如果是option中的任何一个,返回True
is null 是NULL
is not null 不是NULL
like 模糊匹配. %标识多个(包括0个)任意字符串;_标识任意一个字符

逻辑运算符

运算符 说明
and 多个条件必须都为True, 结果才是True
or 任意一个条件为True, 结果就是True
not 条件为True, 返回结果为False
# 挑选语文成绩在[60, 80]的学生输出
MariaDB [table_crud]> select id, name, chinese from exam_result where chinese between 60 and 80;
+----+-----------+---------+
| id | name      | chinese |
+----+-----------+---------+
|  1 | 唐三藏    |      67 |
|  6 | 孙权      |      70 |
|  7 | 宋公明    |      75 |
+----+-----------+---------+
3 rows in set (0.00 sec)# 挑选语文成绩大于70并且数学成绩大于80的
MariaDB [table_crud]> select id, name, chinese, math from exam_result where chinese>=70 and math>= 80;
+----+-----------+---------+------+
| id | name      | chinese | math |
+----+-----------+---------+------+
|  3 | 猪悟能    |      88 |   98 |
|  4 | 曹孟德    |      82 |   84 |
+----+-----------+---------+------+
2 rows in set (0.00 sec)# 使用in (option, 来进行筛选)
MariaDB [table_crud]> select id, name, chinese, math from exam_result where math in (65, 84, 98, 99) or chinese >= 90;
+----+-----------+---------+------+
| id | name      | chinese | math |
+----+-----------+---------+------+
|  1 | 唐三藏    |      67 |   98 |
|  3 | 猪悟能    |      88 |   98 |
|  4 | 曹孟德    |      82 |   84 |
|  7 | 宋公明    |      75 |   65 |
+----+-----------+---------+------+
4 rows in set (0.00 sec)# like模糊查询 %(可以表示多个)/_(智能表示一个)
MariaDB [table_crud]> select name from exam_result where name like '孙%';
+-----------+
| name      |
+-----------+
| 孙悟空    |
| 孙权      |
+-----------+
2 rows in set (0.00 sec)# _代表一个,所以输出的name必须是包含两个字符并且第一个字符为'孙'
MariaDB [table_crud]> select name from exam_result where name like '孙_';
+--------+
| name   |
+--------+
| 孙权   |
+--------+
1 row in set (0.00 sec)# --where语句中不能使用别名
MariaDB [table_crud]> select id, name, chinese+math+english as 总分 from exam_result where chinese+math+english<200;
+----+-----------+--------+
| id | name      | 总分   |
+----+-----------+--------+
|  5 | 刘玄德    |    185 |
|  7 | 宋公明    |    170 |
+----+-----------+--------+
2 rows in set (0.00 sec)

注意

MariaDB [table_crud]> select id, name, chinese+math+english as 总分 from exam_result where 总分<200;
ERROR 1054 (42S22): Unknown column '总分' in 'where clause'# order by + 列名 + asc(升序)/desc(降序) 根据特定字段进行排序
MariaDB [table_crud]> select id, name, chinese + math + english as 总分 from exam_result order by 总分;
# 默认升序排列
+----+-----------+--------+
| id | name      | 总分   |
+----+-----------+--------+
|  7 | 宋公明    |    170 |
|  5 | 刘玄德    |    185 |
|  1 | 唐三藏    |    221 |
|  6 | 孙权      |    221 |
|  4 | 曹孟德    |    233 |
|  2 | 孙悟空    |    242 |
|  3 | 猪悟能    |    276 |
+----+-----------+--------+
7 rows in set (0.00 sec)
  • 我们发现在order by 语句中我们可以使用自定义的别名进行排序,但是在使用筛选语句where的过程中使用别名会报错。这是SQL语句的执行先后顺序决定的,排序中我们是先拿到全部数据,才进行排序,即先创建别名后进行排序,而在筛选语句中我们必须先进行筛选,然后再获取数据,即先进行where语句内部的筛选逻辑,后创建别名。所以在where语句执行的时候别名并未被创建,也就无法使用别名
  • where结构不能使用别名和定义别名,必须使用表结构中存在的数据字段来进行计算,筛选

总结:我们必须要对mysql各个字句的执行顺序,必须要有一定的刻意练习!!!

6.2.3 结果排序

6.2.3.1整数排序

# order by + 列名 + asc(升序)/desc(降序) 根据特定字段进行排序
MariaDB [table_crud]> select id, name, chinese + math + english as 总分 from exam_result order by 总分;
# 默认升序排列
+----+-----------+--------+
| id | name      | 总分   |
+----+-----------+--------+
|  7 | 宋公明    |    170 |
|  5 | 刘玄德    |    185 |
|  1 | 唐三藏    |    221 |
|  6 | 孙权      |    221 |
|  4 | 曹孟德    |    233 |
|  2 | 孙悟空    |    242 |
|  3 | 猪悟能    |    276 |
+----+-----------+--------+
7 rows in set (0.00 sec)MariaDB [table_crud]> select id, name, chinese + math + english as 总分 from exam_result order by 总分 desc;
# 使用desc指定降序排列
+----+-----------+--------+
| id | name      | 总分   |
+----+-----------+--------+
|  3 | 猪悟能    |    276 |
|  2 | 孙悟空    |    242 |
|  4 | 曹孟德    |    233 |
|  1 | 唐三藏    |    221 |
|  6 | 孙权      |    221 |
|  5 | 刘玄德    |    185 |
|  7 | 宋公明    |    170 |
+----+-----------+--------+
7 rows in set (0.00 sec)

注意 : 没有order by子句的查询,返回的顺序是未定义的,永远不要依赖这个顺序

6.2.3.2字符串排序

  • NULL视为比任何值都笑,升序出现在最上面

    MariaDB [table_crud]> select * from students order by qq;
    +----+-----+--------+---------------+
    | id | sn  | name   | qq            |
    +----+-----+--------+---------------+
    |  1 | 108 | 十二   | NULL          |
    |  8 | 105 | 十一   | 1.qq.com      |
    |  3 | 102 | 老九   | 12.qq.com     |
    |  2 | 101 | 李四   | 1234@qq.com   |
    |  4 | 103 | 赵六   | 12345@qq.com  |
    |  5 | 104 | 宋七   | 123456@qq.com |
    |  7 | 106 | 老二   | ab.qq.com     |
    +----+-----+--------+---------------+
    7 rows in set (0.00 sec)
  • 同为字符串则使用字符串的比较规则

6.2.3.3多重排序

MariaDB [table_crud]> select * from exam_result order by math desc, english asc, chinese asc;
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  1 | 唐三藏    |      67 |   98 |      56 |
|  3 | 猪悟能    |      88 |   98 |      90 |
|  5 | 刘玄德    |      55 |   85 |      45 |
|  4 | 曹孟德    |      82 |   84 |      67 |
|  2 | 孙悟空    |      87 |   78 |      77 |
|  6 | 孙权      |      70 |   73 |      78 |
|  7 | 宋公明    |      75 |   65 |      30 |
+----+-----------+---------+------+---------+
7 rows in set (0.00 sec)

要求:首先根据数学进行降序排列,然后再根据英语进行升序排列,最后根据语文进行升序

我们发现查询出来的表中是按数学字段是降序的,但是英语和中文并未升序,这是因为排序的单位是行,最先提出的排序规则会被优先满足**,在数学降序的前提下,比如唐三藏和猪悟能数学同分,接下来才按照语文进行升序排序**

6.2.3.4别名排序

MariaDB [table_crud]> select name, chinese + math + english as 总分 from exam_result order by 总分 desc;
+-----------+--------+
| name      | 总分   |
+-----------+--------+
| 猪悟能    |    276 |
| 孙悟空    |    242 |
| 曹孟德    |    233 |
| 唐三藏    |    221 |
| 孙权      |    221 |
| 刘玄德    |    185 |
| 宋公明    |    170 |
+-----------+--------+
7 rows in set (0.00 sec)

上文也已经说过,先创建总分这个别名然后才进行order by + 别名 排序,为了强化记忆,实现以下例子

MariaDB [table_crud]> select name, chinese + math + english as 总分 from exam_result where chinese + math + english > 200 order by 总分 desc;
+-----------+--------+
| name      | 总分   |
+-----------+--------+
| 猪悟能    |    276 |
| 孙悟空    |    242 |
| 曹孟德    |    233 |
| 唐三藏    |    221 |
| 孙权      |    221 |
+-----------+--------+
5 rows in set (0.00 sec)

6.2.4 筛选分页结果

  • limit from,size 可以来限制结果返回条数
  • limit size offset from 可读性更强(推荐)
# 按照语文降序排序,返回三条数据
MariaDB [table_crud]> select name, chinese from exam_result order by chinese desc limit 3;
+-----------+---------+
| name      | chinese |
+-----------+---------+
| 猪悟能    |      88 |
| 孙悟空    |      87 |
| 曹孟德    |      82 |
+-----------+---------+
3 rows in set (0.00 sec)
# 从排序好的数据的第0条开始,返回2条数据
MariaDB [table_crud]> select id, name, chinese from exam_result order by chinese desc limit 0, 2;
+----+-----------+---------+
| id | name      | chinese |
+----+-----------+---------+
|  3 | 猪悟能    |      88 |
|  2 | 孙悟空    |      87 |
+----+-----------+---------+
2 rows in set (0.00 sec)
# 从排序号的数据的第3条开始,返回2条数据
MariaDB [table_crud]> select id, name, chinese from exam_result order by chinese desc limit 3, 2;
+----+-----------+---------+
| id | name      | chinese |
+----+-----------+---------+
|  7 | 宋公明    |      75 |
|  6 | 孙权      |      70 |
+----+-----------+---------+
2 rows in set (0.00 sec)

6.3 Update

基本使用: updata table_name set chinese = first, math = second

6.3.1 修改记录

# 将孙悟空的数学成绩更改为80分
MariaDB [table_crud]> select * from exam_result;
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  1 | 唐三藏    |      67 |   98 |      56 |
|  2 | 孙悟空    |      87 |   78 |      77 |
|  3 | 猪悟能    |      88 |   98 |      90 |
|  4 | 曹孟德    |      82 |   84 |      67 |
|  5 | 刘玄德    |      55 |   85 |      45 |
|  6 | 孙权      |      70 |   73 |      78 |
|  7 | 宋公明    |      75 |   65 |      30 |
+----+-----------+---------+------+---------+
7 rows in set (0.00 sec)MariaDB [table_crud]> update exam_result set math=80 where name='孙悟空';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0MariaDB [table_crud]> select * from exam_result;
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  1 | 唐三藏    |      67 |   98 |      56 |
|  2 | 孙悟空    |      87 |   80 |      77 |
|  3 | 猪悟能    |      88 |   98 |      90 |
|  4 | 曹孟德    |      82 |   84 |      67 |
|  5 | 刘玄德    |      55 |   85 |      45 |
|  6 | 孙权      |      70 |   73 |      78 |
|  7 | 宋公明    |      75 |   65 |      30 |
+----+-----------+---------+------+---------+
7 rows in set (0.00 sec)# 给总分倒数前三名的同学数学加30分
MariaDB [table_crud]> update exam_result set math=math+30 order by math+english+chinese asc limit 3;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

6.4 Delete

基础语法delete from table_name [where] [order by] [limit]

6.4.1 删除数据

# 删除孙悟空同学的记录
MariaDB [table_crud]> delete from exam_result where name='孙悟空';
Query OK, 1 row affected (0.00 sec)

6.4.2 表删除

语法drop table talbe_name

清空表数据和删除表都非常不推荐使用,这里就不演示了

6.4.3 表清空

# 创建测试表
MariaDB [table_crud]> create table if not exists for_delete(-> id int unsigned primary key auto_increment,-> name varchar(16) not null) engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.01 sec)
# 插入测试数据
MariaDB [table_crud]> insert into for_delete (name) values ('a'), ('b'), ('c');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0
# 清空数据
MariaDB [table_crud]> delete from for_delete;
Query OK, 3 rows affected (0.00 sec)MariaDB [table_crud]> select * from for_delete;
Empty set (0.00 sec)

自增数据并不会因为表的清空而重置

MariaDB [table_crud]> show create table for_delete;
+------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table      | Create Table                                                                                                                                                                           |
+------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| for_delete | CREATE TABLE `for_delete` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT,`name` varchar(16) NOT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 |  # 可以看到清空后自增属性任为4
+------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)MariaDB [table_crud]> insert into for_delete (name) values ('x'); # 插入数据
Query OK, 1 row affected (0.00 sec)
# 自增字段是由4开始的
MariaDB [table_crud]> select * from for_delete;
+----+------+
| id | name |
+----+------+
|  4 | x    |
+----+------+
1 row in set (0.00 sec)

6.4.4 表截断

语法 truncate table table_name

MariaDB [table_crud]> select * from for_delete;
+----+------+
| id | name |
+----+------+
|  4 | x    |
|  5 | a    |
|  6 | b    |
|  7 | c    |
+----+------+
4 rows in set (0.00 sec)
# 表截断
MariaDB [table_crud]> truncate table for_delete;
Query OK, 0 rows affected (0.00 sec)
# 自增字段属性为 0
MariaDB [table_crud]> show create table for_delete;
+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table      | Create Table                                                                                                                                                          |
+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| for_delete | CREATE TABLE `for_delete` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT,`name` varchar(16) NOT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
# 记录被清空
MariaDB [table_crud]> select * from for_delete;
Empty set (0.01 sec)

delete vs truncate 区别

  • MySQL日志:承担很大功能要求 bin log、redo log、undo log(重要)
  • bin log: 几乎所有的sql操作, mysqld服务器都会给我们记录下来,用来多主机同步,增量备份
  • redo log: mysql 数据持久化和 crash-safe 功能
  • undo log: 在事务中承担回滚的日志,数据恢复操作

delete更新日志, truncate不更新日志,只能对整表操作,不能像 delete 一样针对部分数据操作;并且实际上 MySQL 不对数据操作,所以比 delete 更快,但是truncate在删除数据的时候,并不经过真正的事物,所以无法回滚,并且会重置 auto_increment

6.5 其他操作

6.5.1 拷贝表结构

# 创建测试表
MariaDB [table_crud]> create table if not exists duplicate_tb(-> id int not null,-> name varchar(16));
Query OK, 0 rows affected (0.02 sec)MariaDB [table_crud]> desc duplicate_tb;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   |     | NULL    |       |
| name  | varchar(16) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
# 插入测试数据
MariaDB [table_crud]> insert into duplicate_tb values (100,'aaa'), (100 ,'aaa'), (200,'bbb'), (200,'bbb'), (300,'ccc');
Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0
MariaDB [table_crud]> select * from duplicate_tb;
+-----+------+
| id  | name |
+-----+------+
| 100 | aaa  |
| 100 | aaa  |
| 200 | bbb  |
| 200 | bbb  |
| 300 | ccc  |
+-----+------+
5 rows in set (0.01 sec)MariaDB [table_crud]> create table duplicate_tb_bak like duplicate_tb;
Query OK, 0 rows affected (0.01 sec)
# 拷贝表结构
MariaDB [table_crud]> desc duplicate_tb_bak;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   |     | NULL    |       |
| name  | varchar(16) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

6.5.1 数据去重

# select distinct 拿出
MariaDB [table_crud]> select distinct * from duplicate_tb;
+-----+------+
| id  | name |
+-----+------+
| 100 | aaa  |
| 200 | bbb  |
| 300 | ccc  |
+-----+------+
3 rows in set (0.00 sec)# 将去重数据插入到备份表中
MariaDB [table_crud]> insert into duplicate_tb_bak select distinct * from duplicate_tb;
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0MariaDB [table_crud]> select * from duplicate_tb_bak;
+-----+------+
| id  | name |
+-----+------+
| 100 | aaa  |
| 200 | bbb  |
| 300 | ccc  |
+-----+------+
3 rows in set (0.00 sec)
# 将未去重的表重命名
MariaDB [table_crud]> rename table duplicate_tb to old_duplicate_tb;
Query OK, 0 rows affected (0.00 sec)
# 将去重了的表投入使用
MariaDB [table_crud]> rename table duplicate_tb_bak to duplicate_tb;
Query OK, 0 rows affected (0.00 sec)

6.5.3 聚合函数

函数 说明
count([distinct] expr) 返回查询到数据的数量
sum([distinct] expr) 返回查询到的数据的 总和, 不是数字没有意义
avg([distinct] expr) 返回查询到的数据的 平均值, 不是数字没有意义
max([distinct] expr) 返回查询到的数据的 最大值, 不是数字没有意义
min([distinct] expr) 返回查询到的数据的 最小值, 不是数字没有意义
# 使用count 查询班级人数
MariaDB [table_crud]> select count(*) 人数 from students;
+--------+
| 人数   |
+--------+
|      7 |
+--------+
1 row in set (0.00 sec)
# 查询不同的数学分数
MariaDB [table_crud]> select count(distinct math) from exam_result;
+----------------------+
| count(distinct math) |
+----------------------+
|                    6 |
+----------------------+
1 row in set (0.00 sec)# 统计数学成绩的总分
MariaDB [table_crud]> select sum(math) from exam_result;
+-----------+
| sum(math) |
+-----------+
|       593 |
+-----------+
1 row in set (0.00 sec)# 统计数学成绩小于六十分的人数
MariaDB [table_crud]> select count(math) from exam_result where math<60;
+-------------+
| count(math) |
+-------------+
|           0 |
+-------------+
1 row in set (0.00 sec)

注意: 聚合函数和特定的数据拼接打印可能会出现问题

MariaDB [table_crud]> select *, chinese+math+english total from exam_result order by total desc;
+----+-----------+---------+------+---------+-------+
| id | name      | chinese | math | english | total |
+----+-----------+---------+------+---------+-------+
|  3 | 猪悟能    |      88 |   98 |      90 |   276 |
|  1 | 唐三藏    |      67 |  128 |      56 |   251 |
|  4 | 曹孟德    |      82 |   84 |      67 |   233 |
|  6 | 孙权      |      70 |   73 |      78 |   221 |
|  5 | 刘玄德    |      55 |  115 |      45 |   215 |
|  7 | 宋公明    |      75 |   95 |      30 |   200 |
+----+-----------+---------+------+---------+-------+
6 rows in set (0.00 sec)# 当name字段和聚合函数混合使用,发现结果出现逻辑错误
MariaDB [table_crud]> select name, max(english+math+chinese) from exam_result;
+-----------+---------------------------+
| name      | max(english+math+chinese) |
+-----------+---------------------------+
| 唐三藏    |                       276 |
+-----------+---------------------------+
1 row in set (0.00 sec)

6.5.4 group by 子句使用

select + group by可以对指定列进行分组查询

select column1, column2 .. from talbe group by column;
  • 准备工作,创建一个雇员信息表(来自oracle 9i的经典测试表)

    • EMP员工表
    • DEPT部门表
    • SALGRADE工资等级表
# 将指令提前放在当前目录下
[clx@VM-20-6-centos lesson_12_25]$ ll
total 4
-rw-r--r-- 1 clx clx 3878 Dec 25 21:41 scott_data.sql
# 打开数据库
[clx@VM-20-6-centos lesson_12_25]$ mysql -u root -p
Enter password:
# 使用source就可以自动执行命令生成数据
MariaDB [(none)]> source ./scott_data.sql
# 可以发现创建了一个scott数据库,内部有三张表
MariaDB [scott]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| demo_db            |
| mysql              |
| performance_schema |
| scott              |
| table_constraint   |
| table_crud         |
+--------------------+
7 rows in set (0.00 sec)MariaDB [scott]> use scott
Database changed
MariaDB [scott]> show tables;
+-----------------+
| Tables_in_scott |
+-----------------+
| dept            |
| emp             |
| salgrade        |
+-----------------+
3 rows in set (0.00 sec)
  • 显示每个部门的平均工资和最高工资

MariaDB [scott]> select * from emp;
+--------+--------+-----------+------+---------------------+---------+---------+--------+
| empno  | ename  | job       | mgr  | hiredate            | sal     | comm    | deptno |
+--------+--------+-----------+------+---------------------+---------+---------+--------+
| 007369 | SMITH  | CLERK     | 7902 | 1980-12-17 00:00:00 |  800.00 |    NULL |     20 |
| 007499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 00:00:00 | 1600.00 |  300.00 |     30 |
| 007521 | WARD   | SALESMAN  | 7698 | 1981-02-22 00:00:00 | 1250.00 |  500.00 |     30 |
| 007566 | JONES  | MANAGER   | 7839 | 1981-04-02 00:00:00 | 2975.00 |    NULL |     20 |
| 007654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 |     30 |
| 007698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 00:00:00 | 2850.00 |    NULL |     30 |
| 007782 | CLARK  | MANAGER   | 7839 | 1981-06-09 00:00:00 | 2450.00 |    NULL |     10 |
| 007788 | SCOTT  | ANALYST   | 7566 | 1987-04-19 00:00:00 | 3000.00 |    NULL |     20 |
| 007839 | KING   | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 |    NULL |     10 |
| 007844 | TURNER | SALESMAN  | 7698 | 1981-09-08 00:00:00 | 1500.00 |    0.00 |     30 |
| 007876 | ADAMS  | CLERK     | 7788 | 1987-05-23 00:00:00 | 1100.00 |    NULL |     20 |
| 007900 | JAMES  | CLERK     | 7698 | 1981-12-03 00:00:00 |  950.00 |    NULL |     30 |
| 007902 | FORD   | ANALYST   | 7566 | 1981-12-03 00:00:00 | 3000.00 |    NULL |     20 |
| 007934 | MILLER | CLERK     | 7782 | 1982-01-23 00:00:00 | 1300.00 |    NULL |     10 |
+--------+--------+-----------+------+---------------------+---------+---------+--------+
14 rows in set (0.00 sec)# 使用grop by进行分组计算
MariaDB [scott]> select deptno, format(avg(sal),2),  max(sal) from emp group by deptno;
+--------+--------------------+----------+
| deptno | format(avg(sal),2) | max(sal) |
+--------+--------------------+----------+
|     10 | 2,916.67           |  5000.00 |
|     20 | 2,175.00           |  3000.00 |
|     30 | 1,566.67           |  2850.00 |
+--------+--------------------+----------+
3 rows in set (0.00 sec)
  • 显示每个部门的每种岗位的平均工资和最低工资
# 先分组,对每一组分别进行聚合和数据输出
MariaDB [scott]> select deptno, job, avg(sal) 平均工资, min(sal) 最低工资 ,count(*) 人数 from emp group by deptno, job;
+--------+-----------+--------------+--------------+--------+
| deptno | job       | 平均工资     | 最低工资     | 人数   |
+--------+-----------+--------------+--------------+--------+
|     10 | CLERK     |  1300.000000 |      1300.00 |      1 |
|     10 | MANAGER   |  2450.000000 |      2450.00 |      1 |
|     10 | PRESIDENT |  5000.000000 |      5000.00 |      1 |
|     20 | ANALYST   |  3000.000000 |      3000.00 |      2 |
|     20 | CLERK     |   950.000000 |       800.00 |      2 |
|     20 | MANAGER   |  2975.000000 |      2975.00 |      1 |
|     30 | CLERK     |   950.000000 |       950.00 |      1 |
|     30 | MANAGER   |  2850.000000 |      2850.00 |      1 |
|     30 | SALESMAN  |  1400.000000 |      1250.00 |      4 |
+--------+-----------+--------------+--------------+--------+
9 rows in set (0.00 sec)
  • 显示平均工资低于2000的部门和它的平均工资

    having:常与group by 组合使用,对分组进行筛选,作用等同于where

# 思路一:使用group by 将员工分组,然后筛选出平均工资小于2000的
MariaDB [scott]> select deptno, avg(sal) 平均 from emp group by deptno where avg(sal)<2000;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'where avg(sal)<2000' at line 1
# 发现报错,这是因为where筛选是在group by之前的,所以不能用于筛选group by 处理后的数据
MariaDB [scott]> select deptno, avg(sal) 平均 from emp group by deptno having 平均<2000;
+--------+-------------+
| deptno | 平均        |
+--------+-------------+
|     30 | 1566.666667 |
+--------+-------------+
1 row in set (0.00 sec)# having vs where
# where 过滤表数据   having 过滤分组数据

以上即使基本查询的全部内容

【MySQL】6.0 表的增删查改相关推荐

  1. MySQL - 对数据表进行“增删查改”的基础操作 - 细节狂魔

    文章目录 前文知识点回顾 正文 :对数据表进行"增删查改"操作 新增操作 - insert 关键字 查找语句(基础操作) - select关键字 - 重点!!!!!!!! 1.最基 ...

  2. MySQL学习(3)——表的增删查改(进阶)

    1.数据库约束 1.1 约束类型 NOT NULL - 指示某列不能存储null值 UNIQUE - 保证某列的每行必须有唯一的值 DEFAULT - 规定没有给列赋值时的默认值 PRIMARY KE ...

  3. vs连接mysql建一个表并增删查改_VS连接SQL Server数据库,增删改查详细教程(C#代码)...

    工具: 1.Visual Studio (我使用的是vs2013) 2.SQL Server  (我使用的是sql server2008) 操作: 1.打开SQL Server,打开后会看到数据库的初 ...

  4. MySQL:带你掌握表的增删查改

    表的增删查改 Create 单行数据 + 全列插入 多行数据 + 指定列插入 插入否则更新 替换 Retrieve SELECT 列 WHERE 条件 结果排序 筛选分页结果 Update Delet ...

  5. mysql表的增删查改

    mysql3 表的增删查改 增 创建数据表 单行插入 多行插入 指定列插入 替换 查 select列 全列查询 指定列查询 表达式中可以包含一个字段 给输出的结果重命名 去除重复结果 where条件 ...

  6. python学习之flask sqlalchemy介绍和使用 表操作增删查改

    flask sqlalchemy介绍和使用 表操作增删查改 内容详细 1 sqlalchemy介绍和快速使用 1.1 介绍 # SQLAlchemy是一个基于Python实现的ORM框架# djang ...

  7. ASP.NET两个关联的表的增删查改

    两个关联的表的增删查改 主要用了参数 SqlParameter,事务执行多条sql 表Users ID         int Name   varchar 表UsersDetail ID       ...

  8. C++实现静态顺序表的增删查改以及初始化

    C++实现静态顺序表的增删查改 顺序表:用一段地址连续的存储单元依s次存储数据元素的线性结构,是线性表的一种. //SeqList.h#pragma once#include <assert.h ...

  9. 【TcaplusDB知识库】GO快速上手PB表的增删查改操作

    PROTOBUF说明 PROTO表是基于PROTOBUF协议设计的TcaplusDB表,PROTOBUF协议是Google开源的通用RPC通信协议,用于TcaplusDB存储数据的序列化.反序列化等操 ...

最新文章

  1. 迷宫出路代码_如何在软件开发的迷宫中找到自己的出路
  2. 汇编语言 第3版 王爽 检测点习题部分—答案及详细解析 检测点3.1
  3. 进程上下文和中断上下文
  4. Eclipse插件开发总结(第二天)
  5. ABAP表抛FTP通用程序
  6. python123.io作业_Python自动化开发学习3
  7. python c 语言接口,## 人生苦短我用python[0x08] 使用ctypes调用c语言接口 ##
  8. python中float可以和int计算吗_python 中int ,float,字符串的操作
  9. 我一口气发现7个Safari 0day,苹果奖了7.5万美元
  10. 安卓系统校园招聘平台APP答辩PPT免费下载
  11. php mysql修复_php简单备份与还原MySql的方法 | 瑕疵学院 – 学习无瑕疵,成就有保障...
  12. atoi,itoa,strcpy, strcmp,strcpy, strcpy_s, memc...
  13. Androidstudio开发ARcore
  14. 程序人生(计算机系统大作业)
  15. 计算机如何回到桌面,windows下电脑怎么回到桌面主页?
  16. 关于3阶Volterra滤波器一些理解
  17. 大数据常见面试题总结,有问必答
  18. 仿射密码解密(Affine Cipher)
  19. Proteus 8 Professional安装教程
  20. 【Tomcat简介】

热门文章

  1. Gym 103117 Problem - Spicy Restaurant
  2. Linux操作系统下/etc/hosts文件配置方法(域名映射)
  3. 位运算的优点和小白鼠面试题
  4. Linux常用命令——lsmod命令
  5. 站长应该如何挖掘长尾关键词
  6. topsis法matlab程序,TOPSIS算法(示例代码)
  7. fest556_支持键盘映射的FEST-Swing 1.2rc
  8. 【转】十大智能家居系统解决方案
  9. 浅谈滴滴需求响应式公交背后的技术
  10. 浅析企业网络准入控制系统的部署方式