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

CSS multi-level menu implementation code

This is a pretty cool feature that makes web page...

How to use Spark and Scala to analyze Apache access logs

Install First you need to install Java and Scala,...

Summary of Common Letters in Unicode

Most of the earliest computers could only use ASC...

Understanding the MySQL query optimization process

Table of contents Parsers and preprocessors Query...

Using CSS to implement loading animation of Android system

There are two common loading icons on the web, on...

How to modify the user and group of a file in Linux

In Linux, when a file is created, the owner of th...

Several ways to remove the dotted box that appears when clicking a link

Here are a few ways to remove it: Add the link dir...

The perfect solution for highlighting keywords in HTML

I recently encountered a feature while working on...

How to configure Openbox for Linux desktop (recommended)

This article is part of a special series on the 2...

Centos7.3 automatically starts or executes specified commands when booting

In centos7, the permissions of the /etc/rc.d/rc.l...

Using front-end HTML+CSS+JS to develop a simple TODOLIST function (notepad)

Table of contents 1. Brief Introduction 2. Run sc...

How to set npm to load packages from multiple package sources at the same time

Table of contents 1. Build local storage 2. Creat...

A brief discussion of several browser compatibility issues encountered

background Solving browser compatibility issues i...

Detailed explanation of the JavaScript timer principle

Table of contents 1. setTimeout() timer 2. Stop t...