失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 存储过程 触发器和用户自定义函数实验 (存储过程)

存储过程 触发器和用户自定义函数实验 (存储过程)

时间:2023-05-23 14:25:46

相关推荐

存储过程 触发器和用户自定义函数实验 (存储过程)

存储过程、触发器和用户自定义函数实验

实验内容一

练习教材中存储过程、触发器和用户自定义函数的例子。教材中的BookSales数据库,在群共享中,文件名为BookSales.bak。

实验内容二

针对附件1中的教学活动数据库,完成下面的实验内容。

1、存储过程

(1)创建一个存储过程,该存储过程统计“高等数学”的成绩分布情况,即按照各分数段统计人数。

CREATE Proc MATH_NUM @MATH CHAR(20)='高等数学'

AS

SELECT @MATH as canme,

count(case when score>=90 then 1 end)as[90以上],

count(case when score>=80 and score<90 then 1 end)as[80-90],

count(case when score>=70 and score<80 then 1 end)as[70-80],

count(case when score>=60 and score<70 then 1 end)as[60-70],

count(case when score<60 then 1 end)as[60以下] FROM study,course

WHERE o=o and ame=@MATH

GROUP BY ame

(2)创建一个存储过程,该存储过程有一个参数用来接收课程号,该存储过程统计给定课程的平均成绩。

CREATE Proc AVG_SCORE @cno CHAR(20)

AS

SELECT @cno as 课程号,ame as 课程名,STR(AVG(score),5,2) as 平均成绩

FROM study,course

WHERE o=o and o=@cno

GROUP BY ame

(3)创建一个存储过程,该存储过程将学生选课成绩从百分制改为等级制(即 A、B、C、D、E)。

CREATE Proc SCORE_CHANGE

AS

SELECT ame as 课程名,study.sno as 学号,o as 课程号,study.score as 成绩,

case

when score>=90 and score<=100 then'A'

when score>=80 and score<90 then'B'

when score>=70 and score<80 then'C'

when score>=60 and score<70 then'D'

when score<60 then'E'

end as '等级'

from study,course

where o=o

(4)创建一个存储过程,该存储过程有一个参数用来接收学生姓名,该存储过程查询该学生的学号以及选修课程的门数。

CREATE Proc STUDENT_STUDY @name varchar(20)

AS

select @name as 姓名,study.sno as 学号,count(cno) as 选修门数

from study,student

where study.sno=student.sno and sname=@name

group by study.sno

(5)创建一个存储过程,该存储过程有两个输入参数用来接收学号和课程号,一个输出参数用于获取相应学号和课程号对应的成绩。

CREATE Proc STU_COR_SCORE @sno varchar(20),@cno char(20),@word smallint output

AS

select @word=score

from study

where sno=@sno and cno=@cno

实验数据库说明

教学活动数据库包括student、course和study三个基本表,三个基本表的结构说明和数据如下:

(1)学生表(student)

学生表的结构

说明:sno为主键,age的范围为15~35之间,sex只能为“男”或“女”。

CREATE TABLE student (

age smallint check (age >= 15 and age <= 35),

sex varchar(20) check (sex in('男','女')) --性别

sno varchar(20) PRIMARY KEY not null,

sname varchar(20) not null, --姓名

)

insert into student values( '98601' ,'李强',20,'男')

insert into student values( '98602', '刘丽', 21, '男');

insert into student values( '98603', '张兵', 20, '男');

insert into student values( '98604', '陈志坚', 22, '男');

insert into student values( '98605', '王颖', 21,'男');

学生表的记录

(2)课程表(course)

课程表的结构

说明:cno为主键。

create table course

(

cno char(20) not null primary key, --课程编号

cname char(20) not null ,--课程名称

teacher char(20)

)

insert into course

values(

'C601', '高等数学','周振兴')

insert into course

values(

'C602',' 数据结构','刘建平')

insert into course

values(

'C603', '操作系统','刘建平')

insert into course

values(

'C604', '编译原理','王志伟')

课程表的记录

(3)选课表(study)

create table study

(

sno varchar(20) not null ,--学生学号

cno char(20), --上课编号

score smallint ,

primary key(sno,cno),

foreign key(sno) references student(sno),

foreign key(cno) references course(cno)

)

insert into study

values

('98601','C601',90)

insert into study

values(

'98601','C602', 90)

insert into study

values(

'98601', 'C603',85)

insert into study

values(

'98601', 'C604',87)

insert into study

values(

'98602', 'C601',90)

insert into study

values(

'98603', 'C601',75)

insert into study

values(

'98603', 'C602',70)

insert into study

values(

'98603', 'C604',56)

insert into study

values(

'98604', 'C601',90)

insert into study values(

'98604', 'C604',85)

insert into study

values(

'98605','C601', 95)

insert into study

values(

'98605', 'C603',80)

选课表的结构

说明:sno和cno为主键,sno为外键(参照student表的sno),cno为外键(参照course表的cno),score的范围为0~100之间。

选课表的记录

如果觉得《存储过程 触发器和用户自定义函数实验 (存储过程)》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。