MySQL/MariaDB數據庫的函數html
做者:尹正傑mysql
版權聲明:原創做品,謝絕轉載!不然將追究法律責任。sql
MySQL/MariaDB數據庫的函數分爲系統函數和用戶自定義函數(user-defined function,簡稱UDF)。數據庫
一.系統函數ide
系統函數是Mysql/MariaDB內置的函數,下面有幾個案例。
1>.統計表中的行數函數
MariaDB [yinzhengjie]> SELECT * FROM students; +-------+---------------+-----+--------+---------+-----------+ | StuID | Name | Age | Gender | ClassID | TeacherID | +-------+---------------+-----+--------+---------+-----------+ | 1 | Shi Zhongyu | 22 | M | 2 | 3 | | 2 | Shi Potian | 22 | M | 1 | 7 | | 3 | Xie Yanke | 53 | M | 2 | 16 | | 4 | Ding Dian | 32 | M | 4 | 4 | | 5 | Yu Yutong | 26 | M | 3 | 1 | | 6 | Shi Qing | 46 | M | 5 | NULL | | 7 | Xi Ren | 19 | F | 3 | NULL | | 8 | Lin Daiyu | 17 | F | 7 | NULL | | 9 | Ren Yingying | 20 | F | 6 | NULL | | 10 | Yue Lingshan | 19 | F | 3 | NULL | | 11 | Yuan Chengzhi | 23 | M | 6 | NULL | | 12 | Wen Qingqing | 19 | F | 1 | NULL | | 13 | Tian Boguang | 33 | M | 2 | NULL | | 14 | Lu Wushuang | 17 | F | 3 | NULL | | 15 | Duan Yu | 19 | M | 4 | NULL | | 16 | Xu Zhu | 21 | M | 1 | NULL | | 17 | Lin Chong | 25 | M | 4 | NULL | | 18 | Hua Rong | 23 | M | 7 | NULL | | 19 | Xue Baochai | 18 | F | 6 | NULL | | 20 | Diao Chan | 19 | F | 7 | NULL | | 21 | Huang Yueying | 22 | F | 6 | NULL | | 22 | Xiao Qiao | 20 | F | 1 | NULL | | 23 | Ma Chao | 23 | M | 4 | NULL | | 24 | Xu Xian | 27 | M | NULL | NULL | | 25 | Sun Dasheng | 100 | M | NULL | NULL | +-------+---------------+-----+--------+---------+-----------+ 25 rows in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SELECT COUNT(*) FROM students; #統計"students"表中有多少行 +----------+ | COUNT(*) | +----------+ | 25 | +----------+ 1 row in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SELECT COUNT(stuid) FROM students; #統計stuid這一列有多少行 +--------------+ | COUNT(stuid) | +--------------+ | 25 | +--------------+ 1 row in set (0.00 sec) MariaDB [yinzhengjie]> MariaDB [yinzhengjie]> SELECT COUNT(classid) FROM students; #統計classid這一列有多少行,若該列值有NULL出現則不計入,對行號統計有誤,不推薦這也寫(或者在錄入表數據是不容許出現NULL值)。 +----------------+ | COUNT(classid) | +----------------+ | 23 | +----------------+ 1 row in set (0.00 sec) MariaDB [yinzhengjie]>
2>.統計"student"表中的學平生均年齡ui
MariaDB [yinzhengjie]> SELECT * FROM students; +-------+---------------+-----+--------+---------+-----------+ | StuID | Name | Age | Gender | ClassID | TeacherID | +-------+---------------+-----+--------+---------+-----------+ | 1 | Shi Zhongyu | 22 | M | 2 | 3 | | 2 | Shi Potian | 22 | M | 1 | 7 | | 3 | Xie Yanke | 53 | M | 2 | 16 | | 4 | Ding Dian | 32 | M | 4 | 4 | | 5 | Yu Yutong | 26 | M | 3 | 1 | | 6 | Shi Qing | 46 | M | 5 | NULL | | 7 | Xi Ren | 19 | F | 3 | NULL | | 8 | Lin Daiyu | 17 | F | 7 | NULL | | 9 | Ren Yingying | 20 | F | 6 | NULL | | 10 | Yue Lingshan | 19 | F | 3 | NULL | | 11 | Yuan Chengzhi | 23 | M | 6 | NULL | | 12 | Wen Qingqing | 19 | F | 1 | NULL | | 13 | Tian Boguang | 33 | M | 2 | NULL | | 14 | Lu Wushuang | 17 | F | 3 | NULL | | 15 | Duan Yu | 19 | M | 4 | NULL | | 16 | Xu Zhu | 21 | M | 1 | NULL | | 17 | Lin Chong | 25 | M | 4 | NULL | | 18 | Hua Rong | 23 | M | 7 | NULL | | 19 | Xue Baochai | 18 | F | 6 | NULL | | 20 | Diao Chan | 19 | F | 7 | NULL | | 21 | Huang Yueying | 22 | F | 6 | NULL | | 22 | Xiao Qiao | 20 | F | 1 | NULL | | 23 | Ma Chao | 23 | M | 4 | NULL | | 24 | Xu Xian | 27 | M | NULL | NULL | | 25 | Sun Dasheng | 100 | M | NULL | NULL | +-------+---------------+-----+--------+---------+-----------+ 25 rows in set (0.00 sec) MariaDB [yinzhengjie]> MariaDB [yinzhengjie]> SELECT AVG(age) FROM students; +----------+ | AVG(age) | +----------+ | 27.4000 | +----------+ 1 row in set (0.00 sec) MariaDB [yinzhengjie]>
3>.顯示當前系統時間this
MariaDB [yinzhengjie]> SELECT NOW(); +---------------------+ | NOW() | +---------------------+ | 2019-10-28 08:15:55 | +---------------------+ 1 row in set (0.00 sec) MariaDB [yinzhengjie]>
4>.查看一個負數的絕對值spa
MariaDB [yinzhengjie]> SELECT ABS(-1314); +------------+ | ABS(-1314) | +------------+ | 1314 | +------------+ 1 row in set (0.00 sec) MariaDB [yinzhengjie]> MariaDB [yinzhengjie]>
5>.查看數據庫版本3d
MariaDB [yinzhengjie]> SELECT VERSION(); +-----------------+ | VERSION() | +-----------------+ | 10.2.19-MariaDB | +-----------------+ 1 row in set (0.00 sec) MariaDB [yinzhengjie]>
6>.查看當前登陸用戶信息
MariaDB [yinzhengjie]> SELECT USER(); +----------------+ | USER() | +----------------+ | root@localhost | +----------------+ 1 row in set (0.00 sec) MariaDB [yinzhengjie]>
7>.查看當前所在數據庫
MariaDB [yinzhengjie]> SELECT DATABASE(); +-------------+ | DATABASE() | +-------------+ | yinzhengjie | +-------------+ 1 row in set (0.00 sec) MariaDB [yinzhengjie]>
8>.把傳入的參數鏈接成一個字符串
MariaDB [yinzhengjie]> SELECT CONCAT("尹正傑","到此一遊","2019"); +-------------------------------------------+ | CONCAT("尹正傑","到此一遊","2019") | +-------------------------------------------+ | 尹正傑到此一遊2019 | +-------------------------------------------+ 1 row in set (0.00 sec) MariaDB [yinzhengjie]> MariaDB [yinzhengjie]>
9>.將大寫字母小寫
MariaDB [yinzhengjie]> SELECT LOWER('YINZHENGJIE'); +----------------------+ | LOWER('YINZHENGJIE') | +----------------------+ | yinzhengjie | +----------------------+ 1 row in set (0.00 sec) MariaDB [yinzhengjie]>
10>.將小寫字母大寫
MariaDB [yinzhengjie]> SELECT UPPER('yinzhengjie'); +----------------------+ | UPPER('yinzhengjie') | +----------------------+ | YINZHENGJIE | +----------------------+ 1 row in set (0.00 sec) MariaDB [yinzhengjie]> MariaDB [yinzhengjie]>
11>.更多系統函數介紹
因爲系統函數過多,我這裏就不一一例舉啦,感興趣的小夥伴可自行查閱資料。 博主推薦閱讀: https://dev.mysql.com/doc/refman/5.7/en/func-op-summary-ref.html
二.自定義函數(user-defined function,簡稱UDF)
自定義函數 (user-defined function UDF)信息保存在mysql.proc表中; 參數能夠有多個,也能夠沒有參數; 必須有且只有一個返回值; 自定義函數中定義局部變量語法: DECLARE 變量1[,變量2,... ]變量類型 [DEFAULT 默認值] 說明:局部變量的做用範圍是在BEGIN...END程序中,並且定義局部變量語句必須在BEGIN...END的第一行定義 爲變量賦值語法 SET parameter_name = value[,parameter_name = value...] SELECT INTO parameter_name
1>.建立UDF
MariaDB [yinzhengjie]> HELP CREATE FUNCTION UDF Name: 'CREATE FUNCTION UDF' Description: Syntax: CREATE [AGGREGATE] FUNCTION function_name RETURNS {STRING|INTEGER|REAL|DECIMAL} SONAME shared_library_name A user-defined function (UDF) is a way to extend MySQL with a new function that works like a native (built-in) MySQL function such as ABS() or CONCAT(). function_name is the name that should be used in SQL statements to invoke the function. The RETURNS clause indicates the type of the function's return value. DECIMAL is a legal value after RETURNS, but currently DECIMAL functions return string values and should be written like STRING functions. shared_library_name is the basename of the shared object file that contains the code that implements the function. The file must be located in the plugin directory. This directory is given by the value of the plugin_dir system variable. For more information, see http://dev.mysql.com/doc/refman/5.5/en/udf-compiling.html. To create a function, you must have the INSERT privilege for the mysql database. This is necessary because CREATE FUNCTION adds a row to the mysql.func system table that records the function's name, type, and shared library name. If you do not have this table, you should run the mysql_upgrade command to create it. See https://mariadb.com/kb/en/mysql_upgrade/. URL: https://mariadb.com/kb/en/create-function-udf/ MariaDB [yinzhengjie]> MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> CREATE FUNCTION simpleFun() RETURNS VARCHAR(7) RETURN "尹正傑到此一遊"; #無參UDF案例 Query OK, 0 rows affected (0.01 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> DELIMITER // #將SQL語句的結束符臨時修改成"//",由於咱們定義的函數中會屢次用到默認的";" MariaDB [yinzhengjie]> CREATE FUNCTION deleteById(uid SMALLINT UNSIGNED) RETURNS VARCHAR(100) -> BEGIN -> DELETE FROM students WHERE stuid = uid; #根據用戶輸入的uid來刪除對應的stuid這一行數據 -> RETURN (SELECT COUNT(stuid) FROM students); #返回"stuid"現有的行數 -> END// Query OK, 0 rows affected (0.01 sec) MariaDB [yinzhengjie]> DELIMITER ; #再次將SQL語句的結束符修改成默認的";" MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> DELIMITER // #因爲咱們函數中須要執行多條SQL語句,每條語句都須要用";"進行分割,所以咱們臨時將SQL的結束符修改成"//" MariaDB [yinzhengjie]> CREATE FUNCTION addTwoNumber(x SMALLINT UNSIGNED, Y SMALLINT UNSIGNED) #要求用戶傳入2個參數,參數類型爲SMALLINT UNSIGNED -> RETURNS SMALLINT #定義返回值類型爲SMALLINT -> BEGIN -> DECLARE a, b SMALLINT UNSIGNED; #在函數內部定義兩個變量a,b,指定其類型爲SMALLINT UNSIGNED -> SET a = x, b = y; #將用戶傳入的x,y值賦值給咱們上一行定義的兩個變量a和b -> RETURN a+b; #返回a+b,即用戶傳入的x+y的結果。 -> END// Query OK, 0 rows affected (0.00 sec) MariaDB [yinzhengjie]> DELIMITER ; #別忘記將結束符修改成默認的";" MariaDB [yinzhengjie]>
2>.查看函數列表
MariaDB [yinzhengjie]> SHOW FUNCTION STATUS\G *************************** 1. row *************************** Db: yinzhengjie Name: addTwoNumber Type: FUNCTION Definer: root@localhost Modified: 2019-10-28 20:13:59 Created: 2019-10-28 20:13:59 Security_type: DEFINER Comment: character_set_client: utf8mb4 collation_connection: utf8mb4_general_ci Database Collation: utf8_general_ci *************************** 2. row *************************** Db: yinzhengjie Name: deleteById Type: FUNCTION Definer: root@localhost Modified: 2019-10-28 20:06:27 Created: 2019-10-28 20:06:27 Security_type: DEFINER Comment: character_set_client: utf8mb4 collation_connection: utf8mb4_general_ci Database Collation: utf8_general_ci *************************** 3. row *************************** Db: yinzhengjie Name: simpleFun Type: FUNCTION Definer: root@localhost Modified: 2019-10-28 19:49:17 Created: 2019-10-28 19:49:17 Security_type: DEFINER Comment: character_set_client: utf8mb4 collation_connection: utf8mb4_general_ci Database Collation: utf8_general_ci 3 rows in set (0.00 sec) MariaDB [yinzhengjie]> MariaDB [yinzhengjie]>
3>.查看函數定義
MariaDB [yinzhengjie]> SHOW CREATE FUNCTION simpleFun\G *************************** 1. row *************************** Function: simpleFun sql_mode: STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION Create Function: CREATE DEFINER=`root`@`localhost` FUNCTION `simpleFun`() RETURNS varchar(7) CHARSET utf8 RETURN "尹正傑到此一遊" character_set_client: utf8mb4 collation_connection: utf8mb4_general_ci Database Collation: utf8_general_ci 1 row in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SHOW CREATE FUNCTION deleteById\G *************************** 1. row *************************** Function: deleteById sql_mode: STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION Create Function: CREATE DEFINER=`root`@`localhost` FUNCTION `deleteById`(uid SMALLINT UNSIGNED) RETURNS varchar(100) CHARSET utf8BEGIN DELETE FROM students WHERE stuid = uid; RETURN (SELECT COUNT(stuid) FROM students); END character_set_client: utf8mb4 collation_connection: utf8mb4_general_ci Database Collation: utf8_general_ci 1 row in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SHOW CREATE FUNCTION addTwoNumber\G *************************** 1. row *************************** Function: addTwoNumber sql_mode: STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION Create Function: CREATE DEFINER=`root`@`localhost` FUNCTION `addTwoNumber`(x SMALLINT UNSIGNED, Y SMALLINT UNSIGNED) RETURNS smallint(6)BEGIN DECLARE a, b SMALLINT UNSIGNED; SET a = x, b = y; RETURN a+b; END character_set_client: utf8mb4 collation_connection: utf8mb4_general_ci Database Collation: utf8_general_ci 1 row in set (0.00 sec) MariaDB [yinzhengjie]>
4>.調用自定義函數語法
MariaDB [yinzhengjie]> SELECT simpleFun(); #調用無參UDF +-----------------------+ | simpleFun() | +-----------------------+ | 尹正傑到此一遊 | +-----------------------+ 1 row in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [mysql]> SHOW FUNCTION STATUS\G *************************** 1. row *************************** Db: yinzhengjie Name: simpleFun Type: FUNCTION Definer: root@localhost Modified: 2019-10-28 19:49:17 Created: 2019-10-28 19:49:17 Security_type: DEFINER Comment: character_set_client: utf8mb4 collation_connection: utf8mb4_general_ci Database Collation: utf8_general_ci 1 row in set (0.00 sec) MariaDB [mysql]> MariaDB [mysql]> SELECT simpleFun(); #很顯然咱們沒有顯式指定函數屬於哪一個數據庫,所以它默認在當前數據庫查找該函數。 ERROR 1305 (42000): FUNCTION mysql.simpleFun does not exist MariaDB [mysql]> MariaDB [mysql]> SELECT yinzhengjie.simpleFun(); #當函數是跨數據庫調用時,須要顯式指定數據庫名稱喲~(不然會拋出異常) +-------------------------+ | yinzhengjie.simpleFun() | +-------------------------+ | 尹正傑到此一遊 | +-------------------------+ 1 row in set (0.00 sec) MariaDB [mysql]> MariaDB [mysql]>
MariaDB [yinzhengjie]> SELECT * FROM students; +-------+---------------+-----+--------+---------+-----------+ | StuID | Name | Age | Gender | ClassID | TeacherID | +-------+---------------+-----+--------+---------+-----------+ | 1 | Shi Zhongyu | 22 | M | 2 | 3 | | 2 | Shi Potian | 22 | M | 1 | 7 | | 3 | Xie Yanke | 53 | M | 2 | 16 | | 4 | Ding Dian | 32 | M | 4 | 4 | | 5 | Yu Yutong | 26 | M | 3 | 1 | | 6 | Shi Qing | 46 | M | 5 | NULL | | 7 | Xi Ren | 19 | F | 3 | NULL | | 8 | Lin Daiyu | 17 | F | 7 | NULL | | 9 | Ren Yingying | 20 | F | 6 | NULL | | 10 | Yue Lingshan | 19 | F | 3 | NULL | | 11 | Yuan Chengzhi | 23 | M | 6 | NULL | | 12 | Wen Qingqing | 19 | F | 1 | NULL | | 13 | Tian Boguang | 33 | M | 2 | NULL | | 14 | Lu Wushuang | 17 | F | 3 | NULL | | 15 | Duan Yu | 19 | M | 4 | NULL | | 16 | Xu Zhu | 21 | M | 1 | NULL | | 17 | Lin Chong | 25 | M | 4 | NULL | | 18 | Hua Rong | 23 | M | 7 | NULL | | 19 | Xue Baochai | 18 | F | 6 | NULL | | 20 | Diao Chan | 19 | F | 7 | NULL | | 21 | Huang Yueying | 22 | F | 6 | NULL | | 22 | Xiao Qiao | 20 | F | 1 | NULL | | 23 | Ma Chao | 23 | M | 4 | NULL | | 24 | Xu Xian | 27 | M | NULL | NULL | | 25 | Sun Dasheng | 100 | M | NULL | NULL | +-------+---------------+-----+--------+---------+-----------+ 25 rows in set (0.00 sec) MariaDB [yinzhengjie]> MariaDB [yinzhengjie]> SELECT COUNT(stuid) FROM students; +--------------+ | COUNT(stuid) | +--------------+ | 25 | +--------------+ 1 row in set (0.00 sec) MariaDB [yinzhengjie]> MariaDB [yinzhengjie]> MariaDB [yinzhengjie]> SHOW FUNCTION STATUS\G *************************** 1. row *************************** Db: yinzhengjie Name: deleteById Type: FUNCTION Definer: root@localhost Modified: 2019-10-28 20:06:27 Created: 2019-10-28 20:06:27 Security_type: DEFINER Comment: character_set_client: utf8mb4 collation_connection: utf8mb4_general_ci Database Collation: utf8_general_ci *************************** 2. row *************************** Db: yinzhengjie Name: simpleFun Type: FUNCTION Definer: root@localhost Modified: 2019-10-28 19:49:17 Created: 2019-10-28 19:49:17 Security_type: DEFINER Comment: character_set_client: utf8mb4 collation_connection: utf8mb4_general_ci Database Collation: utf8_general_ci 2 rows in set (0.00 sec) MariaDB [yinzhengjie]> MariaDB [yinzhengjie]> SELECT deleteById(15); +----------------+ | deleteById(15) | +----------------+ | 24 | +----------------+ 1 row in set (0.00 sec) MariaDB [yinzhengjie]> MariaDB [yinzhengjie]> SELECT deleteById(16); +----------------+ | deleteById(16) | +----------------+ | 23 | +----------------+ 1 row in set (0.00 sec) MariaDB [yinzhengjie]> MariaDB [yinzhengjie]> SELECT deleteById(17); +----------------+ | deleteById(17) | +----------------+ | 22 | +----------------+ 1 row in set (0.01 sec) MariaDB [yinzhengjie]> MariaDB [yinzhengjie]> SELECT COUNT(stuid) FROM students; +--------------+ | COUNT(stuid) | +--------------+ | 22 | +--------------+ 1 row in set (0.00 sec) MariaDB [yinzhengjie]> MariaDB [yinzhengjie]> SELECT * FROM students; +-------+---------------+-----+--------+---------+-----------+ | StuID | Name | Age | Gender | ClassID | TeacherID | +-------+---------------+-----+--------+---------+-----------+ | 1 | Shi Zhongyu | 22 | M | 2 | 3 | | 2 | Shi Potian | 22 | M | 1 | 7 | | 3 | Xie Yanke | 53 | M | 2 | 16 | | 4 | Ding Dian | 32 | M | 4 | 4 | | 5 | Yu Yutong | 26 | M | 3 | 1 | | 6 | Shi Qing | 46 | M | 5 | NULL | | 7 | Xi Ren | 19 | F | 3 | NULL | | 8 | Lin Daiyu | 17 | F | 7 | NULL | | 9 | Ren Yingying | 20 | F | 6 | NULL | | 10 | Yue Lingshan | 19 | F | 3 | NULL | | 11 | Yuan Chengzhi | 23 | M | 6 | NULL | | 12 | Wen Qingqing | 19 | F | 1 | NULL | | 13 | Tian Boguang | 33 | M | 2 | NULL | | 14 | Lu Wushuang | 17 | F | 3 | NULL | | 18 | Hua Rong | 23 | M | 7 | NULL | | 19 | Xue Baochai | 18 | F | 6 | NULL | | 20 | Diao Chan | 19 | F | 7 | NULL | | 21 | Huang Yueying | 22 | F | 6 | NULL | | 22 | Xiao Qiao | 20 | F | 1 | NULL | | 23 | Ma Chao | 23 | M | 4 | NULL | | 24 | Xu Xian | 27 | M | NULL | NULL | | 25 | Sun Dasheng | 100 | M | NULL | NULL | +-------+---------------+-----+--------+---------+-----------+ 22 rows in set (0.00 sec) MariaDB [yinzhengjie]>
5>.刪除UDF
MariaDB [yinzhengjie]> SHOW FUNCTION STATUS\G *************************** 1. row *************************** Db: yinzhengjie Name: addTwoNumber Type: FUNCTION Definer: root@localhost Modified: 2019-10-28 20:13:59 Created: 2019-10-28 20:13:59 Security_type: DEFINER Comment: character_set_client: utf8mb4 collation_connection: utf8mb4_general_ci Database Collation: utf8_general_ci *************************** 2. row *************************** Db: yinzhengjie Name: deleteById Type: FUNCTION Definer: root@localhost Modified: 2019-10-28 20:06:27 Created: 2019-10-28 20:06:27 Security_type: DEFINER Comment: character_set_client: utf8mb4 collation_connection: utf8mb4_general_ci Database Collation: utf8_general_ci *************************** 3. row *************************** Db: yinzhengjie Name: simpleFun Type: FUNCTION Definer: root@localhost Modified: 2019-10-28 19:49:17 Created: 2019-10-28 19:49:17 Security_type: DEFINER Comment: character_set_client: utf8mb4 collation_connection: utf8mb4_general_ci Database Collation: utf8_general_ci 3 rows in set (0.00 sec) MariaDB [yinzhengjie]> MariaDB [yinzhengjie]> DROP FUNCTION simpleFun; Query OK, 0 rows affected (0.00 sec) MariaDB [yinzhengjie]> MariaDB [yinzhengjie]> SHOW FUNCTION STATUS\G *************************** 1. row *************************** Db: yinzhengjie Name: addTwoNumber Type: FUNCTION Definer: root@localhost Modified: 2019-10-28 20:13:59 Created: 2019-10-28 20:13:59 Security_type: DEFINER Comment: character_set_client: utf8mb4 collation_connection: utf8mb4_general_ci Database Collation: utf8_general_ci *************************** 2. row *************************** Db: yinzhengjie Name: deleteById Type: FUNCTION Definer: root@localhost Modified: 2019-10-28 20:06:27 Created: 2019-10-28 20:06:27 Security_type: DEFINER Comment: character_set_client: utf8mb4 collation_connection: utf8mb4_general_ci Database Collation: utf8_general_ci 2 rows in set (0.00 sec) MariaDB [yinzhengjie]> MariaDB [yinzhengjie]>
6>.自定義函數 (user-defined function UDF)信息保存在mysql.proc表中
MariaDB [yinzhengjie]> SHOW FUNCTION STATUS\G *************************** 1. row *************************** Db: yinzhengjie Name: addTwoNumber Type: FUNCTION Definer: root@localhost Modified: 2019-10-28 20:13:59 Created: 2019-10-28 20:13:59 Security_type: DEFINER Comment: character_set_client: utf8mb4 collation_connection: utf8mb4_general_ci Database Collation: utf8_general_ci *************************** 2. row *************************** Db: yinzhengjie Name: deleteById Type: FUNCTION Definer: root@localhost Modified: 2019-10-28 20:06:27 Created: 2019-10-28 20:06:27 Security_type: DEFINER Comment: character_set_client: utf8mb4 collation_connection: utf8mb4_general_ci Database Collation: utf8_general_ci 2 rows in set (0.00 sec) MariaDB [yinzhengjie]> MariaDB [yinzhengjie]> MariaDB [yinzhengjie]> SELECT * FROM mysql.proc\G *************************** 1. row *************************** db: mysql name: AddGeometryColumn type: PROCEDURE specific_name: AddGeometryColumn language: SQL sql_data_access: CONTAINS_SQL is_deterministic: NO security_type: DEFINER param_list: catalog varchar(64), t_schema varchar(64), t_name varchar(64), geometry_column varchar(64), t_srid int returns: body: begin set @qwe= concat('ALTER TABLE ', t_schema, '.', t_name, ' ADD ', geometry_column,' GEOMETRY REF_SYSTEM_ID=', t_srid); P REPARE ls from @qwe; execute ls; deallocate prepare ls; end definer: root@localhost created: 2019-10-26 22:17:15 modified: 2019-10-26 22:17:15 sql_mode: comment: character_set_client: utf8 collation_connection: utf8_general_ci db_collation: latin1_swedish_ci body_utf8: begin set @qwe= concat('ALTER TABLE ', t_schema, '.', t_name, ' ADD ', geometry_column,' GEOMETRY REF_SYSTEM_ID=', t_srid); P REPARE ls from @qwe; execute ls; deallocate prepare ls; end*************************** 2. row *************************** db: mysql name: DropGeometryColumn type: PROCEDURE specific_name: DropGeometryColumn language: SQL sql_data_access: CONTAINS_SQL is_deterministic: NO security_type: DEFINER param_list: catalog varchar(64), t_schema varchar(64), t_name varchar(64), geometry_column varchar(64) returns: body: begin set @qwe= concat('ALTER TABLE ', t_schema, '.', t_name, ' DROP ', geometry_column); PREPARE ls from @qwe; execute ls; d eallocate prepare ls; end definer: root@localhost created: 2019-10-26 22:17:15 modified: 2019-10-26 22:17:15 sql_mode: comment: character_set_client: utf8 collation_connection: utf8_general_ci db_collation: latin1_swedish_ci body_utf8: begin set @qwe= concat('ALTER TABLE ', t_schema, '.', t_name, ' DROP ', geometry_column); PREPARE ls from @qwe; execute ls; d eallocate prepare ls; end*************************** 3. row *************************** db: yinzhengjie name: deleteById type: FUNCTION specific_name: deleteById language: SQL sql_data_access: CONTAINS_SQL is_deterministic: NO security_type: DEFINER param_list: uid SMALLINT UNSIGNED returns: varchar(100) CHARSET utf8 body: BEGIN DELETE FROM students WHERE stuid = uid; RETURN (SELECT COUNT(stuid) FROM students); END definer: root@localhost created: 2019-10-28 20:06:27 modified: 2019-10-28 20:06:27 sql_mode: STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION comment: character_set_client: utf8mb4 collation_connection: utf8mb4_general_ci db_collation: utf8_general_ci body_utf8: BEGIN DELETE FROM students WHERE stuid = uid; RETURN (SELECT COUNT(stuid) FROM students); END *************************** 4. row *************************** db: yinzhengjie name: addTwoNumber type: FUNCTION specific_name: addTwoNumber language: SQL sql_data_access: CONTAINS_SQL is_deterministic: NO security_type: DEFINER param_list: x SMALLINT UNSIGNED, Y SMALLINT UNSIGNED returns: smallint(6) body: BEGIN DECLARE a, b SMALLINT UNSIGNED; SET a = x, b = y; RETURN a+b; END definer: root@localhost created: 2019-10-28 20:13:59 modified: 2019-10-28 20:13:59 sql_mode: STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION comment: character_set_client: utf8mb4 collation_connection: utf8mb4_general_ci db_collation: utf8_general_ci body_utf8: BEGIN DECLARE a, b SMALLINT UNSIGNED; SET a = x, b = y; RETURN a+b; END 4 rows in set (0.00 sec) MariaDB [yinzhengjie]>
7>.爲變量賦值語法
MariaDB [yinzhengjie]> DELIMITER // MariaDB [yinzhengjie]> CREATE FUNCTION students_numbers() RETURNS SMALLINT -> BEGIN -> DECLARE x SMALLINT; -> SELECT COUNT(StuID) FROM students INTO x; -> RETURN x; -> END// Query OK, 0 rows affected (0.00 sec) MariaDB [yinzhengjie]> DELIMITER ; MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SELECT students_numbers(); +--------------------+ | students_numbers() | +--------------------+ | 22 | +--------------------+ 1 row in set (0.00 sec) MariaDB [yinzhengjie]> MariaDB [yinzhengjie]> SELECT COUNT(StuID) FROM students; +--------------+ | COUNT(StuID) | +--------------+ | 22 | +--------------+ 1 row in set (0.00 sec) MariaDB [yinzhengjie]> MariaDB [yinzhengjie]> SELECT * FROM students; +-------+---------------+-----+--------+---------+-----------+ | StuID | Name | Age | Gender | ClassID | TeacherID | +-------+---------------+-----+--------+---------+-----------+ | 1 | Shi Zhongyu | 22 | M | 2 | 3 | | 2 | Shi Potian | 22 | M | 1 | 7 | | 3 | Xie Yanke | 53 | M | 2 | 16 | | 4 | Ding Dian | 32 | M | 4 | 4 | | 5 | Yu Yutong | 26 | M | 3 | 1 | | 6 | Shi Qing | 46 | M | 5 | NULL | | 7 | Xi Ren | 19 | F | 3 | NULL | | 8 | Lin Daiyu | 17 | F | 7 | NULL | | 9 | Ren Yingying | 20 | F | 6 | NULL | | 10 | Yue Lingshan | 19 | F | 3 | NULL | | 11 | Yuan Chengzhi | 23 | M | 6 | NULL | | 12 | Wen Qingqing | 19 | F | 1 | NULL | | 13 | Tian Boguang | 33 | M | 2 | NULL | | 14 | Lu Wushuang | 17 | F | 3 | NULL | | 18 | Hua Rong | 23 | M | 7 | NULL | | 19 | Xue Baochai | 18 | F | 6 | NULL | | 20 | Diao Chan | 19 | F | 7 | NULL | | 21 | Huang Yueying | 22 | F | 6 | NULL | | 22 | Xiao Qiao | 20 | F | 1 | NULL | | 23 | Ma Chao | 23 | M | 4 | NULL | | 24 | Xu Xian | 27 | M | NULL | NULL | | 25 | Sun Dasheng | 100 | M | NULL | NULL | +-------+---------------+-----+--------+---------+-----------+ 22 rows in set (0.00 sec) MariaDB [yinzhengjie]> MariaDB [yinzhengjie]>