mysql的总结7--存储过程

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

1.定义:存储过程是一组为了完成某个特定功能而编写的SQL程序集

 
2.优点:
 
<1>.通过把处理封装在简单易用的单元中, 简化复杂的操作。
 
<2>. 提高性能。使用存储过程比使用单独的SQL语句要快。
 
<3>. 安全。调用者只需要调用指定的存储过程即可,而不用关心存储过程的内容。(如转账)
 
3.缺点:
 
<1>.编写复杂。
 
<2>.如果没有相应的权限,你将无法创建存储过程。
 
4.
 
<1>. 创建简单的存储过程:如返回用户的平均年龄
 
delimiter // (含义:用于临时将SQL语句的结束符更改为//;注意双斜杠前有空格啊)
create procedure ageAge()  (存储过程的名称为:ageAge())
begin   (相当于 { )
select avg(age) as ageAge from t_user;
end //  (相当于 })
delimiter ;  (存储过程结束,将SQL语句的结束符改回!注意分号前有空格啊!!)
 
<1.2>.存储过程的调用: call avgAge();
 
 
<2>.创建带有返回值参数的存储过程: 如获得用户的最大年龄,最小年龄,平均年龄
delimiter //
create procedure ageMVM(
out ma int,
out mi int,   (注意:out是返回值参数,ma,mi,av试试变量名称,decimal是一种数据类型)
out av decimal(8,2)  (8代表精度,2,代表小数的位数)
)
begin
select max(age) into ma from t_user;   ( 说明:into是将值赋给变量ma
select min(age) into mi from t_user;
select avg(age) into av from t_user;
end //
delimiter ;
  
<2.1>.调用存储过程:两个语句同时使用:
call(@ma,@mi,@av);
select @ma,@mi,@av;
 
 
<3>. 传入参数的存储过程
eg1.根据用户名和密码,找到用户的地址。
delimiter //
create procedure getAddressById(
in  un varchar(20),
in  pd varchar(20),
out  ad varchar(20)
 
begin 
select address into ad from t_user where username=un and password=pd;
end //
delimiter ;
<3.1>.调用该存储过程:
call ('tom','123456',@ad);
select @ad;
 
eg2.其实存储过程应该做 业务不是上面的,而是下面的常用的多,上面的知识一个例子!!
delimiter //
create procedure ordertotal(
in ordernum int,   (in代表传入的参数!! )
out total decimal(8,2)
)
begin
select sum(item.price * quality) into total from orderitems where order_num=ordernum;
end //
delimiter;
 
调用: call ordertotal(2005,@total);
    select @total;
    
 
4. 带有选择条件的存储过程
delimiter //
create procedure ordertotaltax(
in ordernum int ,     (订单号)
in tax boolean ,   (是否需要缴税)
out total decimal(8,2)  (总金额,存储过程的返回值)
)
begin
declare ordertotal decimal(8,2);   (声明变量:总金额)
declare taxvalue float default 0.6;   (声明变量:税率)
(下面计算指定订单的单价)
select sum(item_price*quantity) into ordertotal from orderitems where order_num = ordernum;
(如果需要缴税)
if tax then   
select ordertotal+(ordertotal*taxvalue) into ordertotal;
end if;
select ordertotal into total;    (将当前的总价复制给返回参数,into 也可以用set代替)
end //
delimiter ;
 
 
 
调用该存储过程:
(缴税时) :  call ordertotaltax(2005,1,@total);  (1代表true)
(不交税): call ordertotaltax(2005,0,@total);   (0代表false)
 
 
5. 流程控制语句   if ---while
 
<1>.if的测试使用:
delimiter //
create procedure iftest(
out var int
)
begin
declare id int default 10;
if id=12 then
set var=0;
elseif id<12 then
set  var=1;
else
set var=2;
end if;
end //
delimiter ;
 
调用: call iftest(@var);
select @var;
 
<2>.while的测试使用:
delimiter //
create procedure whiletest()
begin
declare num int;
declare total int default 0;
set num=0;
while num<10 do   (do相当于{)
set total=total+num;
set num=num+1;
end while;
select total;    (打印总和的值)
end //
delimiter ;
 
调用该存储过程:  call whiletest();
 
6.存储过程的删除
如:删除上面的存储过程
drop procedure   whiletest;


     本文转自韩立伟 51CTO博客,原文链接:http://blog.51cto.com/hanchaohan/924823 ,如需转载请自行联系原作者




相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
23天前
|
存储 SQL 关系型数据库
轻松入门MySQL:加速进销存!利用MySQL存储过程轻松优化每日销售统计(15)
轻松入门MySQL:加速进销存!利用MySQL存储过程轻松优化每日销售统计(15)
|
1月前
|
存储 关系型数据库 MySQL
Mysql基础第二十六天,使用存储过程
Mysql基础第二十六天,使用存储过程
28 0
Mysql基础第二十六天,使用存储过程
|
1月前
|
存储 SQL 关系型数据库
【MySQL 数据库】9、存储过程
【MySQL 数据库】9、存储过程
205 0
|
17天前
|
存储 SQL 关系型数据库
mysql存储过程示例
mysql存储过程示例
11 0
|
2月前
|
存储 SQL 关系型数据库
[MySQL]存储过程
[MySQL]存储过程
76 0
[MySQL]存储过程
|
2月前
|
存储 关系型数据库 MySQL
MySQL 中的存储过程-3
MySQL 中的存储过程
20 0
|
2月前
|
存储 SQL 关系型数据库
MySQL 中的存储过程-2
MySQL 中的存储过程
18 0
|
3月前
|
存储 关系型数据库 MySQL
MySQL-调用存储过程
MySQL-调用存储过程
101 2
|
3月前
|
存储 关系型数据库 MySQL
MySQL 中创建存储过程
MySQL 中创建存储过程
24 1
|
3月前
|
存储 SQL 数据库
MySQL-存储过程概述
MySQL-存储过程概述
122 1