当前位置:网站首页>Linux server development, MySQL stored procedures, functions and triggers

Linux server development, MySQL stored procedures, functions and triggers

2022-07-07 07:50:00 Tuen Mun pheasant calls me chicken

Recommend a free open course of zero sound College , Personally, I think the teacher spoke well , Share with you :Linux,Nginx,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK, Streaming media ,CDN,P2P,K8S,Docker,TCP/IP, coroutines ,DPDK Etc , Learn now
SQL Statements need to be compiled before execution , Stored procedures are a group of procedures designed to accomplish specific functions SQL Statements set , It is compiled and stored in a database .

delimiter // -- The ending character is temporarily changed to //
CREATE PROCEDURE pro_test(IN p int ,)
BEGIN
SELECT 1;
SELECT 2;
END
//
delimiter ;
SET @p2_in=1;
CALL pro_test(p2_in);
SELECT @p2_in;

  • IN For input parameters
  • OUT Is the output parameter

1. The cursor

CREATE PROCEDURE proc_normal(IN in_age int ,out out_taotal int)
BEGIN
DECLARE p_id int;
DECLARE p_name varchar(20);
DECLARE p_total int;
DECLARE done int default 0;
SET out_taotal =0;
CREATE cur_teacher CURSOR FOR SELECT teacher.id,teacher.name FROM teacher;
DECALRE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur_teacher;
cur_loop :LOOP
FECTH cur_teacher INTO p_id,p_name;
IF done = 1 THEN
LEAVE cur_loop;
END IF
SELECT p_id,p_name
SET p_total=p_total+p_id;
END LOOP
CLOSE cur_teacher;
SET out_taotal =p_total;
END

Cyclic replacement WHILE
WHILE done != 1
END WHILE

Cyclic replacement REPEATE
REPEATE
UNTIL done =1
END REPEATE

2. function

  • SELECT abs(-2);
  • SELECT round(4,4);
  • SELECT concat(“000”,“123123”);
  • SELECT month(“2022-01-28”);
  • SELECT format(111,5);
  • SHOW FUNCTION STATUS;

3. trigger

Trigger four elements : Monitoring sites , Monitoring events , The trigger condition , Trigger time .
CREATE TRIGER tri_test BEFORE | AFTER INSERT ON work EACH ROW
BEGIN
INSERT INTO ‘works’ value (NULL,NEW.id,‘ Changsha ’);
END

  • stay INSERT Type trigger ,NEW Used to indicate that you are going to (BEFORE) Or already (AFTER) New data inserted .
  • stay DELETE Type trigger ,OLD Used to represent the original data that will be or has been deleted .
  • stay UPDATE Type trigger ,OLD Used to represent data that will be or has been modified ,NEW Used to indicate that the data to be changed into new data .
原网站

版权声明
本文为[Tuen Mun pheasant calls me chicken]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130647575568.html