Home  >  Article  >  Database  >  Analyze MySQL stored procedures and common function codes

Analyze MySQL stored procedures and common function codes

coldplay.xixi
coldplay.xixiforward
2020-08-20 16:04:241893browse

Analyze MySQL stored procedures and common function codes

The concept of mysql stored procedure:

It is stored in the database and can perform specific work (query and update) A set of program segments of SQL code.

The concept of mysql function:

A function is an SQL statement that completes a specific function. Functions are divided into built-in functions and user-defined functions (user-defined function UDF)

The difference between MySQL stored procedures and functions

  • Stored procedures can have multiple in, out, and inout parameters, while functions only have input parameter types and cannot take in .
  • The functions implemented by stored procedures are more complex; while the single functionality (targetedness) of functions is stronger.
  • Stored procedures can return multiple values; stored functions can only have one return value.
  • Stored procedures are generally executed independently; stored functions can appear as components of other SQL statements.
  • Stored procedures can call stored functions. Functions cannot call stored procedures.

Stored procedures are a set of sql statements to complete specific functions. They are compiled, created and saved in the database. The idea is code encapsulation and reuse at the database SQL language level.

Note: in refers to input parameters, out refers to output parameters

Create a custom function

Syntax format : create function name (parameter type, parameter type...) returns type return expression value;

Note: 1. There can be no or multiple parameters.

 2. There must be a return value, and there is only one.

 3. If there is a SQL statement, it should be placed in the middle of begin...end.

4. If you don’t add determministic, an error will be reported (I don’t know how to solve it)

begin...end compound statement

usually appears in Stored procedures, functions and triggers can contain one or more statements, each statement separated by;.

Related learning recommendations: mysql tutorial

The above is the detailed content of Analyze MySQL stored procedures and common function codes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete