登录
首页 >  数据库 >  MySQL

什么是 MySQL 存储函数以及如何创建它们?

来源:tutorialspoint

时间:2023-08-23 22:35:33 370浏览 收藏

“纵有疾风来,人生不言弃”,这句话送给正在学习数据库的朋友们,也希望在阅读本文《什么是 MySQL 存储函数以及如何创建它们?》后,能够真的帮助到大家。我也会在后续的文章中,陆续更新数据库相关的技术文章,有好的建议欢迎大家在评论留言,非常感谢!

MySQL stored function is basically a special kind of stored program that returns a single value. We can use stored functions in MySQL to encapsulate mainly simple formulas or business rules that are reusable among SQL statements or stored programs. Other than that stored functions are used in SQL statements whenever an expression is used.

This feature of stored functions is different from stored procedures. Actually, a stored function parameter is equivalent of the IN parameter of the stored procedure as the functions use RETURN keyword to determine what is passed back. Its syntax can be as follows −

Syntax

CREATE
   [DEFINER = { user | CURRENT_USER }]
   FUNCTION sp_name ([func_parameter[,...]])
   RETURNS type
   [characteristic ...] routine_body

func_parameter:
   param_name type

type:
   Any valid MySQL data type

characteristic:
   COMMENT 'string'
 | LANGUAGE SQL
 | [NOT] DETERMINISTIC
 | { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
 | SQL SECURITY { DEFINER | INVOKER }

routine_body:
   Valid SQL routine statement

函数只有输入参数和返回值,因此在函数定义中必须有一个RETURNS子句来指示返回值的数据类型。此外,在函数体内必须至少有一个RETURN语句来将值返回给调用者。

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于数据库的相关知识,也可关注golang学习网公众号。

声明:本文转载于:tutorialspoint 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>