「MySQL」- 基础增删改查

目录

前言

新增/插入 ( insert )

查询 ( select )

1. 查询所有列

2.查询指定列

3.查询表达式字段

4.查询字段指定别名

5.去重查询

6.排序查询

7.条件查询

条件运算符

逻辑运算符

1.单一条件查询

​ 2.多条件查询

​ 3.范围查询

​ 4.空值查询

​ 5.模糊查询

​ 8.限制查询记录

​ 修改 ( update )

​ 删除 ( delete )

前言

数据库中的 CRUD 操作 , 是最基本也是最重要的基础 , 这里的 CRUD 是指 增查改删 英文首字母

  • C : create 增加

  • R : retrieve 检索

  • U : updata 修改

  • D : delete 删除

本章介绍的 CRUD 操作 , 都是围绕着数据表来进行操作的 , 所以在此之前 , 我们需要先创建一张数据表

-- 创建一个数据库
create database test;

-- 选择数据库
use test;	

-- 创建一张测试表
create table demo1(id int,name varchar(20));	

接下来 , 作者就选择这张测试表来介绍数据表中基本的 CRUD 操作

use test;

新增/插入 ( insert )

语法

insert [into] <数据表名> [(field1,field2,....fieldN)] values(value1,value2,....valueN);

说明

  • [ ] 中的是可选项

  • field1,field2,....fieldN 指指定插入字段名名称 , 每个字段使用逗号分割

  • value1,value2,....valueN 指插入的指 , 数目和类型需要和表结构定义的数目和类型匹配 ! ! !

示例

  1. 单条记录插入 , 向 测试表 中插入一条数据

    insert into demo1 values(1,'张三');
  2. 多条记录插入, 向 测试表 中插入多条数据

    insert into demo1 values(2,'李四') , (3,'王五');

    在一条插入语句中 , 可以插入多条语句 , 每条记录使用 逗号 分割

  3. 指定列插入 , 向 测试表 中 id 列插入一条数据

    insert into demo1(id) values(4);

    当指定列插入后 , 其它列的值为默认值 , 在插入的值中 , 个数及顺序都需要跟前面指定的列相匹配

在插入完数据之后 , 可以使用查询表的 SQL 语句来查看我们刚刚插入的数据

mysql> select * from demo1; -- 查询表内所有列
+------+--------+
| id   | name   |
+------+--------+
|    1 | 张三    |		
|    2 | 李四    |
|    3 | 王五    |
|    4 | NULL   |		-- 指定列插入 , name 列的值为默认值
+------+--------+
4 rows in set (0.00 sec)

ps : 

  • SQL里 字符和字符串都可以使用单引号或者双引号表示 , 例如 '张三' or "张三" 两种表示方式都能表示一个字符或字符串

  • 插入语句中的 into 关键字是可以省略的

查询 ( select )

语法

select <列名/表达式> from <数据表名> [where][limit][offset];

说明

  • [ ] 中的是可选项

  • 可以使用 where语句来包含任何条件。

  • 可以使用 limit 属性来设定返回的记录数。

  • 可以通过offset 指定 select 语句开始查询的数据偏移量。

在 MySQL 中 , 查询语句涉及到的操作是比较多的 , 为了更好的去介绍每一种查询 , 因此 在这准备另一张数据比较多的表

-- 创建考试成绩表
drop table if exists exam_result;
create table exam_result (
id int,
name varchar(20),
chinese decimal(3,1),
math decimal(3,1),
english decimal(3,1)
);
-- 插入测试数据
insert into exam_result (id,name, chinese, math, english) values
(1,'唐三藏', 67, 98, 56),
(2,'孙悟空', 87.5, 78, 77),
(3,'猪悟能', 88, 98.5, 90),
(4,'曹孟德', 82, 84, 67),
(5,'刘玄德', 55.5, 85, 45),
(6,'孙权', 70, 73, 78.5),
(7,'宋公明', 75, 65, 30);

扩展一下小方法 , 对于以上多条 SQL 语句如果一条一条的敲上终端黑框框上,万一出错了是非常不方便的调试的 , 所以我们有以下两种办法

  1. 在 Windows 中建立一个 txt 文本文档 ,在记事本中加上 SQL 语句,然后保存 , 后缀名改成 sql , 然后在 终端 敲以下命令 , 然后回车

    \. 指定.sql后缀文件

  2. 使用 MySQL Workbench 图形化数据库 , 使用快捷键 shift + ctrl + 回车 执行 SQL 语句

老规矩 , 还是得先选择这张表

use exam_result;

1. 查询所有列

顾名思义 , 就是展示出数据表中的所有列的记录

命令

select * from <数据表名>;

说明

  • * 表示通配符,代表所有的列.

示例

  1. 查看 exam_result 表中的所有记录

    mysql> select * from exam_result;
    +------+-----------+---------+------+---------+
    | id   | name      | chinese | math | english |
    +------+-----------+---------+------+---------+
    |    1 | 唐三藏    |    67.0 | 98.0 |    56.0 |
    |    2 | 孙悟空    |    87.5 | 78.0 |    77.0 |
    |    3 | 猪悟能    |    88.0 | 98.5 |    90.0 |
    |    4 | 曹孟德    |    82.0 | 84.0 |    67.0 |
    |    5 | 刘玄德    |    55.5 | 85.0 |    45.0 |
    |    6 | 孙权      |    70.0 | 73.0 |    78.5 |
    |    7 | 宋公明    |    75.0 | 65.0 |    30.0 |
    +------+-----------+---------+------+---------+
    7 rows in set (0.00 sec)

2.查询指定列

可以指定展示表中的哪一列

命令

select <列名1,列名2,....列名N> from <数据表名>;

说明

  • 多列使用逗号分割

示例

  1. 查询数学成绩那一列

    mysql> select math from exam_result;
    +------+
    | math |
    +------+
    | 98.0 |
    | 78.0 |
    | 98.5 |
    | 84.0 |
    | 85.0 |
    | 73.0 |
    | 65.0 |
    +------+
    7 rows in set (0.00 sec)
  2. 查询所有同学所有成绩 , 不显示 id

    mysql> select name,chinese,english from exam_result;
    +-----------+---------+---------+
    | name      | chinese | english |
    +-----------+---------+---------+
    | 唐三藏    |    67.0 |    56.0 |
    | 孙悟空    |    87.5 |    77.0 |
    | 猪悟能    |    88.0 |    90.0 |
    | 曹孟德    |    82.0 |    67.0 |
    | 刘玄德    |    55.5 |    45.0 |
    | 孙权      |    70.0 |    78.5 |
    | 宋公明    |    75.0 |    30.0 |
    +-----------+---------+---------+
    7 rows in set (0.00 sec)

3.查询表达式字段

在查询时 , 也可以根据情况把各列组成一个表达式来进行查询

命令

select <表达式> from <数据表名>;

示例

  1. 查询表的每个同学数学成绩加 10 的结果

    mysql> select name,math+10 from exam_result;
    +-----------+---------+
    | name      | math+10 |
    +-----------+---------+
    | 唐三藏    |   108.0 |
    | 孙悟空    |    88.0 |
    | 猪悟能    |   108.5 |
    | 曹孟德    |    94.0 |
    | 刘玄德    |    95.0 |
    | 孙权      |    83.0 |
    | 宋公明    |    75.0 |
    +-----------+---------+
    7 rows in set (0.00 sec)
  2. 查询每个同学的总成绩, 总成绩 = 语文成绩 + 数学成绩 + 英语成绩

    mysql> select name,chinese+math+english from exam_result;
    +-----------+----------------------+
    | name      | chinese+math+english |
    +-----------+----------------------+
    | 唐三藏    |                221.0 |
    | 孙悟空    |                242.5 |
    | 猪悟能    |                276.5 |
    | 曹孟德    |                233.0 |
    | 刘玄德    |                185.5 |
    | 孙权      |                221.5 |
    | 宋公明    |                170.0 |
    +-----------+----------------------+
    7 rows in set (0.00 sec)

4.查询字段指定别名

查询的时候可以给查询字段取别名 , 可以使其可读性更高

命令

select <列名1 as 别名 , 列名2 as 别名> from <数据表名>;

说明

  • as 表示需要给哪个列名取别名 , as 后面加上指定别名

  • 多列使用逗号分割

示例

  1. 查询每个同学的分数总和 , 并给查询结果分数和取别名为 '总成绩'

    mysql> select name,chinese+math+english as '总成绩' from exam_result;
    +-----------+-----------+
    | name      | 总成绩    |
    +-----------+-----------+
    | 唐三藏    |     221.0 |
    | 孙悟空    |     242.5 |
    | 猪悟能    |     276.5 |
    | 曹孟德    |     233.0 |
    | 刘玄德    |     185.5 |
    | 孙权      |     221.5 |
    | 宋公明    |     170.0 |
    +-----------+-----------+
    7 rows in set (0.00 sec)

ps: as关键字可以省略,省略后只需要用空格代替,不过建议不省略 , 省略了可读性较差

5.去重查询

去除数据表中重复的记录 , 重复记录只展现一条

命令

select distinct <列名/表达式> from <数据表名>;

说明

  • distinct 去重关键字

示例

先插入一条相同的数据 , 试试普通全列查询和去重查询有什么区别

mysql> insert into exam_result values(7,'宋公明',75,65,30);
Query OK, 1 row affected (0.00 sec)
  1. 普通全列查询

    mysql> select * from exam_result;
    +------+-----------+---------+------+---------+
    | id   | name      | chinese | math | english |
    +------+-----------+---------+------+---------+
    |    1 | 唐三藏    |    67.0 | 98.0 |    56.0 |
    |    2 | 孙悟空    |    87.5 | 78.0 |    77.0 |
    |    3 | 猪悟能    |    88.0 | 98.5 |    90.0 |
    |    4 | 曹孟德    |    82.0 | 84.0 |    67.0 |
    |    5 | 刘玄德    |    55.5 | 85.0 |    45.0 |
    |    6 | 孙权      |    70.0 | 73.0 |    78.5 |
    |    7 | 宋公明    |    75.0 | 65.0 |    30.0 |
    |    7 | 宋公明    |    75.0 | 65.0 |    30.0 |
    +------+-----------+---------+------+---------+
    8 rows in set (0.00 sec)
  2. 去重查询

    mysql> select distinct * from exam_result;
    +------+-----------+---------+------+---------+
    | id   | name      | chinese | math | english |
    +------+-----------+---------+------+---------+
    |    1 | 唐三藏    |    67.0 | 98.0 |    56.0 |
    |    2 | 孙悟空    |    87.5 | 78.0 |    77.0 |
    |    3 | 猪悟能    |    88.0 | 98.5 |    90.0 |
    |    4 | 曹孟德    |    82.0 | 84.0 |    67.0 |
    |    5 | 刘玄德    |    55.5 | 85.0 |    45.0 |
    |    6 | 孙权      |    70.0 | 73.0 |    78.5 |
    |    7 | 宋公明    |    75.0 | 65.0 |    30.0 |
    +------+-----------+---------+------+---------+
    7 rows in set (0.00 sec)

可以看到 , 将重复的记录只展示一条 , 只有所有列相同时才会去重 , 去重查询关键字也可以用在其它查询上

6.排序查询

针对指定列对查询结果进行排序

select <列名> from <数据表名> order by 列名1 [asc/desc] , 列名2 [asc/desc] ;

说明

  • order by 排序查询关键字

  • [ ]中为可选项 , 省略默认升序排序

  • asc 为升序排序

  • desc 为降序排序

  • 可以针对多列进行排序,优先第一列,第一列相同再按照后面的条件排序

示例

  1. id 列进行升序查询

    mysql> select * from exam_result order by id asc;
    +------+-----------+---------+------+---------+
    | id   | name      | chinese | math | english |
    +------+-----------+---------+------+---------+
    |    1 | 唐三藏    |    67.0 | 98.0 |    56.0 |
    |    2 | 孙悟空    |    87.5 | 78.0 |    77.0 |
    |    3 | 猪悟能    |    88.0 | 98.5 |    90.0 |
    |    4 | 曹孟德    |    82.0 | 84.0 |    67.0 |
    |    5 | 刘玄德    |    55.5 | 85.0 |    45.0 |
    |    6 | 孙权      |    70.0 | 73.0 |    78.5 |
    |    7 | 宋公明    |    75.0 | 65.0 |    30.0 |
    |    7 | 宋公明    |    75.0 | 65.0 |    30.0 |
    +------+-----------+---------+------+---------+
    8 rows in set (0.00 sec)

    asc 可有可无 , 默认就是升序排序

  2. id 列进行降序排序

    mysql> select * from exam_result order by id desc;
    +------+-----------+---------+------+---------+
    | id   | name      | chinese | math | english |
    +------+-----------+---------+------+---------+
    |    7 | 宋公明    |    75.0 | 65.0 |    30.0 |
    |    7 | 宋公明    |    75.0 | 65.0 |    30.0 |
    |    6 | 孙权      |    70.0 | 73.0 |    78.5 |
    |    5 | 刘玄德    |    55.5 | 85.0 |    45.0 |
    |    4 | 曹孟德    |    82.0 | 84.0 |    67.0 |
    |    3 | 猪悟能    |    88.0 | 98.5 |    90.0 |
    |    2 | 孙悟空    |    87.5 | 78.0 |    77.0 |
    |    1 | 唐三藏    |    67.0 | 98.0 |    56.0 |
    +------+-----------+---------+------+---------+
    8 rows in set (0.00 sec)
  3. 针对表达式进行排序

    mysql> select id,name,chinese+math+english from exam_result order by chinese+math+english asc;
    +------+-----------+----------------------+
    | id   | name      | chinese+math+english |
    +------+-----------+----------------------+
    |    7 | 宋公明    |                170.0 |
    |    7 | 宋公明    |                170.0 |
    |    5 | 刘玄德    |                185.5 |
    |    1 | 唐三藏    |                221.0 |
    |    6 | 孙权      |                221.5 |
    |    4 | 曹孟德    |                233.0 |
    |    2 | 孙悟空    |                242.5 |
    |    3 | 猪悟能    |                276.5 |
    +------+-----------+----------------------+
    8 rows in set (0.00 sec)
  4. 针对别名进行排序

    mysql> select id,name,chinese+math+english as '总成绩' from exam_result order by '总成绩';
    +------+-----------+-----------+
    | id   | name      | 总成绩    |
    +------+-----------+-----------+
    |    1 | 唐三藏    |     221.0 |
    |    2 | 孙悟空    |     242.5 |
    |    3 | 猪悟能    |     276.5 |
    |    4 | 曹孟德    |     233.0 |
    |    5 | 刘玄德    |     185.5 |
    |    6 | 孙权      |     221.5 |
    |    7 | 宋公明    |     170.0 |
    |    7 | 宋公明    |     170.0 |
    +------+-----------+-----------+
    8 rows in set (0.00 sec)

注意:如果待排序中的数据包含NULL,那么默认NULL比任何记录都小

7.条件查询

给查询加入条件 , 达到过滤效果而准确得到记录 , 一般需要用到运算符来构造条件 , 这里先了解 MySQL 中的运算符 , 在 MySQL 中 , 运算符分为两种 条件运算符逻辑运算符

条件运算符

运算符 说明
> >= < <= 大于,大于等于,小于,小于等于
= 等于,NULL 不安全,例如 NULL = NULL 的结果是 NULL
<=> 等于,NULL 安全,例如 NULL <=> NULL 的结果是 TRUE(1)
!= <> 不等于
between a0 and a1 范围匹配,[a0, a1],如果 a0 <= value <= a1,返回 TRUE
int(option, ...) 如果是 option 集合中的任意一个,返回 true
is null 是 null
is not null 不是 null
like 模糊匹配 , % 表示任意多个(包括 0 个)任意字符 _ 表示任意一个字符

逻辑运算符

运算符 说明
and 多个条件必须都为 true,结果才是 true
or 任意一个条件为 true, 结果为 true
not 条件为 true,结果为 false

1.单一条件查询

命令

select <列名> from <数据表名> where <条件>;

说明

  • where 条件关键字 , 类似于各大语言中的 if

示例

  1. 查询英语成绩 小于50 的记录

    mysql> select * from exam_result where english < 50;
    +------+-----------+---------+------+---------+
    | id   | name      | chinese | math | english |
    +------+-----------+---------+------+---------+
    |    5 | 刘玄德    |    55.5 | 85.0 |    45.0 |
    |    7 | 宋公明    |    75.0 | 65.0 |    30.0 |
    |    7 | 宋公明    |    75.0 | 65.0 |    30.0 |
    +------+-----------+---------+------+---------+
    3 rows in set (0.00 sec)
  2. 查询唐三藏同学的成绩

    mysql> select * from exam_result where name='唐三藏';
    +------+-----------+---------+------+---------+
    | id   | name      | chinese | math | english |
    +------+-----------+---------+------+---------+
    |    1 | 唐三藏    |    67.0 | 98.0 |    56.0 |
    +------+-----------+---------+------+---------+
    1 row in set (0.00 sec)

 2.多条件查询

命令

select <列名> from <数据表名> where <条件1> <逻辑运算符> <条件2> <逻辑运算符> <条件3>....;

示例

  1. 查询数学成绩低于80 且 英语成绩高于60的记录

    mysql> select * from exam_result where math < 80 and english > 60;
    +------+-----------+---------+------+---------+
    | id   | name      | chinese | math | english |
    +------+-----------+---------+------+---------+
    |    2 | 孙悟空    |    87.5 | 78.0 |    77.0 |
    |    6 | 孙权      |    70.0 | 73.0 |    78.5 |
    +------+-----------+---------+------+---------+
    2 rows in set (0.00 sec)
  2. 查询语文成绩高于80 或者 英语成绩高于80的记录

    mysql> select * from exam_result where chinese > 80 or english > 80;
    +------+-----------+---------+------+---------+
    | id   | name      | chinese | math | english |
    +------+-----------+---------+------+---------+
    |    2 | 孙悟空    |    87.5 | 78.0 |    77.0 |
    |    3 | 猪悟能    |    88.0 | 98.5 |    90.0 |
    |    4 | 曹孟德    |    82.0 | 84.0 |    67.0 |
    +------+-----------+---------+------+---------+
    3 rows in set (0.00 sec)

 3.范围查询

命令

select <列名> from <数据表名> where <条件> [[not] between <值1> and <值2>] [[not] in(值1,值2,值3,.....)];

说明

  • between and 是范围查询

  • in( option,.... ) 是列表查询 , 符合 option 集合其中一个值即可

示例

  1. 查找数学成绩在80 - 100 之间的记录

    mysql> select * from exam_result where math between 80 and 100;
    +------+-----------+---------+------+---------+
    | id   | name      | chinese | math | english |
    +------+-----------+---------+------+---------+
    |    1 | 唐三藏    |    67.0 | 98.0 |    56.0 |
    |    3 | 猪悟能    |    88.0 | 98.5 |    90.0 |
    |    4 | 曹孟德    |    82.0 | 84.0 |    67.0 |
    |    5 | 刘玄德    |    55.5 | 85.0 |    45.0 |
    +------+-----------+---------+------+---------+
    4 rows in set (0.00 sec)
  2. 查询数学成绩不在80 - 100 之间的记录

    mysql> select * from exam_result where math not between 80 and 100;
    +------+-----------+---------+------+---------+
    | id   | name      | chinese | math | english |
    +------+-----------+---------+------+---------+
    |    2 | 孙悟空    |    87.5 | 78.0 |    77.0 |
    |    6 | 孙权      |    70.0 | 73.0 |    78.5 |
    |    7 | 宋公明    |    75.0 | 65.0 |    30.0 |
    |    7 | 宋公明    |    75.0 | 65.0 |    30.0 |
    +------+-----------+---------+------+---------+
    4 rows in set (0.00 sec)
  3. 查询数学成绩是 98 , 84 , 73 的记录

    mysql> select * from exam_result where math in(98,84,73);
    +------+-----------+---------+------+---------+
    | id   | name      | chinese | math | english |
    +------+-----------+---------+------+---------+
    |    1 | 唐三藏    |    67.0 | 98.0 |    56.0 |
    |    4 | 曹孟德    |    82.0 | 84.0 |    67.0 |
    |    6 | 孙权      |    70.0 | 73.0 |    78.5 |
    +------+-----------+---------+------+---------+
    3 rows in set (0.00 sec)
  4. 查询数学成绩不是 98 , 84 , 73 的记录

    mysql> select * from exam_result where math not in(98,84,73);
    +------+-----------+---------+------+---------+
    | id   | name      | chinese | math | english |
    +------+-----------+---------+------+---------+
    |    2 | 孙悟空    |    87.5 | 78.0 |    77.0 |
    |    3 | 猪悟能    |    88.0 | 98.5 |    90.0 |
    |    5 | 刘玄德    |    55.5 | 85.0 |    45.0 |
    |    7 | 宋公明    |    75.0 | 65.0 |    30.0 |
    |    7 | 宋公明    |    75.0 | 65.0 |    30.0 |
    +------+-----------+---------+------+---------+
    5 rows in set (0.00 sec)

 4.空值查询

先插入一条 null 值方便讲解

mysql> insert into exam_result(id) values(8);
Query OK, 1 row affected (0.00 sec)

命令

select <列名> from <数据表名> where <条件> is [not] null;

说明

  • [ ]为可选项 , 判断非null值可加上 not

示例

  1. 查询数学成绩为空值的记录

    mysql> select * from exam_result where math is null;
    +------+------+---------+------+---------+
    | id   | name | chinese | math | english |
    +------+------+---------+------+---------+
    |    8 | NULL |    NULL | NULL |    NULL |
    +------+------+---------+------+---------+
    1 row in set (0.00 sec)
  2. 查询数学成绩不为空的记录

    mysql> select * from exam_result where math is not null;
    +------+-----------+---------+------+---------+
    | id   | name      | chinese | math | english |
    +------+-----------+---------+------+---------+
    |    1 | 唐三藏    |    67.0 | 98.0 |    56.0 |
    |    2 | 孙悟空    |    87.5 | 78.0 |    77.0 |
    |    3 | 猪悟能    |    88.0 | 98.5 |    90.0 |
    |    4 | 曹孟德    |    82.0 | 84.0 |    67.0 |
    |    5 | 刘玄德    |    55.5 | 85.0 |    45.0 |
    |    6 | 孙权      |    70.0 | 73.0 |    78.5 |
    |    7 | 宋公明    |    75.0 | 65.0 |    30.0 |
    |    7 | 宋公明    |    75.0 | 65.0 |    30.0 |
    +------+-----------+---------+------+---------+
    8 rows in set (0.00 sec)

 5.模糊查询

命令

select <列名> from <数据表名> where <条件> [not] like % / _;

说明

  • % 通配符可以表示任意 0 个或多个字符 , 例如 a%b , 匹配的字符串可以是 ab , acb , acdb ,adfgb

  • _ 通配符只能表示 1 个字符且不能为 0 , 例如 a_b , 匹配的字符串可以是 acb , adb , aeb

示例

  1. 使用 % 模糊匹配 name 列等于孙的记录

    mysql> select * from exam_result where name like '孙%';
    +------+-----------+---------+------+---------+
    | id   | name      | chinese | math | english |
    +------+-----------+---------+------+---------+
    |    2 | 孙悟空    |    87.5 | 78.0 |    77.0 |
    |    6 | 孙权      |    70.0 | 73.0 |    78.5 |
    +------+-----------+---------+------+---------+
    2 rows in set (0.00 sec)

    模糊查询出"孙" 字后面有一个或多个的字符串的记录

  2. 使用 _ 模糊匹配 name 列等于孙的记录

    mysql> select * from exam_result where name like '孙_';
    +------+--------+---------+------+---------+
    | id   | name   | chinese | math | english |
    +------+--------+---------+------+---------+
    |    6 | 孙权   |    70.0 | 73.0 |    78.5 |
    +------+--------+---------+------+---------+
    1 row in set (0.00 sec)

    模糊查询出"孙" 字后面有一个的字符串的记录

注意:

  • where 条件可以使用表达式,但不能使用别名。

  • and 的优先级高于 or,在同时使用时,需要使用小括号 ( ) 包裹优先执行的部分

  • null 的值相当于 false , 如果需要处理 null 就需要多写一条 ( 条件 is null ) 的语句

  • 基于 like 这样的通配符查询,也是比较低效的操作,往往要进行字符串匹配

 8.限制查询记录

可以指定查询的表展示几条记录

命令

select <列名> from <数据表名> [where] limit n [offset n];

说明

  • [ ]表示可选项

  • limit 为分页限制查询关键字, n 为指定几条记录

  • offset 表示偏移 , n 为偏移量 , 偏移量不写默认从 0 开始

示例

  1. 查询成绩表前 5 条记录

    mysql> select * from exam_result limit 5;
    +------+-----------+---------+------+---------+
    | id   | name      | chinese | math | english |
    +------+-----------+---------+------+---------+
    |    1 | 唐三藏    |    67.0 | 98.0 |    56.0 |
    |    2 | 孙悟空    |    87.5 | 78.0 |    77.0 |
    |    3 | 猪悟能    |    88.0 | 98.5 |    90.0 |
    |    4 | 曹孟德    |    82.0 | 84.0 |    67.0 |
    |    5 | 刘玄德    |    55.5 | 85.0 |    45.0 |
    +------+-----------+---------+------+---------+
    5 rows in set (0.00 sec)
  2. 查询从第三条记录开始的五条记录

    mysql> select * from exam_result limit 5 offset 2;
    +------+-----------+---------+------+---------+
    | id   | name      | chinese | math | english |
    +------+-----------+---------+------+---------+
    |    3 | 猪悟能    |    88.0 | 98.5 |    90.0 |
    |    4 | 曹孟德    |    82.0 | 84.0 |    67.0 |
    |    5 | 刘玄德    |    55.5 | 85.0 |    45.0 |
    |    6 | 孙权      |    70.0 | 73.0 |    78.5 |
    |    7 | 宋公明    |    75.0 | 65.0 |    30.0 |
    +------+-----------+---------+------+---------+
    5 rows in set (0.00 sec)

    offset 也可以省略 , 写法会不一样 , 以下写法和上面写法效果一样

    select * from exam_result limit 2,5; -- 从第3条记录开始向后取 5 条记录	

注意:

  1. 查询结果是临时表,不会影响到磁盘上的数据,只是这次查询的结果看起来变了,而实际上没变。

  2. 查询表里面的列的类型不一定和原始表的列的类型一样

  3. select 得到的临时表的列名取决于 SQL 语句具体的写法

  4. SQL中的临时表不一定和原始表的行数相同

ps:查询结果的表格是一个”临时表“,查询结果不会影响到磁盘上的数据,而insert操作是实实在在的修改了磁盘上的数据。

 修改 ( update )

语法

update <数据表名> set <列名>=<值/表达式> [where <条件>] ; 

说明

  • [ ]表示可选项

  • update 修改记录关键字

  • set 表示需要修改的记录

  • 不加 where 子句则默认修改全部记录 ! ! !

示例

  1. 将 唐三藏 同学名字修改成 唐玄奘

    mysql> update exam_result set name='唐玄奘' where name='唐三藏';
    Query OK, 1 row affected (0.00 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    mysql> select * from exam_result;
    +------+-----------+---------+------+---------+
    | id   | name      | chinese | math | english |
    +------+-----------+---------+------+---------+
    |    1 | 唐玄奘    |    67.0 | 98.0 |    56.0 |
    |    2 | 孙悟空    |    87.5 | 78.0 |    77.0 |
    |    3 | 猪悟能    |    88.0 | 98.5 |    90.0 |
    |    4 | 曹孟德    |    82.0 | 84.0 |    67.0 |
    |    5 | 刘玄德    |    55.5 | 85.0 |    45.0 |
    |    6 | 孙权      |    70.0 | 73.0 |    78.5 |
    |    7 | 宋公明    |    75.0 | 65.0 |    30.0 |
    |    7 | 宋公明    |    75.0 | 65.0 |    30.0 |
    |    8 | NULL      |    NULL | NULL |    NULL |
    +------+-----------+---------+------+---------+
    9 rows in set (0.00 sec)

    可以看到 name列中的 唐三藏 已经被修改成 唐玄奘 了.

  2. 将所有记录的 数学成绩 -10 分

    mysql> update exam_result set math=math-10;
    Query OK, 8 rows affected (0.00 sec)
    Rows matched: 9  Changed: 8  Warnings: 0
    
    mysql> select * from exam_result;
    +------+-----------+---------+------+---------+
    | id   | name      | chinese | math | english |
    +------+-----------+---------+------+---------+
    |    1 | 唐玄奘    |    67.0 | 88.0 |    56.0 |
    |    2 | 孙悟空    |    87.5 | 68.0 |    77.0 |
    |    3 | 猪悟能    |    88.0 | 88.5 |    90.0 |
    |    4 | 曹孟德    |    82.0 | 74.0 |    67.0 |
    |    5 | 刘玄德    |    55.5 | 75.0 |    45.0 |
    |    6 | 孙权      |    70.0 | 63.0 |    78.5 |
    |    7 | 宋公明    |    75.0 | 55.0 |    30.0 |
    |    7 | 宋公明    |    75.0 | 55.0 |    30.0 |
    |    8 | NULL      |    NULL | NULL |    NULL |
    +------+-----------+---------+------+---------+
    9 rows in set (0.00 sec)

    可以看到math列中的值都减了10

select 中涉及到的 where 子句和 order by 子句以及 limit 子句在 update 也一样能用,例如:

  • 将最前面三列记录英语成绩 减30

    mysql> update exam_result set english = english-30 limit 3;
    Query OK, 3 rows affected (0.00 sec)
    Rows matched: 3  Changed: 3  Warnings: 0
    
    mysql> select * from exam_result;
    +------+-----------+---------+------+---------+
    | id   | name      | chinese | math | english |
    +------+-----------+---------+------+---------+
    |    1 | 唐玄奘    |    67.0 | 88.0 |    26.0 |
    |    2 | 孙悟空    |    87.5 | 68.0 |    47.0 |
    |    3 | 猪悟能    |    88.0 | 88.5 |    60.0 |
    |    4 | 曹孟德    |    82.0 | 74.0 |    67.0 |
    |    5 | 刘玄德    |    55.5 | 75.0 |    45.0 |
    |    6 | 孙权      |    70.0 | 63.0 |    78.5 |
    |    7 | 宋公明    |    75.0 | 55.0 |    30.0 |
    |    7 | 宋公明    |    75.0 | 55.0 |    30.0 |
    |    8 | NULL      |    NULL | NULL |    NULL |
    +------+-----------+---------+------+---------+
    9 rows in set (0.00 sec)

 删除 ( delete )

语法

delete from <数据表名> [where <条件>] ;

说明

  • [ ]表示可选项

  • delete 删除记录关键字

  • 不加 where 子句则默认删除全部记录 ! ! !

示例

  1. 删除 name 列为 null 的记录

    mysql> delete from exam_result where name<=>null;
    Query OK, 1 row affected (0.00 sec)
    
    mysql> select * from exam_result;
    +------+-----------+---------+------+---------+
    | id   | name      | chinese | math | english |
    +------+-----------+---------+------+---------+
    |    1 | 唐玄奘    |    67.0 | 88.0 |    26.0 |
    |    2 | 孙悟空    |    87.5 | 68.0 |    47.0 |
    |    3 | 猪悟能    |    88.0 | 88.5 |    60.0 |
    |    4 | 曹孟德    |    82.0 | 74.0 |    67.0 |
    |    5 | 刘玄德    |    55.5 | 75.0 |    45.0 |
    |    6 | 孙权      |    70.0 | 63.0 |    78.5 |
    |    7 | 宋公明    |    75.0 | 55.0 |    30.0 |
    |    7 | 宋公明    |    75.0 | 55.0 |    30.0 |
    +------+-----------+---------+------+---------+
    8 rows in set (0.00 sec)
  2. 删除 id 列为 7 的记录

    mysql> delete from exam_result where id=7;
    Query OK, 2 rows affected (0.00 sec)
    
    mysql> select * from exam_result;
    +------+-----------+---------+------+---------+
    | id   | name      | chinese | math | english |
    +------+-----------+---------+------+---------+
    |    1 | 唐玄奘    |    67.0 | 88.0 |    26.0 |
    |    2 | 孙悟空    |    87.5 | 68.0 |    47.0 |
    |    3 | 猪悟能    |    88.0 | 88.5 |    60.0 |
    |    4 | 曹孟德    |    82.0 | 74.0 |    67.0 |
    |    5 | 刘玄德    |    55.5 | 75.0 |    45.0 |
    |    6 | 孙权      |    70.0 | 63.0 |    78.5 |
    +------+-----------+---------+------+---------+
    6 rows in set (0.00 sec)

    如果条件查找中找到的是重复项记录 , 会将所有满足条件的记录都会删除

  3. 不加 where 条件判断则默认删除所有记录

    mysql> delete from exam_result;
    Query OK, 6 rows affected (0.00 sec)
    
    mysql> select * from exam_result;
    Empty set (0.00 sec)

注意:条件筛选到哪些记录,就删除哪些,如果不写条件 那么默认全部删除(把表里的行全删除了) , 所以在删除记录时一般都会加上 where 条件

 本章到此结束,如果文中有写的不对或不懂的地方,欢迎评论区讨论,谢谢!

猜你喜欢

转载自blog.csdn.net/qq_45270751/article/details/121311701