当前位置:   article > 正文

MySQL——存储过程、触发器、游标

MySQL——存储过程、触发器、游标

DDL+DML

  1. -- 创建student表
  2. CREATE TABLE student (
  3. id INT PRIMARY KEY AUTO_INCREMENT,
  4. createDate DATE,
  5. userName VARCHAR(100),
  6. phone VARCHAR(20),
  7. age INT,
  8. sex CHAR(1),
  9. introduce TEXT
  10. );
  11. -- 创建score表
  12. CREATE TABLE score (
  13. id INT PRIMARY KEY AUTO_INCREMENT,
  14. scoreName VARCHAR(100),
  15. result FLOAT,
  16. studentId INT,
  17. FOREIGN KEY (studentId) REFERENCES student(id)
  18. );
  19. -- 插入5条学生信息到student表
  20. INSERT INTO student (createDate, userName, phone, age, sex, introduce) VALUES
  21. ('2023-04-01', '张三', '13800138000', 20, '男', '我是张三,喜欢学习。'),
  22. ('2023-04-02', '李四', '13800138001', 21, '女', '我是李四,擅长数学。'),
  23. ('2023-04-03', '王五', '13800138002', 19, '男', '我是王五,热爱体育。'),
  24. ('2023-04-04', '赵六', '13800138003', 20, '女', '我是赵六,喜欢音乐。'),
  25. ('2023-04-05', '孙七', '13800138004', 22, '男', '我是孙七,对科学感兴趣。');
  26. -- 插入10条成绩信息到score表(每个学生2条)
  27. INSERT INTO score (scoreName, result, studentId) VALUES
  28. ('语文', 85.5, 1), ('数学', 90.0, 1),
  29. ('语文', 78.0, 2), ('数学', 88.5, 2),
  30. ('语文', 82.5, 3), ('体育', 95.0, 3),
  31. ('音乐', 92.0, 4), ('数学', 81.0, 4),
  32. ('物理', 87.5, 5), ('化学', 91.0, 5);
  1. -- 为studentId为3的学生插入数学成绩
  2. INSERT INTO score (scoreName, result, studentId) VALUES ('数学', YOUR_SCORE_VALUE, 3);
  3. -- 为studentId为3的学生插入英语成绩
  4. INSERT INTO score (scoreName, result, studentId) VALUES ('英语', YOUR_SCORE_VALUE, 3);
  5. -- 为studentId为4的学生插入数学成绩
  6. INSERT INTO score (scoreName, result, studentId) VALUES ('数学', YOUR_SCORE_VALUE, 4);
  7. -- 为studentId为4的学生插入英语成绩
  8. INSERT INTO score (scoreName, result, studentId) VALUES ('英语', YOUR_SCORE_VALUE, 4);
  1. SELECT
  2. s.id AS student_id,
  3. s.userName,
  4. SUM(sc.result) AS total_score
  5. FROM
  6. student s
  7. LEFT JOIN
  8. score sc ON s.id = sc.studentId
  9. GROUP BY
  10. s.id, s.userName;

存储过程 

 

  1. DELIMITER //
  2. CREATE PROCEDURE InsertRandomData()
  3. BEGIN
  4. DECLARE v_counter INT DEFAULT 0;
  5. DECLARE v_student_id INT;
  6. DECLARE v_random_score FLOAT;
  7. DECLARE v_random_score_name VARCHAR(100);
  8. DECLARE v_random_string VARCHAR(100);
  9. -- 设置自增ID从6开始
  10. ALTER TABLE student AUTO_INCREMENT = 6;
  11. -- 插入10条随机学生信息
  12. WHILE v_counter < 10 DO
  13. -- 生成随机用户名、电话等(这里简化为示例文本)
  14. SET v_random_string = CONCAT('User', FLOOR(RAND() * 1000));
  15. INSERT INTO student (createDate, userName, phone, age, sex, introduce)
  16. VALUES (
  17. NOW(),
  18. v_random_string,
  19. CONCAT('+1', FLOOR(RAND() * 8999999999) + 1000000000), -- 随机电话号码
  20. FLOOR(RAND() * 10 + 16), -- 随机年龄在16到25之间
  21. IF(RAND() > 0.5, 'M', 'F'), -- 随机性别
  22. 'Random introduction text.' -- 简介文本
  23. );
  24. SET v_counter = v_counter + 1;
  25. END WHILE;
  26. -- 为每个学生插入随机成绩记录
  27. SET v_counter = 0;
  28. WHILE v_counter < 100 DO
  29. SET v_student_id = FLOOR(RAND() * 10 + 6); -- 随机选择已插入的学生ID
  30. SET v_random_score = FLOOR(RAND() * 100); -- 随机分数0到99之间
  31. SET v_random_score_name = CONCAT('Subject ', FLOOR(RAND() * 100)); -- 随机科目名
  32. INSERT INTO score (scoreName, result, studentId)
  33. VALUES (v_random_score_name, v_random_score, v_student_id);
  34. SET v_counter = v_counter + 1;
  35. END WHILE;
  36. -- Reset the AUTO_INCREMENT counter for future inserts (optional)
  37. ALTER TABLE student AUTO_INCREMENT = 1;
  38. END //
  39. DELIMITER ;
  40. CALL InsertRandomData();

触发器 

 

  1. DELIMITER //
  2. CREATE TRIGGER tr_before_update_scoreName
  3. BEFORE UPDATE ON score
  4. FOR EACH ROW
  5. BEGIN
  6. IF NEW.scoreName = '数学' THEN
  7. SET NEW.scoreName = '天书';
  8. END IF;
  9. END;
  10. //
  11. DELIMITER ;
  12. INSERT INTO student (createDate, userName, phone, age, sex, introduce)
  13. VALUES (NOW(), 'Test Student', '1234567890', 20, 'M', 'Test Introduction');
  14. INSERT INTO score (scoreName, result, studentId)
  15. VALUES ('数学', 90.0, LAST_INSERT_ID());
  16. UPDATE score SET scoreName = '数学' WHERE id = LAST_INSERT_ID();
  17. SELECT * FROM score WHERE id = LAST_INSERT_ID();
  18. DROP TRIGGER IF EXISTS tr_before_update_scoreName;

游标 

  1. SELECT s.id AS student_id, s.userName, s.phone, s.age, s.sex, s.introduce,
  2. sc.id AS score_id, sc.scoreName, sc.result
  3. FROM student s
  4. LEFT JOIN score sc ON s.id = sc.studentId
  5. WHERE s.userName = '要查询的学生姓名'; -- 这里替换为实际的学生姓名
  6. DELIMITER //
  7. CREATE PROCEDURE GetStudentInfoByName(IN studentName VARCHAR(100))
  8. BEGIN
  9. SELECT s.id AS student_id, s.userName, s.phone, s.age, s.sex, s.introduce,
  10. sc.id AS score_id, sc.scoreName, sc.result
  11. FROM student s
  12. LEFT JOIN score sc ON s.id = sc.studentId
  13. WHERE s.userName = studentName;
  14. END //
  15. DELIMITER ;
  16. CALL GetStudentInfoByName('要查询的学生姓名'); -- 这里替换为实际的学生姓名

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/614618
推荐阅读
相关标签
  

闽ICP备14008679号