数据库原理与应用实验-视图与存储实验

本文详细介绍了在数据库原理与应用实验中,如何通过创建视图IS_Student、IS_1、IS_SG等来操作信息系学生数据,以及如何运用存储过程StuBy2009、math_Student和P_DeptNum统计学生数量和系别。涵盖了视图的CRUD操作和存储过程的基本应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

一、实验目的

 二、实验内容

1. 建立信息系学生的视图IS_Student。

2. 建立信息系学生的视图,并要求透过该视图进行的更新操作只涉及信息系学生。

3.建立信息系选修了1号课程的学生视图IS_1。

4. 建立信息系选修了1号课程且成绩在90分以上的学生的视图。

5. 将学生的学号及他的平均成绩定义为一个视图S_G。

6. 在信息系学生的视图中找出年龄小于20岁的学生。

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掌握视图的insertupdatedelete操作

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+'个'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sec0nd_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值