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 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"表中的学生平均年龄
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>.显示当前系统时间
MariaDB [yinzhengjie]> SELECT NOW(); +---------------------+ | NOW() | +---------------------+ | 2019-10-28 08:15:55 | +---------------------+ 1 row in set (0.00 sec) MariaDB [yinzhengjie]>
4>.查看一个负数的绝对值
MariaDB [yinzhengjie]> SELECT ABS(-1314); +------------+ | ABS(-1314) | +------------+ | 1314 | +------------+ 1 row in set (0.00 sec) MariaDB [yinzhengjie]> MariaDB [yinzhengjie]>
5>.查看数据库版本
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]>
本文来自博客园,作者:尹正杰,转载请注明原文链接:https://www.cnblogs.com/yinzhengjie/p/11749867.html,个人微信: "JasonYin2020"(添加时请备注来源及意图备注,有偿付费)
当你的才华还撑不起你的野心的时候,你就应该静下心来学习。当你的能力还驾驭不了你的目标的时候,你就应该沉下心来历练。问问自己,想要怎样的人生。