這篇文章帶給大家的內容是關於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教學】
#以上是MySQL自訂函數與預存程序的詳細介紹(附程式碼)的詳細內容。更多資訊請關注PHP中文網其他相關文章!