目录
2. 建立信息系学生的视图,并要求透过该视图进行的更新操作只涉及信息系学生。
4. 建立信息系选修了1号课程且成绩在90分以上的学生的视图。
7. 在S_G视图中查询平均成绩在90分以上的学生学号和平均成绩。
8. 向信息系学生视图IS_Studnet中插入一个新的学生记录:2011029,赵新,20岁。
9. 删除视图IS_Student中学号为2011029的记录。
10. 创建一个名为StuBy2009的存储过程,返回2009级的所有学生的信息。
11. 创建一个名为math_Student的存储过程,返回student表中数学系的学生信息。
13. 创建存储过程P_DeptNum,要求能根据用户给定的系代码,统计该系的人数,并将人数通过输出变量返回给用户。如要查询纱代码为’MA’的学生人数。
14. 创建存储过程Cou_credit,要求能根据用户给定的学分值,统计满足该学分值的课程数目,并把它返回给调用程序。如要统计2个学分的课程门数,如何调用上述存储过程?
实验报告 | ||||||||
开课学院及实验室: | ||||||||
学 院 | 年级、专业、班 | 姓名 | 学号 | |||||
实验课程名称 | 数据库原理与应用实验 | 成绩 |
| |||||
实验项目名称 | 视图与存储实验 | 指 导教师 |
|
一、实验目的
1掌握视图的insert,update,delete操作
2.掌握存储过程的创建与执行。
二、实验内容
使用的是上次实验的环境
1. 建立信息系学生的视图IS_Student。
sql语句:
go
create view IS_Student(sno,sname,ssex,sage,sdept)
as
select sno,sname,ssex,sage,sdept
from Student
where Sdept = 'IS';
Go
因为下面一步还要加上更新操作,此步没有执行
2. 建立信息系学生的视图,并要求透过该视图进行的更新操作只涉及信息系学生。
sql语句:
go
create view IS_Student(sno,sname,ssex,sage,sdept)
as
select sno,sname,ssex,sage,sdept
from Student
where Sdept = 'IS' with check option;
go
3.建立信息系选修了1号课程的学生视图IS_1。
sql语句:
create view IS_C1(sno,sname,grade)
as
select student.sno,sname,grade
from Student,SC
where Sdept = 'IS' /*信息系学生*/
and Student.sno = sc.Sno
and sc.Cno = '1'; /*选修了课程号为1的*/
go
select * from IS_C1; /*查看IS_C1视图*/
4. 建立信息系选修了1号课程且成绩在90分以上的学生的视图。
sql语句:
create view IS_SG(sno,sname,grade)
as
select student.sno,sname,grade
from Student,SC
where Sdept = 'IS'
and Student.sno = sc.Sno
and sc.Cno = '1'
and Grade>=90; /*限制分数在90以上*/
go
select * from IS_SG;
5. 将学生的学号及他的平均成绩定义为一个视图S_G。
sql语句:
create view S_G(sno,avggrade)
as
select sno,AVG(grade)
from SC
group by sno;
go
select * from S_G;
6. 在信息系学生的视图中找出年龄小于20岁的学生。
sql语句:
select * from IS_Student
where sage<20;
7. 在S_G视图中查询平均成绩在90分以上的学生学号和平均成绩。
sql语句:
select *
from S_G
where avggrade>90
8. 向信息系学生视图IS_Studnet中插入一个新的学生记录:2011029,赵新,20岁。
sql语句:
insert into IS_Student values('2011029','赵新','男',20,'IS')
select * from IS_Student;
9. 删除视图IS_Student中学号为2011029的记录。
sql语句:
go
delete
from IS_Student
where sno='2011029';
go
select * from student;
10. 创建一个名为StuBy2009的存储过程,返回2009级的所有学生的信息。
sql语句:
create procedure StuBy2009
as
select *
from Student
where Sgrade='2009'
go
exec StuBy2009 /*调用存储过程*/
11. 创建一个名为math_Student的存储过程,返回student表中数学系的学生信息。
sql语句:
create procedure math_Student
as
select *
from Student
where Sdept='MA'
go
exec math_Student /*调用存储过程*/
12. 创建一个名为StuBySno的存储过程,该存储过程根据给定的学号显示相应学生的信息。
sql语句:
create procedure StuBySno
@S_no char(9)
as
select *
from Student join SC on Student.Sno=sc.Sno
where student.Sno=@S_no
go
declare @S_no char(9) /*调用存储过程查询学号为201105121的个人信息和所有课程的成绩*/
set @S_no='201105121'
exec StuBySno @S_no
13. 创建存储过程P_DeptNum,要求能根据用户给定的系代码,统计该系的人数,并将人数通过输出变量返回给用户。如要查询纱代码为’MA’的学生人数。
sql语句:
create procedure P_DeptNum
@dept char(15)=null,@renshu char(2) output
as
select @renshu=count(*)
from Student
where Sdept=@dept
go
declare @renshu char(2) /*调用存储过程*/
exec P_DeptNum 'MA',@renshu output
print '该系别的人数有:'+@renshu+'个'
14. 创建存储过程Cou_credit,要求能根据用户给定的学分值,统计满足该学分值的课程数目,并把它返回给调用程序。如要统计2个学分的课程门数,如何调用上述存储过程?
sql语句:
create procedure Cou_credit
@credit int,@kechengshu char(2) output
as
select @kechengshu=count(*)
from Course
where Ccredit=@credit
go
declare @kechengshu char(2) /*调用存储过程*/
exec Cou_credit '2',@kechengshu output
print '该学分对应的课程门数有:'+@kechengshu+'个'