MySQL/MariaDB数据库的函数
MySQL/MariaDB数据库的函数
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
MySQL/MariaDB数据库的函数分为系统函数和用户自定义函数(user-defined function,简称UDF)。
一.系统函数
系统函数是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 * FROM students;
MariaDB [yinzhengjie]> SELECT COUNT(*) FROM students; #统计"students"表中有多少行
+----------+
| COUNT(*) |
+----------+
| 25 |
+----------+
1 row in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SELECT COUNT(*) FROM students; #统计"students"表中有多少行
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]>
MariaDB [yinzhengjie]> SELECT COUNT(classid) FROM students; #统计classid这一列有多少行,若该列值有NULL出现则不计入,对行号统计有误,不推荐这也写(或者在录入表数据是不允许出现NULL值)。
2>.统计"student"表中的学生平均年龄
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]>
MariaDB [yinzhengjie]> SELECT AVG(age) FROM students;
3>.显示当前系统时间
MariaDB [yinzhengjie]> SELECT NOW();
+---------------------+
| NOW() |
+---------------------+
| 2019-10-28 08:15:55 |
+---------------------+
1 row in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SELECT NOW();
4>.查看一个负数的绝对值
MariaDB [yinzhengjie]> SELECT ABS(-1314);
+------------+
| ABS(-1314) |
+------------+
| 1314 |
+------------+
1 row in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SELECT ABS(-1314);
5>.查看数据库版本
MariaDB [yinzhengjie]> SELECT VERSION();
+-----------------+
| VERSION() |
+-----------------+
| 10.2.19-MariaDB |
+-----------------+
1 row in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SELECT VERSION();
6>.查看当前登录用户信息
MariaDB [yinzhengjie]> SELECT USER();
+----------------+
| USER() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SELECT USER();
7>.查看当前所在数据库
MariaDB [yinzhengjie]> SELECT DATABASE();
+-------------+
| DATABASE() |
+-------------+
| yinzhengjie |
+-------------+
1 row in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SELECT DATABASE();
8>.把传入的参数连接成一个字符串
MariaDB [yinzhengjie]> SELECT CONCAT("尹正杰","到此一游","2019");
+-------------------------------------------+
| CONCAT("尹正杰","到此一游","2019") |
+-------------------------------------------+
| 尹正杰到此一游2019 |
+-------------------------------------------+
1 row in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SELECT CONCAT("尹正杰","到此一游","2019");
9>.将大写字母小写
MariaDB [yinzhengjie]> SELECT LOWER('YINZHENGJIE');
+----------------------+
| LOWER('YINZHENGJIE') |
+----------------------+
| yinzhengjie |
+----------------------+
1 row in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SELECT LOWER('YINZHENGJIE');
10>.将小写字母大写
MariaDB [yinzhengjie]> SELECT UPPER('yinzhengjie');
+----------------------+
| UPPER('yinzhengjie') |
+----------------------+
| YINZHENGJIE |
+----------------------+
1 row in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SELECT UPPER('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]> HELP CREATE FUNCTION UDF
MariaDB [yinzhengjie]> CREATE FUNCTION simpleFun() RETURNS VARCHAR(7) RETURN "尹正杰到此一游"; #无参UDF案例
Query OK, 0 rows affected (0.01 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> CREATE FUNCTION simpleFun() RETURNS VARCHAR(7) RETURN "尹正杰到此一游"; #无参UDF案例
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]>
有参UDF案例一(根据用户输入的id删除对应的行)
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]>
有参UDF案例二(根据用户输入两个数字并将这两个数字相加返回给用户)
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]>
MariaDB [yinzhengjie]> SHOW FUNCTION STATUS\G
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 simpleFun\G
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 deleteById\G
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]>
MariaDB [yinzhengjie]> SHOW CREATE FUNCTION addTwoNumber\G
4>.调用自定义函数语法
MariaDB [yinzhengjie]> SELECT simpleFun(); #调用无参UDF
+-----------------------+
| simpleFun() |
+-----------------------+
| 尹正杰到此一游 |
+-----------------------+
1 row in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SELECT simpleFun(); #调用无参UDF
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 [mysql]> SELECT yinzhengjie.simpleFun(); #当函数是跨数据库调用时,需要显式指定数据库名称哟~(否则会抛出异常)
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]>
MariaDB [yinzhengjie]> SELECT deleteById(15); #调用有参UDF
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]>
MariaDB [yinzhengjie]> DROP FUNCTION simpleFun;
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]>
MariaDB [yinzhengjie]> SELECT * FROM mysql.proc\G
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]>
MariaDB [yinzhengjie]> SELECT students_numbers();
MySQL/MariaDB数据库的函数的更多相关文章
- MySQL/MariaDB数据库的mysqldump工具备份还原实战
MySQL/MariaDB数据库的mysqldump工具备份还原实战 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.mysqldump概述 1>.逻辑备份工具 mysq ...
- MySQL/MariaDB数据库的冷备份和还原
MySQL/MariaDB数据库的冷备份和还原 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL/MariaDB数据库的备份和还原概述 1>.为什么要备份 为了 ...
- MySQL/MariaDB数据库的索引工作原理和优化
MySQL/MariaDB数据库的索引工作原理和优化 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 实际工作中索引这个技术是影响服务器性能一个非常重要的指标,因此我们得花时间去了 ...
- MySQL/MariaDB数据库的查询缓存优化
MySQL/MariaDB数据库的查询缓存优化 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL架构 Connectors(MySQL对外提供的交互接口,API): ...
- MySQL/MariaDB数据库的用户和权限管理
MySQL/MariaDB数据库的用户和权限管理 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.元数据数据库(mysql) 系统授权表(均在mysql数据库中): db hos ...
- MySQL/MariaDB数据库的存储过程
MySQL/MariaDB数据库的存储过程 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.存储过程概述 1>.存储过程优势 存储过程把经常使用的SQL语句或业务逻辑封装起 ...
- MySQL/MariaDB数据库的性能测试
MySQL/MariaDB数据库的性能测试 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.数据库服务衡量指标 qps: query per second(每秒支持多少查询 ...
- MySQL/MariaDB数据库的Galera高可用性集群实战
MySQL/MariaDB数据库的Galera高可用性集群实战 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Galera Cluster概述 1>.什么是Gale ...
- MySQL/MariaDB数据库的MHA实现高可用实战
MySQL/MariaDB数据库的MHA实现高可用实战 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL高可用常见的解决方案 1>.Multi-Master ...
随机推荐
- TransactionScope处理分布式事物时提示"事务已被隐式或显式提交,或已终止"
在连接字符串中加入"Enlist=false",问题就这样解决了. ConnectionString = "Data Source=.;Initial Catalog=c ...
- 快速修改Windows系统密码命令
因现场需要,要对30多台虚拟机进行密码修改.正常修改方式为进入控制面板--用户账户--修改密码,输入原始密码.2遍新密码(一遍用于密码确认)完成密码修改. 这种方式操作较为繁琐,我们可以直接通过命令的 ...
- FLINK-11738
caused by: akka.pattern.asktimeoutexception: ask timed out on flink Caused by: akka.pattern.AskTimeo ...
- 【Spring Cloud学习之三】负载均衡
环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2 Spring Cloud 1.2 主流的负载均衡技术有nginx.LVS.HAproxy.F5,Spring Clou ...
- 解析Java反射java.lang.IllegalArgumentException: wrong number of arguments
项目中遇到的问题 import org.springframework.util.ReflectionUtils; import java.lang.reflect.Method; public cl ...
- Codeforces Round #249 (Div. 2) C. Cardiogram
C. Cardiogram time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- (八)pdf的构成之文件体(page属性)
资源(Resources) 类型:字典 属性: Font :一个字典,内部是该资源包含的各个字体的名称(字体名称F开头) ProcSet :该资源中使用图画还是文字,可选 /PDF /Text /Im ...
- [UOJ #393]【NOI2018】归程
题目大意:有一张$n$个点$m$条边的图,每个边有两个属性$a_i,b_i$.有$Q$个询问,每个询问给出$v,p$,表示所有边中$b_i\leqslant p$的边会被标记,在点$v$,可以通过不被 ...
- 24H玩转 Grafana 被工程师称相当专业,如何做到?
国庆假期发生了两件小事,其一是我默默度过 35 周岁生日,其二是玩了下grafana `并在节后第一天被工程师 M 称赞:相当专业. 1.我为什么要玩 grafana 呢? 数月前我提交了一份数据后台 ...
- Kafka启用SASL_PLAINTEXT动态配置JAAS文件的几种方式
Kafka是广泛使用消息服务,很多情况下关于认证部分我都是默认的配置,也就是不需要用户名/密码,也不配置证书.在内网或者在项目组内部可以,但是设计的跨部门时一般处于安全考虑都需要加上认证,防止kafk ...