数据库系列MySQL:存储过程

# 创建一个名为ShowStu的存储过程,查询student表的内容
delimiter // 
create PROCEDURE ShowStu()
begin
select * from student;
end//
delimiter ;

call ShowStu();

# 存储过程的参数
1. IN输入参数  表示调用者向过程传入值(传入值可以是字面量或变量),默认是IN输入参数,如果不填写,就是默认的IN输入参数
# 用于查询指定姓名的学生信息
delimiter // 
create PROCEDURE GetNameByStu(in name varchar(50))
begin
select * from student where stuName=name;
end//
delimiter ;

call GetNameByStu('李四');

2. OUT输出参数  表示过程向调用者传出值(可以返回多个值)(传出值只能是变量)
# 查询指定姓名的学生学号。
delimiter // 
create PROCEDURE GetIDByName(in name varchar(50),out id int)
begin
select stuID into id from student where stuName=name;
end//
delimiter ;

call GetIDByName('王五',@sid);
select @sid;

# 查询指定课程名称的课程编号和课程学时。
delimiter // 
create PROCEDURE GetIDAndHoursByName(in name varchar(50),out id int,out hours int)
begin
select couID,couHours into id,hours from courses where couName=name;
end//
delimiter ;

call GetIDAndHoursByName('大学英语',@id,@hours);
select @id;
select @hours;

3. INOUT修改传入参数值
# 传入学生名字,使用“-”拼接学号,并传入一个月的薪水值,输出年薪。
delimiter //
create PROCEDURE GetSalaryByName(inout sname varchar(50) ,inout salary float)
begin
select concat(stuID,"-",stuName) into sname from student where stuName=sname;
set salary=salary*12;
end//
delimiter ;

set @sname='王五';
set @salary=4000.5;
call GetSalaryByName(@sname,@salary);
select @sname;
select @salary;

# 存储过程中的流程控制
1. 分支控制语句
# 使用多分支控制语句实现传入整型数字0,1,2,输出周日,周一,周二,如果传入其他数字则输出无效日期
delimiter //
create PROCEDURE `weekchoose`(in `day` int)
BEGIN
if `day`=0 THEN
  select '星期天';
elseif `day`=1 THEN
  select '星期一';
elseif `day`=2 THEN
  select '星期二';
ELSE
  select '无效日期';
end if;
end//
delimiter ;

call weekchoose(2);

# 向student表中插入一条信息,判断学号是否存在,如果存在则提示“学号已存在,不能添加”,否则向数据库添加一条数据,提示“数据添加成功”
DELIMITER $$
CREATE PROCEDURE `InsertTostudent`(IN stu_id int,IN stu_name varchar(20),OUT s_result VARCHAR(20))
    BEGIN
       -- 声明一个变量 用来决定学号是否已经存在
       DECLARE s_count INT DEFAULT 0;
       -- 验证这么名字是否已经存在
       SELECT COUNT(*) INTO s_count FROM student WHERE `stuID` = stu_id;    
       IF s_count = 0 THEN
            INSERT INTO student(`stuID`, `stuName`) VALUES(stu_id, stu_name);
         SET s_result = '数据添加成功';
       ELSE
                SET s_result = '学号已存在,不能添加';
                SELECT s_result;
       END IF;
    END$$
DELIMITER ;

call InsertTostudent(1010,'李红',@s_result);
SELECT @s_result;

call InsertTostudent(1011,'李红',@s_result);
SELECT @s_result;

2. 条件控制语句case
# 使用case条件控制语句实现
delimiter //
create PROCEDURE `weekchoose2`(in `day` int)
BEGIN
case `day`
when 0 THEN
  select '星期天';
when 1 THEN
  select '星期一';
when 2 THEN
  select '星期二';
ELSE
  select '无效日期';
end case;
end//
delimiter ;

call weekchoose2(2);

2. while循环语句
# 使用while循环实现传入小于0的整数i,输出i+1到10的和
delimiter //
CREATE PROCEDURE `sum`(in i int,out sum int)
BEGIN
  set sum=0;
while i<10 DO
  set i=i+1;
  set sum=sum+i;
end while;
end//
delimiter ;

call `sum`(0,@s);
select @s;

# 在students数据库中创建一个名为test的表,字段为id int PRIMARY key,tname varchar(50) not null,time TIMESTAMP DEFAULT now(),使用while循环向test表中插入10万条数据。
use students;
create table test
(
id int PRIMARY key,
tname varchar(50) not null,
time TIMESTAMP DEFAULT now()
);

delimiter //
create PROCEDURE `InsertTotest`()
BEGIN
DECLARE i int DEFAULT 1;
while i<=100000 DO
   insert into `students`.`test`(id,tname) values(i,'张三');
   set i=i+1;
end while;
end//
delimiter ;

call InsertTotest();

做测试的建表参数

create table if not EXISTS student
(
    stuID  int(5)  not null primary key,
    stuName  varchar(50)  not null,
    stuSex CHAR(10),
    stuAge smallint
);

CREATE TABLE if not EXISTS courses(
         couID int  not null primary key auto_increment COMMENT '学号',
         couName varchar(50) not null DEFAULT('大学英语'),
         couHours  smallint UNSIGNED COMMENT '学时',
         couCredit  float DEFAULT(2) COMMENT '学分'
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE  utf8mb4_0900_ai_ci;

CREATE TABLE if not EXISTS stu_cou(
         ID int not null primary key auto_increment,
         stuID int(5)  not null  COMMENT '学号',
         couID int  not null  COMMENT '课程编号',
         time timestamp not null DEFAULT(now())
);

 /*添加外键约束*/
 alter table stu_cou add CONSTRAINT fk_stuid foreign key(stuID) REFERENCES student(stuID) ON UPDATE CASCADE ON DELETE CASCADE ;
 alter table stu_cou add CONSTRAINT fk_couid foreign key(couID) REFERENCES courses(couID) ON UPDATE CASCADE ON DELETE CASCADE ;

  /*插入数据*/
 insert into student(stuID,stuName,stuSex,stuAge) values(1001,'张三','男',19),(1002,'李四','男',18),(1003,'王五','男',18),(1004,'丽丽','女',18),(1005,'李仁','女',19),(1006,'张敏','女',18); insert into student VALUES(1007,'五条人','男',20),(1008,'五伍','女',19);
 insert into courses(couID,couName,couHours,couCredit) values(50,'大学英语',64,2),(60,'计算机基础',78,2.5),(70,'Java程序设计',108,6),(80,'数据库应用',48,2.5);
 insert into stu_cou(stuID,couID) values(1001,50),(1001,60),(1001,70),(1001,80),(1002,50),(1002,60),(1002,70),(1002,80),(1003,50),(1003,60),(1003,70),(1003,80),(1004,50),(1004,60),(1004,70),(1004,80),(1005,50),(1005,60),(1005,70),(1005,80),(1006,50),(1006,60),(1006,70),(1006,80);

alter table stu_cou add COLUMN grade FLOAT null;
UPDATE stu_cou set grade=(SELECT FLOOR(50 +RAND() * 50));

alter table student add COLUMN stuColleage varchar(100) null;
update student set stuColleage='大数据学院' where stuID BETWEEN 1001 and  1003;
update student set stuColleage='物流学院' where stuID BETWEEN 1004 and  1006;
update student set stuColleage='康养学院' where stuID BETWEEN 1007 and  1008;

alter table courses add COLUMN couColleage varchar(50) null;

update courses set couColleage='通识教育学院' where couName='大学英语';
update courses set couColleage='通识教育学院' where couName='计算机基础';
update courses set couColleage='大数据学院' where couName='Java程序设计';
update courses set couColleage='大数据学院' where couName='数据库应用';

insert into courses values(90,'大学体育',56,1.5,'通识教育学院'),(100,'Android程序设计',92,5,'大数据学院'),(101,'大学物理',48,2,'通识教育学院');

insert into stu_cou(stuID,couID,grade) values(1007,50,86),(1007,60,71),(1008,70,56),(1008,80,63);
insert into student VALUES(1009,'小小','男',17,'物流学院'),(1010,'大大','女',21,'大数据学院');

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值