MySQL basics three: functions and stored procedures

6. Built-in functions and custom functions

6.1 Built-in functions

  1. character function
  2. Numeric operators and functions
  3. Comparison operators and functions
  4. datetime function
  5. information function
  6. aggregate function
  7. encryption function

6.2 Introduction to custom functions

  1. Custom function syntax format
CREATE FUNCTION 函数名(参数名 参数类型) # mysql中参数数量不能超过1024个(一般没问题)
RETURNS {
    
    返回值类型}
函数体 #(函数体中如果为复合结构要用BEGIN...END语句;复合结构可以包含声明、循环、流程控制等;)
  1. Create a custom function without parameters
# 创建无参函数
CREATE FUNCTION f1() 
RETURNS VARCHAR(40)
DETERMINISTIC	# 确定性声明(不然会报错:> 1418-This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in...)
RETURN DATE_FORMAT(NOW(),'%Y年%m月%d日 %H点:%i分:%s秒');  # RETURN表示有返回值

# 函数调用
SELECT f1();
  1. Create a custom function with parameters
# 创建带参的函数
CREATE FUNCTION f2(num1 INT,num2 INT) # 两个参数
RETURNS FLOAT(10,2)			# 返回值类型为float
DETERMINISTIC
RETURN (num1+num2)/3;		# 一个返回值

SELECT f2(3,2);
  1. Create a custom function with a composite structure function body.
    When more than one statement is executed, it needs to start with begin and end with end. Put multiple execution statements between begin and end. If the return value is a query statement, it must be enclosed in parentheses.
# 创建带参的函数
delimiter //
CREATE FUNCTION fAddUser2(username11 VARCHAR(20),unit11 VARCHAR(20)) 
RETURNS INT
DETERMINISTIC
BEGIN
INSERT test_user(username) VALUES (username11);
INSERT test_unit(unit) VALUES (unit11);
RETURN (SELECT COUNT(id) FROM test_unit);# 注意返回值这里要带括号
END
//
delimiter ;

SELECT fAddUser2('马超','蜀汉');

7. Stored procedures

7.1 Introduction to stored procedures

Advantages:
Fast execution speed during secondary and multiple calls (no need for syntax analysis and compilation),
reduced network traffic
, data tables and databases cannot be created,
parentheses for stored procedures without parameters can be omitted,
the names of parameters in stored procedures cannot be combined with data Table fields have the same names

7.2 Create a stored procedure without parameters

slightly

7.3 Create a stored procedure with IN type parameters

slightly

7.4 Create stored procedures with IN and OUT type parameters

# 修改结束符号
delimiter //
# 创建存储过程
CREATE PROCEDURE delUserAndReturnUserNums(IN p_id INT, OUT userNums INT)
BEGIN
DELETE FROM test_user WHERE id = p_id;
SELECT count(id) FROM test_user INTO userNums;# 通过into将数值放到变量中
END //
# 将结束符号修改回来
delimiter ;


# 存储过程的调用与返回值的查看
CALL delUserAndReturnUserNums(10,@nums);	# 用@来定义变量
SELECT @nums;

7.5 Create a stored procedure with multiple OUT type parameters

delimiter //	# 修改结束符号 ";" 为 "//"
CREATE PROCEDURE removeUserByAgeAndReturnInfos(	# 创建名为removeUserByAgeAndReturnInfos的存储过程
 IN p_age INT,	# IN输入变量,
 OUT deleteUsers INT,# OUT输出变量1
 OUT userRemain INT# OUT输出变量2
 )
BEGIN	# 过程体开始
DELETE FROM test_user WHERE age = p_age;# 按照年龄删除用户
SELECT ROW_COUNT() INTO deleteUsers;# 相当于php中mysql_affected_rows指的是被影响的行数,利用INTO放入OUT类型的变量1中
SELECT COUNT(id) FROM test_user INTO userRemain;# 将剩余用户的个数利用into放入OUT类型的变量2中
END 	# 过程体结束
//	# 整个存储过程结束
delimiter ;	# 将结束符号修改回来
CALL removeUserByAgeAndReturnInfos(20,@deleteUsers,@userRemain);	# 使用CALL调用存储过程,并用@符号定义变量
SELECT @deleteUsers,@userRemain;# 查看变量的值

Summary: The difference between stored procedures and functions:

  1. The functions implemented by stored procedures are more complex, while functions are more targeted
  2. The error storage process can return multiple values, while the function can only have one value (or use a table or json string to output information about multiple values ​​in disguise)
  3. Stored procedures are generally executed independently, while functions can appear as components of other SQL statements (the usage is the same as the usage of built-in functions such as count())

Guess you like

Origin blog.csdn.net/qq_38662733/article/details/122228524