在一个成绩管理系统数据库GradeDB中,定义如下关系表: STUDENT(SID,SName,Age,Sex) GRADE(SID,CID,Score, Note) COURSE(CID,CName,Teacher) (1)编写SQL程序创建数据库及其关系表,并插入20个学生的2门课程(“数据库原理及应用”、“数据结构与算法”)成绩数据; (2)GradeLOG表的信息包括user操作用户、SID学号、CID课程号、changetime修改时间、oldscore修改前成绩、newscore修改后成绩。 创建GradeLOG表。 编写触发器程序实现GRADE表数据的插入、删除、修改在日志表(GradeLOG)记录相关操作数据,如操作用户、学号、课程号、修改时间、修改前成绩、修改后成绩。 (3)编写存储过程程序实现统计各课程不及格学生人数,并在屏幕输出;
时间: 2023-12-15 15:05:36 浏览: 203
(1)创建数据库及其关系表,并插入数据:
```sql
CREATE DATABASE GradeDB;
USE GradeDB;
CREATE TABLE STUDENT (
SID CHAR(10) PRIMARY KEY,
SName VARCHAR(20) NOT NULL,
Age INT NOT NULL,
Sex CHAR(2) NOT NULL
);
CREATE TABLE COURSE (
CID CHAR(10) PRIMARY KEY,
CName VARCHAR(20) NOT NULL,
Teacher VARCHAR(20) NOT NULL
);
CREATE TABLE GRADE (
SID CHAR(10) REFERENCES STUDENT(SID),
CID CHAR(10) REFERENCES COURSE(CID),
Score INT NOT NULL,
Note VARCHAR(50),
PRIMARY KEY (SID, CID)
);
INSERT INTO STUDENT VALUES ('200001', '张三', 19, '男');
INSERT INTO STUDENT VALUES ('200002', '李四', 20, '女');
INSERT INTO STUDENT VALUES ('200003', '王五', 18, '男');
INSERT INTO STUDENT VALUES ('200004', '赵六', 21, '女');
INSERT INTO STUDENT VALUES ('200005', '刘七', 20, '男');
INSERT INTO STUDENT VALUES ('200006', '陈八', 19, '女');
INSERT INTO STUDENT VALUES ('200007', '张九', 18, '男');
INSERT INTO STUDENT VALUES ('200008', '李十', 21, '女');
INSERT INTO STUDENT VALUES ('200009', '王麻子', 20, '男');
INSERT INTO STUDENT VALUES ('200010', '赵大', 19, '女');
INSERT INTO STUDENT VALUES ('200011', '刘二', 18, '男');
INSERT INTO STUDENT VALUES ('200012', '陈三', 21, '女');
INSERT INTO STUDENT VALUES ('200013', '张四', 20, '男');
INSERT INTO STUDENT VALUES ('200014', '李五', 19, '女');
INSERT INTO STUDENT VALUES ('200015', '王六', 18, '男');
INSERT INTO STUDENT VALUES ('200016', '赵七', 21, '女');
INSERT INTO STUDENT VALUES ('200017', '刘八', 20, '男');
INSERT INTO STUDENT VALUES ('200018', '陈九', 19, '女');
INSERT INTO STUDENT VALUES ('200019', '张十', 18, '男');
INSERT INTO STUDENT VALUES ('200020', '李一', 21, '女');
INSERT INTO COURSE VALUES ('C001', '数据库原理及应用', '张三');
INSERT INTO COURSE VALUES ('C002', '数据结构与算法', '李四');
INSERT INTO GRADE VALUES ('200001', 'C001', 80, '优秀');
INSERT INTO GRADE VALUES ('200001', 'C002', 75, '良好');
INSERT INTO GRADE VALUES ('200002', 'C001', 65, '及格');
INSERT INTO GRADE VALUES ('200002', 'C002', 70, '良好');
INSERT INTO GRADE VALUES ('200003', 'C001', 55, '不及格');
INSERT INTO GRADE VALUES ('200003', 'C002', 60, '及格');
INSERT INTO GRADE VALUES ('200004', 'C001', 90, '优秀');
INSERT INTO GRADE VALUES ('200004', 'C002', 85, '优秀');
INSERT INTO GRADE VALUES ('200005', 'C001', 70, '良好');
INSERT INTO GRADE VALUES ('200005', 'C002', 75, '良好');
INSERT INTO GRADE VALUES ('200006', 'C001', 80, '优秀');
INSERT INTO GRADE VALUES ('200006', 'C002', 85, '优秀');
INSERT INTO GRADE VALUES ('200007', 'C001', 60, '及格');
INSERT INTO GRADE VALUES ('200007', 'C002', 65, '及格');
INSERT INTO GRADE VALUES ('200008', 'C001', 75, '良好');
INSERT INTO GRADE VALUES ('200008', 'C002', 80, '优秀');
INSERT INTO GRADE VALUES ('200009', 'C001', 55, '不及格');
INSERT INTO GRADE VALUES ('200009', 'C002', 60, '及格');
INSERT INTO GRADE VALUES ('200010', 'C001', 70, '良好');
INSERT INTO GRADE VALUES ('200010', 'C002', 75, '良好');
```
(2)创建GradeLOG表,并编写触发器程序实现GRADE表数据的插入、删除、修改在日志表记录相关操作数据:
```sql
CREATE TABLE GradeLOG (
user VARCHAR(20),
SID CHAR(10),
CID CHAR(10),
changetime DATETIME,
oldscore INT,
newscore INT
);
DELIMITER $$
CREATE TRIGGER log_insert
AFTER INSERT ON GRADE
FOR EACH ROW
BEGIN
INSERT INTO GradeLOG VALUES (USER(), NEW.SID, NEW.CID, NOW(), NULL, NEW.Score);
END$$
CREATE TRIGGER log_delete
AFTER DELETE ON GRADE
FOR EACH ROW
BEGIN
INSERT INTO GradeLOG VALUES (USER(), OLD.SID, OLD.CID, NOW(), OLD.Score, NULL);
END$$
CREATE TRIGGER log_update
AFTER UPDATE ON GRADE
FOR EACH ROW
BEGIN
INSERT INTO GradeLOG VALUES (USER(), NEW.SID, NEW.CID, NOW(), OLD.Score, NEW.Score);
END$$
DELIMITER ;
-- 测试触发器
DELETE FROM GRADE WHERE SID = '200001' AND CID = 'C001';
UPDATE GRADE SET Score = 90 WHERE SID = '200002' AND CID = 'C002';
```
(3)编写存储过程程序实现统计各课程不及格学生人数,并在屏幕输出:
```sql
DELIMITER $$
CREATE PROCEDURE count_failed_students()
BEGIN
DECLARE c_name VARCHAR(20);
DECLARE c_id CHAR(10);
DECLARE n INT;
DECLARE done INT DEFAULT FALSE;
DECLARE cur1 CURSOR FOR SELECT CName, CID FROM COURSE;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO c_name, c_id;
IF done THEN
LEAVE read_loop;
END IF;
SELECT COUNT(*) INTO n FROM GRADE WHERE CID = c_id AND Score < 60;
SELECT CONCAT(c_name, '不及格人数为', n) AS Result;
END LOOP;
CLOSE cur1;
END$$
DELIMITER ;
-- 执行存储过程
CALL count_failed_students();
```
以上代码是一个完整的MySQL数据库程序,可以创建数据库、关系表、插入数据、创建触发器和存储过程,实现对学生成绩的管理。
阅读全文