存储过程和函数是事先经过编译的存储在数据库中一段sql语句集合,可以理解为为了完成一件事情 将很多sql语句有条理的组织起来 放在一起存储在数据库中 当需要使用的时候直接调用就行
存储和函数的区别:
函数必须有返回值,而存储过程没有
存储过程参数可以是in out inout类型 函数的参数类型只能使in
存储过程只在创建时编译,而sql语句每执行一次编译一次,所以存储能提高速度
简化操作,结合事务一起封装,复用性和安全性都高
并发量少的情况不建议使用存储,并发量大的情况建议使用存储提高效率
存储创建方式: delimiter
c
r
e
a
t
e
p
r
o
c
e
d
u
r
e
存
储
名
称
(
参
数
列
表
)
b
e
g
i
n
s
q
l
语
句
e
n
d
create procedure 存储名称(参数列表) begin sql语句 end
createprocedure存储名称(参数列表)beginsql语句end
delimiter ;
调用存储: call 存储名称(参数列表)
参数列表: in(输入参数) out(输出参数) inout(根据条件判断是输入还是输出参数)
查看存储: show create procedure 存储名称\G
删除存储: drop procedure 存储名称\G
创建统级用户个数的存储 countuser();
mysql> \d
m
y
s
q
l
>
c
r
e
a
t
e
p
r
o
c
e
d
u
r
e
c
o
u
n
t
u
s
e
r
(
)
−
>
b
e
g
i
n
−
>
s
e
l
e
c
t
c
o
u
n
t
(
∗
)
f
r
o
m
u
s
e
r
;
−
>
e
n
d
mysql> create procedure countuser() -> begin -> select count(*) from user; -> end
mysql>createprocedurecountuser()−>begin−>selectcount(∗)fromuser;−>end
Query OK, 0 rows affected (0.00 sec)
mysql> \d ;
mysql> call countuser();
±---------+
| count(*) |
±---------+
| 4 |
±---------+
1 row in set (0.00 sec)
创建带有参数in的autoinsert(in a int) 自定义插入数据条数
mysql> \d
m
y
s
q
l
>
c
r
e
a
t
e
p
r
o
c
e
d
u
r
e
a
u
t
o
i
n
s
e
r
t
1
(
i
n
a
i
n
t
)
−
>
b
e
g
i
n
−
>
d
e
c
l
a
r
e
i
i
n
t
d
e
f
a
u
l
t
1
;
−
>
w
h
i
l
e
(
i
<
a
)
d
o
−
>
i
n
s
e
r
t
i
n
t
o
t
2
v
a
l
u
e
s
(
i
,
′
c
c
′
)
;
−
>
s
e
t
i
=
i
+
1
;
−
>
e
n
d
w
h
i
l
e
;
−
>
e
n
d
mysql> create procedure autoinsert1(in a int) -> begin -> declare i int default 1; -> while(i<a)do -> insert into t2 values(i,'cc'); -> set i=i+1; -> end while; -> end
mysql>createprocedureautoinsert1(inaint)−>begin−>declareiintdefault1;−>while(i<a)do−>insertintot2values(i,′cc′);−>seti=i+1;−>endwhile;−>end
Query OK, 0 rows affected (0.00 sec)
mysql> \d ;
mysql> call autoinsert1(10);
Query OK, 1 row affected (0.01 sec)
创建带有返回参数的过程 countt2(out b int)
mysql> \d
m
y
s
q
l
>
c
r
e
a
t
e
p
r
o
c
e
d
u
r
e
c
o
u
n
t
t
2
(
o
u
t
b
i
n
t
)
b
是
返
回
值
−
>
b
e
g
i
n
−
>
s
e
l
e
c
t
c
o
u
n
t
(
∗
)
i
n
t
o
b
f
r
o
m
t
2
;
−
>
e
n
d
mysql> create procedure countt2(out b int) b是返回值 -> begin -> select count(*) into b from t2; -> end
mysql>createprocedurecountt2(outbint)b是返回值−>begin−>selectcount(∗)intobfromt2;−>end
Query OK, 0 rows affected (0.00 sec)
mysql> \d ;
mysql> call countt2(@num); 将返回值传递给num
Query OK, 1 row affected (0.04 sec)
mysql> select @num; 查看num值
±-------+
| @num |
±-------+
| 200009 |
±-------+
1 row in set (0.00 sec)
创建一个inout类型countt3(inout c int) 根据c值是否为0来决定
mysql> \d
m
y
s
q
l
>
c
r
e
a
t
e
p
r
o
c
e
d
u
r
e
c
o
u
n
t
3
(
i
n
o
u
t
c
i
n
t
)
−
>
b
e
g
i
n
−
>
i
f
c
=
0
t
h
e
n
−
>
s
e
t
c
=
c
+
1
;
为
0
c
为
输
入
−
>
e
l
s
e
−
>
s
e
l
e
c
t
c
o
u
n
t
(
∗
)
i
n
t
o
c
f
r
o
m
t
2
;
不
为
0
c
为
输
出
−
>
e
n
d
i
f
;
−
>
e
n
d
mysql> create procedure count3(inout c int) -> begin -> if c=0 then -> set c=c+1; 为0 c为输入 -> else -> select count(*) into c from t2; 不为0 c为输出 -> end if; -> end
mysql>createprocedurecount3(inoutcint)−>begin−>ifc=0then−>setc=c+1;为0c为输入−>else−>selectcount(∗)intocfromt2;不为0c为输出−>endif;−>end
Query OK, 0 rows affected (0.00 sec)
mysql> \d ;
函数定义:create function 函数名(参数列表) returns 返回值列表
[特性] 函数体
函数参数形式: 函数名 类型
具体定义:\d
c
r
e
a
t
e
f
u
n
c
t
i
o
n
函
数
名
(
参
数
列
表
)
r
e
t
u
r
n
s
返
回
值
类
型
b
e
g
i
n
有
效
的
s
q
l
语
句
e
n
d
create function 函数名(参数列表) returns 返回值类型 begin 有效的sql语句 end
createfunction函数名(参数列表)returns返回值类型begin有效的sql语句end
\d ;
调用函数: select 函数名(参数列表)
查看函数: show function 函数名称\G
删除函数: drop function 函数名称
案例:
mysql> \d
m
y
s
q
l
>
c
r
e
a
t
e
f
u
n
c
t
i
o
n
h
e
l
l
o
(
)
r
e
t
u
r
n
s
v
a
r
c
h
a
r
(
50
)
−
>
b
e
g
i
n
−
>
r
e
t
u
r
n
c
o
n
c
a
t
(
′
h
e
l
l
o
′
,
′
:
′
,
′
b
e
i
j
i
n
g
′
)
;
−
>
e
n
d
mysql> create function hello() returns varchar(50) -> begin -> return concat('hello',':','beijing'); -> end
mysql>createfunctionhello()returnsvarchar(50)−>begin−>returnconcat(′hello′,′:′,′beijing′);−>end
Query OK, 0 rows affected (0.00 sec)
mysql> \d ;
mysql> select hello();
±--------------+
| hello() |
±--------------+
| hello:beijing |
±--------------+
1 row in set (0.00 sec)