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

Detailed explanation of four solutions to floating problems in CSS layout

1. Cause: The effect after the subbox is set to f...

Common commands for deploying influxdb and mongo using docker

Deploy database based on docker sudo docker pull ...

Vue uses three methods to refresh the page

When we are writing projects, we often encounter ...

Docker exec executes multiple commands

The docker exec command can execute commands in a...

30 excellent examples of color matching in web design

Today, this article has collected 30 excellent cas...

CSS3 custom scroll bar style::webkit-scrollbar sample code detailed explanation

The default scroll bar style in Windows is ugly, ...

Python 3.7 installation tutorial for MacBook

The detailed process of installing python3.7.0 on...

How to deploy k8s in docker

K8s k8s is a cluster. There are multiple Namespac...

Detailed explanation of JQuery selector

Table of contents Basic selectors: Level selector...

Detailed explanation of long transaction examples in MySQL

Preface: The "Getting Started with MySQL&quo...

Detailed explanation of the use of $emit in Vue.js

1. Parent components can use props to pass data t...

Example code for converting Mysql query result set into JSON data

Mysql converts query result set into JSON data Pr...