MQL实验(三)视图与游标

1.建立计算机系学生的视图student_cs,视图中包含sno,sname,dept,sex,totalcredit。使用select命令显示视图中的数据。

sql 复制代码
create view student_cs as 
select student.sno,sname,dept,sex,totalcredit from student 
where dept='计算机';
select * from student_cs;

create view student_cs as

2.向student_cs视图中录入学生sno:001242,sname:王五,dept:通信工程,sex:女,totalcredit:40,并用select命令查询显示上述录入的sno,sname,dept,sex,totalcredit信息。

sql 复制代码
insert into student(sno,sname,dept,sex,totalcredit)
values('001242','王五','通信工程','女','40');
select sno,sname,dept,sex,totalcredit from student where sno=001242;

3.建立通信工程系选修了"计算机基础"课程且成绩在80分以上的学生的视图,视图中包括sno,sname,cname,grade。用select命令显示视图中所有数据。

sql 复制代码
create view shitu(sno,sname,cname,grade) as
select student.sno,sname,cname,grade from student
join score on student.sno=score.sno
join course on score.cno=course.cno
where cname='计算机基础' and grade>80 and dept='通信工程';
select *from shitu;

4.将学生的学号及其平均成绩定义为一个视图S_G,视图包括sno,avggrade两个字段。用select命令显示视图S_G所有数据。

sql 复制代码
create view S_G as
select score.sno,avg(grade) from score
group by sno;
select *from S_G;

5.将Score中课程成绩超过该课平均分的学生学号定义成一个视图GOOD_SC,视图包括cno,sno,grade。用select命令显示视图GOOD_SC所有内容。

sql 复制代码
create view GOOD_SC as
select score.sno ,score.cno,grade from score where 
grade>(select avg(grade)from score where cno=score.cno );
select *from GOOD_SC;

6.基于视图S_G查询"通信工程"系学生的平均成绩,要求输出sno,sname,avggrade。

sql 复制代码
select student.sno,sname,avggrade from S_G join student on S_G.sno=student.sno
where dept='通信工程';

7.雇员表employees结构定义如下:(表中的数据由系统提供)

CREATE TABLE employees (

id INT,

name VARCHAR(50) NOT NULL,

salary DECIMAL(20, 2) NOT NULL,

PRIMARY KEY (id)

);

编写带有游标的存储过程,实现为职工长10%的工资。从最低工资开始长,最后工资总额限制在60万元以内。将涨过工资的职工id、name、涨后的工资,存入下面的临时表temp。

CREATE TABLE temp (

id INT,

name VARCHAR(50) NOT NULL,

salary DECIMAL(20, 2) NOT NULL

);

特别提示:要求编写存储过程后,用call命令调用过程,并用select命令显示temp表中的内容。

sql 复制代码
delimiter $$
create procedure updatesalaries() 
begin
    DECLARE curid INT;   
    DECLARE curname VARCHAR(50);   
    DECLARE cursal DECIMAL(20,2);   
    DECLARE flat TINYINT DEFAULT 0;   
    DECLARE sum DECIMAL(20,2);   
    DECLARE cur CURSOR FOR 
    SELECT id,name,salary FROM employees ORDER BY salary;   
declare continue handler for not found set flat=1;
select sum(salary) into sum from employees;
open cur;
 label:loop
       fetch cur into curid,curname,cursal;
       if flat=1 then  
          leave label;
       end if;
       if sum>600000 then
          leave label;
       end if;
       update employees set salary=salary*1.1 where id=curid;
       set sum=sum+cursal*0.1;
       insert into temp values(curid,curname,cursal*1.1);
  end loop label;
end $$
delimiter ;
call updatesalaries();
select * from temp;
相关推荐
why技术2 分钟前
Stack Overflow,轰然倒下!
前端·人工智能·后端
GISer_Jing7 分钟前
0704-0706上海,又聚上了
前端·新浪微博
止观止30 分钟前
深入探索 pnpm:高效磁盘利用与灵活的包管理解决方案
前端·pnpm·前端工程化·包管理器
whale fall31 分钟前
npm install安装的node_modules是什么
前端·npm·node.js
Hello.Reader36 分钟前
Redis 延迟排查与优化全攻略
数据库·redis·缓存
烛阴36 分钟前
简单入门Python装饰器
前端·python
袁煦丞1 小时前
数据库设计神器DrawDB:cpolar内网穿透实验室第595个成功挑战
前端·程序员·远程工作
简佐义的博客1 小时前
破解非模式物种GO/KEGG注释难题
开发语言·数据库·后端·oracle·golang
爬山算法1 小时前
MySQL(116)如何监控负载均衡状态?
数据库·mysql·负载均衡
天天扭码2 小时前
从图片到语音:我是如何用两大模型API打造沉浸式英语学习工具的
前端·人工智能·github