MySQL触发器应用与其存储过程的实际操作

数据库 MySQL
今天我们主要向大家描述的是MySQL触发器的实际应用与其存储过程的实际操作,以下就是文章的详细内容描述,望你看完会有所收获。

以下的文章主要描述的是MySQL触发器的实际应用与其存储过程的实际操作,以下的文章将会给你提供相应的详细方案讲解,以下就是相关内容的具体描述。希望会给你带来一些帮助在此方面。

触发器:

MySQL数据库创建触发器的格式如下:

create trigger <触发器名称>

{ before | after}

 

{insert | update | delete}

 

on <表名>

 

for each row

 

<触发器SQL语句>

 

 

<触发器SQL语句>:触发器要执行的SQL语句,如果该触发器要执行多条SQL语句,要将多条语句放在begin…end块中。

Show triggers 显示所有的触发器信息!

 

存储过程:

 

创建 

  1. DELIMITER $$  
  2. DROP PROCEDURE IF EXISTS `test`.`outprint`$$  
  3. CREATE PROCEDURE print1(out a varchar(100))  
  4. BEGIN  
  5. select name from info into a;  
  6. END$$  
  7. DELIMITER ;  

调用

  1. Call print1(@c);  
  2. Select @c;  

注意

这样传值只能查找一个值赋给a

Select * from info into a就错了

Show create {procedure | function } 名字

 

查看存储过程和函数

 

Drop {procedure | function} [if exists] 名字

 

Alter {procedure | function} 名字

 

 

注意:通过 begin end 可以来包含多个语句,每个语句以“;”结尾。

 

 

创建插入

  1. DELIMITER $$  
  2. DROP PROCEDURE IF EXISTS `test`.`insert_info`$$  
  3. CREATE DEFINER=`root`@`%` PROCEDURE `insert_info`(in id int(5),in name varchar(50),in age int(5))  
  4. begin  
  5. insert into info values (id,name,age);  
  6. end$$  
  7. DELIMITER ;  

调用

 

  1. call insert_info(2,'rrr',56); 

 

创建插入输出(验证密码用)

 

  1. Delimiter $$   
  2. Create procedure validateuser  
  3. (in username varchar(20),out param1 varchar(100))   
  4. Begin   
  5. Select Pwd into param1 from welefen.user   
  6. where Name=username;   
  7. End $$ 

 

调用

 

 

  1. call check1('sss',@pw);  
  2. select @pw; 

验证角谷猜想

 

角谷猜想:给定一个整数x,若x%2=1,则x=3*x+1,

 

否则x=x/2,

 

如此循环下去

 

,经过有限步骤必 能得到1。

 

例如: 初始整数为9 则

 

9->28->14->7->22->11->34->17->52->26

 

->13->40->20->10->5->16->8->4->2->1

 

为了说明存储过程语法的应用,存储过程来实现它:

 

创建存储过程:

  1. delimiter $$  
  2. drop procedure if exists jgguess$$  
  3. create procedure jgguess(in number int)   
  4. begin   
  5. declare param1 int default 1;   
  6. set @a=concat(number);   

jiaogu:loop #循环开始

set param1=number%2;

if param1=1 then set number=number*3+1; #number 为奇数,将它乘3加 1

else set number=number/2;

end if;

set @a=concat(@a,'->',number);

if number>1 then iterate jiaogu; #number 不为 1,继续循环

else

leave jiaogu; #退出循环

  1. end if;  
  2. end loop jiaogu;   
  3. end $$   
  4. delimiter ;  

 

调用:

  1. call jgguess(9);  
  2. select @a;  

 

 

以上的相关内容就是对MySQL中触发器和存储过程的介绍,望你能有所收获。

 

【编辑推荐】

  1. MySQL触发器如何正确使用
  2. MySQL修改root密码并不难
  3. 恢复MySQL数据库root密码2方案
  4. MySQL数据表中字段的批量修改与复制
  5. MySQL查询优化的5个好用方法

 

责任编辑:佚名 来源: 博客园
相关推荐

2010-05-18 14:35:06

MySQL触发器

2010-06-13 09:46:44

MySQL5触发器

2010-04-26 14:12:23

Oracle使用游标触

2010-05-26 14:55:43

MySQL存储过程

2010-04-07 13:02:14

Oracle 存储过程

2010-05-26 17:57:44

MySQL 触发器

2010-06-04 14:18:10

MySQL 分页存储过

2010-07-23 14:26:37

SQL Server存

2010-05-18 09:31:05

MySQL修改表

2010-03-30 12:50:42

Oracle存储

2010-07-21 14:21:53

SQL Server存

2010-05-27 15:11:44

MySQL保存

2010-05-17 13:28:15

MySQL 复制

2010-04-27 15:11:20

Oracle树的存储过

2010-08-05 14:34:26

DB2存储过程

2010-05-18 17:39:13

MySQL alter

2010-04-23 13:29:01

Oracle存储过程

2010-06-12 13:39:33

MySQL操作blob

2010-06-04 14:32:34

MySQL 触发器in

2010-05-20 17:40:54

MySQL编码
点赞
收藏

51CTO技术栈公众号