MySQL和数据库系列文章目录
【MySQL和数据库】MySQL & database 第一章:数据库的安装与卸载
【MySQL和数据库】MySQL & database 第二章:数据库与MySQL的基本概述
【MySQL和数据库】MySQL & database 第三章:DQL学习(主要是记录的查询)
【MySQL和数据库】MySQL & database 第四章:DML学习(主要是对记录的增删改)
【MySQL和数据库】MySQL & database 第五章:DDL学习(主要是对库、表、字段的增删改)
【MySQL和数据库】MySQL & database 第六章:TCL学习(主要是针对事务的控制)
【MySQL和数据库】MySQL & database 第七章:视图的学习
【MySQL和数据库】MySQL & database 第八章:MySQL中的变量
【MySQL和数据库】MySQL & database 第九章:存储过程的学习
【MySQL和数据库】MySQL & database 第十章:函数的学习
【MySQL和数据库】MySQL & database 第十一章:流程控制结构
流程控制结构的分类
- 分类:
- 顺序结构:程序从上到下依次执行;
- 分支结构:程序从两条或多条路径中选择一条去执行;
- 循环结构:程序在满足一定条件的基础上,重复执行一段代码;
分支结构
IF函数
-
功能:实现简单的双分支;
-
语法:
if(表达式1,表达式2,表达式3);
-
执行顺序:
如果表达式1成立,则返回表达式2的值,否则返回表达式3的值; -
应用:任何地方;
CASE函数
-
功能:类似编程语言中的switch语句和if else语句;
-
特点:
- 作为表达式时,嵌套在其它语句中使用,可以放在任何地方,begin end中或begin end外面;
作为独立语句时,只能放在begin end中使用; - 如果when中的值满足或条件成立,则执行对应then后面的语句,并且结束case;
如果都不满足,则执行else中的语句或值; - else语句可以省略;
如果省略else语句,并且所有when条件都不满足,则返回null;
- 作为表达式时,嵌套在其它语句中使用,可以放在任何地方,begin end中或begin end外面;
-
作为表达式的情况
-
类似switch语句
case 表达式 when 值1 then 值2 when 值2 then 值2 ... else 值n end;
-
类似if else语句
case when 条件1 then 值1 when 条件2 then 值2 ... else 值n end;
-
-
作为独立语句的情况
-
类似switch语句
case 表达式 when 值1 then 语句1; when 值2 then 语句2; ... else 语句n; end case;
-
类似if else语句
case when 条件1 then 语句1; when 条件2 then 语句2; ... else 语句n; end case;
-
ELSEIF函数
-
功能:实现多重分支;
-
语法:
if 条件1 then 语句1; elseif 条件2 then 语句2; ... else 语句n; end if;
-
应用:在begin end中使用;
-
注意:可以省略最后的else语句;
循环结构
-
分类:while、loop、repeat;
-
循环控制:
- iterate 类似编程语言的 continue,结束本次循环,执行下一次循环;
- leave 类似编程语言的 break,结束当前最近的循环;
-
应用:三种循环结构都在begin end中使用;
-
while语句
-
语法:
【标签:】 while 循环条件 do 循环体; end while 【标签】;
-
类似编程语言的while语句;
-
-
loop语句
-
语法:
【标签:】 loop 循环体; end loop 【标签】;
-
可以用来模拟简单的死循环;
-
-
repeat语句
-
语法:
【标签:】 repeat 循环体; until 结束循环的条件 end repeat 【标签】;
-
类似编程语言的do while语句;
-
-
没有添加循环控制的情况;
案例1:批量插入,根据次数插入到admin表中多条记录;delimiter $ create procedure pro_while1(in insertCount int) begin declare i int default 1; while i <= insertCount do insert into admin(username, `password`) values (concat('Rose', i), 666); set i = i + 1; end while; end $ call pro_while1(100) $
-
添加leave语句;
案例2:批量插入,根据次数插入到admin表中多条记录,如果次数 >20 则停止;delimiter $ create procedure pro_while2(in insertCount int) begin declare i int default 1; a: while i <= insertCount do insert into admin(username, `password`) values(concat('xiaohua', i), '000'); if i >= 20 then leave a; end if; set i = i + 1; end while a; end $ call pro_while2(100) $
-
添加iterate语句
案例3:批量插入,根据次数插入到admin表中多条记录,只插入偶数次;delimiter $ create procedure pro_while3(in insertCount int) begin declare i int default 0; a: while i <= insertCount do set i = i + 1; if mod(i, 2) != 0 then iterate a; end if; insert into admin(username, 'password') values (concat('xiaogou', i), '222'); end while a; end $ call pro_while3(100) $
综合案例
- 创建表stringContent,其中字段为id(整型,自增长)、content(字符串);并向该表插入指定个数的、随机的字符串;
drop table if exists stringContent;
create table stringContent(
id int primary key auto_increment,
content varchar(20)
);
delimiter $
create procedure randstr_insert(in insertCount int)
begin
declare i int default 1;
declare str varchar(20) default 'abcdefghijklmnopqrstuvwxyz';
declare startIndex int default 1;
declare len int default 1;
while i <= insertCount do
set len = floor(rand() * (20 - startIndex + 1) + 1);
set startIndex = floor(rand() * 26 + 1);
insert into stringContent(content)
values (substr(str, startIndex, len));
set i = i + 1;
end while;
end $
call randstr_insert(10) $
select * from stringContent $