Analysis of the principle and usage of MySQL custom functions

Analysis of the principle and usage of MySQL custom functions

This article uses examples to illustrate the principles and usage of MySQL custom functions. Share with you for your reference, the details are as follows:

In this article:

  • What is a function
  • Function creation
  • Function call
  • Function View
  • Function modification
  • Deleting a function

Release date: 2018-04-18


What is a function:

  • A function stores a series of SQL statements, and calling a function executes these statements at once. So functions can reduce statement duplication. [But please note that the function focuses on the return value, not the execution process, so some statements cannot be executed. So the function is not just a collection of SQL statements. 】
  • MySQL functions have their own custom functions (defined functions). For more information, please refer to my other blog post: Common MySQL functions
  • Here we mainly introduce how to customize functions.

Replenish:

  • The difference between a function and a stored procedure: A function can only return a value and is not allowed to return a result set. Functions emphasize return values, so functions are not allowed to return multiple values, even in query statements.
    -- Unacceptable code: Not allowed to return a result set from a function
    create function myf()returns int 
    begin
    select * from student;
    return 100;
    end;

Function creation:

  • grammar:
    create function function name([parameter list]) returns data type begin
     sql statement;
     return value;
    end;
    • The format of the parameter list is: variable name data type
  • Example:
    -- The simplest function with only one SQL statement: create function myselect2() returns int return 666;
    select myselect2(); -- call function --
    create function myselect3() returns int
    begin 
      declare c int;
      select id from class where cname="python" into c;
      return c;
    end;
    select myselect3();
    -- Function with parameter passing create function myselect5(name varchar(15)) returns int
    begin 
      declare c int;
      select id from class where cname=name into c;
      return c;
    end;
    select myselect5("python");

Replenish:

  • There can also be some special options, which are written after return and before begin, such as:
    • comment: a description of the function
    • There are some other options such as sql security. If you are interested, you can search on Baidu. I won’t explain it here, I’ll just mention this knowledge point.

Function call:

  • You can call it directly using the function name (). [ Although it is said, it returns a result. If select is not used in sql, no result can be displayed (so a simple call will report an error).]
  • If you want to pass in parameters, you can use the function name (parameters)
  • Calling method [The functions called below are all created above. 】:
    -- Call select myselect3() without parameters;
    -- Call select myselect5("python");
    select * from class where id=myselect5("python");

Function view:

  • View the function creation statement: show create function function name;
  • View all functions: show function status [like 'pattern'];

Function modification:

  • The function can only modify some options such as comment, but cannot modify the internal SQL statement and parameter list.
  • alter function function name option;

Deletion of functions:

  • drop function function name;

Readers who are interested in more MySQL-related content can check out the following topics: "Summary of MySQL Common Functions", "Summary of MySQL Log Operation Skills", "Summary of MySQL Transaction Operation Skills", "Summary of MySQL Stored Procedure Skills" and "Summary of MySQL Database Lock-Related Skills".

I hope this article will be helpful to everyone's MySQL database design.

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
  • Detailed explanation of the definition and usage of MySQL stored functions (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

<<:  Nginx Linux installation and deployment detailed tutorial

>>:  A brief discussion on this.$store.state.xx.xx in Vue

Recommend

JS removeAttribute() method to delete an attribute of an element

In JavaScript, use the removeAttribute() method o...

MySQL max_allowed_packet setting

max_allowed_packet is a parameter in MySQL that i...

MySQL installation diagram summary

MySQL 5.5 installation and configuration method g...

Share 13 excellent web wireframe design and production tools

When you start working on a project, it’s importa...

How to add Nginx proxy configuration to allow only internal IP access

location / { index index.jsp; proxy_next_upstream...

How to change the root password in MySQL 5.7

Starting from MySQL 5.7, many security updates ha...

An example of using Lvs+Nginx cluster to build a high-concurrency architecture

Table of contents 1. Lvs Introduction 2. Lvs load...

A brief analysis of the basic concepts of HTML web pages

What is a web page? The page displayed after the ...

Detailed installation process of MySQL 8.0 Windows zip package version

The installation process of MySQL 8.0 Windows zip...

A brief analysis of crontab task scheduling in Linux

1. Create a scheduling task instruction crontab -...

Detailed explanation of MySQL data rows and row overflow mechanism

1. What are the formats of lines? You can see you...

Solution to the img tag problem below IE10

Find the problem I wrote a simple demo before, bu...

Complete steps to use samba to share folders in CentOS 7

Preface Samba is a free software that implements ...

How to set up virtual directories and configure virtual paths in Tomcat 7.0

Tomcat7.0 sets virtual directory (1) Currently, o...