700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > MySQL自定义函数与存储过程的详细介绍(code)

MySQL自定义函数与存储过程的详细介绍(code)

时间:2019-05-04 08:53:00

相关推荐

MySQL自定义函数与存储过程的详细介绍(code)

数据库|mysql教程

MySQL

数据库-mysql教程

android音乐播放器开发源码,Ubuntu编译魔趣,tomcat报错怎么看,爬虫添加图片,php url数组处理,凤岗seo优化关键词多少钱lzw

本篇文章给大家带来的内容是关于MySQL自定义函数与存储过程的详细介绍(附代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

秒赞网源码,vscode设置文件格式化,ubuntu 挂起黑屏,tomcat8 最大并发,数据爬虫python,php如何跳转网页,seo万词霸屏还能做吗lzw

1、前置条件

电影网址导航源码,vscode设置自动补全,ubuntu创建.s,tomcat下设置内存,爬虫培育,武汉php工资,哈尔滨专业seo排名总部,asp 视频 网站 源码,政府投票系统模板lzw

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@ | 15103887470 | admin | male | 200 | -04-12 20:11:30 | -04-19 20:11:30 | 30 | NULL || 2 | StephenWang8 | 123456 | 123@ | 15103887470 | viewer | male | 200 | -04-12 20:11:30 | -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 1mysql> 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 1mysql> 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 1mysql> 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 latin1return (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 existmysql>

3、存储过程

存储功能和自定义函数相似,也是一组完成特定功能的SQL语句集合。把复杂或频繁调用的SQL提前写好并指定一个名称。待到要使用时,直接调用即可。

定义存储过程的语法如下:

CREATE PROCEDURE ( [过程参数[,…] ] ) [过程参数[,…] ] 格式[ IN | OUT | INOUT ] #语法定义来自:/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语句,而存储过程则不需要。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。