对学生-课程数据库编写存储过程,完成下述功能:(1)统计离散数学的成绩分布情况,即按照各分数段统计人数(2)统计任意一门课的平均成绩(3)将学生选课成绩从百分制改为等级制(A、B、C、D、E)
时间: 2024-05-15 14:13:13 浏览: 154
以下是对应的三个存储过程:
1. 统计离散数学的成绩分布情况
```
CREATE PROCEDURE `DiscreteMathScoreDistribution`()
BEGIN
SELECT '90-100' AS 'Score Range', COUNT(*) AS 'Number of Students'
FROM student_course
WHERE course_id = 'MATH101' AND score >= 90 AND score <= 100
UNION
SELECT '80-89', COUNT(*)
FROM student_course
WHERE course_id = 'MATH101' AND score >= 80 AND score < 90
UNION
SELECT '70-79', COUNT(*)
FROM student_course
WHERE course_id = 'MATH101' AND score >= 70 AND score < 80
UNION
SELECT '60-69', COUNT(*)
FROM student_course
WHERE course_id = 'MATH101' AND score >= 60 AND score < 70
UNION
SELECT '0-59', COUNT(*)
FROM student_course
WHERE course_id = 'MATH101' AND score >= 0 AND score < 60;
END
```
2. 统计任意一门课的平均成绩
```
CREATE PROCEDURE `CourseAvgScore`(IN courseID VARCHAR(10))
BEGIN
SELECT AVG(score) AS 'Average Score'
FROM student_course
WHERE course_id = courseID;
END
```
3. 将学生选课成绩从百分制改为等级制
```
CREATE PROCEDURE `ConvertToGradeScore`()
BEGIN
UPDATE student_course
SET score =
CASE
WHEN score >= 90 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'
ELSE 'E'
END;
END
```
注意事项:
- 第一个存储过程中的 course_id = 'MATH101' 需要改为对应需要统计的课程ID
- 第二个存储过程中的 IN courseID 可以改为任意需要统计平均成绩的课程ID
- 第三个存储过程将原来的分数覆盖为等级,需要谨慎操作,并且要确保该过程只执行一次。
阅读全文