以下的文章主要是介绍MySQL 触发器,存储过程以及函数与视图的实例演示过程,以下就是触发器,存储过程以及函数与视图的具体操作方案的描述,希望在你今后的学习中会对你有所帮助。
MySQL 触发器,存储过程以及函数与视图的实例演示:
0.test数据库有userinfo用户信息表 和userinfolog用户信息日志表
1.建立一个userinfo表新增记录时的MySQL 触发器 将新增日志加入到userinfolog
2.建立一个向userinfo表新增记录的存储过程
3.根据userinfo表的出生日期字段 我们将建立一个简单算得年龄的自定义函数
4.创建一个userinfo的视图 调用年龄函数
0.准备相关表
MySQL>usetest; MySQL>createtableuserinfo(useridint,usernamevarchar(10),userbirthdaydate); MySQL>createtableuserinfolog(logtimedatetime,loginfovarchar(100)); MySQL>describeuserinfo;
1.MySQL 触发器
MySQL>delimiter| MySQL>createtriggerbeforeinsertuserinfo ->beforeinsertonuserinfo ->foreachrowbegin ->insertintouserinfologvalues(now(),CONCAT(new.userid,new.username)); ->end; ->| MySQL>delimiter; MySQL>showtriggers;
2.存储过程
MySQL>delimiter// MySQL>createprocedurespinsertuserinfo( ->puseridint,pusernamevarchar(10) ->,puserbirthdaydate ->) ->begin ->insertintouserinfovalues(puserid,pusername,puserbirthday); ->end; ->// MySQL>showprocedurestatuslike'spinsertuserinfo'; MySQL>callspinsertuserinfo(1,'zhangsan',current_date); MySQL>select*fromuserinfo;
3.自定义函数
MySQL>updateuserinfo ->setuserbirthday='2000.01.01'->whereuserid='1'; MySQL>dropfunctionifexistsfngetage; MySQL>delimiter// MySQL>createfunctionfngetage(pbirthdaydate) ->returnsinteger ->begin ->returnyear(now())-year(pbirthday); ->end ->//
4.视图
MySQL>createviewviewuserinfo ->asselect*,fngetage(userbirthday)asuseragefromuserinfo; MySQL>select*fromviewuserinfo;
清除日志记录
MySQL>truncatetableuserinfolog; MySQL>deletefromuserinfolog;
以上的相关内容就是对MySQL 触发器 存储过程 函数 视图的介绍,望你能有所收获。
【编辑推荐】
- MySQL 数据库开启远程连接并不难MySQL 基本命令的用法与注意事项MySQL忘记密码的正确解决方法MySQL配置SSL的实际操作流程安装MySQL在linux as3之下