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 c876f86cee2994e63e06fcc1c31cc77b0fc21c23..65364e6491f42e59095917588e93b2b37279cd57 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 查询不及格的课程 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 0000000000000000000000000000000000000000..d8187afcd5bb16f97de671a959c0403403f32b59 --- /dev/null +++ "b/\345\217\266\345\212\237\347\205\247/20241018 \347\264\242\345\274\225.md" @@ -0,0 +1,108 @@ +# 笔记 + +- ## 创建普通索引 + + ```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); + ``` + + ![image-20241018143520967](https://gitee.com/ygzdegitee/img/raw/master/imgs/202410181435167.png) + +- ### -- 1. 不用索引查询 一次姓名uname /并记录时间 + +- ![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