Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

MYSQL-- custom function

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Custom function

User-defined functions are a way to extend MYSQL, using the same as built-in functions

The 1 function can return any type of value, and it can also receive these types of parameters

2 the function body is composed of legal SQL statements.

3 the function body can be a simple SELECT or INSERT statement

4 if the function body is a compound structure, use the begin.. end statement.

5 composite structures can contain declarations, loops, and copy structures

6. Create a custom function without parameters

The first step is CREATE FUNCTION function name () FETURNS string length such as VARCHAR (30)

The second step is RETURN type (such as DATE) _ FORMAT (NOW ()), and the function content is like'% Y year% m month% d day% H point:% I minute:% s second')

7 create a custom function with parameters

The first step is CREATE FUNCTION function name such as F1 (parameter type and number of parameters such as num1 character length such as SMALLINT character type such as UNSIGNED, parameter type and number of parameters such as num2 character length such as SMALLINT character type such as UNSIGNED)

In the second step, RETUPNS returns a value type such as FLOAT (10cine 2) UNSIGNED.

Step 3: RETUPNS returns values such as (num1+num2) / 2

8 call function

SELECT function name (parameter name such as 101015)

Modify the default delimiter of the current MYSQL

New delimiters for DELIMITER

9 create a custom function with the body of the compound structure function (change the default delimiter of MYSQL to / / before creation, and modify it back after creation)

First step CREATE FUNCTION adduser (username such as username character length such as VARCHAR (20)

Step 2 RETURNS data types such as INT character types such as UNSIGNED

Step 3 BEGIN

Step 4 INSERT table name (user name) VALUES (user name)

Step 5 RETURNLAST_INSERT_ID ()

Step 6 END

Step 7 / /

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report