From 1a13cf002632bc4b24a6af37f4076ce3d0d11e01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=82=96=E9=A3=9E=E9=B9=8F?= <2879562915@qq.com> Date: Sun, 20 Oct 2024 23:59:36 +0800 Subject: [PATCH 1/2] update --- ...\347\273\217\345\205\27050\351\242\230.md" | 88 ++++++++++++++++++- 1 file changed, 86 insertions(+), 2 deletions(-) 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 65364e6..71ced83 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" @@ -375,7 +375,7 @@ b as (select distinct a.s_id,s_score from score,a where a.s_id=score.s_id and a. select s.*,b.s_score from student s,b where s.s_id=b.s_id; ``` -23 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60] 及所占百分比 +# 23 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60] 及所占百分比 ```sql select s_id,c_id,case @@ -440,7 +440,9 @@ select s_sex,count(s_id) from student group by s_sex; select * from student where s_name like '%风%'; ``` -30 查询同名同性的学生名单,并统计同名人数 +# 30 查询同名同性的学生名单,并统计同名人数 + + ```sql ? ? ? ? @@ -490,28 +492,110 @@ select s_name,c.c_name,a.s_score from course c right join a on a.c_id=c.c_id; 36 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 +```sql +with -- CTE +a as (select s_id,c_id,s_score from score where s_score >70), -- 筛选成绩在70分以上的学生 +b as (select s_id,c_name,s_score from course,a where course.c_id=a.c_id) +select s_name,c_name,s_score from student s,b where b.s_id=s.s_id; +``` + 37 查询不及格的课程 +```sql +with -- CTE +a as (select * from score where s_score <60) +select s_id,c_name,s_score from course c,a where a.c_id=c.c_id; +``` + 38 查询课程编号为 01 且课程成绩大于等于 80 的学生的学号和姓名 +```sql +with -- CTE +a as (select s_id from score where c_id=1 and s_score>=80) +select s.s_id,s_name from student s,a where a.s_id=s.s_id; +``` + 39 每门课程的学生人数 +```sql +with -- CTE +a as (select distinct c_id,count(s_id) over(partition by c_id) 学生人数 from score) -- 使用窗口函数统计选课人数时要使用distinct +select c_name 课程,a.学生人数 from course,a where course.c_id=a.c_id; +------------------------------------------------------------------------------------------------------------------- +with -- CTE +a as (select c_id,count(s_id) 学生人数 from score group by c_id) -- 使用聚合函数统计选课人数 +select c_name 课程,a.学生人数 from course,a where course.c_id=a.c_id; +``` + 40 查询选修“张三”老师所授课程的学生中,成绩最高的学生信息及其成绩 +```sql +with -- CTE +a as (select c_id,max(s_score) 最高分 from score group by c_id having c_id =(select c_id from course where t_id=(select t_id from teacher where t_name='张三'))), -- 查找张三老师教的课程的最高分 +b as (select s_id,s_score from score where c_id=(select c_id from course where t_id=(select t_id from teacher where t_name='张三'))), -- 查找选修张三老师的学生id和成绩 +c as (select s_id,a.最高分 from b,a where a.最高分=b.s_score) -- 找最高分和成绩相等的学生id +select s.*,c.最高分 from student s,c where s.s_id=c.s_id; +------------------------------------------------------------------------------------------------------------------- +with -- CTE 这种方法只能用于最高成绩不相等的时候也就是只有一个最高分的情况 +a as (select s_id,s_score from score,course +where score.c_id=course.c_id and score.c_id=(select c_id from course where t_id=(select t_id from teacher where t_name='张三')) order by s_score desc limit 1) -- 通过排序找出最高分之后有limit限制条数 +select s.*,s_score from student s,a where s.s_id=a.s_id; +``` + 41 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 +```sql +select distinct t.s_id,t.s_score,t.c_id from score t join score s on t.s_score=s.s_score where s.c_id != t.c_id +-- 自内连接找成绩相同的学生id并且课程不同 +``` + 42 查询每门功成绩最好的前两名 +```sql +with -- CTE +a as (select s_id,c_id,s_score,DENSE_RANK() over(partition by c_id order by s_score desc) sk from score) +-- 使用dense_rank()窗口函数按照课程分区课程成绩倒序 +select s.s_id,s_name,c_id,s_score from student s,a where s.s_id=a.s_id and a.sk <=2; -- 查找排名前两名的信息 +``` + 43 统计每门课程的学生选修人数(超过 5 人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列, 若人数相同,按课程号升序排列 +```sql +select c_id,count(s_id) from score group by c_id having count(s_id)>5 order by c_id asc; +``` + 44 检索至少选修两门课程的学生学号 +```sql +with -- CTE +a as (select s_id,count(c_id) from score group by s_id having count(c_id)>=2) -- 查找选修课程大于等于2的学生学号 +select distinct a.s_id from score,a where score.s_id=a.s_id; +------------------------------------------------------------------------------------------------------------------- +with -- CTE +a as (select s_id,count(c_id) over(partition by s_id) ss from score) -- 给count(id)使用别名 +select distinct s.s_id from score s,a where s.s_id=a.s_id and a.ss>=2; +``` + 45 查询选修了全部课程的学生信息 +```sql +with -- CTE +a as (select s_id,count(c_id) from score group by s_id having count(c_id)=(select count(c_id) from course)) -- 先统计有多少课程,再查找选修课程的数量是不是等于统计过后的课程数 +select s.* from student s,a where s.s_id=a.s_id; +``` + 46 查询各学生的年龄:按照出生日期来算,当前月日 < 出生年月的月日则,年龄减 1 +```sql +select s_id,case when month(s_birth) > month(now()) and day(s_birth) > day(now()) then (year(now())-year(s_birth))-1 else year(now())-year(s_birth) end from student; -- 使用case判断 +``` + 47 查询本周过生日的学生 +```sql +select s_id,day(s_birth) from student where month(s_birth) = month(now()) and day(s_birth)-day(now())<=7; -- 先确保学生的月份是在当前月份之后再确保学生的出生 +``` + 48 查询下周过生日的学生 49 查询本月过生的同学 -- Gitee From 12bfab9b6e8b6403f8abae7647e8e03321571a3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=82=96=E9=A3=9E=E9=B9=8F?= <2879562915@qq.com> Date: Mon, 21 Oct 2024 17:44:33 +0800 Subject: [PATCH 2/2] update --- ...15\344\271\240\345\244\247\347\272\262.md" | 261 ++++++++++++++++++ 1 file changed, 261 insertions(+) create mode 100644 "\345\217\266\345\212\237\347\205\247/20241021 \345\244\215\344\271\240\345\244\247\347\272\262.md" diff --git "a/\345\217\266\345\212\237\347\205\247/20241021 \345\244\215\344\271\240\345\244\247\347\272\262.md" "b/\345\217\266\345\212\237\347\205\247/20241021 \345\244\215\344\271\240\345\244\247\347\272\262.md" new file mode 100644 index 0000000..e6b442c --- /dev/null +++ "b/\345\217\266\345\212\237\347\205\247/20241021 \345\244\215\344\271\240\345\244\247\347\272\262.md" @@ -0,0 +1,261 @@ +# 数据库高级应用 + + + +- ## PowerDesigner + + 1. ### CDM(概念模型) + + 2. ### LDM(逻辑模型) + + 3. ### PDM(物理模型) + + 4. ### RBAC(角色权限控制)-- 通过给角色权限而修改用户权限 + + 5. ### SPU(是商品信息聚合的最小单位,切记是商品信息聚合不是当个信息) + + 6. ### SKU(库存进出计量的单位,由多个SPU构成) + +- ## VIEW(视图)-- 跟表的操作语句一样 + + 1. ### 创建视图 + + ```sql + create view v_name as select * from ... -- SQL语句 + ``` + + 2. ### 删除视图 + + ```sql + drop view [if exists] v_name; + ``` + + 3. ### 更新视图 + + ```sql + create or repeat view v_name as select * from ...; -- 新的SQL语句 修改 + ----------------------------------------------------------------------------- + insert into v_name values(num_1,num_2,...); -- 插入新数据到视图 新增/插入 + ``` + + 4. ### 查询视图 + + ```sql + select * from v_name; + ``` + +- ## PROCEDURE (存储过程) + + 1. ### 创建存储过程 + + ```sql + create procedure pro_name() + begin + -- 主体 + end; + ``` + + 2. ### 使用存储过程 + + ```sql + call pro_name; + ``` + + 3. ### 查看有多少存储过程 + + ```sql + show procedure status[like '%..%']; -- 可加like查看某张表中的存储过程 + ``` + + 4. ### 删除存储过程 + + ```sql + drop procedure pro_name; + ``` + +- ## 窗口函数 + + 1. ### 窗口函数语法 + + ```sql + select sum(字段) over([partition by 字段],[order by 字段]) from table_name; + ``` + + 2. ### 普通排序 + + ```sql + select row_number() over(order by 字段) from table_name; + ``` + + 3. ### 重复排名(不连续) + + ```sql + select rank() over(order by 字段) from table_name; + ``` + + 4. ### 重复排名(连续) + + ```sql + select dense_rank() over(order by 字段) from table_name; + ``` + + 5. ### LAG() + + ```sql + lag(column_expression,offest,default_value) over(order by 字段名) + -- column_expression:要获取的列或表达式 + -- offest:表示要获取的行数 + -- defalut_value:如果没有前面的行则返回默认值 + ``` + + 6. ### LEAD() + + ```sql + lead(column_expression,offest,default_value) over(order by 字段名) + -- column_expression:要获取的列或表达式 + -- offest:表示要获取的行数 + -- defalut_value:如果没有后面的行则返回默认值 + ``` + + 7. ### FIRST_VALUE() + + ```sql + first_value(字段名) over(partition by 字段名 order by 字段名 desc) from 表名 + ``` + + 8. ### LAST_VALUE() + + ```sql + first_value(字段名) over(partition by 字段名 order by 字段名) from 表名 + ``` + +- ## 自定义函数 + + 1. ### 定义一个函数 + + ```sql + create function fun_name(变量1 date_type,变量2 date_type) returns 返回类型 + deterministic + begin + -- 主体 + return 变量; -- 变量的数据类型要与returns的数据类型相同 + end; + ``` + + 2. ### 使用函数 + + ```sql + select fun_name; + ``` + + 3. ### 删除函数 + + ```sql + drop function fun_name; + ``` + +- ## 游标 + + 1. ### 创建游标 + + ```sql + declare cursor_name cursor for select * from ...; + ``` + + 2. ### 打开游标 + + ```sql + open cursor_name; + ``` + + 3. ### 获取数值 + + ```sql + fetch cursor_name into 变量1,变量2; -- 一行一行获取 + ``` + + 4. ### 关闭游标 + + ```sql + close cursor_name; + ``` + +- ## 触发器 + + 1. ### 创建触发器 + + ```sql + create trigger trigger_name before|after insert|update|delete on table_name for each row + ``` + + 2. ### 触发器报错 + + ```sql + signal sqlstate '45000' set message_text='文字'; -- 放在判断体中 + ``` + +- ## 事务 + + 1. ### 打开事务 + + ```sql + start transaction;|begin + ``` + + 2. ### 创建存储点 + + ```sql + savepoint savepoint_name; + ``` + + 3. ### 提交事务 + + ```sql + commit; + ``` + + 4. ### 回滚事务 + + ```sql + rollback; + ``` + +- ## 索引 + + 1. ### 创建普通索引 + + ```sql + create index index_name on table_name(字段); + ``` + + 2. ### 创建唯一索引 + + ```sql + create unique index index_name on table_name(字段); + ``` + + 3. ### 创建联合索引 + + ```sql + create index index_name on table_name(字段1,字段2); + ``` + + 4. ### 查看索引 + + ```sql + show index from table_name; + ``` + + 5. ### 查看sql是否用到索引 + + ```sql + explain sql语句; + ``` + + 6. ### 删除索引 + + ```sql + drop index index_name from table_name; + ``` + + \ No newline at end of file -- Gitee