本示例实现如下效果:
0.test数据库有userinfo用户信息表 和userinfolog用户信息日志表
1.建立一个userinfo表新增记录时的触发器 将新增日志加入到userinfolog
2.建立一个向userinfo表新增记录的存储过程
3.根据userinfo表的出生日期字段 我们将建立一个简单算得年龄的自定义函数
4.创建一个userinfo的视图 调用年龄函数
-------------
0.准备相关表
mysql> use test;
mysql> create table userinfo(userid int,username varchar(10),userbirthday date);
mysql> create table userinfolog(logtime datetime,loginfo varchar(100));
mysql> describe userinfo;
1.触发器
mysql> delimiter |
mysql> create trigger beforeinsertuserinfo
-> before insert on userinfo
-> for each row begin
-> insert into userinfolog values(now(),CONCAT(new.userid,new.username));
-> end;
-> |
mysql> delimiter ;
mysql> show triggers;
2.存储过程
mysql> delimiter //
mysql> create procedure spinsertuserinfo(
-> puserid int,pusername varchar(10)
-> ,puserbirthday date
-> )
-> begin
-> insert into userinfo values(puserid,pusername,puserbirthday);
-> end;
-> //
mysql> show procedure status like 'spinsertuserinfo';
mysql> call spinsertuserinfo(1,'zhangsan',current_date);
mysql> select * from userinfo;
3.自定义函数
mysql> update userinfo
-> set userbirthday='2000.01.01'
-> where userid='1';
mysql> drop function if exists fngetage;
mysql> delimiter //
mysql> create function fngetage(pbirthday date)
-> returns integer
-> begin
-> return year(now()) - year(pbirthday);
-> end
-> //
4.视图
mysql> create view viewuserinfo
-> as select * ,fngetage(userbirthday) as userage from userinfo;
mysql> select * from viewuserinfo;
清除日志记录
mysql> truncate table userinfolog;
mysql> delete from userinfolog;