mysql: Quickly master stored procedures


1. What is a stored procedure

      Is a set of SQL statements in order to complete a specific function. After being compiled and stored in the database, it is stored and executed in the MySQL server, which can reduce the data transmission between the client and the server.

      A stored procedure is an important object in the database, and the user executes it by specifying the name of the stored procedure and giving parameters (if the stored procedure has parameters). A stored procedure is a procedure written by flow control and SQL statements. This procedure is compiled and optimized and stored in the database server. The stored procedure can be executed by an application program with one call and allows the user to declare variables. At the same time, the stored procedure can receive and output parameters, return the state value of executing the stored procedure, and can also nest calls.

2. Why use stored procedures:

  • Stored procedures are certified technology!
  • Stored procedures make the system run faster!
  • Stored procedures are reusable components! It's database logic not an application.
  •  The stored procedure will be saved!

3. Advantages of stored procedures:

  • The stored procedure is only compiled when it is created, and it does not need to be recompiled every time the stored procedure is executed in the future, and the general SQL statement is compiled once every time it is executed, so the use of the stored procedure can improve the execution speed of the database.
  • When performing complex operations on the database (such as when performing Update, Insert, Query, and Delete on multiple tables), this complex operation can be encapsulated in a stored procedure and used in conjunction with the transaction processing provided by the database.
  • Stored procedures can be reused, reducing the workload of database developers.
  • High security, it can be set that only a certain user has the right to use the specified stored procedure.

4. Create a stored procedure

CREATE PROCEDURE
    sp_name ([proc_parameter[,...]])
    [characteristic ...] routine_body

Each parameter in proc_parameter consists of 3 parts.
These three parts are input and output type, parameter name and parameter type respectively, the form is as follows:
[ IN | OUT | INOUT ] param_name type
CREATE PROCEDURE
food_price_count (IN price_info1 FLOAT,IN price_info2 FLOAT, OUT count INT )
   READS SQL DATA
   BEGIN
    DECLARE temp FLOAT;
    DECLARE match_price CURSOR FOR SELECT price FROM food;
    DECLARE EXIT HANDLER FOR NOT FOUND CLOSE match_price;
    SET @sum=0;
    SELECT COUNT(*) INTO count FROM food
     WHERE price>price_info1 AND price<price_info2 ;
    OPEN match_price;
    REPEAT
     FETCH match_price INTO temp;
     IF temp>price_info1 AND temp<price_info2
        THEN SET @sum=@sum+temp;
    END IF;
    UNTIL 0 END REPEAT;
    CLOSE match_price;
   END


CALL food_price_count(2,18,@count) ;

SELECT @count, @sum ;

in:
  •     IN represents the input parameter;
  •     OUT represents the output parameter;
  •    INOUT means that it can be either input or output;
  •    The param_name parameter is the parameter name of the stored procedure;
  •     The type parameter specifies the parameter type of the stored procedure, which can be any data type of the MySQL database.
The characteristic parameter has multiple values. Its value description is as follows:
  •     LANGUAGE SQL: indicates that the routine_body part is composed of statements in the SQL language, which is also the default language of the database system.
  •     [NOT] DETERMINISTIC:指明存储过程的执行结果是否是确定的。DETERMINISTIC表示结果是确定的。每次执行存储过程时,相同的输入会得到相同的输出。NOT DETERMINISTIC表示结果是非确定的,相同的输入可能得到不同的输出。默认情况下,结果是非确定的。
  •     { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }:指明子程序使用SQL语句的限制。CONTAINS SQL表示子程序包含SQL语句,但不包含读或写数据的语句;NO SQL表示子程序中不包含SQL语句;READS SQL DATA表示子程序中包含读数据的语句;MODIFIES SQL DATA表示子程序中包含写数据的语句。默认情况下,系统会指定为CONTAINS SQL。
  •     SQL SECURITY { DEFINER | INVOKER }:指明谁有权限来执行。DEFINER表示只有定义者自己才能够执行;INVOKER表示调用者可以执行。默认情况下,系统指定的权限是DEFINER。
  •     COMMENT 'string':注释信息。
技巧:创建存储过程时,系统默认指定CONTAINS SQL,表示存储过程中使用了SQL语句。但是,如果存储过程中没有使用SQL语句,最好设置为NO SQL。而且,存储过程中最好在COMMENT部分对存储过程进行简单的注释,以便以后在阅读存储过程的代码时更加方便。

5.调用存储过程

MySQL中使用 CALL语句来调用存储过程。调用存储过程后,数据库系统将执行存储过程中的语句。
然后,将结果返回给输出值。CALL语句的基本语法形式如下:
CALL  sp_name([parameter[,…]]) ;

6.查看存储过程的定义

MySQL中可以通过SHOW CREATE语句查看存储过程和函数的状态。其基本语法形式如下:
SHOW CREATE { PROCEDURE |FUNCTION} sp_name ;
# The "PROCEDURE" parameter represents the query stored procedure;
#“FUNCTION” parameter represents the query stored function;
# The "sp_name" parameter represents the name of the stored procedure or function.

7. Modify the stored procedure

Modifying stored procedures and functions refers to modifying already defined stored procedures and functions.
MySQL via ALTER PROCEDURE statement to modify the stored procedure. The stored function is modified by the ALTER FUNCTION statement.
The syntax of statements that modify stored procedures and functions in MySQL is as follows:
ALTER {PROCEDURE | FUNCTION} sp_name [characteristic ...]
#characteristic:
{ CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER }
| COMMENT 'string'

8. Delete the stored procedure

Deleting stored procedures and functions refers to deleting stored procedures and functions that already exist in the database.
The DROP PROCEDURE statement is used in MySQL to delete stored procedures. Drop a stored function with the DROP FUNCTION statement. Its basic form is as follows:
DROP { PROCEDURE| FUNCTION } sp_name;

Note: This article is transferred from http://blog.chinaunix.net/uid-26602509-id-3139913.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324776888&siteId=291194637