【精选】SQL Server实验三

上传人:姜** 文档编号:856247 上传时间:2017-05-19 格式:DOC 页数:11 大小:1.28MB
返回 下载 相关 举报
【精选】SQL Server实验三_第1页
第1页 / 共11页
【精选】SQL Server实验三_第2页
第2页 / 共11页
【精选】SQL Server实验三_第3页
第3页 / 共11页
【精选】SQL Server实验三_第4页
第4页 / 共11页
【精选】SQL Server实验三_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《【精选】SQL Server实验三》由会员分享,可在线阅读,更多相关《【精选】SQL Server实验三(11页珍藏版)》请在金锄头文库上搜索。

1、实验七(1)创建并运行存储过程student_grade,要求实现如下功能:查询studb数据库中每个学生各门课的成绩,其中包括每个学生的sno、sname、cname和score。create procedure student_gradeasselect student.sno,student.sname,ame,student_course.scorefrom student join student_course on student.sno=student_course.snojoin course on o=student_o运行结果代码:use Studbgoexecute st

2、udent_gradego(2)创建并运行名为proc_exp的存储过程,要求实现如下功能:从 student_course表中查询某一学生考试的平均成绩。create procedure proc_expsname varchar(8)asbeginselect sname,AVG(score)from student join student_course on student.sno=student_course.snowhere sname=snamegroup by snameend运行结果代码:use Studbgoexecute proc_exp sname=刘招香go(3)修改

3、存储过程proc_exp,要求实现如下功能:输入学生学号,根据该学生所选课程的平均成绩给出提示信息,即如果平均成绩在60分以上,显示“成绩合格,成绩为XX 分”,否则显示“成绩不合格,成绩为XX分”;然后调用存储过程proc_exp ,输入学号0705010131,显示成绩是否合格。alter procedure proc_expstudent_sno varchar (20)asdeclare avg varchar(20)set avg=(select AVG(score)from student_coursewhere sno=student_sno)if avg=60 print 成绩

4、合格,成绩为+ avg+分else print 成绩不合格,成绩为+avg+ 分运行结果代码:use Studbgodeclare student_sno varchar (20)select student_sno=0705010131exec proc_exp student_sno(4)创建名为proc_add的存储过程,要求实现以下功能:向 student_course表中添加学生记录;然后调用存储过程proc_add,向student_course表中添加学生成绩记录。create procedure proc_addsno char(10),cno char(10), score

5、tinyintasbeginset nocount onif not exists(select * from student_course where sno=sno and cno=cno and score=score )insert into student_course(sno,cno,score)values(sno,cno,score)end运行结果代码:use studb goexec proc_add 0705010102,0208,80go执行前:执行后:(5)删除存储过程 proc_exp 和 proc_addIF OBJECT_ID(proc_exp) IS NOT N

6、ULLDROP PROCEDURE proc_expIF OBJECT_ID(proc_add) IS NOT NULLDROP PROCEDURE proc_add实验八(1)创建触发器 student_trg,当删除 student 表中的数据时,同时删除 student_course 表中相同的数据;然后通过删除 student 表中的某个学生记录来验证该触发器。create trigger student_trg on student after deleteas begindeletefrom student_coursewhere sno IN(select snofrom del

7、eted)end运行结果代码:DELETE FROM studentWHERE sno=0705010303(2)修改触发器 student_trg,当更新 student 表中 sno 的值时,同时更新student_course 表中相同的 sno 的值;然后通过修改 student 表中的某个学生的学号(sno)来验证该触发器ALTER TRIGGER student_trgON student AFTER UPDATEASBEGINIF UPDATE(sno)UPDATE student_courseSET sno=(SELECT sno FROM inserted)WHERE sno

8、 IN (SELECT sno FROM DELETED)END运行结果代码:UPDATE studentSET sno=0705010217WHERE sno=0705010215(3) 删除触发器student_trgDROP TRIGGER student_trg(4)创建一个新的触发器,要求实现“计算机系的学生选课不能超过三门”这一完整性约束,并验证该触发器。CREATE TRIGGER student_cho ON student_course AFTER INSERTASBEGINIF EXISTS (SELECT sno FROM inserted WHERE sno in (S

9、ELECT sno FROMstudent WHERE dept=计算机系 ) AND (SELECT COUNT(*) FROM student_course WHEREstudent_course.sno=inserted.sno)3)BEGINPRINT 计算机系的学生选课不能超过三门ROLLBACK TRANSACTIONENDEND实验九(1) 利用游标逐行显示所查询的数据块的内容:在student表中定义一个包含sno、sname 、sex 和dept的只读游标,游标名为c_cursor,并将游标中的数据逐条显示出来。1在数据库引擎上查询文档中输入如下代码:declare c_cu

10、rsor scroll cursorforselect sno,sname,sex,deptfrom studentfor read onlyopen c_cursorfetch from c_cursor 2单击执行3接着读取游标中的第二行,在查询编辑器重输入如下语句:fetch from c_cursor4连续单击“执行”按钮,就可以逐条显示记录5最后关闭游标、释放游标close c_cursor(2) 利用游标显示指定行的数据的内容:在student表中定义一个所在系为“计算机系” ,包含sno、sname 、sex、和dept的游标,游标名为c_cursor ,完成如下操作:decla

11、re c_cursor scroll cursorforselect sno,sname,sex,deptfrom student where dept=计算机系for read onlyopen c_cursor1 读取第一行数据,并输出;fetch first from c_cursor2 读取最后一行数据,并输出;fetch last from c_cursor3 读取当前行的前一行数据,并输出;fetch prior from c_cursor4 读取从游标开始的第三行数据,并输出。fetch absolute 3 from c_cursor(3) 利用游标修改指定的数据元组:在stu

12、dent表中定义一个所在系为“计算机系” ,一个包含sno、sname 、sex、和dept的游标,游标名为c_cursor ,将游标中绝对位置为3的学生姓名改为“胡平” ,性别改为“男” 。declare c_cursor scroll cursorforselect sno,sname,sex,deptfrom student where dept=计算机系for update of sname,sexopen c_cursorfetch absolute 3 from c_cursorupdate studentset sname=胡平,sex=男where current of c_c

13、ursorfetch absolute 3 from c_cursor(4) 编写一个使用游标的存储过程并查看运行结果,要求该存储过程以课程名(cname )和系( dept)作为输入参数,计算指定系的学生指定课程的成绩分布情况,要求分别输出大于90,8089,7079,6069和60分以下的学生人数。create procedure proc_discname varchar(20),dept varchar(50)asbegindeclare c_cursor cursorselect count(*) less60from student_coursejoin student on student.sno=student_course.snojoin course on o=student_owhere dept=dept and cname=cname and score=60 and score=70 and score=80 and score=90open c_cursorwhile FETCH_STATUS=0beginfetch next FROM c_cursorendclose c_cursorendgoexecute proc_dis cname=计算机基础, dept=计算机系go

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 实验/测试

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号