MySQL自定义函数和存储过程示例详解
前言
本文主要给大家介绍的是关于mysql自定义函数和存储过程的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧
1、前置条件
mysql数据库中存在表user_info,其结构和数据如下:
mysql> desc user_info; +-----------+----------+------+-----+---------+-------+ | field | type | null | key | default | extra | +-----------+----------+------+-----+---------+-------+ | id | int(10) | no | pri | null | | | name | char(20) | no | | null | | | passwd | char(40) | no | | null | | | email | char(20) | no | | null | | | phone | char(20) | no | | null | | | role | char(10) | no | | null | | | sex | char(10) | no | | null | | | status | int(10) | no | | null | | | createat | datetime | no | | null | | | exprat | datetime | no | | null | | | validdays | int(10) | no | | null | | | delat | datetime | yes | | null | | +-----------+----------+------+-----+---------+-------+ 12 rows in set (0.10 sec) mysql> select * from user_info; +----+--------------+----------+------------+-------------+--------+------+--------+---------------------+---------------------+-----------+-------+ | id | name | passwd | email | phone | role | sex | status | createat | exprat | validdays | delat | +----+--------------+----------+------------+-------------+--------+------+--------+---------------------+---------------------+-----------+-------+ | 1 | stephenwang7 | py123456 | 123@qq.com | 15103887470 | admin | male | 200 | 2019-04-12 20:11:30 | 2019-04-19 20:11:30 | 30 | null | | 2 | stephenwang8 | 123456 | 123@qq.com | 15103887470 | viewer | male | 200 | 2019-04-12 20:11:30 | 2019-04-19 20:11:30 | 30 | null | +----+--------------+----------+------------+-------------+--------+------+--------+---------------------+---------------------+-----------+-------+ 2 rows in set (0.00 sec)
2、自定义函数
函数:可以完成特定功能的一段sql集合。mysql支持自定义函数来完成特定的业务功能。
创建自定义函数(user defined function 简称udf)的语法如下:
create function <函数名称> ([参数1] [类型1], [参数n] [类型n])
returns <类型>
return
<函数主体>
调用udf的语法如下:
select <函数名称> ([参数])
创建无参的udf
示例1:查询user_info表中有多少条记录
#定义函数 mysql> create function user_info_count() -> returns int(10) -> return -> (select count(*) from user_info);
调用函数user_info_count()
mysql> select user_info_count(); +-------------------+ | user_info_count() | +-------------------+ | 2 | +-------------------+ 1 row in set (0.00 sec)
创建有参udf
示例2:根据id查询用户name。
#定义函数 mysql> create function querynamebyid(uid int(10)) -> returns char(20) -> return -> (select name from user_info where id=uid); query ok, 0 rows affected (0.01 sec)
调用函数,查询id为1的用户名称。
mysql> select querynamebyid(1); +------------------+ | querynamebyid(1) | +------------------+ | stephenwang7 | +------------------+ 1 row in set (0.00 sec)
查看udf
查询系统中所有的udf
show function status;
查询指定的udf
# show create function 函数名称; mysql> show function querynamebyid; error 1064 (42000): you have an error in your sql syntax; check the manual that corresponds to your mysql server version for the right syntax to use near 'querynamebyid' at line 1 mysql> show function querynamebyid(); error 1064 (42000): you have an error in your sql syntax; check the manual that corresponds to your mysql server version for the right syntax to use near 'querynamebyid()' at line 1 mysql> show create function querynamebyid(); error 1064 (42000): you have an error in your sql syntax; check the manual that corresponds to your mysql server version for the right syntax to use near '()' at line 1 mysql> show create function querynamebyid; +---------------+-------------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+ | function | sql_mode | create function | character_set_client | collation_connection | database collation | +---------------+-------------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+ | querynamebyid | only_full_group_by,strict_trans_tables,no_zero_in_date,no_zero_date,error_for_division_by_zero,no_auto_create_user,no_engine_substitution | create definer=`root`@`localhost` function `querynamebyid`(uid int(10)) returns char(20) charset latin1 return (select name from user_info where id=uid) | utf8 | utf8_general_ci | latin1_swedish_ci | +---------------+-------------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+ 1 row in set (0.00 sec
修改udf
如果想要修改函数的内容,先删除后再重新创建。
删除udf
删除udf语法如下:
drop function <函数名称>;
示例3:删除函数querynameid后再次调用并观察现象。
mysql> drop function querynamebyid; query ok, 0 rows affected (0.45 sec) mysql> select querynamebyid(1); error 1305 (42000): function rms.querynamebyid does not exist mysql>
3、存储过程
存储功能和自定义函数相似,也是一组完成特定功能的sql语句集合。把复杂或频繁调用的sql提前写好并指定一个名称。待到要使用时,直接调用即可。
定义存储过程的语法如下:
create procedure <过程名> ( [过程参数[,…] ] ) <过程体>
[过程参数[,…] ] 格式
[ in | out | inout ] <参数名> <类型>
#语法定义来自:http://c.biancheng.net/view/2593.html
创建无参的存储过程
示例4:查询用户name。
mysql> delimiter // mysql> craete procedure queryname() -> begin -> select name from user_info; -> end //
关于delimiter命令,修改mysql结束命令的字符。默认的结束命令字符为分号,当存储过程中包含多条语句时,遇到第一个分号会作为存储过程结束的标志。这样不符合预期,因此需要修改默认结束命令字符。 delimiter //就是将结束命令字符修改为//。调用存储过程的命令为:call 存储过程名称。
#此时的命令的结束符号为// 不是; mysql> call queryname()// +--------------+ | name | +--------------+ | stephenwang7 | | stephenwang8 | +--------------+ 2 rows in set (0.00 sec) query ok, 0 rows affected (0.00 sec)
创建带参数的存储过程
示例5:根据id查询name。
mysql> create procedure querynamebyid -> (in uid int(15)) -> begin -> select name from user_info where id=uid; -> end -> // query ok, 0 rows affected (0.03 sec)
调用存储过程querynamebyid
mysql> call querynamebyid(1); -> // +--------------+ | name | +--------------+ | stephenwang7 | +--------------+ 1 row in set (0.03 sec) query ok, 0 rows affected (0.04 sec)
修改存储过程
如果想创建存储过程的内容可以先删除再重新创建存储过程。
查看存储过程
show create procedure <过程名称>
mysql> show create procedure querynamebyid; -> // +---------------+-------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+ | procedure | sql_mode | create procedure | character_set_client | collation_connection | database collation | +---------------+-------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+ | querynamebyid | only_full_group_by,strict_trans_tables,no_zero_in_date,no_zero_date,error_for_division_by_zero,no_auto_create_user,no_engine_substitution | create definer=`root`@`localhost` procedure `querynamebyid`(in uid int(15)) begin select name from user_info where id=uid; end | utf8 | utf8_general_ci | latin1_swedish_ci | +---------------+-------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+ 1 row in set (0.04 sec)
删除存储过程
drop procedure <过程名称>
删除存储过程querynamebyid
mysql> drop procedure querynamebyid// query ok, 0 rows affected (0.02 sec) mysql> call querynamebyid(1)// error 1305 (42000): procedure rms.querynamebyid does not exist
4、总结
自定义函数和存储过程都是完成特定功能的sql集合,那么他们有什么不同呢?
a、调用方式不同
#自定义函数
select <函数名>
#存储过程
call <存储过程名>
b、自定义函数不能有输出参数,而存储过程可以。
c、自定义函数必须要包含return语句,而存储过程则不需要。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。
上一篇: Shell 字符串截取
下一篇: 查看服务器并发连接数