mysql database | stored procedures

mysql database | stored procedures

1. What is a stored procedure?

If you have been exposed to other programming languages, then it is easy to understand that stored procedures are like methods.

Since it is a method, it has a similar method name, the variables to be passed by the method and the return result, so the stored procedure has a stored procedure name, a stored procedure parameter, and a return value.

2. What are the advantages of stored procedures?

  1. It is pre-compiled SQL, which runs very fast. Traditional scattered SQL needs to be compiled and run first.

  2. It is equivalent to a function in java, encapsulating a piece of sql code, which not only facilitates the reuse of the code, but also ensures the security of the database by controlling the authority to call the stored procedure interface.

  3. Convenient division of labor, convenient for DBA (database administrator) to focus on SQL writing and optimization;

  4. The ability of the stored procedure greatly enhances the function and flexibility of the SQL language.

  5. The security and integrity of the data can be guaranteed.

  6. Through stored procedures, users without permission can access the database indirectly under control, thereby ensuring data security.

  7. Can reduce network traffic.
    Put the calculation program that reflects the corporate rules into the database server for centralized control.

3. The disadvantages of stored procedures?

  1. Debugging is not very convenient (not as easy as developing a program)
  2. May not have the right to create a stored procedure
  3. Recompilation problem
  4. Portability issues (stored procedures depend on the specific database)

4. Grammar Specification

create

CREATE procedure demo1(in a int, in b int) 
begin    所有被封装的语句一定在beginend之间!
select a+b;
end;

transfer

call demo1(1, 2);

delete

DROP PROCEDURE demo1;

Declare variable

declare c int default 10;

Judgment branch

if 条件 then 执行语句 end if
if 条件 then 执行语句1 else 执行语句2 end if

if a+b+c<10 then
select a+b+c
else select a+b
end if;

Cyclic structure

while 条件 do
执行语句;
条件改变语句;
end while;

declare result int default 0;
while c<3 do;
set result=result+c;
set c=c+1;
end while;

Guess you like

Origin blog.csdn.net/weixin_40597409/article/details/115368739