MySQL 触发器,存储过程实例演示

数据库 MySQL
文章主要讲述的是MySQL 触发器,存储过程以及函数与视图的实例演示,如果你掌握了这项技术,在以后的学习中带来很大的帮助。

以下的文章主要是介绍MySQL 触发器,存储过程以及函数与视图的实例演示过程,以下就是触发器,存储过程以及函数与视图的具体操作方案的描述,希望在你今后的学习中会对你有所帮助。

MySQL 触发器,存储过程以及函数与视图的实例演示:

0.test数据库有userinfo用户信息表 和userinfolog用户信息日志表

 

1.建立一个userinfo表新增记录时的MySQL 触发器 将新增日志加入到userinfolog

 

2.建立一个向userinfo表新增记录的存储过程

 

3.根据userinfo表的出生日期字段 我们将建立一个简单算得年龄的自定义函数

 

4.创建一个userinfo的视图 调用年龄函数

 

 

0.准备相关表

 

  1. MySQL> use test;  
  2. MySQL> create table userinfo(userid int,username varchar(10),userbirthday date);  
  3. MySQL> create table userinfolog(logtime datetime,loginfo varchar(100));  
  4. MySQL> describe userinfo; 

 

1.MySQL 触发器

 

  1. MySQL> delimiter |  
  2. MySQL> create trigger beforeinsertuserinfo  
  3. -> before insert on userinfo  
  4. -> for each row begin  
  5. -> insert into userinfolog values(now(),CONCAT(new.userid,new.username));  
  6. -> end;  
  7. -> |  
  8. MySQL> delimiter ;  
  9. MySQL> show triggers; 

 

2.存储过程

 

  1. MySQL> delimiter //  
  2. MySQL> create procedure spinsertuserinfo(  
  3. -> puserid int,pusername varchar(10)  
  4. -> ,puserbirthday date  
  5. -> )  
  6. -> begin  
  7. -> insert into userinfo values(puserid,pusername,puserbirthday);  
  8. -> end;  
  9. -> //  
  10. MySQL> show procedure status like 'spinsertuserinfo';  
  11. MySQL> call spinsertuserinfo(1,'zhangsan',current_date);  
  12. MySQL> select * from userinfo; 

3.自定义函数

  1. MySQL> update userinfo  
  2. -> set userbirthday='2000.01.01' 
  3. -> where userid='1';  
  4. MySQL> drop function if exists fngetage;  
  5. MySQL> delimiter //  
  6. MySQL> create function fngetage(pbirthday date)  
  7. -> returns integer  
  8. -> begin  
  9. -> return year(now()) - year(pbirthday);  
  10. -> end  
  11. -> // 

 

4.视图

 

  1. MySQL> create view viewuserinfo  
  2. -> as select * ,fngetage(userbirthday) as userage from userinfo;  
  3. MySQL> select * from viewuserinfo; 

 

清除日志记录

 

  1. MySQL> truncate table userinfolog;  
  2. MySQL> delete from userinfolog; 

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

 

【编辑推荐】

  1. MySQL 数据库开启远程连接并不难
  2. MySQL 基本命令的用法与注意事项
  3. MySQL忘记密码的正确解决方法
  4. MySQL配置SSL的实际操作流程
  5. 安装MySQL在linux as3之下
责任编辑:佚名 来源: 博客园
相关推荐

2010-05-31 18:06:07

MySQL 触发器

2010-05-19 11:25:46

MySQL触发器

2010-06-04 14:32:34

MySQL 触发器in

2010-06-01 16:50:29

MySQL存储过程

2010-04-26 14:12:23

Oracle使用游标触

2018-08-10 09:40:02

数据库MySQL存储过程

2024-01-19 09:37:19

MySQL数据库

2010-10-12 10:04:15

MySQL触发器

2010-10-12 10:24:58

mysql触发器

2010-05-18 15:58:39

MySQL触发器

2021-07-30 10:33:57

MySQL触发器数据

2010-07-16 10:19:31

2019-04-30 15:28:46

数据库存储过程触发器

2011-05-20 14:06:25

Oracle触发器

2010-04-09 09:07:43

Oracle游标触发器

2010-10-12 10:10:55

mysql触发器

2010-05-18 15:36:44

MySQL触发器

2010-10-11 14:52:43

Mysql触发器

2019-01-14 14:41:27

Mysql存储触发器

2011-07-21 15:42:53

SQL触发器存储过程
点赞
收藏

51CTO技术栈公众号