Detailed explanation of the definition and usage of MySQL stored functions (custom functions)

Detailed explanation of the definition and usage of MySQL stored functions (custom functions)

Stored Functions

What is a stored function: It encapsulates a piece of SQL code, completes a specific function, and returns the result.

The syntax of a stored function is:

create function function([function parameters[,….]]) Returns return type Begin

    If(

      Return (returned data)

    Else 

      Return (returned data)

    end if;

  end;

For example: create function count_news(hits int) returns int

Unlike stored procedure return parameters, stored functions do not directly declare which variable is the return parameter when they are defined. Instead, they only use returns to declare the data type of the return parameter. The return parameter is represented in the function body by using return to return the data variable to be returned. It is important to note that:
Stored functions only support input parameters, and there is no IN or INOUT before the input parameters.

Limitations in stored functions

Flow-of-control statements (IF, CASE, WHILE, LOOP, WHILE, REPEAT, LEAVE, ITERATE) are also legal.

Variable declarations (DECLARE) and assignments (SET) are legal.

Allows conditional statements.

Exception handling statements are also allowed.

But remember here that functions have restrictions: you cannot access tables in a function. Therefore, it is illegal to use the following statement in a function.

ALTER 'CACHE INDEX' CALL COMMIT CREATE DELETE
DROP 'FLUSH PRIVILEGES' GRANT INSERT KILL
LOCK OPTIMIZE REPAIR REPLACE REVOKE
ROLLBACK SAVEPOINT 'SELECT FROM table'
'SET system variable' 'SET TRANSACTION'
SHOW 'START TRANSACTION' TRUNCATE UPDATE

The difference between stored functions and stored procedures

1. A stored function has one and only one return value, while a stored procedure cannot have a return value.

2. Functions can only have input parameters and cannot have "in", while stored procedures can have multiple "in", "out", and "inout" parameters.

3. The statements in stored procedures are more powerful. Stored procedures can implement very complex business logic, while functions have many limitations. For example, you cannot use insert, update, delete, create and other statements in functions. Stored functions only complete query work, can accept input parameters and return a result, that is, the functions implemented by functions are more targeted.

4. Stored procedures can call stored functions. But functions cannot call stored procedures.

5. Stored procedures are generally executed as an independent part (call). Functions can be called as part of a query statement.

Example 1:

Id

Name

QQ

phone

1

Qin Yun

10102800

13500000

2

On the Road

10378

13600000

3

LEO

10000

13900000

Id

Name

Computer time

administrator

1

Qin Yun

2004-1-1

Li Dawei

2

Qin Yun

2005-1-1

Ma Huateng

3

On the Road

2005-1-1

Ma Huateng

4

Qin Yun

2005-1-1

Li Dawei

5

On the Road

2005-1-1

Li Dawei

Achievement purpose:

Get the list of all personnel from Table 1, and the number of times they use the computer and the administrator from Table 2.

List of users Number of users Administrator Qin Yun 3 Li Dawei, Ma Huateng, Li Dawei on the way 2 Ma Huateng, Li Dawei
LEO 0

You may also be interested in:
  • MySQL custom function CREATE FUNCTION example
  • In-depth explanation of creating custom functions and stored procedures in MySQL
  • MySQL Chinese character conversion pinyin custom function and usage examples (first letter of the first word)
  • A brief discussion on mysql custom functions
  • How to use custom functions to extract numbers from strings in MySQL
  • Problems with creating custom functions in mysql
  • Simple usage examples of MySQL custom functions
  • MYSQL custom function to determine whether it is a positive integer example code
  • Detailed explanation of MySQL custom functions and stored procedures
  • MySQL uses custom functions to recursively query parent ID or child ID
  • Analysis of the principle and usage of MySQL custom functions

<<:  Application and implementation of data cache mechanism for small programs

>>:  How to configure NAS on Windows Server 2019

Recommend

VMware Tools installation and configuration tutorial for Ubuntu 18.04

This article records the installation and configu...

The most complete 50 Mysql database query exercises

This database query statement is one of 50 databa...

Two methods to implement MySQL group counting and range aggregation

The first one: normal operation SELECT SUM(ddd) A...

How to develop uniapp using vscode

Because I have always used vscode to develop fron...

CSS new feature contain controls page redrawing and rearrangement issues

Before introducing the new CSS property contain, ...

Things You Don’t Know About the CSS ::before and ::after Pseudo-Elements

CSS has two pseudo-classes that are not commonly ...

How to use ECharts in WeChat Mini Programs using uniapp

Today, we use uniapp to integrate Echarts to disp...

Mysql master-slave synchronization configuration scheme under Centos7 system

Preface Recently, when working on a high-availabi...

JavaScript to achieve the effect of tab bar switching

Tab bar: Click different tabs to display differen...

Summary of Vue's cross-domain problem handling and solutions

When you send a network request, the following sa...

Mysql | Detailed explanation of fuzzy query using wildcards (like,%,_)

Wildcard categories: %Percent wildcard: indicates...

Detailed explanation of Vuex overall case

Table of contents 1. Introduction 2. Advantages 3...

CSS uses radial-gradient to implement coupon styles

This article will introduce how to use radial-gra...

Mysql sql slow query monitoring script code example

1. Modify my.cnf #The overall effect is that both...