From d57d130af83599d1bb91fc89a243afa3ec4e92e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=96=87=E6=9D=B0?= <3023096670@qq.com> Date: Thu, 23 Mar 2023 18:56:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9D=8E=E6=96=87=E6=9D=B0=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230305\346\237\245\350\257\242.md" | 0 .../20230323mysql\345\244\215\344\271\240.md" | 331 ++++++++++++++++++ ...72\345\272\223\345\273\272\350\241\250.md" | 0 3 files changed, 331 insertions(+) rename "25 \346\235\216\346\226\207\346\235\260/20230305.md" => "25 \346\235\216\346\226\207\346\235\260/20230305\346\237\245\350\257\242.md" (100%) create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230323mysql\345\244\215\344\271\240.md" rename "25 \346\235\216\346\226\207\346\235\260/2023222\344\275\234\344\270\232.md" => "25 \346\235\216\346\226\207\346\235\260/2023222\345\273\272\345\272\223\345\273\272\350\241\250.md" (100%) diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230305.md" "b/25 \346\235\216\346\226\207\346\235\260/20230305\346\237\245\350\257\242.md" similarity index 100% rename from "25 \346\235\216\346\226\207\346\235\260/20230305.md" rename to "25 \346\235\216\346\226\207\346\235\260/20230305\346\237\245\350\257\242.md" diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230323mysql\345\244\215\344\271\240.md" "b/25 \346\235\216\346\226\207\346\235\260/20230323mysql\345\244\215\344\271\240.md" new file mode 100644 index 0000000..3c7de2a --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230323mysql\345\244\215\344\271\240.md" @@ -0,0 +1,331 @@ +```sql +/*0.创建数据库练习 + 1.直接创建数据库db3 + 2.判断是否存在并创建数据库db4 + 3.查看所有的数据库 + 4.删除db3数据库 + 5.查看正在使用的数据库 + 6.使用db4数据库*/ +create database if not exists db4; +show databases; +drop database db3; +select database(); +use db4; + + +​ +​ +​ + + /*1.创建表练习 + +- 需求:设计一张学生表,请注重数据类型、长度的合理性 + + 1.编号 + 2.姓名,姓名最长不超过10个汉字 + 3.性别,因为取值只有两种可能,因此最多一个汉字 + 4.生日,取值为年月日 + 5.入学成绩,小数点后保留两位 + 6.邮件地址,最大长度不超过 64 + 7.家庭联系电话,不一定是手机号码,可能会出现 - 等字符 + 8.学生状态(用数字表示,正常、休学、毕业...)*/ + create table student( + id int, + name varchar(10), + sex varchar(1), + age date, + coret double(5,2), + address varchar(64), + contacts varchar(10), + state varchar(9) + ); + +-- 2.更改表练习 + -- 1.修改stu表名为student + rename table student to stu; + -- 2.给学生表添加一列remark,类型为varchar(20) + alter table stu add remark varchar(20); + -- 3.修改remark列的类型是varchar(100) + alter table stu modify remark varchar(100); + -- 4.修改remark列的名变为intro,类型varchar(30) + alter table stu change remark intro varchar(30); + -- 5.删除intro列 + alter table stu drop intro; + + + + +-- 3.添加数据 + -- 1.创建表 + create table if not exists student( + id int, + name varchar(10), + age int, + sex varchar(1), + coret double(5,2), + address varchar(64), + contacts varchar(10), + state varchar(9) + ); + alter table student add sexx enum('男','女'); + -- 2.插入部分数据,给id name age sex赋值 + insert into student(id,name,age,sex) values + (1,'张三',48,'男'); + + /* + 1.在mysql中插入字符数据,数据使用单引号或者双引号都可以,建议使用单引号 + 2.在mysql中插入的数据是date类型,那么将数据书写在单引号或者双引号都可以 + */ + + -- 3.插入所有的字段,不写字段名 + insert into student values + (2,'李四',57,'男',45.43,'北京故宫','68768-89','1'); + -- 4.插入所有的字段,写出字段名 + insert into student(id,name,age,sex,coret,address,contacts,state) values + (3,'李四',18,'男',45.43,'北京故宫','68768-89','1'); + -- 5.批量插入数据 + insert into student VALUES + (4,'李四',16,'男',45.43,'北京故宫','68768-89','1'), + (5,'李四',23,'男',45.43,'北京故宫','68768-89','1'), + (6,'李四',23,'男',45.43,'北京故宫','68768-89','1'); + -- 插入数据注意 + -- 6.插入数据时 省略列名,那么如果不需要给列赋值那么使用null + + insert into student VALUES + (7,'李四',12,'男',45.43,null,'68768-89','1'); + + + + + +-- 4.修改数据 + + -- 1.修改数据:修改性别都变为女 + update student set sex='女'; + -- 2.修改数据:带条件修改id是2的学生性别变为男 + update student set sex='男' where id=2; + -- 3.一次修改多个列,把id为3的学生,年龄改成26岁,address改成北京 + + update student set age=26,address='北京' where id=3; + + + +-- 5.删除数据 + -- 1.带条件删除数据,删除id是3的记录 + delete from student where id=3; + -- 2.带条件删除数据,删除id是1和2的记录 id in(1,2) : 表示id的值是1或者2 + delete from student where id=1 or id=2; + delete from student where idin(1,2); + -- 3.不带条件删除 + -- 还可以使用:DDL + -- 这里的table可以省略 + + + + + +-- 6.查询数据 + -- 1.查询多个字段 + select id,name,age from student; + -- * 表示所有字段 + select * from student; + -- 2.写出查询每列的名称 +desc student; + -- 3.查询表中name和age列,name列的别名为姓名,age列的别名为年龄 + select name 姓名,age 年龄 from student; + -- 使用as起给字段和表起别名 + select id as 编号,name AS 姓名,age as 年龄 from student AS 学生表; + -- as可以省略 + + + -- 4.去重 + -- 对age去重 + +select * from student GROUP BY age; + + -- 对age和address去重 + select * from student GROUP BY age,address; + -- 如果多个字段一起去重,如果所有的字段值全部一样才会去重 + + + + +-- 7.带条件的查询 +create table score( + id int, + shu int, + ying int +); +insert into score values + (1,48,74), + (2,89,79), + (3,95,44), + (4,45,97), + (5,86,78), + (6,56,86), + (7,34,67); + + -- 1.查询数学成绩大于80学生 + +select * from score where shu>80; + -- 2.查询英语成绩小于等于80学生 +select * from score where ying<80; + + -- 3.查询年龄等于20的学生 + +select * from student where age=20; + -- 4.查询年龄不等于20的学生 +select * from student where age!=20; + + -- 5.查询年龄大于35并且性别是男的学生 + +select * from where age>35 and sex='男'; + -- 6.查询年龄大于35或者性别是男的学生 +select * from where age<35 and sex='男'; + + -- 7.查询id是1 3 5的学生 + +select * from student where id in (1,3,5); + + -- 8.查询id不是1 3 5的学生 + select * from student where id not in (1,3,5); + + -- not in 是in的取反,表示不在什么其中 + + -- 范围查询 + -- 9.英语成绩在75和90之间 + +select * from score where ying between 75 and 90;; + + + -- 模糊查询 使用like + -- 10.查询姓赵的学生 + select * from student where name like '赵%'; + -- name like '赵%' 表示name的值以赵开始,后面是什么都可以 + -- %在模糊查询中表示任意个数的字符 + + + -- 11.查询包含岩 + -- name like '%岩%' 表示name的值只要含有岩即可,前后%表示匹配任意多个字符 + select * from student where name like '%岩%'; + + + -- 12.查询姓赵并且是三个字的学生 + select * from student where name like '赵__'; + + -- _下划线在模糊查询中表示单个字符 + + + -- 排序 + -- desc表示降序 + -- 13.按照年龄降序排序 + +select * from student order by age desc; + -- 14.先按照年龄降序排序,如果年龄相同在按照数学降序排序 +select * from student left join score on student.id=score.id order by age desc,shu desc; + + -- 15.先按照年龄降序排序,如果年龄相同在按照数学升序排序 + +select * from student left join score on student.id=score.id order by age desc,shu asc; + + + -- 聚合函数 + -- 16.查询学生总数即有多少名学生 + +select count(id) from student; + -- 17.查询数学成绩总分数 +select sum(shu) from score; + -- 18.查询数学成绩最高分数 +select max(shu) from score; + -- 19.统计数学和英语总和值 +select sum(shu)+sum(ying) from score; + -- 实现一:分别统计数学和英语的每个总成绩,然后进行相加 +select sum(shu),sum(ying),sum(shu)+sum(ying) from score; +insert into score VALUES +(8,null,78); + + + + -- 实现二:分别统计每个人的数学和英语成绩,然后将每个人的数学和英语成绩相加 + select id,shu,ying,shu+ying from score; + + /* + 结果是:380 少了柳岩的90 + sum(math+english): + 问题原因: + +-- 因为sum(math+english)是两科成绩的总和,而柳岩成绩可能就一科, +所以使用sum(math+english)不算柳岩的成绩 + + + 解决上述问题:使用mysql自带函数:ifnull(字段值,默认值) + 说明: + ifnull(字段值,默认值) + 如果字段值是null,那么使用默认值作为ifnull函数的整体结果 + 如果字段值不是null,那么使用字段值作为ifnull函数的整体结果 + 举例: + ifnull(english,0): + 1)假设english的值是null===>ifnull(null,0)===>整体结果是0 + 2)假设english的值是80===>ifnull(80,0)===>整体结果是80 + */ + select sum(shu),sum(ying),sum(ifnull(shu,0)+ifnull(ying,0)) from score; + + + + +​ + + -- 分组 + -- 准备数据 + create table car( + id int, + color char(2), + price float + ); + + insert into car(id,color,price) values(1,'黄色',16); + insert into car(id,color,price) values(2,'黄色',16); + insert into car(id,color,price) values(3,'蓝色',5); + insert into car(id,color,price) values(4,'红色',60); + insert into car(id,color,price) values(5,'白色',8); + insert into car(id,color,price) values(6,'红色',60); + + -- 1.查询每种颜色车辆的总价 + +select color,price from car GROUP BY color; + -- 2.查询每种颜色车辆总价大于30车辆的颜色和总价 +select color,price from car GROUP BY color having price>30; + -- 3.注意:分组查询的结果最好是分组字段和聚合函数,不要是其他字段 + -- 4.where后面不能使用聚合函数 报错的 + -- 小结:where是在分组前筛选,having是在分组后筛选 + -- 5.查询车的单价大于15的每种颜色车辆总价大于30车辆的颜色和总价 +select color,price,sum(price) from car where price>15 GROUP BY color having sum(price)>30; + + + -- 分页 + -- 1.分页查询数据,每页显示2条数据 + -- 第一页 + -- 0 表示起始索引,对应第一行数据 + -- 2表示每页显示2条数据 + select * from car ORDER BY id limit 0,2; + + + +​ -- 当起始索引是0的情况下可以不写索引0,写法如下, +​ -- 下面的2表示每页显示的数据行数 +​ select * from car ORDER BY id limit 2; + +​ -- 第二页 +​ -- 第一个2表示起始索引,对应三行数据 +​ -- 第二个2表示每页显示两条数据,对于第二个参数每个公司基本是固定的,最后一页剩下几行数据 +​ -- 就会显示几行数据 + +​ -- 第三页 +​ /* +​ 公式: 起始索引 = (当前页码-1)*每页显示的条数 +​ 0 1 2 +​ 2 2 2 +​ 4 3 2 +​ */ +``` + diff --git "a/25 \346\235\216\346\226\207\346\235\260/2023222\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/2023222\345\273\272\345\272\223\345\273\272\350\241\250.md" similarity index 100% rename from "25 \346\235\216\346\226\207\346\235\260/2023222\344\275\234\344\270\232.md" rename to "25 \346\235\216\346\226\207\346\235\260/2023222\345\273\272\345\272\223\345\273\272\350\241\250.md" -- Gitee