当前位置:网站首页>MySQL learning notes (2) -- stored procedures and stored functions
MySQL learning notes (2) -- stored procedures and stored functions
2022-07-27 19:09:00 【Xiao Li, who loves traveling】
MySQL Learning notes (2)—— Stored procedures and stored functions
List of articles
- MySQL Learning notes (2)—— Stored procedures and stored functions
- Conclusion
One 、 stored procedure
1. Concept : Precompiled sql Collection of statements , Understand batch statements
2. benefits ;
1. Improved code reusability
2. Simplified operation
3. Reduce the number of compilations and the number of database servers , Improved efficiency
3. grammar :
create procedure Stored procedure name ( Parameter mode Parameter name Parameter type )
begin
Stored procedure body
end
4. parameter list :
The parameter list consists of three parts :
Parameter mode Parameter name Parameter type
give an example
in stuname varchar(32)
5. Parameter mode :
in This parameter can be used as input , The caller needs to pass in the value
out This parameter can be used as output , It can be used as a return value
inout Change parameters and can be used as input 、 It can also be used as output
6. matters needing attention :
1. If the stored procedure has only one sentence , that begin end It can be omitted
2. Each of the stored procedure bodies sql The result of the statement must be added with a semicolon ’;’
3. The result of the stored procedure can be used delemiter To reset
grammar :
delimiter End mark
example :delimiter //
7. Call syntax
call Stored procedure name ( Argument list );
8. actual combat
(1) Parameter list is empty
practice :
1. Bulk insert data
DELIMITER //
CREATE PROCEDURE pro1()
BEGIN
INSERT INTO stu(id,NAME) VALUES(NULL,'jack');
INSERT INTO stu(id,NAME) VALUES(NULL,'tom');
INSERT INTO stu(id,NAME) VALUES(NULL,'mike');
INSERT INTO stu(id,NAME) VALUES(NULL,'rose');
END;
//
-- call
CALL pro1();
SELECT * FROM stu;

(2) The parameter mode is in type :
-- With parameters
DELIMITER //
CREATE PROCEDURE pro2(IN t_name VARCHAR(32))
BEGIN
SELECT * FROM stu WHERE NAME = t_name;
END;
//
-- call
CALL pro2('jack');
SELECT * FROM stu;

DELIMITER //
CREATE PROCEDURE pro3(IN id INT,IN NAME VARCHAR(32))
BEGIN
DECLARE result VARCHAR(32) DEFAULT '';
SELECT COUNT(*)
INTO result
FROM stu
WHERE stu.`id`=id AND stu.`name`=NAME;
SELECT result;
END;
//
-- call
CALL pro3(15,'jack');

(3) The parameter list is out type
-- out type
DELIMITER //
CREATE PROCEDURE pro4(IN id INT,OUT NAME VARCHAR(32))
BEGIN
SELECT stu.`name`
INTO NAME
FROM stu
WHERE stu.`id`=id;
END;
//
-- call
-- Define a variable to receive the return value
CALL pro4(1,@name);
SELECT @name;

-- There are two out Type return value
DELIMITER //
CREATE PROCEDURE pro5(IN id INT,OUT NAME VARCHAR(32),OUT age INT)
BEGIN
SELECT stu.`name`,stu.`age`
INTO NAME,age
FROM stu
WHERE stu.`id`=id;
END;
//
-- call
CALL pro5(1,@name2,@age);
SELECT @name2;
SELECT @age;
SELECT @name2,@age;

(4) Parameter type is inout type :
-- Create a inout Stored procedure of mode parameters
-- Pass in two values a and b, Final a and b All double and return
DELIMITER //
CREATE PROCEDURE pro6(INOUT a INT,INOUT b INT)
BEGIN
SET a=a*2;
SET b=b*2;
END;
-- call
SET @m=10;
SET @n=20;
CALL pro6(@m,@n);
SELECT @m,@n;
//

Other operations of stored procedures
Delete
Format :DROP PROCEDURE Stored procedure name
Be careful : Only one stored procedure can be deleted at a time
DELIMITER //
CREATE PROCEDURE pro7()
BEGIN
SELECT * FROM stu;
END;
//
DROP PROCEDURE pro7;
Display stored procedure
Format :SHOW CREATE PROCEDURE Stored procedure name
SHOW CREATE PROCEDURE pro6;

Be careful :
The following operations are not supported
Don't allow desc
DESC PROCEDURE pro7;
Modify stored procedure ( I won't support it )
Two 、 Storage function
1. The difference between stored procedure and stored function :
Stored procedures can have 0 Return values , There can also be multiple return values , Suitable for bulk insertion , Batch update
function : Yes and no 1 Return values , It is suitable to return a result after processing data
2. grammar
2.1 Create function syntax :
CREATE FUNCTION Function name ( parameter list ) RETURNS Return type
BEGIN
The body of the function
END
Be careful :
The parameter list contains two parts : Parameter name Parameter type
The body of the function :
Be sure to have return sentence , Otherwise, an error will be reported , It is suggested to put it at the end of the function body
When the function body has only one sentence , be begin end It can be omitted
2.2 Call syntax :
SELECT Function name ( parameter list )
Execute statement , Get the return value
2.3 View functions
grammar : SHOW CREATE FUNCTION The name of the function
SHOW CREATE FUNCTION fun2;
test
2.4 Delete function
grammar :DROP FUNCTION Function name
DROP FUNCTION fun2;
2.5 matters needing attention :
Functions are generally not modified
3. actual combat
3.1 No parameters return
Return the number of students
DELIMITER //
CREATE FUNCTION fun1() RETURNS INT
BEGIN
DECLARE result INT DEFAULT 0;-- Variable declarations
SELECT COUNT(*) INTO result FROM stu;
RETURN result;
END;
//
SELECT fun1();
SELECT * FROM stu;

3.2 There are parameters and return values
-- Return students according to their names id
DELIMITER //
CREATE FUNCTION fun2(sname VARCHAR(32)) RETURNS INT
BEGIN
SET @a=0; -- Define a user variable
SELECT id
INTO @a
FROM stu
WHERE NAME = sname;
RETURN @a;
END;
//
SELECT fun2('jack');
SELECT * FROM stu;

3.3 Small exercise
-- Create a function : The implementation passes in two float, Return to the sum of the two
DELIMITER //
CREATE FUNCTION num_sum(a FLOAT,b FLOAT) RETURNS FLOAT
BEGIN
DECLARE result FLOAT DEFAULT 0;
SET result = a + b;
RETURN result;
END;
//
SELECT num_sum(1,2);

Conclusion
Level co., LTD. , It's hard to avoid mistakes , I hope you guys can knock !
边栏推荐
- Code interview of Amazon
- Extension of regular expression
- MongoDB学习笔记(1)——安装MongoDB及其相关配置
- webservice的疑问
- "Testing novice encyclopedia" 5-minute quick start pytest automated testing framework
- 用函数在Excel中从文本字符串提取数字
- Automatic testing of Web UI: Selenium syntax explanation is the most complete in the history
- Kinect for Unity3D——BackgroundRemovalDemo学习
- Latex use - subfigure vertical graphics
- Study notes of Microcomputer Principles - general integer instructions and Applications
猜你喜欢

IDEA成功连接Database但不显示表怎么办

Led learning eye protection table lamp touch chip-dlt8t10s-jericho

Kinect2 for Unity3D——AvatarDemo学习

正则表达式的扩展

Product recommendation and classified product recommendation

express

Nacos的基本使用(1)——入门

Sentinel1.8.4 persistent Nacos configuration

MySQL 04 advanced query (II)

NPM basic use
随机推荐
Extension of regular expression
An experience
Performance analysis of continuous time system (1) - performance index and first and second order analysis of control system
Unity learning notes - six common functions of object movement
The understanding of string in C.
NPM basic use
Latex使用-控制表格或者图形的显示位置
Resource for NS2 beginner
PHP字符串操作
normal distribution, lognormal distribution,正态随机数的生成
CMD command
IDEA连接数据库时区问题,报红Server returns invalid timezone. Need to set ‘serverTimezone‘ property.
微机原理学习笔记-常见寻址方式
MicaZ+Tinyos学习笔记(1)
The great idea of NS2
PHP string operation
MySQL学习笔记(2)——存储过程与存储函数
npm 基本使用
NPM, cnpm Taobao image
C interface knowledge collection suggestions collection