当前位置:网站首页>Introduction and basic use of stored procedures

Introduction and basic use of stored procedures

2022-07-07 13:17:00 Soup key TJ

  • Catalog

    Introduce

    benefits :

    The difference between stored procedures and functions ( Almost no difference )

    Create and call stored procedures

    Create stored procedure

    Calling stored procedure

    Examples demonstrate

    Data preparation

    Create and call stored procedures

    View and delete of stored procedures

    View all stored procedures in the database

    Delete stored procedure

    Example operation


  • Introduce

  • Stored procedures and functions are compiled in advance and stored in the database a section SQL Collection of statements
  • benefits :

  • Improve code reusability
  • Reduce data transfer between database and application server , Increase of efficiency
  • ( For databases , Later, it should be defined on one or more servers , Transmission later will definitely affect efficiency )
  • Reduce business processing at the code level
  • The difference between stored procedures and functions ( Almost no difference )

  • The storage function must have a return value
  • A stored procedure may not return a value
  • Create and call stored procedures

  • Create stored procedure

  • -- Modify the end separator
  • delimiter $
  • -- Create stored procedure
  • create procedure Stored procedure name ( parameter list )
  • begin
  • SQL Statement list
  • end$
  • -- Modify the end separator
  • delimiter ;
  • Calling stored procedure

  • call Stored procedure name ( The actual parameter )
  • Examples demonstrate

  • Data preparation

  • --  Create student table 
    CREATE TABLE student(
      id INT PRIMARY KEY auto_increment, --  Student id
    	name VARCHAR(20), --  The student's name 
    	age INT, --  Student age 
    	gender VARCHAR(5), --  Student gender 
    	score INT --  Student achievement 
    );
    --  Add data 
    INSERT INTO student VALUES (NULL,' Zhang San ',23,' male ',95),(NULL,' Li Si ',24,' male ',98),(NULL,' Wang Wu ',25,' Woman ',100),(NULL,' Zhao Liu ',26,' Woman ',90);
  • Create and call stored procedures

  • --  establish stu_group() stored procedure , encapsulation   Group query total score , And sort in ascending order according to the total score 
    delimiter $
    CREATE PROCEDURE stu_group()
    BEGIN
        SELECT gender,SUM(score) getsum FROM student GROUP BY gender ORDER BY getsum ASC;
    END$
    delimiter ;
  • --  call stu_group() stored procedure 
    CALL stu_group;
  • View and delete of stored procedures

  • View all stored procedures in the database

  • select * from mysql.proc where db=' Database name '
  • Delete stored procedure

  • drop procedure [if exists] Stored procedure name
  • Example operation

  • --  see dp1 All stored procedures in the database 
    SELECT * FROM mysql.proc WHERE db='dp1';
  • --  Delete stored procedure 
    DROP PROCEDURE IF EXISTS stu_group;

原网站

版权声明
本文为[Soup key TJ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071119337025.html