Mysql高级 - 初始存储过程

存储引擎

和大多数数据库不同,MySQL中有一个存储引擎的概念,针对不同的存储需求可以选择最优的存储引擎

存储引擎就是存储数据,建立索引,更新查询数据等等技术的实现方式 。存储引擎是基于表的,而不是基于库的。所以存储引擎也可被称为表类型。

MySQL5.5之前的默认存储引擎是MyISAM,5.5之后就改为了InnoDB

存储过程

创建存储过程的基本格式

过程与函数的区别

**过程:**事先经过编译并存储在数据库中的一段 SQL 语句的集合

**函数:**函数是具有返回值的过程被称为函数

create procedure 过程名称 ( in / out / inout  参数名称 参数类型 )
begin
 ......
end;

调用存储过程

call 过程名称;

实例:

#创建函数过程
create procedure mytest(in a int,in b int,out sum int)
begin
 set sum=a+b;
end;

#调用函数过程
call mytest(1,2,@num);
select @num;

1、定义变量

MySQL 中的存储过程类似 java 中的方法。既然如此,在存储过程中也同样可以使用变量。java 中的局部变量作用域是变量所在的方法,而 MySQL 中的局部变量作用域是所在的存储过程

declare 变量名 变量类型 default 默认值;

变量赋值

set 变量名 = 表达式值;

实例

#创建过程
create procedure mytest02()
begin
 declare `nametest` varchar(20);
 set `nametest` = '张三';
 select * from `user` where `name` = `nametest`;
end;
#调用过程
call mytest02();

判断

2、if条件语句

IF 语句包含多个条件判断,根据结果为 TRUE、FALSE 执行语句,与编程语言中的 if、else if、else 语法类似。

if then 条件
  ...
elseif then 条件
  ...
else
  ...
end if;

实例

#创建过程
create procedure mypro2(in num int)
begin
	if num<0 then -- 条件开始
		select '负数';
	elseif num=0 then
		select '不是正数也不是负数';
	else
		select '正数';
	end if;-- 条件结束	
end;

#调用过程
call mypro2(-1);

3、case条件语句

case 语句的基本格式

case 
	when 条件 then ...;
	when 条件 then ...;
	...
else ...
end case;

实例

#创建过程
create procedure casetest(in num int)
begin
	case
		when num > 0 then select '正数';
		when num < 0 then select '负数';
  else select '0';
	end case;
end;

#调用过程
call casetest(10);
call casetest(0);
call casetest(-1);

循环

4、while循环语句

while语句的用法和java中的while循环类似

基本格式如下:

while 条件 do
....
end while;

实例

#创建过程
create procedure whiletest(out sum int)
begin 
	declare num int default 0; # 默认num的数值为0
	set sum = 0;
	while num < 10 do
		set num = num + 1;
		set sum = sum + num;
	end while;
end;

#调用过程
call whiletest(@sum);
select @sum;

5、repeat循环语句

repeat 语句的用法和 java 中的 do…while 语句类似,都是先执行循环操作,再判断条件,区别是 repeat 表达式值为 false 时才执行循环操作,直到表达式值为 true 停止。

基本格式如下:

repeat 
.... 循环体
until 条件
end repeat;

实例如下:

#创建过程
create procedure repeattest(out sum int)
begin
	declare num int default 0;
	set sum = 0;
	repeat
		set num = num + 1;
		set sum = sum + num;
	until num >= 10
	end repeat;
end;

#调用过程
call repeattest(@sum);
select @sum;

6、loop循环语句

循环语句,用来重复执行某些语句。执行过程中可使用 leave 语句或 iterate 跳出循环,也可以嵌套 IF 等判断语句。
1、leave 语句效果相当于 java 中的 break,用来终止循环;
2、iterate 语句效果相当于 java 中的 continue,用来结束本次循环操作,进入下一次循环。

基本格式如下:

循环名称:loop
 ...
if 条件 then 
 ...
 leave 循环名称; / iterate;
end if;
end loop 循环名称;

实例:

#创建过程
create procedure looptest(out sum int)
begin
	declare num int default 0;
	set sum = 0;
	loop_sum:loop
	set num = num + 1;
	set sum = sum + num;
	if num >= 10 then 
		leave loop_sum;
	end if;
	end loop loop_sum;
end;

#调用过程
call looptest(@sum);
select @sum;

过程管理

7、显示所有存储过程

show procedure status;

8、显示特定数据库的存储过程

show procedure status where `name` = '存储过程名称';

9、显示特定模式的存储过程,模糊查询

show procedure status `name` like '%模糊查询%';

10、显示存储过程的源码

show create procedure 过程名称;

11、删除存储过程

drop procedure 过程名称;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值