1.建立计算机系学生的视图student_cs,视图中包含sno,sname,dept,sex,totalcredit。使用select命令显示视图中的数据。
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信息。
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命令显示视图中所有数据。
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所有数据。
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所有内容。
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。
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表中的内容。
delimiter $$
create procedure updatesalaries()
beginDECLARE 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:loopfetch cur into curid,curname,cursal;if flat=1 then leave label;end if;if sum>600000 thenleave 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;