006,2存储函数 MySQL 存储函数也是过程式对象之一,创建存储函数与存储过程大致相同。 1创建存储函数 CREATE FUNCTION 语法格式 CREATE FUNCT I0N存储过程名([参数.]) RETURNs type [特征..]存储函数体 说明 存储函数不能拥有与存储过程相同的名字。 存储函数的参数只有名称和类型,不能指定N、0UT和|NoUT。 RETURNs type子句声明函数返回值的数据类型 存储函数体:存储函数体中必须包含一个 RETURN value语句。 MySQL数据库应用》http:/mysql.xg.hactcm.edu.cn 第11页
《MySQL数据库应用》 http://mysql.xg.hactcm.edu.cn/ 第 11 页 6.2 存储函数 存储函数也是过程式对象乊一,创建存储函数与存储过程大致相同。 1.创建存储函数CREATE FUNCTION 语法格式: CREATE FUNCTION 存储过程名 ([参数 ... ]) RETURNS type [特征 ...] 存储函数体 说明: - 存储函数不能拥有与存储过程相同的名字。 - 存储函数的参数只有名称和类型,不能指定IN、OUT和INOUT。 - RETURNS type子句声明函数返回值的数据类型。 - 存储函数体:存储函数体中必须包含一个RETURN value语句
MySQL 0621创建存储函数 【例6.15】刨建一个存储函数,它返回xs表中学生的数目作为结果。 del imiter $$ create function num of Xs() returns Integer beg in return(select count(*) from xs) endS del imiter 说明: RETURN子句中包含 SELECT语句时,SELE0T语句的返回结果只能 是一行且只能有一列值。 MySQL数据库应用》ht: mysql xg. hactcm edu.cn 第12页
《MySQL数据库应用》 http://mysql.xg.hactcm.edu.cn/ 第 12 页 6.2.1 创建存储函数 【例6.15】创建一个存储函数,它返回xs表中学生的数目作为结果。 delimiter $$ create function num_of_xs( ) returns integer begin return (select count(*) from xs); end$$ delimiter ; 说明:RETURN子句中包含SELECT语句时,SELECT语句的返回结果只能 是一行且只能有一列值
MySQL 0621创建存储函数 【例6.16】刨建一个存储函数,返回某个学生的姓名。 de l imiter $s create funct ion name of stu (xh char(6)) returns char(8) beg in return( select姓名 from xs where学号=xh) en d$$ del imiter MySQL数据库应用》http:/mysql.xg.hactcm.edu.cn 第13页
《MySQL数据库应用》 http://mysql.xg.hactcm.edu.cn/ 第 13 页 6.2.1 创建存储函数 【例6.16】创建一个存储函数,返回某个学生的姓名。 delimiter $$ create function name_of_stu(xh char(6)) returns char(8) begin return (select 姓名 from xs where 学号=xh); end$$ delimiter ;
MySQL 0621创建存储函数 【例6.17】创建一个存储函数来删除xskc表中有,但xs表中不存在 的学号。如果调用存储函数时,参数中的学号在x表中不存在,那么将删 除xskc表中所有与该学号相关的行,之后返回1。如果学号在Xs中存在则 直接返回零。 del imiter Ss create function delete stu (xh char( 6)) returns boo lean beg in declare stu char(6) select姓名 into stu from xs where学号=xh; if stu is nulI then delete from xs kc where学号=xh; return true eI se return false: end if del imiter MySQL数据库应用》http:/mysql.xg.hactcm.edu.cn 第14页
《MySQL数据库应用》 http://mysql.xg.hactcm.edu.cn/ 第 14 页 6.2.1 创建存储函数 【例6.17】创建一个存储函数来删除xs_kc表中有,但xs表中丌存在 的学号。如果调用存储函数时,参数中的学号在xs表中丌存在,那么将删 除xs_kc表中所有不该学号相关的行,之后返回1。如果学号在xs中存在则 直接返回零。 delimiter $$ create function delete_stu(xh char(6)) returns boolean begin declare stu char(6); select 姓名 into stu from xs where 学号=xh; if stu is null then delete from xs_kc where 学号=xh; return true; else return false; end if; end$$ delimiter ;
66.22存储函数的调用、删除和修崁 1.存储函数的调用 存储函数创建完后,就如同系统提供的内置函数(如 VERS I0N) 所以调用存储函数的方法也差不多,都是使用SELE0T关键字。 语法格式: SELE0T存储函数名([参数[,]]) 例如,无参数调用存储函数。命令如下 num of xs) select num of xs o 执行结果如图所示 name of stu(g81186’) 例如,有参数调用存储函数。命令如下 李方方 select name of stu(081106) 执行结果如图所示。 (0. 03 sec) MySQL数据库应用》ht: mysql xg. hactcm edu.cn 第15页
《MySQL数据库应用》 http://mysql.xg.hactcm.edu.cn/ 第 15 页 6.2.2 存储函数的调用、删除和修改 1. 存储函数的调用 存储函数创建完后,就如同系统提供的内置函数(如VERSION()), 所以调用存储函数的方法也差不多,都是使用SELECT关键字。 语法格式: SELECT 存储函数名 ([参数[,...]]) 例如,无参数调用存储函数。命令如下: select num_of_xs(); 执行结果如图所示。 例如,有参数调用存储函数。命令如下: select name_of_stu('081106'); 执行结果如图所示