新聞中心
以下的文章主要是介紹MySQL 觸發(fā)器,存儲(chǔ)過程以及函數(shù)與視圖的實(shí)例演示過程,以下就是觸發(fā)器,存儲(chǔ)過程以及函數(shù)與視圖的具體操作方案的描述,希望在你今后的學(xué)習(xí)中會(huì)對(duì)你有所幫助。

MySQL 觸發(fā)器,存儲(chǔ)過程以及函數(shù)與視圖的實(shí)例演示:
0.test數(shù)據(jù)庫(kù)有userinfo用戶信息表 和userinfolog用戶信息日志表
1.建立一個(gè)userinfo表新增記錄時(shí)的MySQL 觸發(fā)器 將新增日志加入到userinfolog
2.建立一個(gè)向userinfo表新增記錄的存儲(chǔ)過程
3.根據(jù)userinfo表的出生日期字段 我們將建立一個(gè)簡(jiǎn)單算得年齡的自定義函數(shù)
4.創(chuàng)建一個(gè)userinfo的視圖 調(diào)用年齡函數(shù)
0.準(zhǔn)備相關(guān)表
- 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 觸發(fā)器
- 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.存儲(chǔ)過程
- 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.自定義函數(shù)
- 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;
以上的相關(guān)內(nèi)容就是對(duì)MySQL 觸發(fā)器 存儲(chǔ)過程 函數(shù) 視圖的介紹,望你能有所收獲。
【編輯推薦】
- MySQL 數(shù)據(jù)庫(kù)開啟遠(yuǎn)程連接并不難
- MySQL 基本命令的用法與注意事項(xiàng)
- MySQL忘記密碼的正確解決方法
- MySQL配置SSL的實(shí)際操作流程
- 安裝MySQL在linux as3之下
網(wǎng)站名稱:MySQL觸發(fā)器,存儲(chǔ)過程實(shí)例演示
分享URL:http://www.dlmjj.cn/article/dhijogo.html


咨詢
建站咨詢
