将xscjgl文件内容导人xscjgl数据库,完成以下内容。
(1)创建一个显示学生的学号、姓名、课程名、成绩的存储过程
CREATE PROCEDURE p1()
BEGIN
SELECT a.学号, a.姓名, b.课程名, c.成绩
FROM xs a
JOIN xs_kc c ON a.学号 = c.学号
JOIN kc b ON c.课程号 = b.课程号;
END;
验证:
call p1();
(2)创建一个输入学号、课程号,以及输出学生成绩的存储过程。
CREATE PROCEDURE p2(IN student_id CHAR(10), IN course_id CHAR(10), OUT score INT)
BEGIN
SELECT 成绩 INTO score
FROM xs_kc
WHERE 学号 = student_id AND 课程号 = course_id;
END;
验证:
set @output_score = null;
call p2('081241', '101', @output_score);
select @output_score as score;
(3)创建一个游标,循环显示所有学生成绩(学号、课程号、成绩)。
CREATE PROCEDURE p3()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE student_id CHAR(10);
DECLARE course_id CHAR(10);
DECLARE score INT;
DECLARE cur CURSOR FOR
SELECT 学号, 课程号, 成绩 FROM xs_kc;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
read_loop: LOOP
FETCH cur INTO student_id, course_id, score;
IF done THEN
LEAVE read_loop;
END IF;
SELECT student_id, course_id, score;
END LOOP;
CLOSE cur;
END;
验证:
call p3();
(4)创建一个游标,指定学生学号显示此学生的成绩信息(姓名、课程名、成绩)。
CREATE PROCEDURE p4(IN p_student_id CHAR(10))
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE v_name VARCHAR(5);
DECLARE v_course_name VARCHAR(10);
DECLARE v_score INT;
DECLARE cur CURSOR FOR
SELECT xs.姓名, kc.课程名, xs_kc.成绩
FROM xs_kc
JOIN xs ON xs_kc.学号 = xs.学号
JOIN kc ON xs_kc.课程号 = kc.课程号
WHERE xs_kc.学号 = p_student_id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
read_loop: LOOP
FETCH cur INTO v_name, v_course_name, v_score;
IF done THEN
LEAVE read_loop;
END IF;
SELECT v_name AS 姓名, v_course_name AS 课程名, v_score AS 成绩;
END LOOP;
CLOSE cur;
END;
验证:
call p4('081101');
(5)创建一个存储过程,按学号输入参数,删除xs和xs_kc表中的学生相关信息,必须采用事务处理。
CREATE PROCEDURE p5(IN p_student_id CHAR(10))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
RESIGNAL;
END;
START TRANSACTION;
DELETE FROM xs_kc WHERE 学号 = p_student_id;
IF ROW_COUNT() = 0 THEN
END IF;
DELETE FROM xs WHERE 学号 = p_student_id;
COMMIT;
END;
验证:
call p5('081101');
SELECT * FROM xs WHERE 学号 = '081101';
SELECT * FROM xs_kc WHERE 学号 = '081101';
--以上内容仅供参考学习!