在C表中添加一个平均成绩avg_Grade字段(记录每门课程的平均成绩),创建一个触发器Grade_modify,当SC表中的某学生的成绩发生变化时,则C表中的平均成绩也能及时相应的发生改变。
时间: 2024-02-13 20:04:35 浏览: 156
好的,你需要在SQL中实现这个触发器Grade_modify。以下是实现这个触发器的SQL代码:
```
ALTER TABLE C ADD COLUMN avg_grade FLOAT;
CREATE TRIGGER Grade_modify
AFTER UPDATE ON SC
FOR EACH ROW
BEGIN
UPDATE C SET avg_grade = (
SELECT AVG(grade) FROM SC WHERE cno = NEW.cno
) WHERE cno = NEW.cno;
END;
```
首先,我们需要向C表中添加一个平均成绩avg_grade字段,用于记录每门课程的平均成绩。然后,我们创建一个触发器Grade_modify,在SC表中的某学生的成绩发生变化时自动执行,它会计算相应课程的平均成绩,并将结果更新到C表中的avg_grade字段中。注意,触发器中用到了NEW关键字,它表示正在更新的行。
希望这个SQL代码可以帮助你实现这个触发器。
相关问题
能否提供一个关于如何使用MySQL编程语言实现数据库后台系统的详细设计示例或者步骤?
当然可以!以下是从《数据库系统课程设计项目任务书》中提取的相关信息,结合您的需求整理出的一个详细的MySQL数据库后台系统设计方案:
### 项目背景
本项目旨在设计并实现一个教学管理系统,主要涉及四个核心表:`student`(学生)、`course`(课程)、`teach`(教师)、`score`(成绩)。每个表都有特定的字段和关联关系,用于存储和管理相关的教学信息。
### 项目目标
- **数据库设计**:创建合理的数据库表结构,确保数据一致性、完整性。
- **功能实现**:实现基本的增删改查操作,同时支持复杂的查询和统计功能。
- **性能优化**:通过索引、视图等手段提高查询效率。
- **安全机制**:设置适当的权限,保证数据安全。
- **维护性**:代码编写规范,易于理解和维护。
### 技术栈
- **数据库**:MySQL
- **编程语言**:SQL
### 步骤详解
#### 1. 数据库设计
首先,创建数据库 `teachingdb` 和四个核心表。
```sql
CREATE DATABASE IF NOT EXISTS teachingdb;
USE teachingdb;
-- 创建学生表
CREATE TABLE student (
sno CHAR(5) PRIMARY KEY,
sname VARCHAR(50) NOT NULL,
sdept VARCHAR(50) NOT NULL,
sclass CHAR(2) NOT NULL,
ssex CHAR(1) NOT NULL,
sbirth DATE NOT NULL,
stotcredit INT DEFAULT 0
);
-- 创建课程表
CREATE TABLE course (
cno CHAR(3) PRIMARY KEY,
cname VARCHAR(50) NOT NULL UNIQUE,
cpcredit FLOAT NOT NULL,
chours INT NOT NULL
);
-- 创建教师表
CREATE TABLE teach (
tno CHAR(6) PRIMARY KEY,
tname VARCHAR(50) NOT NULL,
tsex CHAR(1) NOT NULL,
tdept VARCHAR(50) NOT NULL
);
-- 创建成绩表
CREATE TABLE score (
sno CHAR(5),
cno CHAR(3),
tno CHAR(6),
grade FLOAT,
PRIMARY KEY (sno, cno, tno),
FOREIGN KEY (sno) REFERENCES student(sno),
FOREIGN KEY (cno) REFERENCES course(cno),
FOREIGN KEY (tno) REFERENCES teach(tno)
);
```
#### 2. 数据插入
接下来,插入一些初始数据以供后续操作使用。
```sql
-- 插入学生数据
INSERT INTO student VALUES ('96001', '马小燕', '计算机', '01', '女', '2000-01-02', 0);
-- 更多数据省略...
-- 插入课程数据
INSERT INTO course VALUES ('001', '数学分析', 64, 4);
-- 更多数据省略...
-- 插入教师数据
INSERT INTO teach VALUES ('052501', '王成刚', '男', '计算机');
-- 更多数据省略...
-- 插入成绩数据
INSERT INTO score VALUES ('96001', '001', '052503', 77.5);
-- 更多数据省略...
```
#### 3. 功能实现
##### 基础操作
- **增加字段**
```sql
ALTER TABLE student ADD COLUMN nativeplace VARCHAR(20);
```
- **删除字段**
```sql
ALTER TABLE student DROP COLUMN nativeplace;
```
- **修改字段**
```sql
ALTER TABLE student MODIFY COLUMN ssex VARCHAR(3);
```
- **添加唯一性约束**
```sql
ALTER TABLE course ADD CONSTRAINT uk_cno UNIQUE (cname);
```
- **插入数据**
```sql
INSERT INTO student VALUES ('11111', '马明', '计算机', '01', '女', '2000-01-02', null);
```
- **更新数据**
```sql
UPDATE student SET sbirth = '2000-01-22' WHERE sname = '马小燕';
```
- **删除数据**
```sql
DELETE FROM student WHERE sno = '11111';
```
##### 复杂查询
- **求2000年出生的学生的学号、姓名**
```sql
SELECT sno AS 学号, sname AS 姓名 FROM student WHERE YEAR(sbirth) = 2000;
```
- **求选修了课程的学生学号,结果显示唯一**
```sql
SELECT DISTINCT sno FROM score;
```
- **求学时在1-50之间的课程信息**
```sql
SELECT * FROM course WHERE chours BETWEEN 1 AND 50;
```
- **求不是计算机系或信息系学生**
```sql
SELECT * FROM student WHERE sdept NOT IN ('计算机', '信息');
```
- **求姓名长度至少是三个汉字且倒数第三个汉字必须是“马”的学生**
```sql
SELECT * FROM student WHERE LENGTH(sname) >= 6 AND SUBSTRING(sname, -3, 1) = '马';
```
- **求选修'052501'老师,成绩在80至90之间,学号为96xxx的学生成绩信息**
```sql
SELECT * FROM score WHERE tno = '052501' AND grade BETWEEN 80 AND 90 AND sno LIKE '96%';
```
- **求没有成绩的学生的学号和课程号**
```sql
SELECT sno, cno FROM score WHERE grade IS NULL;
```
- **求学生总人数**
```sql
SELECT COUNT(*) AS 总人数 FROM student;
```
- **求选修了课程的学生人数**
```sql
SELECT COUNT(DISTINCT sno) AS 选修人数 FROM score;
```
- **求选修各门课程的最高、最低与平均成绩**
```sql
SELECT cno, MAX(grade) AS 最高分, MIN(grade) AS 最低分, AVG(grade) AS 平均分 FROM score GROUP BY cno;
```
- **求学生人数不足3人的系及其相应的学生数**
```sql
SELECT sdept, COUNT(*) AS 学生数 FROM student GROUP BY sdept HAVING COUNT(*) < 3;
```
- **求计算机系中各班的学生人数,结果按班级人数倒序排列**
```sql
SELECT sclass, COUNT(*) AS 学生数 FROM student WHERE sdept = '计算机' GROUP BY sclass ORDER BY 学生数 DESC;
```
- **使用自然连接求选修了严敏老师的数学分析课程的学生的姓名、课程名称、教师名和成绩**
```sql
SELECT s.sname, c.cname, t.tname, sc.grade
FROM student s
NATURAL JOIN score sc
NATURAL JOIN course c
NATURAL JOIN teach t
WHERE c.cname = '数学分析' AND t.tname = '严敏';
```
- **使用等值连接求选修数学分析课程的学生的姓名、课程名称和成绩**
```sql
SELECT s.sname, c.cname, sc.grade
FROM student s
JOIN score sc ON s.sno = sc.sno
JOIN course c ON sc.cno = c.cno
WHERE c.cname = '数学分析';
```
- **使用JOIN连接求选修了课程的学生的学号和姓名**
```sql
SELECT s.sno, s.sname
FROM student s
JOIN score sc ON s.sno = sc.sno;
```
- **使用自身连接查询求年龄大于'刘东明'的所有学生的姓名与出生日期**
```sql
SELECT s1.sname, s1.sbirth
FROM student s1
JOIN student s2 ON s1.sbirth < s2.sbirth
WHERE s2.sname = '刘东明';
```
- **使用外部连接查询求未选修任何课程的学生的学号和姓名**
```sql
SELECT s.sno, s.sname
FROM student s
LEFT JOIN score sc ON s.sno = sc.sno
WHERE sc.sno IS NULL;
```
- **使用不相关子查询求年龄大于'刘东明'的所有学生的姓名与出生日期**
```sql
SELECT sname, sbirth
FROM student
WHERE sbirth < (SELECT sbirth FROM student WHERE sname = '刘东明');
```
- **求比数学系中全体学生年龄大的学生的姓名和系**
```sql
SELECT sname, sdept
FROM student
WHERE sbirth > ALL (SELECT sbirth FROM student WHERE sdept = '数学');
```
- **求选修了004课程的学生的姓名和系**
```sql
SELECT s.sname, s.sdept
FROM student s
JOIN score sc ON s.sno = sc.sno
WHERE sc.cno = '004';
```
- **求选修了'刘东明'同学选修的全部课程的学生的学号**
```sql
SELECT sno
FROM score
WHERE cno IN (SELECT cno FROM score WHERE sno = (SELECT sno FROM student WHERE sname = '刘东明'))
GROUP BY sno
HAVING COUNT(DISTINCT cno) = (SELECT COUNT(DISTINCT cno) FROM score WHERE sno = (SELECT sno FROM student WHERE sname = '刘东明'));
```
- **求选修了全部课程的学生的学号**
```sql
SELECT sno
FROM score
GROUP BY sno
HAVING COUNT(DISTINCT cno) = (SELECT COUNT(*) FROM course);
```
##### 高级功能
- **创建存储过程 pro_findname**
```sql
DELIMITER //
CREATE PROCEDURE pro_findname(IN keyword VARCHAR(20))
BEGIN
SELECT * FROM student WHERE sname LIKE CONCAT('%', keyword, '%');
END //
DELIMITER ;
```
- **设计函数 count_credit**
```sql
DELIMITER //
CREATE FUNCTION count_credit(sno CHAR(5)) RETURNS INT
BEGIN
DECLARE total INT DEFAULT 0;
SELECT SUM(c.cpcredit) INTO total
FROM score sc
JOIN course c ON sc.cno = c.cno
WHERE sc.sno = sno AND sc.grade >= 60;
RETURN total;
END //
DELIMITER ;
```
- **创建存储过程 p_count_credit**
```sql
DELIMITER //
CREATE PROCEDURE p_count_credit()
BEGIN
UPDATE student s
SET stotcredit = (SELECT count_credit(s.sno));
END //
DELIMITER ;
```
- **创建触发器 sum_credit**
```sql
DELIMITER //
CREATE TRIGGER sum_credit AFTER INSERT ON score
FOR EACH ROW
BEGIN
DECLARE credit FLOAT;
SELECT cpcredit INTO credit FROM course WHERE cno = NEW.cno;
IF NEW.grade >= 60 THEN
UPDATE student SET stotcredit = stotcredit + credit WHERE sno = NEW.sno;
END IF;
END //
DELIMITER ;
```
- **创建级联删除触发器 del_student_score**
```sql
DELIMITER //
CREATE TRIGGER del_student_score BEFORE DELETE ON student
FOR EACH ROW
BEGIN
DELETE FROM score WHERE sno = OLD.sno;
END //
DELIMITER ;
```
### 项目报告
项目报告应包含以下内容:
- **数据库设计**:描述数据库的整体架构和各个表的设计细节。
- **功能实现**:展示主要功能的实现代码和测试结果。
- **数据备份与恢复计划**:详细说明数据备份策略和恢复步骤。
- **系统性能优化**:介绍如何通过索引、视图等手段提高查询效率。
- **安全机制**:描述如何设置权限以保护数据安全。
- **维护性**:讨论代码的可读性和可维护性。
希望以上内容对您有所帮助!如果有更多具体需求或疑问,请随时提问。
帮我建出相关表并且回答相关问题
好的,我将帮助您构建相关的表并回答您的问题。首先,我们来创建所需的表:
### 创建表的 SQL 语句
#### 学生表 `student`
```sql
CREATE TABLE student (
sno CHAR(5) PRIMARY KEY,
sname VARCHAR(20) NOT NULL,
sdept VARCHAR(20) NOT NULL,
sclass VARCHAR(10) NOT NULL,
ssex VARCHAR(10),
sbirth DATE,
scredit INT
);
```
#### 课程表 `course`
```sql
CREATE TABLE course (
cno CHAR(3) PRIMARY KEY,
cname VARCHAR(50) UNIQUE,
chour INT,
ccredit FLOAT
);
```
#### 教师表 `teach`
```sql
CREATE TABLE teach (
tno CHAR(6) PRIMARY KEY,
tname VARCHAR(20),
tsex VARCHAR(10),
tdept VARCHAR(20)
);
```
#### 成绩表 `score`
```sql
CREATE TABLE score (
sno CHAR(5),
cno CHAR(3),
tno CHAR(6),
grade FLOAT,
PRIMARY KEY (sno, cno, tno),
FOREIGN KEY (sno) REFERENCES student(sno),
FOREIGN KEY (cno) REFERENCES course(cno),
FOREIGN KEY (tno) REFERENCES teach(tno)
);
```
### 插入数据
#### 插入学生表 `student` 的数据
```sql
INSERT INTO student VALUES ('96001', '马小燕', '计算机', '01', '女', '2000-01-02', 0);
INSERT INTO student VALUES ('96002', '黎明', '计算机', '01', '男', '2000-03-05', 0);
INSERT INTO student VALUES ('96003', '刘东明', '数学', '01', '男', '2000-10-05', 0);
INSERT INTO student VALUES ('96004', '赵志勇', '信息', '02', '男', '2000-08-08', 0);
INSERT INTO student VALUES ('97001', '马蓉', '数学', '02', '女', '2001-03-04', 0);
INSERT INTO student VALUES ('97002', '李成功', '计算机', '01', '男', '2001-09-10', 0);
INSERT INTO student VALUES ('97003', '黎明', '信息', '03', '女', '2002-02-08', 0);
INSERT INTO student VALUES ('97004', '李丽', '计算机', '02', '女', '2002-01-05', 0);
INSERT INTO student VALUES ('96005', '司马志明', '计算机', '02', '男', '2001-11-23', 0);
```
#### 插入课程表 `course` 的数据
```sql
INSERT INTO course VALUES ('001', '数学分析', 64, 4);
INSERT INTO course VALUES ('002', '普通物理', 64, 4);
INSERT INTO course VALUES ('003', '微机原理', 56, 3.5);
INSERT INTO course VALUES ('004', '数据结构', 64, 4);
INSERT INTO course VALUES ('005', '操作系统', 56, 3.5);
INSERT INTO course VALUES ('006', '数据库原理', 56, 3.5);
INSERT INTO course VALUES ('007', '编译原理', 48, 3);
INSERT INTO course VALUES ('008', '程序设计', 32, 2);
```
#### 插入教师表 `teach` 的数据
```sql
INSERT INTO teach VALUES ('052501', '王成刚', '男', '计算机');
INSERT INTO teach VALUES ('052502', '李正科', '男', '计算机');
INSERT INTO teach VALUES ('052503', '严敏', '女', '数学');
INSERT INTO teach VALUES ('052504', '赵高', '男', '数学');
INSERT INTO teach VALUES ('052505', '刘玉兰', '女', '计算机');
INSERT INTO teach VALUES ('052506', '王成刚', '男', '信息');
INSERT INTO teach VALUES ('052507', '马悦', '女', '计算机');
```
#### 插入成绩表 `score` 的数据
```sql
INSERT INTO score VALUES ('96001', '001', '052503', 77.5);
INSERT INTO score VALUES ('96001', '003', '052501', 89);
INSERT INTO score VALUES ('96001', '004', '052502', 86);
INSERT INTO score VALUES ('96001', '005', '052505', 82);
INSERT INTO score VALUES ('96002', '001', '052504', 88);
INSERT INTO score VALUES ('96002', '003', '052502', 92.5);
INSERT INTO score VALUES ('96002', '006', '052507', 90);
INSERT INTO score VALUES ('96005', '004', '052502', 92);
INSERT INTO score VALUES ('96005', '005', '052505', 90);
INSERT INTO score VALUES ('96005', '006', '052505', 89);
INSERT INTO score VALUES ('96005', '007', '052507', 78);
INSERT INTO score VALUES ('96003', '001', '052504', 69);
INSERT INTO score VALUES ('97001', '001', '052504', 96);
INSERT INTO score VALUES ('97001', '008', '052505', 95);
INSERT INTO score VALUES ('96004', '001', '052503', 87);
INSERT INTO score VALUES ('96003', '003', '052501', 91);
INSERT INTO score VALUES ('97002', '003', '052502', 91);
INSERT INTO score VALUES ('97002', '004', '052505', NULL);
INSERT INTO score VALUES ('97002', '006', '052507', 92);
INSERT INTO score VALUES ('97004', '005', '052502', 90);
INSERT INTO score VALUES ('97004', '006', '052501', 85);
```
### 实现相关功能
#### 基础部分
1. **在 `student` 表中增加籍贯字段**
```sql
ALTER TABLE student ADD COLUMN nativeplace VARCHAR(20);
```
2. **删除 `student` 表中籍贯字段**
```sql
ALTER TABLE student DROP COLUMN nativeplace;
```
3. **将 `student` 表中的 `ssex` 字段定义改为 `VARCHAR(3)`**
```sql
ALTER TABLE student MODIFY COLUMN ssex VARCHAR(3);
```
4. **在 `course` 表中 `cname` 上添加唯一性约束 `uk_cno`**
```sql
ALTER TABLE course ADD CONSTRAINT uk_cno UNIQUE (cname);
```
5. **在 `student` 表中插入一条数据**
```sql
INSERT INTO student VALUES ('11111', '马明', '计算机', '01', '女', '2000-01-02', null);
```
6. **将 `student` 表中所有学生的总学分加2,空值记录也参加计算**
```sql
UPDATE student SET scredit = IFNULL(scredit, 0) + 2;
```
7. **将 `student` 表中马小燕的出生日期修改为 ‘2000-01-22’**
```sql
UPDATE student SET sbirth = '2000-01-22' WHERE sname = '马小燕';
```
8. **删除 `student` 表中学号为 '11111' 的记录**
```sql
DELETE FROM student WHERE sno = '11111';
```
9. **利用 `student` 表创建表 `s1`,`s1` 的结构与内容与 `student` 表完全相同**
```sql
CREATE TABLE s1 AS SELECT * FROM student;
```
10. **删除学生表 `s1` 中计算机系学生信息**
```sql
DELETE FROM s1 WHERE sdept = '计算机';
```
11. **删除 `s1` 表**
```sql
DROP TABLE s1;
```
12. **求2000年出生的学生的学号、姓名,结果列别名为汉字**
```sql
SELECT sno AS '学号', sname AS '姓名' FROM student WHERE YEAR(sbirth) = 2000;
```
13. **求选修了课程的学生学号,结果表中学号显示唯一**
```sql
SELECT DISTINCT sno FROM score;
```
14. **求学时在 1-50 之间的课程信息**
```sql
SELECT * FROM course WHERE chour BETWEEN 1 AND 50;
```
15. **求不是 计算机系或信息系学生**
```sql
SELECT * FROM student WHERE sdept NOT IN ('计算机', '信息');
```
16. **求姓名长度至少是三个汉字且倒数第三个汉字必须是“马”的学生**
```sql
SELECT * FROM student WHERE LENGTH(sname) >= 3 AND SUBSTRING(sname, -3, 1) = '马';
```
17. **求选修 '052501' 老师,成绩在80至90之间,学号为96xxx的学生成绩信息**
```sql
SELECT * FROM score WHERE tno = '052501' AND grade BETWEEN 80 AND 90 AND sno LIKE '96%';
```
18. **求没有 成绩的学生的学号和课程号**
```sql
SELECT sno, cno FROM score WHERE grade IS NULL;
```
19. **求学生总人数**
```sql
SELECT COUNT(*) AS '学生总人数' FROM student;
```
20. **求选修了课程的学生人数, 输出列的别名为“选修人数”**
```sql
SELECT COUNT(DISTINCT sno) AS '选修人数' FROM score;
```
21. **求选修各门课程的最高、最低与平均成绩**
```sql
SELECT cno, MAX(grade) AS '最高成绩', MIN(grade) AS '最低成绩', AVG(grade) AS '平均成绩'
FROM score
GROUP BY cno;
```
22. **求学生人数不足3人的系及其相应的学生数**
```sql
SELECT sdept, COUNT(*) AS '学生数'
FROM student
GROUP BY sdept
HAVING COUNT(*) < 3;
```
23. **求计算机系中各班的学生人数, 结果按班级人数倒序排列**
```sql
SELECT sclass, COUNT(*) AS '学生人数'
FROM student
WHERE sdept = '计算机'
GROUP BY sclass
ORDER BY COUNT(*) DESC;
```
24. **使用自然连接求选修了严敏老师的数学分析课程的学生的姓名、课程名称、教师名和成绩**
```sql
SELECT s.sname, c.cname, t.tname, sc.grade
FROM student s
NATURAL JOIN score sc
NATURAL JOIN course c
NATURAL JOIN teach t
WHERE c.cname = '数学分析' AND t.tname = '严敏';
```
25. **使用等值连接求选修数学分析课程的学生的姓名、课程名称和成绩**
```sql
SELECT s.sname, c.cname, sc.grade
FROM student s
INNER JOIN score sc ON s.sno = sc.sno
INNER JOIN course c ON sc.cno = c.cno
WHERE c.cname = '数学分析';
```
26. **使用 JOIN 连接求选修了课程的学生的学号和姓名**
```sql
SELECT s.sno, s.sname
FROM student s
INNER JOIN score sc ON s.sno = sc.sno;
```
27. **使用自身连接查询 求年龄大于'刘东明' 的所有学生的姓名与出生日期**
```sql
SELECT s1.sname, s1.sbirth
FROM student s1
INNER JOIN student s2 ON s1.sbirth < s2.sbirth
WHERE s2.sname = '刘东明';
```
28. **使用外部连接查询求未选修任何课程的学生的学号和姓名**
```sql
SELECT s.sno, s.sname
FROM student s
LEFT JOIN score sc ON s.sno = sc.sno
WHERE sc.sno IS NULL;
```
29. **使用不相关子查询 求年龄大于'刘东明' 的所有学生的姓名与出生日期**
```sql
SELECT sname, sbirth
FROM student
WHERE sbirth < (SELECT sbirth FROM student WHERE sname = '刘东明');
```
30. **求未选修任何课程的学生的学号和姓名**
```sql
SELECT sno, sname
FROM student
WHERE sno NOT IN (SELECT sno FROM score);
```
31. **求比数学系中全体学生年龄大的学生的姓名和系**
```sql
SELECT sname, sdept
FROM student
WHERE sbirth < ALL (SELECT sbirth FROM student WHERE sdept = '数学');
```
32. **求选修了004课程的学生的姓名和系**
```sql
SELECT s.sname, s.sdept
FROM student s
INNER JOIN score sc ON s.sno = sc.sno
WHERE sc.cno = '004';
```
33. **求选修了'刘东明'同学选修的全部课程的学生的学号**
```sql
SELECT sno
FROM score
WHERE cno IN (SELECT cno FROM score WHERE sno = (SELECT sno FROM student WHERE sname = '刘东明'))
GROUP BY sno
HAVING COUNT(cno) = (SELECT COUNT(cno) FROM score WHERE sno = (SELECT sno FROM student WHERE sname = '刘东明'));
```
34. **求选修了全部课程的学生的学号**
```sql
SELECT sno
FROM score
GROUP BY sno
HAVING COUNT(DISTINCT cno) = (SELECT COUNT(*) FROM course);
```
### 高级部分
1. **创建存储过程 `pro_findname` 对学生姓名进行模糊查找**
```sql
DELIMITER //
CREATE PROCEDURE pro_findname(IN name_part VARCHAR(20))
BEGIN
SELECT * FROM student WHERE sname LIKE CONCAT('%', name_part, '%');
END //
DELIMITER ;
```
2. **设计函数 `count_credit`,根据学号计算该学生的总学分**
```sql
DELIMITER //
CREATE FUNCTION count_credit(sno_in CHAR(5)) RETURNS INT
BEGIN
DECLARE total_credit INT DEFAULT 0;
SELECT SUM(c.credit) INTO total_credit
FROM score s
INNER JOIN course c ON s.cno = c.cno
WHERE s.sno = sno_in AND s.grade >= 60;
RETURN total_credit;
END //
DELIMITER ;
```
3. **创建存储过程 `p_count_credit`,调用 `count_credit` 函数更新学生表的总学分值**
```sql
DELIMITER //
CREATE PROCEDURE p_count_credit()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE current_sno CHAR(5);
DECLARE cur CURSOR FOR SELECT sno FROM student;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
read_loop: LOOP
FETCH cur INTO current_sno;
IF done THEN
LEAVE read_loop;
END IF;
UPDATE student SET scredit = count_credit(current_sno) WHERE sno = current_sno;
END LOOP;
CLOSE cur;
END //
DELIMITER ;
```
4. **创建触发器 `sum_credit`,实现对 `student` 表总学分的计算**
```sql
DELIMITER //
CREATE TRIGGER sum_credit AFTER INSERT ON score
FOR EACH ROW
BEGIN
DECLARE total_credit INT DEFAULT 0;
SELECT SUM(c.credit) INTO total_credit
FROM score s
INNER JOIN course c ON s.cno = c.cno
WHERE s.sno = NEW.sno AND s.grade >= 60;
UPDATE student SET scredit = total_credit WHERE sno = NEW.sno;
END //
DELIMITER ;
```
5. **创建级联删除触发器 `del_student_score`,当删除 `student` 表中的学生时,也删除 `score` 表中的对应学号的学生成绩记录**
```sql
DELIMITER //
CREATE TRIGGER del_student_score BEFORE DELETE ON student
FOR EACH ROW
BEGIN
DELETE FROM score WHERE sno = OLD.sno;
END //
DELIMITER ;
```
以上是为您创建相关表并实现相关功能的 SQL 语句。如果您有任何进一步的问题或需要更多的帮助,请随时告诉我!
阅读全文