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 ...
随机推荐
- StandardServer.await: Invalid command '' received
tomcat服务运行时 后台提输出警告:StandardServer.await: Invalid command '' received 这个警告是 多个tomcat启动时会出现端口占用的情况, 将 ...
- Spring BeanFactory 初始化 和 Bean 生命周期
(version:spring-context-4.3.15.RELEASE) AbstractApplicationContext#refresh() public void refresh() t ...
- [Linux]Linux下samba创建共享文件
1. 安装samba服务 yum install -y samba 2. 创建需要共享的目录 在目录/home/xxxx/share xxx为用户名 mkdir share 修改该目录权限(上层文件夹 ...
- 自动化办公:xlsxwriter操作Excel
转载原链接:https://www.jianshu.com/p/d685cfaaeef7 1.安装 pip install xlsxwriter 2.操作一个简单的Excel文档 # 引入依赖模块 i ...
- RocketMQ 4.5.1 双主双从异步复制环境搭建
基础环境 操作系统:CentOS7 实例:192.168.63.129,192.168.63.130,192.168.63.131,192.168.63.132 Java:jdk-8u191-linu ...
- Selenium+Java(七)Selenium对话框的处理
HTML代码如图所示: 一.alert String url = "file:///C:/Users/ex_yuhao/Desktop/index.html"; //引用IE浏览器 ...
- 精通Spring Boot
原 精通Spring Boot—— 第二十一篇:Spring Social OAuth 登录简介 1.什么是OAuth OAuth官网介绍是这样的: An open protocol to allow ...
- Linux RedHat7.0_64位系统中安装Oracle_11g_R2
步骤一: 当然是安装rhel7操作系统啦(废话),建议在安装过程中系统软件类型选择最后一项[Server with GUI].其他的默认一般即可. 步骤二:在初装完成的系统中无法像Windows那样直 ...
- 静态工具类注入service的方法
http://blog.sina.com.cn/s/blog_6e2d53050102wl3x.html
- python学习之利用socketserver的文件传输
使用socketserver进行多用户的文件传输 服务端 class FtpServer(socketserver.BaseRequestHandler): # 继承socketserver.Base ...