From e30e6acb787cd259f90dee9ffe043c2c3905f9b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=82=96=E9=A3=9E=E9=B9=8F?= <2879562915@qq.com> Date: Thu, 17 Oct 2024 23:24:51 +0800 Subject: [PATCH 1/3] update --- ...\347\273\217\345\205\27050\351\242\230.md" | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git "a/\345\217\266\345\212\237\347\205\247/20241013 Mysql\347\273\217\345\205\27050\351\242\230.md" "b/\345\217\266\345\212\237\347\205\247/20241013 Mysql\347\273\217\345\205\27050\351\242\230.md" index c876f86..65364e6 100644 --- "a/\345\217\266\345\212\237\347\205\247/20241013 Mysql\347\273\217\345\205\27050\351\242\230.md" +++ "b/\345\217\266\345\212\237\347\205\247/20241013 Mysql\347\273\217\345\205\27050\351\242\230.md" @@ -442,16 +442,52 @@ select * from student where s_name like '%风%'; 30 查询同名同性的学生名单,并统计同名人数 +```sql +? ? ? ? +``` + 31 查询 1990 年出生的学生信息 +```sql +select * from student where s_birth >='1990-01-01'; +``` + 32 查询每门课程的平均成绩,结果按平均成绩降序排列;平均成绩相同时,按课程编号 c_id 升序排列 +```sql +with -- CTE +a as (select c_id,avg(s_score) 平均成绩 from score group by c_id order by avg(s_score) desc) -- 平均成绩不相同时 +select c_name,a.平均成绩 from course,a where a.c_id=course.c_id; +------------------------------------------------------------------------------------------------------------------- +with -- CTE +a as (select c_id,avg(s_score) 平均成绩 from score group by c_id order by c_id asc) -- 平均成绩不同时 +select c_name,a.平均成绩 from course,a where a.c_id=course.c_id; +``` + 33 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 +```sql +with -- CTE +a as (select s_id,avg(s_score) 平均成绩 from score group by s_id having 平均成绩 >= 85) +select s.s_id,s_name,a.平均成绩 from student s,a where a.s_id = s.s_id; +``` + 34 查询课程名称为数学,且分数低于 60 的学生姓名和分数 +```sql +with -- CTE +a as (select s_id,s_score from score where c_id=(select c_id from course where c_name='数学') and s_score<60) -- 子查询作为列当条件 +select s_name,a.s_score from student s,a where s.s_id = a.s_id; +``` + 35 查询所有学生的课程及分数情况 +```sql +with -- CTE +a as (select student.s_name,score.c_id,score.s_score from score right join student on score.s_id=student.s_id) +select s_name,c.c_name,a.s_score from course c right join a on a.c_id=c.c_id; +``` + 36 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 37 查询不及格的课程 -- Gitee From c2720e455bff5d047c23471d3b37f415b2179924 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B6=E5=8A=9F=E7=85=A7?= <3210416196@qq.com> Date: Fri, 18 Oct 2024 11:42:13 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20241018 \347\264\242\345\274\225.md" | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 "\345\217\266\345\212\237\347\205\247/20241018 \347\264\242\345\274\225.md" diff --git "a/\345\217\266\345\212\237\347\205\247/20241018 \347\264\242\345\274\225.md" "b/\345\217\266\345\212\237\347\205\247/20241018 \347\264\242\345\274\225.md" new file mode 100644 index 0000000..c676e08 --- /dev/null +++ "b/\345\217\266\345\212\237\347\205\247/20241018 \347\264\242\345\274\225.md" @@ -0,0 +1,100 @@ +# 笔记 + +- ## 创建普通索引 + + ```sql + create index 索引名 on 表名(字段名); + ``` + +- ## 创建联合索引 + + ```sql + create index 索引名 on 表名(字段名1,字段名2); + ``` + +- ## 查看创建的索引 + + ```sql + show index from 表名; + ``` + +- ## 查看sql语句使用标记到索引 + + ```sql + explain sql语句; + ``` + +- ## 删除索引 + + ```sql + drop index 索引名 from 表名; + ``` + +# 练习与作业 + +- ### -- 1.给emp分别建立 普通索引和唯一索引 + + ```sql + create index index_job on emp(job); -- 普通索引 + create unique index index_ename on emp(ename); -- 唯一索引 + ``` + +- ### -- 2.查询emp表有哪些索引 + + ```sql + show index from emp; + ``` + +- ### -- 3. 使用有索引的字段进行查询,再查看这条语句是否使用到了索引。 + + ```sql + explain select * from emp where ename = '甘宁'; + explain select * from emp where ename like '%甘%'; -- 没有遵循最左原则导致索引失效 + explain select * from emp where ename like '甘%'; -- 遵循最左原则索引没有失效 + ``` + +- ### -- 4. 删除前面建立的两个索引 + + ```sql + drop index index_ename on emp; + drop index index_job on emp; + ``` + +- ### -- 5. 选择两个字段添加一个复合索引 + + ```sql + create index index_ename_job on emp(ename,job); + ``` + +- ### -- 6. 使用复合索引的字段进行查询 + + ```sql + select * from emp where ename='刘备' and job ='经理'; + + explain select * from emp where ename='刘备' and job ='经理'; -- 查看是否用到复合索引 + ``` + +- ### -- 想办法用自己的电脑,生成500万行数据,id,uname,age 尽量随机,并记录时间。 + + ```sql + create procedure pro_addNumberData(in num int) + begin + declare temp_age int; + declare temp_ename varchar(255); + declare t_num int default 0; + a:loop + if(t_num >= num) then leave a; + end if; + select round(rand()*100+1) into temp_age; + select MD5(rand()) into temp_ename; + insert into cs(uname,age) values(temp_ename,temp_age); + set t_num=t_num+1; + end loop; + end; + + call pro_addNumberData(5000000); + ``` + +- ### -- 1. 不用索引查询 一次姓名uname /并记录时间 + +- ### -- 2. 建立索引查询 一次姓名uname /并记录时间 \ No newline at end of file -- Gitee From 4046cae7060196c69fd078eed641d6084251bb72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=82=96=E9=A3=9E=E9=B9=8F?= <2879562915@qq.com> Date: Fri, 18 Oct 2024 17:06:54 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20241018 \347\264\242\345\274\225.md" | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git "a/\345\217\266\345\212\237\347\205\247/20241018 \347\264\242\345\274\225.md" "b/\345\217\266\345\212\237\347\205\247/20241018 \347\264\242\345\274\225.md" index c676e08..d8187af 100644 --- "a/\345\217\266\345\212\237\347\205\247/20241018 \347\264\242\345\274\225.md" +++ "b/\345\217\266\345\212\237\347\205\247/20241018 \347\264\242\345\274\225.md" @@ -95,6 +95,14 @@ call pro_addNumberData(5000000); ``` + ![image-20241018143520967](https://gitee.com/ygzdegitee/img/raw/master/imgs/202410181435167.png) + - ### -- 1. 不用索引查询 一次姓名uname /并记录时间 -- ### -- 2. 建立索引查询 一次姓名uname /并记录时间 \ No newline at end of file +- ![image-20241018163538978](https://gitee.com/ygzdegitee/img/raw/master/imgs/202410181635127.png) + +- ### -- 2. 建立索引查询 一次姓名uname /并记录时间 + + ![image-20241018170552284](https://gitee.com/ygzdegitee/img/raw/master/imgs/202410181705337.png) + + ![image-20241018170615526](https://gitee.com/ygzdegitee/img/raw/master/imgs/202410181706572.png) \ No newline at end of file -- Gitee