[MYSQL Notes] Using Stored Procedures

Note: Stored procedures can be used in version 5.0 or higher.

Stored procedure :

Multiple SQL statements are combined into a collection that can be executed only by using the command "call xx", the collection is called a stored procedure.

Storage means saving, and process means steps.

That is to say, a stored procedure is a collection that summarizes and stores a series of steps.

Since the commands prepared in advance can be automatically executed, the processing efficiency is high.

However, in storing important data, it is very dangerous to execute stored procedures that are not fully validated.

Create a stored procedure:

create procedure 存储过程名()
begin
SQL语句1
SQL语句2
end

The content from begin to end is the body of the stored procedure.

Delimiter problem:

Because the content of the stored procedure is an ordinary SQL statement, you need to add the delimiter ; at the end of the command

begin
sql1;
sql2;
end

In this way, the create procedure command will be executed in an incomplete state of the stored procedure. Because once the separator is entered in the MYSQL monitor, no matter what the content is, the part before the separator will be executed first.

Solution: Modify the settings of the separator

When creating a stored procedure, you need to change the delimiter from; to other conformances, generally use //

delimiter //

Example: Create a stored procedure to execute display tb and tb1

delimiter //
create procedure pr1()
begin
select * from tb;
select * from tb1;
end
//delimiter ;

Execute the stored procedure:

Example: call pr1 of the previous example

call pr1;

 

Create a stored procedure with parameters:

create procedure 存储过程名(参数名 数据类型);

Example: Create a record showing that sales are greater than or equal to the specified value. And execute the stored procedure

delimiter //
create procedure pr(d int)
begin
select * from tb where sales>=d;
end


call pr(200); 

Note: adding in in front of the parameter will also get the same result (in d int);

Display the contents of the stored procedure:

show create procedure pr;

 delete stored procedure

drop procedure 存储过程名;

 Stored functions: Stored functions are basically the same way of thinking and operating as stored procedures. The only difference from a stored procedure is that a stored function returns a value after execution.

Stored functions can work as functions, and custom functions can be created using stored functions. Therefore, stored functions are also called user-defined functions.

The value returned by the stored function can be used in commands such as select and update as normal functions.

create function 存储函数名(参数 数据类型)returns 返回值的数据类型
begin
SQL语句
return 返回值 表达式
end

Declare the variable:

declare 变量名 变量类型;

Assign values ​​to variables:

a into b;//b=a;

 

Exercise: Calculate Standard Weight Using Stored Functions

Standard weight = height * height (cm) * 22/10000

delimiter //
create function fu1(height int) returns double
begin
return height*height*22/10000;
end
//
delimiter;

select fu1(174);

 

 Exercise: Return the mean of the sales column

delimiter //
create function fun2() return double
begin
declare r double;
select avg(sales) into r from tb;
return r;
end
//
delimiter ;

select fun2();

Display the contents of the stored function

show create function 存储函数名;

delete stored function

drop function 存储函数名;

Trigger: A mechanism that triggers the execution of other commands after an operation is performed on a table.

When executing commands such as insert, update, delete, etc., the actions set in advance as triggers will also be executed.

Triggers are also often used as a record of processing or as a backup in the event of a processing failure.

When the trigger is fired:

before: fires before the table is processed

after: Triggered after the table is processed

 

Get column values ​​before table processing: old.column_name

Get the column value after table processing:  new.column name

However, depending on the command, some column values ​​can be taken out, and some column values ​​can be taken out.

 

Example: Create a "if you delete a record in a table, the deleted record will be copied to other tables" trigger.

Create an empty table tb1 in advance for inserting deleted records in tb

create table tb1 like tb;

Create trigger and delete tb

delimiter //
create trigger tr before delete on tb1 for each row
begin
insert into tb1(old.id,old.name,old.age);
end
//
delimiter ;

delete from tb1;

 Confirm the set trigger:

show triggers;

Delete trigger:

drop trigger 触发器名;

 

 

Guess you like

Origin blog.csdn.net/m0_52043808/article/details/124221961