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 ...
随机推荐
- Python Linux系统管理与自动化运维
Python Linux系统管理与自动化运维 前言 第1章Python语言与Linux系统管理1 1.1Python语言有多流行1 1.2Python语言为什么流行3 1.3Python语言有什么缺点 ...
- Python - importlib 模块
importlib 模块可以根据字符串来导入相应的模块 目录结构: 在根目录下创建 importlib_test.py 和 aaa/bbb.py bbb.py: class Person(object ...
- (转)搭建Elasticsearch和kibana环境
搭建Elasticsearch和kibana环境 作者:IT云清 原文:https://blog.csdn.net/weixin_39800144/article/details/81162002 1 ...
- [转]java 根据模板文件生成word文档
链接地址:https://blog.csdn.net/ai_0922/article/details/82773466
- mysql笔记7--一句查询语句的过程
1 sql语句示例 select *from A where id=1 2 mysql基本架构图 (1)Mysql分为Server层和引擎层两个部分 (2)Server层包括连接器,查询缓存,分析器, ...
- Ribbon和Nignx的区别
Ribbon属于客户端负载均衡:在调用接口的时候,会通过服务别名到eureka上获取服务的信息列表,缓存到jvm本地,在本地采用RPC远程调用技术去调用接口,实现负载均衡.可以设置调用的规则是请求总数 ...
- [转帖]NSA武器库知识整理
NSA武器库知识整理 https://www.cnblogs.com/FrostDeng/p/7120812.html 美国国家安全局(NSA)旗下的“方程式黑客组织”(shadow brokers) ...
- Shell编程学习(六)
Shell 脚本条件测试与比较 条件测试方法综述 在Bash的各种条件结构和控制结构中都要进行各种测试,然后根据测试结果执行不同的操作,有时也会与if等条件语句相结合,来完成测试判断,以减少程序运行的 ...
- .net Dapper 实践系列(3) ---数据显示(Layui+Ajax+Dapper+MySQL)
目录 写在前面 产生问题 解决方案 写在前面 上一小节,我们使用Dapper 里事务实现了一对多关系的添加.这一小节,主要记录如何使用Dapper 实现多表的查询显示. 产生问题 在mvc控制器中查询 ...
- 让js中的函数只有一次有效调用
设置隐藏域~ <input type="hidden" value="1" id="flag" /> 其它三种方法