您好,登錄后才能下訂單哦!
下文主要給大家帶來什么是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)
函數:可以完成特定功能的一段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)
示例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
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語法如下:
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>
存儲功能和自定義函數相似,也是一組完成特定功能的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
自定義函數和存儲過程都是完成特定功能的SQL集合,那么他們有什么不同呢?
a、調用方式不同
#自定義函數 select <函數名> #存儲過程 call <存儲過程名>
b、自定義函數不能有輸出參數,而存儲過程可以。
c、自定義函數必須要包含return語句,而存儲過程則不需要。
對于以上關于什么是MySQL自定義函數與創建存儲過程,大家是不是覺得非常有幫助。如果需要了解更多內容,請繼續關注我們的行業資訊,相信你會喜歡上這些內容的。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。