SQL Server实验三_第1页
SQL Server实验三_第2页
SQL Server实验三_第3页
SQL Server实验三_第4页
SQL Server实验三_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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.sno join course on o=student_o运行结果代码:use Studbgoexecute s

2、tudent_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 tinyi

5、ntasbeginset 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 NULLDROP

6、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 deleted)end运行结果代码:

7、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 IN (SELECT sno FROM DELE

8、TED)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 (SELECT sno FROMstudent WHE

9、RE 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_cursor scroll cursorforselect

10、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,完成如下操作:declare c_cursor scroll cursorforsel

11、ect 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) 利用游标修改指定的数据元组:在student表中定义一个所在系为“计算机系”,一个包含sno、sn

12、ame、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_cursorfetch absolute 3 from c_cursor(

13、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_ow

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论