MySQL stored procedures and triggers
MySql:
View all stored procedures show procedure status;
View stored procedures in a database: show procedure status where db = ' database name ';
View the creation code of a stored procedure: show create procedure pro_test( stored procedure name );
First, the stored procedure
The basic format of a stored procedure is as follows:
--declaration terminator -- create stored procedure DELIMITER $ -- declare the terminator of the stored procedure CREATE PROCEDURE pro_test () --stored procedure name (parameter list) BEGIN --can write multiple sql statements; -- sql statement + process Control SELECT * FROM employee; END $ -- end terminator --Execute stored procedure CALL pro_test(); -- CALL stored procedure name (parameters); --delete the stored procedure DROP PROCEDURE pro_test ; parameter: IN : Indicates the input parameter, which can carry data in the stored procedure OUT: Indicates output parameters, which can return results from stored procedures INOUT: Indicates input and output parameters, which can either input functions or output functions
1. Stored procedure with input parameters
Requirement: Pass in an employee's id to query employee information
DELIMITER $ CREATE PROCEDURE pro_findById(IN eid INT) -- IN: 输入参数 BEGIN SELECT * FROM employee WHERE id=eid; END $ --call CALL pro_findById( 4 );
2. Stored procedure with output parameters
DELIMITER $ CREATE PROCEDURE pro_testOut(OUT str VARCHAR(20)) -- OUT:输出参数 BEGIN -- 给参数赋值 SET str='hellojava'; END $
如何接受返回参数的值呢?这里涉及到MySQL的变量
MySQL变量一共有三种:
全局变量
全局变量又叫内置变量,是mysql数据库内置的变量 ,对所有连接都起作用。
查看所有全局变量: show variables
查看某个全局变量: select @@变量名
修改全局变量: set 变量名=新值
character_set_client: mysql服务器的接收数据的编码
character_set_results:mysql服务器输出数据的编码
会话变量
只存在于当前客户端与数据库服务器端的一次连接当中。如果连接断开,那么会话变量全部丢失!
定义会话变量: set @变量=值
查看会话变量: select @变量
局部变量
在存储过程中使用的变量就叫局部变量。只要存储过程执行完毕,局部变量就丢失。
回到上面这个存储过程,如何接受返回参数的值呢?
定义一个会话变量name, 使用name会话变量接收存储过程的返回值
CALL pro_testOut(@NAME);
查看变量值
SELECT @NAME;
3. 带有输入输出参数的存储过程
DELIMITER $ CREATE PROCEDURE pro_testInOut(INOUT n INT) -- INOUT: 输入输出参数 BEGIN -- 查看变量 SELECT n; SET n =500; END $ -- 调用 SET @n=10; CALL pro_testInOut(@n); SELECT @n;
4. 带有条件判断的存储过程
需求:输入一个整数,如果1,则返回“星期一”,如果2,返回“星期二”,如果3,返回“星期三”。其他数字,返回“错误输入”;
DELIMITER $ CREATE PROCEDURE pro_testIf(IN num INT,OUT str VARCHAR(20)) BEGIN IF num=1 THEN SET str='星期一'; ELSEIF num=2 THEN SET str='星期二'; ELSEIF num=3 THEN SET str='星期三'; ELSE SET str='输入错误'; END IF; END $ --调用 CALL pro_testIf(4,@str); SELECT @str;
5. 带有循环功能的存储过程
需求: 输入一个整数,求和。例如,输入100,统计1-100的和
DELIMITER $ CREATE PROCEDURE pro_testWhile(IN num INT,OUT result INT) BEGIN -- 定义一个局部变量 DECLARE i INT DEFAULT 1; DECLARE vsum INT DEFAULT 0; WHILE i<=num DO SET vsum = vsum+i; SET i=i+1; END WHILE; SET result=vsum; END $ --调用 CALL pro_testWhile(100,@result); SELECT @result;
6. 使用查询的结果赋值给变量(INTO)
DELIMITER $ CREATE PROCEDURE pro_findById2(IN eid INT,OUT vname VARCHAR(20) ) BEGIN SELECT empName INTO vname FROM employee WHERE id=eid; END $ --调用 CALL pro_findById2(1,@NAME); SELECT @NAME;
二、 触发器
触发器作用:当操作了某张表时,希望同时触发一些动作/行为,可以使用触发器完成。
需求: 当向员工表插入一条记录时,希望mysql自动同时往日志表插入数据
CREATE TRIGGER tri_empAdd AFTER INSERT ON employee FOR EACH ROW -- 当往员工表插入一条记录时 INSERT INTO test_log(content) VALUES('员工表插入了一条记录');
以上触发器是基于insert的,以下两个是基于update和delete的。
-- 创建触发器(修改) CREATE TRIGGER tri_empUpd AFTER UPDATE ON employee FOR EACH ROW -- 当往员工表修改一条记录时 INSERT INTO test_log(content) VALUES('员工表修改了一条记录'); -- 创建触发器(删除) CREATE TRIGGER tri_empDel AFTER DELETE ON employee FOR EACH ROW -- 当往员工表删除一条记录时 INSERT INTO test_log(content) VALUES('员工表删除了一条记录');
说明:
1. MySQL uses a semicolon, that is, ";", as a statement separator by default. If this is the case, a stored procedure will be difficult to create properly, because it can be any number of SQL statements between BEGIN and END, and each SQL statement ends with a semicolon. Therefore, when creating a stored procedure, you must redefine the delimiter. In the above use cases, "$" is used as the new delimiter. Note that after the stored procedure is created, the original delimiter ";" must be reset.
The above is based on the MySQL-related content of the Chuanzhi Podcast JAVA employment class video.
Reference: http://www.cnblogs.com/ivictor/p/5045378.html