当前位置:   article > 正文

MySQL数据库——存储过程练习

MySQL数据库——存储过程练习

DDL+DML

  1. CREATE TABLE `student` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '学号',
  3. `createDate` datetime DEFAULT NULL,
  4. `userName` varchar(20) DEFAULT NULL,
  5. `pwd` varchar(36) DEFAULT NULL,
  6. `phone` varchar(11) DEFAULT NULL,
  7. `age` tinyint(3) unsigned DEFAULT NULL,
  8. `sex` char(2) DEFAULT '男',
  9. `introduce` varchar(255) DEFAULT NULL,
  10. PRIMARY KEY (`id`)
  11. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
  12. CREATE TABLE `result` (
  13. `rid` int(11) NOT NULL AUTO_INCREMENT COMMENT '成绩编号',
  14. `testName` varchar(255) DEFAULT NULL COMMENT '测试名称',
  15. `score` double(4,2) DEFAULT NULL COMMENT '成绩',
  16. `studentId` int(11) DEFAULT NULL COMMENT '学生id',
  17. PRIMARY KEY (`rid`),
  18. KEY `studentId` (`studentId`),
  19. CONSTRAINT `result_ibfk_1` FOREIGN KEY (`studentId`) REFERENCES `student` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
  20. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
  21. insert into student values(0,'2024-02-25 10:10:10','赵灵儿','123',
  22. '15612345678',16,'女','逍遥哥哥,你终于找到我了。');
  23. insert into student values(0,'2024-02-25 10:10:10','王语嫣','123',
  24. '15612345678',17,'女','慕容复,我和你不共戴天。');
  25. insert into student values(0,'2024-02-25 10:10:10','龙姑娘','123',
  26. '15612345678',22,'女','我想过过过儿过过的日子。');
  27. insert into student values(0,'2024-02-25 10:10:10','杨过','123',
  28. '15612345678',18,'男','一遇杨过误终身。');
  29. insert into student values(0,'2024-02-25 10:10:10','杨逍','123',
  30. '15612345678',27,'男','杨过跟程英的大儿子。');
  31. insert into student (userName,age,introduce)values('黄衣女子',26,'杨过与龙姑娘的大女儿。');
  32. insert into result values(0,'政治',96,1);
  33. insert into result values(0,'地理',86,5);
  34. insert into result values(0,'政治',77,3);
  35. insert into result values(0,'英语',99,3);
  36. insert into result values(0,'历史',22,6);
  37. insert into result values(0,'化学',69,2);
  38. insert into result values(0,'Java',79,5);
  39. insert into result values(0,'Java',67,6);
  40. insert into result values(0,'政治',97,2);
  41. insert into result values(0,'物理',76,6);
  42. insert into result values(0,'物理',82,2);
  43. insert into result values(0,'化学',56,4);

DQL

  1. #存储过程的框架
  2. delimiter $$ #分隔符开始
  3. #声明存储过程函数。函数名应该符合标识符命名规范
  4. create procedure get_student_userName(in p_userName varchar(20))
  5. #开始撰写存储过程
  6. begin
  7. # sql语句
  8. declare p_introduce varchar(255);
  9. select introduce into p_introduce from student where userName=p_userName;
  10. select now() '时间',p_introduce '读取的简介';
  11. end $$ #分隔符终止
  12. delimiter ; #分隔符结束
  13. # 使用存储过程(调用存储过程 )
  14. call get_student_userName('赵灵儿');

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

闽ICP备14008679号