diff --git "a/05 \350\260\242\351\223\226\346\265\251/20231012.md" "b/05 \350\260\242\351\223\226\346\265\251/20231012.md" new file mode 100644 index 0000000000000000000000000000000000000000..346d9904c7985ce0051f81339600fa9095196409 --- /dev/null +++ "b/05 \350\260\242\351\223\226\346\265\251/20231012.md" @@ -0,0 +1,95 @@ +作业 + +``` +-- 要求,循环部分,要用三种语法分别做一遍 +-- 写一个存储过程,可以输入一个整数,输入小于或等于0时,提示非法输入,并中止这个存储过程, +-- 否则先判断这个数和是不是大于20,如果大于20就从1循环到这个数,并找出所有的偶数(但遇到逢10的数要跳过)。小于等于20就提示数太小了,并退出。 + +drop procedure if exists pro; +-- 要求,循环部分,要用三种语法分别做一遍 + +-- 1.使用loop循环: +delimiter // +create procedure pro_loop(in num int) +a:begin + -- 1. 定义变量 + declare i int default 1; + -- 2. 判断,如果小于等于20 提示非法输入,并中止这个存储过程 + if num <= 0 then select '非法输入'; leave a; + ##小于等于20就提示数太小了,并退出 + elseif num <=20 then select '数太小了'; leave a; + #大于20,循环判断 + else + b:loop + # 如果是10的倍数,就跳出本次,继续循环 + if i % 10 = 0 then set i = i+1;iterate b; + #如果是2的倍数,就打印 + elseif i % 2 = 0 then select i; + end if; + set i = i+1; + # 退出循环 + if i > num then leave b; end if; + end loop; + end if; +end // +delimiter ; + +call pro_loop(-3); +call pro_loop(8); +call pro_loop(45); + +# 2.使用while循环: +delimiter // +create procedure pro_while(in num int) +a:begin + declare i int default 1; + if num <= 0 then select '非法输入'; leave a; + elseif num <=20 then select '数太小了'; leave a; + else + b:while i<=num do + if i % 10 = 0 then set i = i+1;iterate b; + elseif i % 2 = 0 then select i; + end if; + set i = i+1; + end while; + end if; +end // +delimiter ; + +call pro_while(-3); +call pro_while(3); +call pro_while(40); + +# 3.使用repeat循环: +delimiter // +create procedure pro_repeat(in num int) +a:begin + declare i int default 1; + if num <= 0 then select '非法输入 '; leave a; + elseif num <=20 then select '数太小了 '; leave a; + else + b:repeat + if i % 10 = 0 then set i = i+1;iterate b; + elseif i % 2 = 0 then select i; + end if; + set i = i+1; + until i>num + end repeat b; + end if; +end // +delimiter ; + +call pro_repeat(-3); +call pro_repeat(3); +call pro_repeat(40); +``` + +笔记 + +leave 语句 + +跳出循环,类似于break + +iterate 语句 + +只能用在循环语句中,表示重新开始循环 类似于continue ’再次循环‘ \ No newline at end of file diff --git "a/05 \350\260\242\351\223\226\346\265\251/20231013.md" "b/05 \350\260\242\351\223\226\346\265\251/20231013.md" new file mode 100644 index 0000000000000000000000000000000000000000..bb91a55bb79b57b663077a604d226f34ca2fcf64 --- /dev/null +++ "b/05 \350\260\242\351\223\226\346\265\251/20231013.md" @@ -0,0 +1,167 @@ +# 触发器 + +触发器(trigger)是与表有关的数据库对象,指在insert/update/delete之前(BEFORE)或之后(AFTER),触发并执行触发器中定义的SQL语句集合。 + +事件A 对user表新增一条数据 姓名name 年龄age 性别sex + +事件B 对userlogs记录一条user表的操作 new.name,new.age + +触发器的这种特性可以协助应用在数据库端确保数据的完整性 , 日志记录 , 数据校验等操作。 + +使用别名OLD和NEW来引用触发器中发生变化的记录内容,这与其他的数据库是相似的。**触发器只支持行级触发** + +### 触发器的类型 + +| 类型 | NEW和OLD | +| ------ | ---------------------------------------- | +| insert | new代表将要新增或已新增的数据 | +| update | old代表更新前的数据、new代表更新后的数据 | +| delete | old代表将要删除或已删除的数据 | + +### 语法 + +创建触发器 + +``` +create trigger 触发器名称 +before/after(触发时机) insert/update/delete(触发类型) +on 表名 for each row -- 行级触发器 +begin + 触发的语句... +end; +``` + +查看 + +``` +show triggers; +``` + +删除 + +``` +drop trigger 触发器名称; +``` + +### 案例 + +通过触发器记录 tb_user 表的数据变更日志,将变更日志插入到日志表user_logs中, 包含增、删、改 ; + +user_logs表结构 + +``` +create table user_logs( + id int(11) primary key auto_increment, + operation varchar(20) not null comment '操作类型, insert/update/delete', + operate_time datetime not null comment '操作时间', + operate_id int(11) not null comment '操作的ID', + operate_params varchar(500) comment '操作参数' +) +``` + +A.插入数据触发器 + +B.更新数据触发器 + +C.删除数据触发器 + +### 练习 + +模拟一个食品库存表,当食品采购时,记录采购信息并更改库存。 + +##### 食品库存表 (Food): + +food_id: 食品ID (主键) food_name: 食品名称 quantity: 食品数量 + +##### 食品采购记录表 (DeliveryLog): + +log_id: 采购记录ID (主键) food_id: 食品ID (外键) quantity: 采购数量 delivery_date: 采购日期 + +``` +drop table food; +create table food( +food_id int primary key auto_increment,-- 食品ID +food_name varchar(20),-- 食品名称 +quantity int-- 食品数量 +); + +insert into food values (null,'蛋糕','20'),(null,'冰淇淋','40'); +select * from food; +drop table DeliveryLog; +create table DeliveryLog( +log_id int primary key auto_increment,-- 采购记录ID (主键) +quantity int, -- 采购数量 +delivery_date date, -- 采购日期 +id int,-- 食品ID +foreign key (id) references food(food_id) -- 食品ID (外键) +); + +-- 模拟一个食品库存表,当食品采购时,记录采购信息并更改库存。 + +drop trigger test_1; +delimiter // +create trigger test_1 +after insert on food +for each row +begin +-- 定义num,cou 两个目标表 +declare num int; +declare cou int; +-- 将指定值插入目标表中,count是计算出food表的id数 quantity是查出food id关联表 对应的数量 +select count(*) into cou from food; +select quantity into num from food where food_id=cou; +-- 将查出的id 和quantity分别插入DeliveryLog表中 +insert into DeliveryLog values(null,num,now(),cou); + end// + delimiter ; +insert into food values (null,'糖果','20') +``` + +课上练习 + +``` +create database zy charset utf8; +use zy; +-- 1 创建两个表a,b + +drop table if exists a; +create table a( +id int primary key auto_increment, +namea varchar(20) +); +drop table if exists b; +create table b( +id int primary key auto_increment, +nameb varchar(20) + +); +-- 2 分别创建三个触发器,监控a表对其增加,删除,修改动作,相应在b表生成一条监控数据 +drop trigger test_1; +delimiter // +create trigger test_1 +after insert on a +for each row +begin +insert into b (nameb) values ('生成一条监控数据'); +end// +delimiter; +insert into a (namea) values('反效果'); +insert into a (namea) values('大米恩本'); +insert into a (namea) values('发美女吧'); + + +select * from b; + +drop trigger test_2; +delimiter // +create trigger test_2 +after delete on a +for each row +begin +insert into b (nameb) values ('shan生成一条监控数据'); +end// +delimiter; +delete from a where namea='发美女吧'; +select * from a; +select * from b; +``` \ No newline at end of file diff --git "a/05 \350\260\242\351\223\226\346\265\251/20231018.md" "b/05 \350\260\242\351\223\226\346\265\251/20231018.md" new file mode 100644 index 0000000000000000000000000000000000000000..79c92676740ec2f43603dd37e472c7b7c48d965e --- /dev/null +++ "b/05 \350\260\242\351\223\226\346\265\251/20231018.md" @@ -0,0 +1,12 @@ +```mysql +窗口大小 +riws 启用窗口大小 +between ... and ... 区间范围 +unbounded preceding 起始行 n preceding 当前行开始 往前n行 +current row 当前行 n following 从当前开始往后n行 +例:rows between unbounded preceding and current row +这句就是起始行到尽头 +datediff n (a,b) 返回天数差 a-b +timestampdif (单位,a,b) 可返回年差 月差 天差 时差 分差 秒差等b-a +date_format()函数 时间输出格式 +``` \ No newline at end of file diff --git "a/05 \350\260\242\351\223\226\346\265\251/20231019 \344\272\213\345\212\241.md" "b/05 \350\260\242\351\223\226\346\265\251/20231019 \344\272\213\345\212\241.md" new file mode 100644 index 0000000000000000000000000000000000000000..f0e5ba6fa05a29ce56bac86daadfc0eac5672781 --- /dev/null +++ "b/05 \350\260\242\351\223\226\346\265\251/20231019 \344\272\213\345\212\241.md" @@ -0,0 +1,116 @@ +``` +事务 +一、 +1.原子性 +2.一致性 +3.隔离性 +4.持久性 +二、 +show variables like 'autocommit';-- 显示所有的系统变量 +-- 当autocommit 为1或on时 会自动将insert update delete 等操作自动提交 称为隐藏事务 +三、 +-- 关闭自动提交的功能 +set autocommit =off; +set autocommit =0;-- 需要手动提交。到rollback commit会结束事务 +-- 开启自动提交功能 +set autocommit =on; +set autocommit =1; +-- rollback 回滚 +-- commit 确认 +-- 不管autocommit是什么状态,由程序自己临时开启事务 +start transaction;-- 手动启动了一个新事物,从本行开始的操作就由此事务管理,直到使用了rollback,commit 才会结束 +四、执行大量操作。只想回滚部分内容咋办 +savepoint 保存点;-- 在本行前设一个保存点 +rollback to 保存点;-- 则保存点之后的内容都撤销 +五、只读事务 +start transaction read only;-- 只能用select(胀读:读未提交的数据) +六、默认可重复读 +show variables like (transaction_isolation); +七、四种隔离级别 +1.读未提交(脏读) +2.读已提交(不可重复读) +3.可重复读(幻读) +4.串行(最严格,性能最低) +``` + +##### 作业 + +``` +create database zy charset utf8; +use zy; +-- Sql12练习 +-- 建表语句: +-- 执行以下SQL,建表插数:8989 +-- 部门表 +create table dept( + deptno int primary key auto_increment, -- 部门编号 + dname varchar(14) , -- 部门名字 + loc varchar(13) -- 地址 +) ; +-- 员工表 +create table emp( + empno int primary key auto_increment,-- 员工编号 + ename varchar(10), -- 员工姓名 - + job varchar(9), -- 岗位 + mgr int, -- 直接领导编号 + hiredate date, -- 雇佣日期,入职日期 + sal int, -- 薪水 + comm int, -- 提成 + deptno int not null, -- 部门编号 + foreign key (deptno) references dept(deptno) +); +insert into dept values(10,'财务部','北京'); +insert into dept values(20,'研发部','上海'); +insert into dept values(30,'销售部','广州'); +insert into dept values(40,'行政部','深圳'); +insert into emp values(7369,'刘一','职员',7902,'1980-12-17',800,null,20); +insert into emp values(7499,'陈二','推销员',7698,'1981-02-20',1600,300,30); +insert into emp values(7521,'张三','推销员',7698,'1981-02-22',1250,500,30); +insert into emp values(7566,'李四','经理',7839,'1981-04-02',2975,null,20); +insert into emp values(7654,'王五','推销员',7698,'1981-09-28',1250,1400,30); +insert into emp values(7698,'赵六','经理',7839,'1981-05-01',2850,null,30); +insert into emp values(7782,'孙七','经理',7839,'1981-06-09',2450,null,10); +insert into emp values(7788,'周八','分析师',7566,'1987-06-13',3000,null,20); +insert into emp values(7839,'吴九','总裁',null,'1981-11-17',5000,null,10); +insert into emp values(7844,'郑十','推销员',7698,'1981-09-08',1500,0,30); +insert into emp values(7876,'郭十一','职员',7788,'1987-06-13',1100,null,20); +insert into emp values(7900,'钱多多','职员',7698,'1981-12-03',950,null,30); +insert into emp values(7902,'大锦鲤','分析师',7566,'1981-12-03',3000,null,20); +insert into emp values(7934,'木有钱','职员',7782,'1983-01-23',1300,null,10); +-- 完成以下练习题 +-- +-- 1、列出最低薪金大于1500的各种工作。 + +select distinct job from emp where ifnull(comm,0) +sal>1500; + +-- 2、列出在部门 "销售部" 工作的员工的姓名,假定不知道销售部的部门编号。 +select deptno from dept where dname='销售部'; +select ename from emp where deptno = (select deptno from dept where dname='销售部' +); +-- 3、列出薪金高于公司平均薪金的所有员工。 +select * from emp where sal>avg(sal); +-- 4、列出与"周八"从事相同工作的所有员工。 +select job from emp where ename='周八'; +select ename from emp where job =(select job from emp where ename='周八'); +-- 5、列出薪金等于部门30中员工的薪金的所有员工的姓名和薪金。 +select ename,ifnull(comm,0)+sal from emp; +select distinct ifnull(comm,0) +sal,ename from emp where deptno='30'; +select ename,ifnull(comm,0) +sal from emp where ifnull(comm,0) +sal in (select distinct ifnull(comm,0) +sal from emp where deptno='30'); +-- 6、列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金。 +select ename from emp where ifnull(comm,0) +sal = (select max(ifnull(comm,0) +sal ) from emp where deptno='30'); +-- 7、列出在每个部门工作的员工数量、平均工资、平均服务年限。 +select deptno,count(*),floor(avg(ifnull(comm,0)+sal)),floor( avg(timestampdiff(year,hiredate,now()))) from emp group by deptno; + +-- 8、列出所有员工的姓名、部门名称和工资。 +select * from dept; +select ename,dname,ifnull(comm,0)+sal 工资 from emp e,dept d where e.deptno=d.deptno; +-- 9、列出所有部门的详细信息和部门人数。 + select count(empno) 部门人数, d.* from emp e,dept d where e.deptno=d.deptno group by deptno; +-- 10、列出各种工作的最低工资。 +select job,min(ifnull(comm,0)+sal) from emp group by job; + +-- 11、列出各个部门的 经理 的最低薪金。 +select deptno,min(ifnull(comm,0)+sal) from emp where job='经理' group by deptno; +-- 12、列出所有员工的年工资,按年薪从低到高排序。 +select ename,(ifnull(comm,0)+sal)*12 年工资 from emp order by 年工资; +``` \ No newline at end of file diff --git "a/05 \350\260\242\351\223\226\346\265\251/20231022 \346\225\260\346\215\256\345\272\223\347\273\217\345\205\27050\351\242\230.md" "b/05 \350\260\242\351\223\226\346\265\251/20231022 \346\225\260\346\215\256\345\272\223\347\273\217\345\205\27050\351\242\230.md" new file mode 100644 index 0000000000000000000000000000000000000000..2801d3cc08eac83c814fcdd6a267643af67f2f51 --- /dev/null +++ "b/05 \350\260\242\351\223\226\346\265\251/20231022 \346\225\260\346\215\256\345\272\223\347\273\217\345\205\27050\351\242\230.md" @@ -0,0 +1,259 @@ +``` +create database zy charset utf8; +use zy; +# 学生表 +CREATE TABLE IF NOT EXISTS `student`( + `student_id` INT(10) NOT NULL AUTO_INCREMENT COMMENT '学号', + `student_name` VARCHAR(10) NOT NULL DEFAULT '匿名' COMMENT '姓名', + `birthday` DATETIME NOT NULL COMMENT '出生日期', + `gender` VARCHAR(10) NOT NULL DEFAULT '男' COMMENT '性别', + PRIMARY KEY(`student_id`) +)ENGINE=INNODB CHARSET=utf8; + +INSERT INTO `student` VALUES +(1 , '赵雷' , '1990-01-01' , '男'), +(2 , '钱电' , '1990-12-21' , '男'), +(3 , '孙风' , '1990-12-20' , '男'), +(4 , '李云' , '1990-12-06' , '男'), +(5 , '周梅' , '1991-12-01' , '女'), +(6 , '吴兰' , '1992-01-01' , '女'), +(7 , '郑竹' , '1989-01-01' , '女'), +(8 , '张三' , '2017-12-20' , '女'), +(9 , '李四' , '2017-12-25' , '女'), +(10 , '李四' , '2012-06-06' , '女'), +(11 , '赵六' , '2013-06-13' , '女'), +(12 , '孙七' , '2014-06-01' , '女'); +-- 课程表 +CREATE TABLE IF NOT EXISTS `course`( + `course_id` INT(4) NOT NULL AUTO_INCREMENT COMMENT '课程编号', + `course_name` VARCHAR(10) NOT NULL COMMENT '课程名', + `teacher_id` INT(10) NOT NULL COMMENT '任课教师工号', + PRIMARY KEY(`course_id`) +)ENGINE=INNODB CHARSET=utf8; + +INSERT INTO `course` VALUES +(1, '语文', 2), +(2, '数学', 1), +(3, '英语', 3); +# 教师表 +CREATE TABLE IF NOT EXISTS `teacher`( + `teacher_id` INT(10) NOT NULL AUTO_INCREMENT COMMENT '教师工号', + `teacher_name` VARCHAR(10) NOT NULL DEFAULT '匿名' COMMENT '教师姓名', + PRIMARY KEY(`teacher_id`) +)ENGINE=INNODB CHARSET=utf8; + +INSERT INTO `teacher` VALUES +(1, '高斯'), +(2, '李白'), +(3, 'Trump'); + +-- 成绩表 +CREATE TABLE IF NOT EXISTS `score`( + `student_id` INT(10) NOT NULL COMMENT '学号', + `course_id` INT(4) NOT NULL COMMENT '课程编号', + `score` DECIMAL(18,1) COMMENT '成绩', + KEY(`course_id`) +)ENGINE=INNODB CHARSET=utf8; + +INSERT INTO `score` VALUES +(1 , 1 , 80), +(1 , 2 , 90), +(1 , 3 , 99), +(2 , 1 , 70), +(2 , 2 , 60), +(2 , 3 , 80), +(3 , 1 , 80), +(3 , 2 , 80), +(3 , 3 , 80), +(4 , 1 , 50), +(4 , 2 , 30), +(4 , 3 , 20), +(5 , 1 , 76), +(5 , 2 , 87), +(6 , 1 , 31), +(6 , 3 , 34), +(7 , 2 , 89), +(7 , 3 , 98); + +-- 1. 查询" 1 "课程比" 2 "课程成绩高的学生的信息(学号、姓名、性别、出生日期)及课程分数 +select s.*,a.score from student s, +(select * from score where course_id='1') a, +(select * from score where course_id='2') b where s.student_id=a.student_id and a.student_id=b.student_id and a.score>b.score; +-- 2. 查询同时参与" 1 "课程和" 2 "课程考试的学生信息 +select s.* from student s, +(select * from score where course_id='1') a, +(select * from score where course_id='2') b +where +s.student_id=a.student_id +and +a.student_id=b.student_id ; +-- 3. 查询存在" 1 "课程但可能不存在" 2 "课程的情况(不存在时显示为 null ) +select * from +(select * from score where course_id='1') a left join +(select * from score where course_id='2') b on a.student_id=b.student_id ; +-- 4. 查询不存在" 1 "课程但存在" 2 "课程的情况 +select * from +(select * from score where course_id='2') a left join +(select * from score where course_id='1') b on a.student_id=b.student_id ; +-- 5. 查询平均成绩大于等于 60 分的同学的学生编号、姓名和平均成绩 +-- select * from score group by student_id where avg(score)>60; +-- +select +a.student_id,student_name,avg(score) + from + (select *from score where score in (select floor(avg( score)) 平均成绩 from score group by student_id having 平均成绩>=60)) a + left join +student b + on + a.student_id=b.student_id + group by student_id +; + +-- 6. 查询在成绩表存在成绩的学生信息 +select * from score a left join student b on a.student_id=b.student_id; + +-- 7. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select a.student_id,student_name,count(b.course_id),sum(score) from student a left join score b on a.student_id=b.student_id group by student_id; +select * from score; +-- 8. 查询「李」姓老师的数量 +select * from teacher where teacher_name like '李%'; + +-- 9. 查询学过「李白」老师授课的同学的信息 +select + * + from + student a,teacher b,score c + where + a.student_id=c.student_id + and + c.course_id=b.teacher_id + and + b.teacher_name='李白'; + +-- 10. 查询没有学全所有课程的同学的信息 +select * from student where student_id not in ( +select student_id from score group by student_id having count(course_id) = (select count(1) from teacher)); +select * from student a left join score b on a.student_id=b.student_id; + +-- 11. 查询至少有一门课与学号为" 1 "的同学所学相同的同学的信息 + select distinct a.student_id,a.student_name from student a left join score b on a.student_id=b.student_id where course_id in( + select course_id from score where student_id='1'); +-- 12. 查询和" 1 "号的同学学习的课程完全相同的其他同学的信息 + + select student_id,student_name,course_id from student s,(select +select * +from student +where student.student_id not in ( +select * +from +(select student.student_id,a.course_id +from student ,(select course_id from score where student_id='01') as a )as b +left join score on b.student_id=score.student_id and b.course_id=score.course_id +where score.course_id is null ) +and student.student_id !='01'; +-- 13. 查询没学过「李白」老师讲授的任一门课程的学生姓名 +select student_name from student where student_id not in ( +select student_id from teacher a,score b where a.teacher_id=b.course_id and teacher_name='李白'); + +-- 14. 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 +select * from student; +select * from score where score<=60 ; +select a.student_id,student_name,avg(score) from student a,score b where a.student_id=b.student_id and score<=60 group by student_id having count(course_id)>=2; +-- 15. 检索" 1 "课程分数小于 60,按分数降序排列的学生信息 +select b.student_id,b.birthday,b.gender,b.student_name from score a,student b where course_id=1 and score<=60 and a.student_id=b.student_id; +-- 16. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 +select * from +score a, +( +select student_id,avg(score) from score group by student_id order by avg(score) desc +) b +where +a.student_id=b.student_id; +-- 17. 查询各科成绩最高分、最低分和平均分 + select course_id,max(score),min(score),avg(score) from score group by course_id; +-- 18. 以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率(及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90),要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + select a.course_id,max(a.score) 最高分 ,min(a.score) 最低分,avg(a.score) 平均分, + ((select count(student_id) from score where score>=60 and course_id=b.course_id )/(select count(student_id) from score where course_id=b.course_id)) 及格率 +from score a +inner join course b on a.course_id = b.course_id +group by b.course_id; +-- 19. 按各科成绩进行排序,并显示排名,Score 重复时保留名次空缺 + select student_id,course_id,score,rank() over (partition by course_id order by score) from score; +-- 20. 按各科成绩进行排序,并显示排名,Score 重复时合并名次 + select student_id,course_id,score,row_number() over (partition by course_id order by score) from score; +-- 21. 查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + select a.student_id,rank() over (order by a.score desc) 排名 ,a.score from (select student_id,sum(score) score from score group by student_id) a; +-- 22. 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + select a.student_id,dense_rank() over (order by a.score desc) 排名 ,a.score from (select student_id,sum(score) score from score group by student_id) a; +-- 23. 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + select c.course_id,c.course_name +,((select count(*) from score sc where sc.course_id=c.course_id and sc.score<=100 and sc.score>80)/(select count(*) from score sc where sc.course_id=c.course_id )) "100-85" +,((select count(*) from score sc where sc.course_id=c.course_id and sc.score<=85 and sc.score>70)/(select count(*) from score sc where sc.course_id=c.course_id )) "85-70" +,((select count(*) from score sc where sc.course_id=c.course_id and sc.score<=70 and sc.score>60)/(select count(*) from score sc where sc.course_id=c.course_id )) "70-60" +,((select count(*) from score sc where sc.course_id=c.course_id and sc.score<=60 and sc.score>=0)/(select count(*) from score sc where sc.course_id=c.course_id )) "60-0" +from course c order by c.course_id; +-- 24. 查询各科成绩前三名的记录 + select * from +(select student_id,score,rank() over (partition by course_id order by score desc) ranks from score) s +where ranks<4; +-- 25. 查询每门课程被选修的学生数 + select distinct s.course_id,c.course_name,count(s.student_id) over (partition by s.course_id) 学生数 from score s left join course c on s.course_id=c.course_id; +-- 26. 查询出只选修两门课程的学生学号和姓名 + select s.student_name 姓名,a.学号 from (select distinct student_id 学号,count(course_id) over (partition by student_id) 选修数 from score) a left join student s on a.`学号`=s.student_id where 选修数=2; +-- 27. 查询男生、女生人数 + select gender,count(student_id) from student group by gender; +-- 28. 查询名字中含有「风」字的学生信息 + select * from student where student_name like '%风%'; +-- 29. 查询同名同性学生名单,并统计同名人数 + select student_name from student group by student_name having count(student_id)>1; +-- 30. 查询 1990 年出生的学生名单 + select * from student where year(birthday)='1990'; +-- 31. 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + select course_id,avg(score) from score group by course_id order by avg(score) desc,course_id asc; +-- 32. 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + select s.student_id,s.student_name,a.`平均成绩` from student s right join (select student_id,avg(score) 平均成绩 from score group by student_id having avg(score)>=85) a on s.student_id=a.student_id; +-- 33. 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + select student_name,score from student right join score on student.student_id=score.student_id left join course on score.course_id=course.course_id where course_name='数学' and score<60; +-- 34. 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + select s2.student_name,s1.* from score s1 right join student s2 on s1.student_id=s2.student_id; +-- 35. 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + select s1.student_name,c.course_name,s2.score from student s1 right join score s2 on s1.student_id=s2.student_id left join course c on s2.course_id=c.course_id where score>70; +-- 36. 查询不及格的课程 + select distinct c.course_name from score s2 left join course c on s2.course_id=c.course_id where score<60; +-- 37. 查询课程编号为 1 且课程成绩在 80 分以上的学生的学号和姓名 + select s1.student_id,s2.student_name from score s1,student s2 where course_id=1 and score>80 and s1.student_id=s2.student_id; +-- 38. 求每门课程的学生人数 + select distinct course_id,count(student_id) over (partition by course_id) from score; +-- 39. 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 +-- 40. 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 +-- 41. 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + select distinct s1.* from score s1,score s2 where s1.score=s2.score and s1.course_id!=s2.course_id; +-- 42. 查询每门功成绩最好的前两名 + select * from +(select student_id,score,rank() over (partition by course_id order by score desc) ranks from score) s +where ranks<=2; +-- 43. 统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + select course_id,count(student_id) from score group by course_id having count(student_id)>5; +-- 44. 检索至少选修两门课程的学生学号 + select student_id +from score +group by student_id +having count(course_id)>=2; +-- 45. 查询选修了全部课程的学生信息 + select s2.* from score s1 right join student s2 on s1.student_id=s2.student_id group by student_id having count(course_id)>=(select count(course_id) from course); +-- 46. 查询各学生的年龄,只按年份来算 + select student_name,year(now())-year(birthday) from student; +-- 47. 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + select birthday,(date_format(now(),'%y') - date_format(birthday,'%y') - + (case when date_format(now(),'%m%d') > date_format(birthday,'%m%d') then 0 else 1 end)) as age +from student; +-- 48. 查询本周过生日的学生 + select * from student where day(birthday) between 16 and 22 and month(birthday)=month(now()); +-- 49. 查询下周过生日的学生 + select * from student where day(birthday) between 23 and 29 and month(birthday)=month(now()); +-- 50. 查询本月过生日的学生 + select * from student where month(birthday)=month(now()); +-- 51. 查询下月过生日的学生 + +select * from student where month(birthday)=month(now())+1; +``` \ No newline at end of file diff --git "a/05 \350\260\242\351\223\226\346\265\251/20231025 \346\200\273\345\244\215\344\271\240.md" "b/05 \350\260\242\351\223\226\346\265\251/20231025 \346\200\273\345\244\215\344\271\240.md" new file mode 100644 index 0000000000000000000000000000000000000000..7fecd411a73d616dc8702a147bfd0d3459a5311c --- /dev/null +++ "b/05 \350\260\242\351\223\226\346\265\251/20231025 \346\200\273\345\244\215\344\271\240.md" @@ -0,0 +1,274 @@ +### 表之间的关系 + +1.一对一的关系:将其中任一表中主键,放到另一个表当外键; + +2.一对多的关系:将一所在的表的主键,放到多的表当外键; + +3.多对多的关系:必须第三张表,将前面两个表的主键放进来当外键 + +### 数据库设计的方法 + +1.直观设计法; + +2.规范设计法:E-R模型; + +3.计算机辅助设计法:PowerDesigner + +### E-R图 + +E-R图:实体关系图 + +要素:实体(表)、属性(字段)、关系(类似外键约束) + +绘图软件推荐:visio等 + +### 数据库的范式 + +1.第一范式:要求字段的内容,不可再分割,为的是保证数据的原子性 + +2.第二范式:要求在满足第一范式的基础上,要求非主键字段要完全依赖主键(非主键,要依赖整个联合主键),而不能只依赖部分 + +3.第三范式:满足于第二范式的前提上,要求,非关键属性要直接依赖于主键 + +补充:所谓几对几是表中数据相对,不是一整张表相对 + +### 概念模型 + +一个软件:PowerDesigner + +第一步,创建概念模型(类似ER图)CDM(以用户的角度) + +第二步,转换成逻辑模型 LDM(以计算机角度) + +第三步,转换成物理模型 PDM(以数据库角度) + +第四步,生成DDL + +### RBAC + +基于角色访问控制(Role-Based Access Control) + +数据库能存什么: + +1.业务数据表:用户、商品; + +2.功能资源表:菜单信息表、页面代码表 + +权限的使用场景: + +网页不一样;网页一样,但可用的菜单不一样;菜单一样,但同一个菜单下的网页元素也可能不一样(按钮,数据不一样,权限一样); + +菜单权限:不同的用户登录系统后,展开的菜单不一样; + +按钮权限:不同的用户查看同一个对象时,展示的按钮不一样; + +数据权限:不同用户查看同一个对象时,可见的数据不一样; + +操作权限:能看到,却操作不了; + +文件资源的权限 + +学习RBAC需要掌握的要素: + +RBAC的核心是角色; + +RBAC是目前开发系统中主流的设计模式 + +### SKU + +最小存货单位(Stock Keeping Unit),即库存进出计量的基本单元,可以是以件,盒,托盘等为单位 + +针对电商而言,SKU有另外的注解: + +1、SKU是指一款商品,每款都有出现一个SKU,便于电商品牌识别商品; + +2、一款商品多色,则是有多个SKU,例:一件衣服,有红色、白色、蓝色,则SKU编码也不相同,如相同则会出现混淆,发错货 + +### 视图view + +1.视图是一种虚拟表,本身是不具有数据的,占用很少的内存空间 + +2.视图建立在已有的基础上,视图耐以建立的这些表叫做基表 + +3.将过滤后的数据,保存成一个视图,有利于数据的安全性 + +4.语法: + +``` +-- 创建视图 +create view 视图名称 as select语句; +-- 查询视图 +select * from 视图名称; +-- 修改视图 +alter view 视图名称 as select语句; +-- 删除视图 +drop view if exists 视图名称; +-- 查看视图结构 +desc 视图名称; +``` + +as 查询语句 + +5.视图只有视图和基表有一对一的情况可以更新,一对多或者多对多不可更新(一般情况下不会去更新视图) + +6.当我们创建好一张视图后,还可以在它基础上再创建视图 + +7.更新视图: + +方法1:使用create or replace view子句修改视图,有这个视图就更新,没有就创建 + +方法2:alter view 视图名称 as select 语句,前提是被修改的视图要先存在 + +8.删除视图:drop view 视图名称 + +9.总结: + +(1)创建视图: + +第一种:create view 视图名称 as select 语句 + +第二种:create view 视图的名称(视图的字段)as select 语句 字段数要与select语句结果的字段数一致 + +(2)查询视图: + +第一种:select * from 视图名称 + +第二种:select 指定的字段名 from 视图名称 + +(3)修改视图: + +第一种:使用create or replace view子句修改视图,有这个视图就更新,没有就创建 + +第二种:alter view 视图名称 as select 语句,前提是被修改的视图要先存在 + +(4)删除视图:drop view 视图名称 + +### 数值函数 + +基本函数: + +1.rand()返回大于等于0且小于1的小数--【0,1) + +select floor(rand()*10);随机生成一个【0,9】的数 + +2.length(s)返回字符串s的字节数,和字符集有关 utf8 一个汉字=3个字节 + +3.拼接字符串用concat() + +concant_ws() + +4.trim()去掉字符串两端的空格 + +rtrim()去掉字符串右端的空格 + +ltrim()去掉字符串左端的空格 + +5.替换:replace(原始字符串,要被替换的字符串,新字符串) + +6.upper()把字母全部转化成大写字母 + +lower()把字母全部转化成小写字母 + +7.从一个字符串中取出对应的部分字符串:left(字符串,长度N)从字符串的左边开始,截取对应长度N的字符 + +right(字符串,长度N)从字符串的右边开始截取对应长度N的字符 + +8.直接从某个字符串中截取指定位置指定长度的字符串的函数有三个:substr(str,index,len),substring(),mid() + +9.去除xx中的全部空格:replace(字段名,'','') from .. + +10.if(表达式,值1,值2)类似我们三元运算符 + +11.substring_index(字段名,'',count) + +### 存储过程 + +1.存储过程(Stored Procedure)是一种在数据库中存储复杂程序,以便外部程序调用的一种数据库对象; + +2.存储过程是为了完成特定功能的SQL语句集,经编译创建并保存在数据库中,用户可通过指定存储过程的名字并给定参数(需要时)来调用执行; + +3.存储过程思想上很简单,就是数据库 SQL 语言层面的代码封装与重用; + +4.优点: + +存储过程可封装,并隐藏复杂的商业逻辑; + +存储过程可以回传值,并可以接受参数; + +存储过程无法使用 SELECT 指令来运行,因为它是子程序,与查看表,数据表或用户定义函数不同; + +存储过程可以用在数据检验,强制实行商业逻辑等; + +5.缺点: + +存储过程,往往定制化于特定的数据库上,因为支持的编程语言不同。当切换到其他厂商的数据库系统时,需要重写原有的存储过程; + +存储过程的性能调校与撰写,受限于各种数据库系统; + +6.存储过程的创建和调用: + +存储过程就是具有名字的一段代码,用来完成一个特定的功能; + +创建的存储过程保存在数据库的数据字典中。 + +### 索引 + +索引(index)是帮助MySQL高效获取数据的数据结构 + +如果不使用索引,MySQL必须从第一条记录开始读完整个表,直到找出相关的行,表越大,查询数据所花费的时间就越多 + +建立索引要花费对应的时间和硬盘空间 + +优点:提高数据检索的效率,降低数据库的IO成本(提升了select速度) + +缺点:降低了insert,update,delete速度,索引列也是要占用空间的。索引大大提高了查询效率。同时却也降低更新表的速度 + +索引的分类 + +单列索引:一个索引建立在一个列上,一张表可以拥有多个单列索引 + +联合索引:可以同时为多个列创建一个索引 + +普通索引:index 单纯地为了提高搜索效率 + +唯一索引:unique index + +主键索引:primary key 唯一性,非空主键约束,不能重复,也不能null,一个只能有一个主键,create index不能用来创建主键索引 + +查看表的索引:show index from tb_1; + +唯一索引:建立列的唯一约束时,会自动创建唯一的索引,索引名就是列名 + +创建主键索引就是创建主键约束 + +### 什么是事务? + +事务指的是一个操作序列,该操作序列中的多个操作要么都做,要么都不做,是一个不可分割的工作单位,是数据库环境中的逻辑工作单位,由DBMS(数据库管理系统)中的事务管理子负者事务的处理 + +### 事务的特性 + +事务处理可以确保除非事务性序列内的所有操作都成功完成,否则不会永久更新面向数据的资源。通过将一组相关操作组合为一个要么全部成功要么全部失败的序列,可以简化错误恢复并使应用程序更加可靠 + +但不是所有的操作序列都可以称为事务,这是因为一个操作序列要成为事务,必须满足事务的原子性、一致性、隔离性和持久性。这四个特性简称为ACID特性 + +### 事务的四个特性 + +1.原子性:事务中的所有操作可以看做一个原子(自然界最小的颗粒,具有不可再分的特性),事务是应用中不可再分的最小的逻辑执行体。使用事务对数据进行修改的操作序列,要么全部执行,要么全不执行 + +2.一致性:是指事务执行的结果必须使数据库从一个一致性状态,变到另一个一致性状态。当数据库中只能包含事务成功提交的结果时,数据库处于一致性状态。一致性是通过原子性来保证的 + +例如:在转账时,只有保证转出和转入的金额一致才能构成事务。也就是说事务发生前和发生后,数据的总额依然匹配 + +3.隔离性:是指各个事务的执行互不干扰,任意一个事务的内部操作对其他并发的事务都是隔离的。也就是说:并发执行的事务之间既不能看到对方的中间状态,也不能相互影响 + +例如:在转账时,只有当A账户中的转出和B账户中转入操作都执行成功后才能看到A账户中的金额减少以及B账户中的金额增多。并且其他的事务对于转账操作的事务是不能产生任何影响的 + +4.持久性:持久性指事务一旦提交,对数据所做的任何改变,都要记录到永久存储器中,通常是保存进物理数据库,即使数据库出现故障,提交的数据也应该能够恢复。但如果是由于外部原因导致的数据库故障,如硬盘被损坏,那么之前提交的数据则有可能会丢失 + +### 事务并发问题 + +脏读(Dirty read):当一个事务正在访问数据并且对数据进行了修改,而这种修改还没有提交到数据库中,这时另外一个事务也访问了这个数据,然后使用了这个数据。因为这个数据是还没有提交的数据,那么另外一个事务读到的这个数据是"脏数据",依据"脏数据"所做的操作可能是不正确的 + +不可重复读(Unrepeatableread):指在一个事务内多次读同一个数据。在这个事务还没有结束时, 另一个事务也访问该数据。那么,在第一个事务中的两次读数据之间,由于第二个事务的修改导致第一个事务两次读取的数据可能不太一样。这就发生了在一个事务内两次读到的数据是不一样的情况。因此称为不可重复读 + +幻读(Phantom read):幻读与不可重复读类似。它发生在一个事务读取了几行数据,接着另一个并发事务插入了一些数据时。在随后的查询中,第一个事务就会发现多了一些原本吧存在的记录,就好像发生了幻觉一样,所以称为幻读 \ No newline at end of file diff --git "a/05 \350\260\242\351\223\226\346\265\251/20231025 \347\254\224\350\256\260.md" "b/05 \350\260\242\351\223\226\346\265\251/20231025 \347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..25419e097cababa9d5004f4a4cfd83bc4a67206c --- /dev/null +++ "b/05 \350\260\242\351\223\226\346\265\251/20231025 \347\254\224\350\256\260.md" @@ -0,0 +1,15 @@ +### 子查询的三种方法 + +1.放在select后面当列来用,要求子查询的结果是单列单行 + +2.放在from后面当表来用,放任意子查询,要求子查询的结果要取一个别名 + +3.放在where后面当条件用,一种是结果单列单行,此时条件可以直接用 = < > ,另一种情况是单列多行 + +反过来,你写的查询是多列多行,只能当表来用 + +条件中用in关键字时,( )里的值会自动去重 + +count(*)查询所有列,包括null,括号里可以换成常量 + +count(列名时)只统计该列名非null值的数量 \ No newline at end of file