旗下导航:搜·么
当前位置:网站首页 > MySQL教程 > 正文

MySQL自定义函数与存储历程的细致引见(附代码)【MySQL教程】,MySQL

作者:搜教程发布时间:2019-12-01分类:MySQL教程浏览:52评论:0


导读:本篇文章给人人带来的内容是关于MySQL自定义函数与存储历程的细致引见(附代码),有肯定的参考价值,有需要的朋侪能够参考一下,愿望对你有所协助。1、前置条件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语句,而存储历程则不需要。

【相干引荐:MySQL教程】

以上就是MySQL自定义函数与存储历程的细致引见(附代码)的细致内容,更多请关注ki4网别的相干文章!

标签:MySQL


欢迎 发表评论: