The use and difference of stored procedures and triggers in MySql

Stored procedure:
In a large database system,
a set of SQL statements to complete a specific function is
stored in the database. After the first compilation, the user does not need to compile again.
The user specifies the name of the stored procedure and gives parameters (if the stored procedure has parameters) to execute it.
Stored procedure is an important object in the database

Advantages:
1 Allows modular programming (create once used many times)
2 Allows faster execution
3 Reduces network traffic
4 Better security mechanism

Format:

DELIMITER //
CREATE PROCEDURE storage name ([ IN ,OUT ,INOUT ]?Parameter name?Data type...)
BEGIN
SQL statement
END //
DELIMITER ;

 

Calling process:

Use the call procedure name ( )


View all stored procedures show procedure status;
view created stored procedures show create procedure procedure name;
delete procedure drop procedure procedure name

 

In means that the parameter is passed in from the outside to be used inside the process (used inside
the process) Out means that the parameter saves the data from the process to the variable and hands it to the outside for use, all incoming must be variables. If the incoming out variable itself is external If there is data, then after entering the process, the first thing is to be emptied, set to null
Inout data can be passed in from the outside to the inside of the process, and after the internal operation, the data will be returned to the outside

 

 


trigger:

Triggers are a special type of stored procedures, which are different from stored procedures.
Triggers are mainly triggered by events and executed, while stored procedures can be called directly through the name of the stored procedure.


Functions:
1. Before writing to the data table, the data can be forcibly checked or converted
2. When an error occurs in the trigger, the result of the transaction will be cancelled

Format
DELIMITER //
Create trigger trigger name trigger time trigger event on table for each
row content of
Begin
operation
End //
DELIMITER ;

Trigger object: on table for each row Trigger binding is essentially all rows in the table, so when each row changes, the trigger will be triggered
Trigger timing: The corresponding row in each table will have a different state, when SQL When the command occurs,
the data in the row will be changed, and each row will always have two states.
Trigger events before operating data (before) and after operating data (after) :
The target of triggers in Mysql is that data changes, and the corresponding operations are only (add, delete, modify) queries without data changes,
so the query is not triggered Event
Notes:
In a table, there can only be one trigger type corresponding to the trigger event bound to each trigger timing;
there can only be one after insert trigger in a table. Therefore, there can only be up to six triggers in a table.


案列
-------------------------------创建存储过程---------------------------
DELIMITER //
CREATE PROCEDURE addUser
(IN uCode VARCHAR(50),IN uName VARCHAR(20),IN uRole INT,IN sex INT,IN tel VARCHAR(30))
BEGIN
INSERT INTO smbms_user (userCode,userName,userRole,gender,phone)
VALUES(uCode,uName,uRole,sex,tel);
END//
DELIMITER //

View the stored procedure show procedure status;

<insert id="saveUser">
CALL addUser(#{userCode},#{userName},#{userRole},#{gender},#{phone})
</insert>

public int saveUser(
@Param("userCode") String userCode,
@Param("userName") String userName,
@Param("userRole") Integer userRole,
@Param("gender") Integer gender,
@Param("phone") String phone);

public List<User> findUserListPage(String queryUserName,
Integer queryUserRole,
Integer currentPageNo, Integer pageSzie);


public boolean saveUser(String userCode, String userName, Integer userRole,
Integer gender, String phone) {
SqlSession sqlSession = null;
int row = 0; // 受影响的行数
try {
sqlSession = MyBatisUtil.createSqlSession();
row = sqlSession.getMapper(UserMapper.class).saveUser(userCode, userName, userRole, gender, phone);
// 提交事务
sqlSession.commit();
} catch (Exception e) {
if (sqlSession != null) {
sqlSession.rollback();
}
row = 0;
e.printStackTrace();
} finally {
MyBatisUtil.closeSqlSession(sqlSession);
}
if (row > 0) {
return true;
}
return false;
}


userService.saveUser("zhangcuishan", "亚索", 1, 2, "15645678941");

 

 


------------------------------------------trigger

 

Create two tables
create table my_goods(
id int primary key auto_increment,
name varchar(20) not null,
inv int
)

create table my_orders(
id int primary key auto_increment,
goods_id int not null,
goods_num int not null)


insert into my_goods values(null,'mobile phone',1000),(null,'computer',500),(null,'game console',100);


DELIMITER //
CREATE TRIGGER a_i_o_t AFTER INSERT ON my_orders FOR EACH ROW
BEGIN
UPDATE my_goods SET inv =inv -new.goods_num WHERE id=new.goods_id;
END
//
DELIMITER ;


DELIMITER //
CREATE TRIGGER b_i_o_t BEFORE INSERT ON my_orders FOR EACH ROW
BEGIN
SELECT inv FROM my_goods WHERE id=new.goods_id INTO @inv;
IF @inv <new.goods_num THEN
INSERT INTO xxx VALUES('xx');
END IF;
END
//
DELIMITER //

 


test insert into my_orders values(null,3,5);

 

Guess you like

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