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語句,而儲存過程則不需要。