【MySQL】—— 数据库 增删查改 练习(基础)
  TEZNKK3IfmPf 2023年11月14日 18 0

(目录)


增加

建表

mysql> create database test_exam;
Query OK, 1 row affected (0.00 sec)

mysql> use test_exam;
Database changed
mysql> create table exam (
    -> id int,
    -> name varchar(20),
    -> chinese decimal(3,1),
    -> math decimal(3,1),
    -> english decimal(3,1)
    -> );
Query OK, 0 rows affected (0.02 sec)

多行数据 全列插入

mysql> insert into exam values
    -> (1, '刘备', 90.1, 70.3, 40.9),
    -> (2, '关羽', 90.1, 60.3, 30.8),
    -> (3, '大乔', 99, 70.3, 30.8),
    -> (4, '吕布', 80.5, 50.3, 70.2),
    -> (5, '貂蝉', 80.5, 87.6, 90.3),
    -> (6, '孙尚香', 99, 12.1, 10),
    -> (7, '小乔', 98, 99, 4.3),
    -> (8, '孙权', 85, 77, 87);
Query OK, 8 rows affected (0.01 sec)
Records: 8  Duplicates: 0  Warnings: 0

多行数据 指定列插入

mysql> insert into exam (id, name) values (9, '孙策'), (10, '刘禅');
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

查询

全列查询

  • 注意:这种方式只限测试方式查询,千万不能再生产环境上的服务器执行这样的sql

最基础的查找,全列查找,直接把一个表所有的列,和所有的行都查询出来~ 语法:select * from 表名* 叫做"通配符",表示一个表的所有列

mysql> select * from exam;
+------+-----------+---------+------+---------+
| id   | name      | chinese | math | english |
+------+-----------+---------+------+---------+
|    1 | 刘备      |    90.1 | 70.3 |    40.9 |
|    2 | 关羽      |    90.1 | 60.3 |    30.8 |
|    3 | 大乔      |    99.0 | 70.3 |    30.8 |
|    4 | 吕布      |    80.5 | 50.3 |    70.2 |
|    5 | 貂蝉      |    80.5 | 87.6 |    90.3 |
|    6 | 孙尚香    |    99.0 | 12.1 |    10.0 |
|    7 | 小乔      |    98.0 | 99.0 |     4.3 |
|    8 | 孙权      |    85.0 | 77.0 |    87.0 |
|    9 | 孙策      |    NULL | NULL |    NULL |
|   10 | 刘禅      |    NULL | NULL |    NULL |
+------+-----------+---------+------+---------+
10 rows in set (0.00 sec)

把一个表里面的所有行和所有列都查找出到,查找的结果是一个“临时表”,之前我们说的 数据库 的表都是在硬盘上的,而此处的这个查询结果 临时表 ,不是在硬盘上,而是在内存中,随着进行了输出之后,数据也就被释放了~ 注意:

select * from 表名 这个操作也是一个 危险操作! 如果在生产环境的服务器上,执行,很可能带来灾难!所以我们以后出去工作的时候一i的那个要注意!!!


指定列查询

语法:select 列名,列名.... from 表名;

image-20220426160652220

查询字段为表达式

mysql> select id, name, english + 10 from exam;
+------+-----------+--------------+
| id   | name      | english + 10 |
+------+-----------+--------------+
|    1 | 刘备      |         50.9 |
|    2 | 关羽      |         40.8 |
|    3 | 大乔      |         40.8 |
|    4 | 吕布      |         80.2 |
|    5 | 貂蝉      |        100.3 |
|    6 | 孙尚香    |         20.0 |
|    7 | 小乔      |         14.3 |
|    8 | 孙权      |         97.0 |
|    9 | 孙策      |         NULL |
|   10 | 刘禅      |         NULL |
+------+-----------+--------------+
10 rows in set (0.00 sec)
mysql> select id, name, chinese + math + english from exam;
+------+-----------+--------------------------+
| id   | name      | chinese + math + english |
+------+-----------+--------------------------+
|    1 | 刘备      |                    201.3 |
|    2 | 关羽      |                    181.2 |
|    3 | 大乔      |                    200.1 |
|    4 | 吕布      |                    201.0 |
|    5 | 貂蝉      |                    258.4 |
|    6 | 孙尚香    |                    121.1 |
|    7 | 小乔      |                    201.3 |
|    8 | 孙权      |                    249.0 |
|    9 | 孙策      |                     NULL |
|   10 | 刘禅      |                     NULL |
+------+-----------+--------------------------+
10 rows in set (0.00 sec)

注意: 临时表中的结果的数据类型不一定和原始的表的数据类型完全一致,临时表的类型会自动的适应,保证计算结果是i正确的


查询字段 名字重定义

mysql> select id, name, chinese + math + english as totle from exam;
+------+-----------+-------+
| id   | name      | totle |
+------+-----------+-------+
|    1 | 刘备      | 201.3 |
|    2 | 关羽      | 181.2 |
|    3 | 大乔      | 200.1 |
|    4 | 吕布      | 201.0 |
|    5 | 貂蝉      | 258.4 |
|    6 | 孙尚香    | 121.1 |
|    7 | 小乔      | 201.3 |
|    8 | 孙权      | 249.0 |
|    9 | 孙策      |  NULL |
|   10 | 刘禅      |  NULL |
+------+-----------+-------+
10 rows in set (0.00 sec)

去重 distinct

  • 使用去重distinct时,所有的列都必须放在distinct的后面

语法select distinct 列名 from 表名;

mysql> select distinct chinese from exam;
+---------+
| chinese |
+---------+
|    90.1 |
|    99.0 |
|    80.5 |
|    98.0 |
|    85.0 |
|    NULL |
+---------+
6 rows in set (0.01 sec)

排序 order by

  • asc升序 desc降序 什么都不加 默认asc

语法select 列名... from 表名 order by 列名 asc/desc;

查询同学及总分,由高到低

-- 查询同学及总分,由高到低
mysql> select id, name, chinese + math + english as totle from exam order by totle desc;
+------+-----------+-------+
| id   | name      | totle |
+------+-----------+-------+
|    5 | 貂蝉      | 258.4 |
|    8 | 孙权      | 249.0 |
|    1 | 刘备      | 201.3 |
|    7 | 小乔      | 201.3 |
|    4 | 吕布      | 201.0 |
|    3 | 大乔      | 200.1 |
|    2 | 关羽      | 181.2 |
|    6 | 孙尚香    | 121.1 |
|    9 | 孙策      |  NULL |
|   10 | 刘禅      |  NULL |
+------+-----------+-------+
10 rows in set (0.00 sec)

查询同学各门成绩,依次按 数学降序,英语升序,语文升序的方式显示

-- 查询同学各门成绩,依次按 数学降序,英语升序,语文升序的方式显示
mysql> select * from exam order by chinese desc, math desc, english desc;
+------+-----------+---------+------+---------+
| id   | name      | chinese | math | english |
+------+-----------+---------+------+---------+
|    3 | 大乔      |    99.0 | 70.3 |    30.8 |
|    6 | 孙尚香    |    99.0 | 12.1 |    10.0 |
|    7 | 小乔      |    98.0 | 99.0 |     4.3 |
|    1 | 刘备      |    90.1 | 70.3 |    40.9 |
|    2 | 关羽      |    90.1 | 60.3 |    30.8 |
|    8 | 孙权      |    85.0 | 77.0 |    87.0 |
|    5 | 貂蝉      |    80.5 | 87.6 |    90.3 |
|    4 | 吕布      |    80.5 | 50.3 |    70.2 |
|    9 | 孙策      |    NULL | NULL |    NULL |
|   10 | 刘禅      |    NULL | NULL |    NULL |
+------+-----------+---------+------+---------+
10 rows in set (0.00 sec)


条件查询

运算符

  1. >, >=, <, <= 大于,大于等于,小于,小于等于
  2. = 等于,NULL 不安全,例如 NULL = NULL 的结果是 NULL
  3. <=> 等于,NULL 安全,例如 NULL <=> NULL 的结果是 TRUE(1)
  4. !=, <> 不等于
  5. BETWEEN a0 AND a1范围匹配,[a0, a1],如果 a0 <= value <= a1,返回 TRUE(1)
  6. IN (option, ...) 如果是 option 中的任意一个,返回TRUE(1)
  7. IS NULL 是 NULL
  8. IS NOT NULL 不是 NULL
  9. LIKE模糊匹配 % 表示任意多个(包括 0 个)任意字符; _ 表示任意一个字符
  10. AND 多个条件必须都为 TRUE(1),结果才是 TRUE(1)
  11. OR 任意一个条件为 TRUE(1), 结果为 TRUE(1)
  12. NOT 条件为 TRUE(1),结果为 FALSE(0)

selcet 的后面加上一个where 字句,后面跟上一个具体的筛选条件 语法;select 列名 from 表名 where 条件;

(>,>=,<,<=)这一组是比较熟悉的大小操作,= 表示比较相等,而不是赋值。注意:NULL = NULL 结果仍然是 NULL,条件是不成立的。 (<=>)也是比较相等,用法和 = 基本一直,只是 使用 <=> 比较空值结果是 真。NULL <=> NULL 进行比较,结果就是真。

  1. where条件可以使用表达式,但是不能使用别名
  2. and 的优先级高于 or 在同时使用时,需要使用小括号()包裹优先执行的部分

比较查询

-- 查询英语不及格的同学及英语成绩 ( < 60 )
mysql> select id, name, english from exam where english < 60;
+------+-----------+---------+
| id   | name      | english |
+------+-----------+---------+
|    1 | 刘备      |    40.9 |
|    2 | 关羽      |    30.8 |
|    3 | 大乔      |    30.8 |
|    6 | 孙尚香    |    10.0 |
|    7 | 小乔      |     4.3 |
+------+-----------+---------+
5 rows in set (0.00 sec)

-- 查询语文成绩好于英语成绩的同学
mysql> select id, name, chinese, english from exam where chinese > english;
+------+-----------+---------+---------+
| id   | name      | chinese | english |
+------+-----------+---------+---------+
|    1 | 刘备      |    90.1 |    40.9 |
|    2 | 关羽      |    90.1 |    30.8 |
|    3 | 大乔      |    99.0 |    30.8 |
|    4 | 吕布      |    80.5 |    70.2 |
|    6 | 孙尚香    |    99.0 |    10.0 |
|    7 | 小乔      |    98.0 |     4.3 |
+------+-----------+---------+---------+
6 rows in set (0.00 sec)

-- 查询总分在 200 分以下的同学
mysql> select id, name, chinese + math + english as totle from exam where chinese + math + english < 200;
+------+-----------+-------+
| id   | name      | totle |
+------+-----------+-------+
|    2 | 关羽      | 181.2 |
|    6 | 孙尚香    | 121.1 |
+------+-----------+-------+
2 rows in set (0.00 sec)

and与or

-- 查询语文成绩大于80分,且英语成绩大于80分的同学
mysql> select id, name, chinese, english from exam where chinese > 80 and english > 80;
+------+--------+---------+---------+
| id   | name   | chinese | english |
+------+--------+---------+---------+
|    5 | 貂蝉   |    80.5 |    90.3 |
|    8 | 孙权   |    85.0 |    87.0 |
+------+--------+---------+---------+
2 rows in set (0.00 sec)


-- 查询语文成绩大于80分,或英语成绩大于80分的同学
mysql> select id, name, chinese, english from exam where chinese > 80 or english > 80;
+------+-----------+---------+---------+
| id   | name      | chinese | english |
+------+-----------+---------+---------+
|    1 | 刘备      |    90.1 |    40.9 |
|    2 | 关羽      |    90.1 |    30.8 |
|    3 | 大乔      |    99.0 |    30.8 |
|    4 | 吕布      |    80.5 |    70.2 |
|    5 | 貂蝉      |    80.5 |    90.3 |
|    6 | 孙尚香    |    99.0 |    10.0 |
|    7 | 小乔      |    98.0 |     4.3 |
|    8 | 孙权      |    85.0 |    87.0 |
+------+-----------+---------+---------+
8 rows in set (0.00 sec)

范围查询

-- 查询语文成绩在 [80, 90] 分的同学及语文成绩
mysql> select id, name, chinese from exam where chinese between 80 and 90;
+------+--------+---------+
| id   | name   | chinese |
+------+--------+---------+
|    4 | 吕布   |    80.5 |
|    5 | 貂蝉   |    80.5 |
|    8 | 孙权   |    85.0 |
+------+--------+---------+
3 rows in set (0.00 sec)

in 查询

-- 查询数学成绩是 99.0 或者 60.3 或者 70.3 的同学及数学成绩
mysql> select name, math from exam where math in (99.0, 60.3, 70.3);
+--------+------+
| name   | math |
+--------+------+
| 刘备   | 70.3 |
| 关羽   | 60.3 |
| 大乔   | 70.3 |
| 小乔   | 99.0 |
+--------+------+
4 rows in set (0.00 sec)

模糊查询 like

like 搭配通配符来使用,只要对方的字符串符合你此处的描述的一个形式就行. % 代表任意个任意字符~ _ 代表任意一个字符

此处这里的'孙%'就能匹配到任意 孙 开头的字符串,其他的类型于这样的名字也能匹配到. 比如: ,或者 孙sadsadasda 但是类似于 这种就匹配不到:aaa孙aaa,bbb孙

-- % 匹配任意多个(包括 0 个)字符
mysql> select id, name from exam where name like '孙%';
+------+-----------+
| id   | name      |
+------+-----------+
|    6 | 孙尚香    |
|    8 | 孙权      |
|    9 | 孙策      |
+------+-----------+
3 rows in set (0.00 sec)


-- _ 匹配严格的一个任意字符
mysql> select id, name from exam where name like '孙__';
+------+-----------+
| id   | name      |
+------+-----------+
|    6 | 孙尚香    |
+------+-----------+
1 row in set (0.00 sec)

mysql> select id, name from exam where name like '孙_';
+------+--------+
| id   | name   |
+------+--------+
|    8 | 孙权   |
|    9 | 孙策   |
+------+--------+
2 rows in set (0.00 sec)


-- 语文成绩以9开头并且姓为孙
mysql> select id, name, chinese from exam where name like '孙%' and chinese like '9%';
+------+-----------+---------+
| id   | name      | chinese |
+------+-----------+---------+
|    6 | 孙尚香    |    99.0 |
+------+-----------+---------+
1 row in set (0.00 sec)

null查询

语法:select 列名 from 表名 where 条件 查找语文成绩为空的同学信息: 在这里插入图片描述

注意:这里 比较 NULL 是不能用 = 的, = 是无法比较的,结果是 假,如果要比较空 只能用 <=> 比较

或者 这样写 也可以

在这里插入图片描述

-- 查成绩为null的同学
mysql> select id, name, chinese + math + english as totle from exam where chinese + math + english <=> null;
+------+--------+-------+
| id   | name   | totle |
+------+--------+-------+
|    9 | 孙策   |  NULL |
|   10 | 刘禅   |  NULL |
+------+--------+-------+
2 rows in set (0.00 sec)

分页查询

SQL中可以通过 limit 来实现分页查询 语法:select 列名 from 表名 limt 值(数字)

-- 查询成绩前三 0-3
mysql> select * from exam order by chinese + math + english desc limit 3;
+------+--------+---------+------+---------+
| id   | name   | chinese | math | english |
+------+--------+---------+------+---------+
|    5 | 貂蝉   |    80.5 | 87.6 |    90.3 |
|    8 | 孙权   |    85.0 | 77.0 |    87.0 |
|    1 | 刘备   |    90.1 | 70.3 |    40.9 |
+------+--------+---------+------+---------+
3 rows in set (0.00 sec)


-- 查询成绩4~6 4-6
-- offset 可以想成数组下标一样
mysql> select * from exam order by chinese + math + english desc limit 3 offset 3;
+------+--------+---------+------+---------+
| id   | name   | chinese | math | english |
+------+--------+---------+------+---------+
|    7 | 小乔   |    98.0 | 99.0 |     4.3 |
|    4 | 吕布   |    80.5 | 50.3 |    70.2 |
|    3 | 大乔   |    99.0 | 70.3 |    30.8 |
+------+--------+---------+------+---------+
3 rows in set (0.00 sec)


-- 查询  limit  或者 offset  过大也不会报错
mysql> select * from exam order by chinese + math + english desc limit 300 offset 300;
Empty set (0.00 sec)

image-20220426155815519


修改

语法:update 表名 set 列名 = 值,列名 = 值 where 条件 注意:这里的 set不能省略的,where 条件—指的是针对哪些进行修改,符合条件的就会修改,不符合条件的就不变,如果这里的 where 省略了,就是修改所有记录. update 是会修改数据库 服务器 上面原始数据的!

-- 将孙权同学的数学成绩变更为 80 分
-- Rows matched 表示where涉及到几行  Changed: 1表示改了几行 Warnings: 0表示出错多少
mysql> update exam set math = 80 where name = '孙权';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0


-- 将总成绩倒数前三的 3 位同学的数学成绩加上 10 分
mysql> update exam set math = math + 10 order by chinese + math + english asc limit 3;
Query OK, 1 row affected (0.00 sec)
Rows matched: 3  Changed: 1  Warnings: 0

删除

-- 删除 刘备的成绩
mysql> delete from exam where name = '刘备';
Query OK, 1 row affected (0.01 sec)

-- 删除整张表的数据
mysql> delete from test_exam;
Query OK, 0 rows affected (0.00 sec)

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月14日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2024年05月31日   24   0   0 mysql
  TEZNKK3IfmPf   2024年05月17日   48   0   0 sqlmysql
  TEZNKK3IfmPf   2024年05月31日   26   0   0 数据库mysql
  TEZNKK3IfmPf   2024年05月17日   45   0   0 查询mysql索引
  TEZNKK3IfmPf   2024年05月17日   50   0   0 jsonmysql
  TEZNKK3IfmPf   2024年05月17日   48   0   0 mysqlphp
  TEZNKK3IfmPf   2024年05月31日   25   0   0 数据库mysql
TEZNKK3IfmPf