存储过程、触发器和用户自定义函数实验 (存储过程)
来源:互联网 发布:数控宏程序编程100例 编辑:程序博客网 时间:2024/06/04 14:21
存储过程、触发器和用户自定义函数实验
实验内容一
练习教材中存储过程、触发器和用户自定义函数的例子。教材中的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 study.cno=course.cno and course.cname=@MATH
GROUP BY course.cname
(2)创建一个存储过程,该存储过程有一个参数用来接收课程号,该存储过程统计给定课程的平均成绩。
CREATE Proc AVG_SCORE @cno CHAR(20)
AS
SELECT @cno as 课程号,course.cname as 课程名,STR(AVG(score),5,2) as 平均成绩
FROM study,course
WHERE study.cno=course.cno and course.cno=@cno
GROUP BY course.cname
(3)创建一个存储过程,该存储过程将学生选课成绩从百分制改为等级制(即 A、B、C、D、E)。
CREATE Proc SCORE_CHANGE
AS
SELECT course.cname as 课程名,study.sno as 学号,study.cno 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 study.cno=course.cno
(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
char
5
NO
学号
sname
char
8
NO
姓名
age
smallint
年龄
sex
nchar
1
性别
说明: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,'男');
学生表的记录
sno
sname
age
sex
98601
李强
20
男
98602
刘丽
21
女
98603
张兵
20
男
98604
陈志坚
22
男
98605
王颖
21
女
(2)课程表(course)
课程表的结构
列名
数据类型
长度
是否允许为空值
说明
cno
char
4
NO
课程号
cname
char
20
NO
课程名
teacher
char
8
任课教师
说明: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', '编译原理','王志伟')
课程表的记录
cno
cname
teacher
C601
高等数学
周振兴
C602
数据结构
刘建平
C603
操作系统
刘建平
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
char
5
NO
学号
cno
char
4
NO
课程号
score
smallint
成绩
说明:sno和cno为主键,sno为外键(参照student表的sno),cno为外键(参照course表的cno),score的范围为0~100之间。
选课表的记录
sno
cno
score
98601
C601
90
98601
C602
90
98601
C603
85
98601
C604
87
98602
C601
90
98603
C601
75
98603
C602
70
98603
C604
56
98604
C601
90
98604
C604
85
98605
C601
95
98605
C603
80
- 存储过程、触发器和用户自定义函数实验2、触发器
- 存储过程、触发器和用户自定义函数实验 (存储过程)
- 存储过程、触发器和用户自定义函数实验(用户自定义函数)
- 存储过程,函数和触发器
- Oracle存储过程、自定义函数、和触发器 示例
- 存储过程和触发器以及自定义函数的区别
- orcl中的存储过程,自定义函数,触发器
- 存储过程、函数、触发器
- 存储过程、函数、触发器
- 存储过程,函数,触发器
- 存储过程,存储函数,触发器。。。
- mysql 自定义存储过程和触发器
- 触发器和存储过程
- 触发器和存储过程
- 触发器和存储过程
- 触发器和存储过程
- 存储过程和触发器
- 存储过程和触发器
- Evolve Your Hierarchy : Refactoring Game Entities with Components
- 神奇的Android Studio Template
- 一个 SQL 分段排序的问题(也可以理解为 段内汇总排序问题)
- Excel -- 条件统计函数
- Scapy中文使用文档
- 存储过程、触发器和用户自定义函数实验 (存储过程)
- sql语句判断两个时间段是否有交集
- Android Studio教程从入门到精通
- c++实验7--最大公约数与最小公倍数
- c++拾遗-----内存模型
- c++作业7
- HDU:2087 剪花布条(水)
- 养殖的设想
- vim简易教程