Mysql存儲(chǔ)過(guò)程、觸發(fā)器、事件調(diào)度器使用入門指南
存儲(chǔ)過(guò)程(Stored Procedure)是一種在數(shù)據(jù)庫(kù)中存儲(chǔ)復(fù)雜程序的數(shù)據(jù)庫(kù)對(duì)象。為了完成特定功能的SQL語(yǔ)句集,經(jīng)過(guò)編譯創(chuàng)建并保存在數(shù)據(jù)庫(kù)中。
一、存儲(chǔ)過(guò)程的簡(jiǎn)單使用
創(chuàng)建存儲(chǔ)過(guò)程
create procedure test() begin select * from users; end;
調(diào)用存儲(chǔ)過(guò)程
call test();
二、存儲(chǔ)過(guò)程中的變量
create procedure test() begin -- 使用 declare語(yǔ)句聲明一個(gè)變量 declare username varchar(32) default ''; -- 使用set語(yǔ)句給變量賦值 set username='xiaoxiao'; -- 將users表中id=1的名稱賦值給變量username select name into username from users where id=1; -- 返回變量 select username; end;
注意:
- 變量可以通過(guò)set來(lái)賦值,也可以通過(guò)select into的方式賦值;
- 變量需要返回,可以使用select語(yǔ)句,如:select 變量名。
三、變量的作用域
存儲(chǔ)過(guò)程的作用域在begin和end塊之間,變量聲明在begin之外,可以作為全局變量使用:
create procedure test() begin declare userscount int default 0; -- 用戶表中的數(shù)量 begin select count(*) into userscount from users; select userscount; -- 返回用戶表中的數(shù)量 end; begin declare maxmoney int default 0; -- 最大金額 select max(money) into maxmoney from orders; select userscount,maxmoney; -- 返回用戶表中的數(shù)量、最大金額 end; end;
四、存儲(chǔ)過(guò)程參數(shù)
create procedure 名稱([IN|OUT|INOUT] 參數(shù)名 參數(shù)數(shù)據(jù)類型 ) begin ...... end
IN: 傳入?yún)?shù)(不指定時(shí),默認(rèn)就是IN類型)
create procedure test(userId int) begin declare username varchar(32) default ''; select name into username from users where id=userId; select username; end;
OUT:傳出參數(shù)
create procedure test(in userId int,out username varchar(32)) begin select name into username from users where id=userId; end;
INOUT: 既是傳入又是傳出參數(shù)
create procedure test6(inout userId int,inout username varchar(32)) begin set userId=2; set username=''; select id,name into userId,username from users where id=userId; end;
五、邏輯控制語(yǔ)句
1、條件語(yǔ)句
if() then... elseif() then... else ... end if;
create procedure test(in userid int) begin declare my_status int default 0; select status into my_status from users where id=userid; if(my_status=1) then update users set score=score+10 where id=userid; elseif(my_status=2) then update users set score=score+20 where id=userid; else update users set score=score+30 where id=userid; end if; end;
2、循環(huán)語(yǔ)句
(1)while
while(表達(dá)式) do ...... end while;
create procedure test() begin declare i int default 0; while(i<10) do begin select i; set i=i+1; insert into test1(id) values(i); end; end while; end;
(2)repeat
repeat...until...end repeat;
只有當(dāng)until為真是才跳出循環(huán):
create procedure test() begin declare i int default 0; repeat begin select i; set i=i+1; insert into test1(id) values(i); end; until i>=10 -- 如果i>=10,則跳出循環(huán) end repeat; end;
3、case分支
case ... when ... then.... when.... then.... else ... end case;
create procedure testcate(userid int) begin declare my_status int default 0; select status into my_status from users where id=userid; case my_status when 1 then update users set score=10 where id=userid; when 2 then update users set score=20 where id=userid; when 3 then update users set score=30 where id=userid; else update users set score=40 where id=userid; end case; end;
六、游標(biāo)
游標(biāo)保存了查詢結(jié)果的臨時(shí)區(qū)域
declare 變量名 cursor ... -- 創(chuàng)建一個(gè)游標(biāo)變量 close 變量名; -- 關(guān)閉游標(biāo)
create procedure test() begin declare stopflag int default 0; declare username VARCHAR(32); declare username_cur cursor for select name from users where id%2=0; -- 游標(biāo)變量username_cur保存了查詢的臨時(shí)結(jié)果,即結(jié)果集 -- 在游標(biāo)變量中數(shù)據(jù)的結(jié)尾,將變量stopflag設(shè)置為1,用于循環(huán)中判斷是否結(jié)束 declare continue handler for not found set stopflag=1; open username_cur; -- 打卡游標(biāo) fetch username_cur into username; -- 游標(biāo)向前走一步,取出一條記錄放到變量username中 while(stopflag=0) do -- 如果游標(biāo)還沒(méi)有結(jié)尾,就繼續(xù) begin -- 在用戶名前門拼接 '_cur' 字符串 update users set name=CONCAT(username,'_cur') where name=username; fetch username_cur into username;-- 游標(biāo)向前走一步,取出一條記錄放到變量username中 end; end while; -- 結(jié)束循環(huán) close username_cur; -- 關(guān)閉游標(biāo) end;
七、自定義函數(shù)
-- 創(chuàng)建函數(shù) create function 函數(shù)名(參數(shù)) returns 返回類型; -- 函數(shù)體 begin ...... end; -- 指定函數(shù)的返回值 returns --函數(shù)調(diào)用 select 函數(shù)名()。
create function getusername(userid int) returns varchar(32) reads sql data -- 從數(shù)據(jù)庫(kù)中讀取數(shù)據(jù),但不修改數(shù)據(jù) begin declare username varchar(32) default ''; select name into username from users where id=userid; return username; end;
八、觸發(fā)器
觸發(fā)器也是一種數(shù)據(jù)庫(kù)對(duì)象,在滿足定義條件時(shí)觸發(fā),并執(zhí)行觸發(fā)器中定義的語(yǔ)句集合。
創(chuàng)建觸發(fā)器:create trigger 觸發(fā)器名
after、before:在對(duì)表操作之前(before)或者之后(after)觸發(fā)動(dòng)作。
操作事件:insert,update,delete等修改操作
影響的范圍:for each row
1、需求:出于審計(jì)目的,當(dāng)有人往表users插入一條記錄時(shí),把插入的userid,username,插入動(dòng)作和操作時(shí)間記錄下來(lái)。
create trigger tr_users_insert after insert on users for each row begin insert into oplog(userid,username,action,optime) values(NEW.userid,NEW.name,'insert',now()); end;
2、需求:出于審計(jì)目的,當(dāng)刪除users表時(shí),記錄刪除前該記錄的主要字段值
create trigger tr_users_delete before delete on users for each row begin insert into oplog(userid,username,action,optime) values(OLD.id,OLD.name,'delete',now()); end;
九、事件
觸發(fā)器只是針對(duì)某個(gè)表產(chǎn)生的事件執(zhí)行一些語(yǔ)句,而事件調(diào)度器則是在某一個(gè)(間隔)時(shí)間執(zhí)行一些語(yǔ)句。
在使用這個(gè)功能之前必須確保事件調(diào)度器event_scheduler已開(kāi)啟:
SET GLOBAL event_scheduler = 1; -- 或者 SET GLOBAL event_scheduler = on; --查看開(kāi)啟情況 show variables like '%event_scheduler%';
create event[IF NOT EXISTS]event_name -- 創(chuàng)建使用create event ON SCHEDULE schedule -- on schedule 什么時(shí)候來(lái)執(zhí)行 [ON COMPLETION [NOT] PRESERVE] -- 調(diào)度計(jì)劃執(zhí)行完成后是否還保留 [ENABLE | DISABLE] -- 是否開(kāi)啟事件,默認(rèn)開(kāi)啟 [COMMENT 'comment'] -- 事件的注釋 DO sql_statement; -- 這個(gè)調(diào)度計(jì)劃要做什么?
需求:設(shè)計(jì)一個(gè)福彩的開(kāi)獎(jiǎng)過(guò)程,每3分鐘開(kāi)獎(jiǎng)一次
-- 存儲(chǔ)過(guò)程 create procedure test() begin insert into lottery(num1,num2,num3,ctime) select FLOOR(rand()*9)+1,FLOOR(rand()*9)+1,FLOOR(rand()*9)+1,now(); end;
-- 事件 create event if not exists test_event -- 創(chuàng)建一個(gè)事件 on schedule every 3 minute -- on schedule 每三分鐘執(zhí)行一次 on completion preserve do call test; --調(diào)用存儲(chǔ)過(guò)程
參考文章:mysql存儲(chǔ)過(guò)程學(xué)習(xí)筆記
到此這篇關(guān)于Mysql存儲(chǔ)過(guò)程、觸發(fā)器、事件調(diào)度器使用入門的文章就介紹到這了,更多相關(guān)Mysql存儲(chǔ)過(guò)程、觸發(fā)器、事件調(diào)度器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL中由load data語(yǔ)句引起死鎖的解決案例
這篇文章主要介紹了MySQL中由load data語(yǔ)句引起死鎖的解決案例,文中講到了InnoDB引擎的數(shù)據(jù)表中一些鎖的機(jī)制,需要的朋友可以參考下2016-01-01MySQL服務(wù)器 IO 100%的分析與優(yōu)化方案
這篇文章主要給大家介紹了關(guān)于MySQL服務(wù)器 IO 100%的相關(guān)資料,文中通過(guò)示例代碼介紹的介紹非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用mysql具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10MySQL?Workbench操作圖文詳解(史上最細(xì))
Workbench是MySQL最近釋放的可視數(shù)據(jù)庫(kù)設(shè)計(jì)工具,這個(gè)工具是設(shè)計(jì) MySQL數(shù)據(jù)庫(kù)的專用工具,下面這篇文章主要給大家介紹了關(guān)于MySQL?Workbench操作的相關(guān)資料,需要的朋友可以參考下2023-03-03關(guān)于mysql中的json解析函數(shù)JSON_EXTRACT
這篇文章主要介紹了關(guān)于mysql中的json解析函數(shù)JSON_EXTRACT講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07mysql 5.6 從陌生到熟練之_數(shù)據(jù)庫(kù)備份恢復(fù)的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇mysql 5.6 從陌生到熟練之_數(shù)據(jù)庫(kù)備份恢復(fù)的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10MySQL之MHA高可用配置及故障切換實(shí)現(xiàn)詳細(xì)部署步驟
這篇文章主要介紹了MySQL之MHA高可用配置及故障切換實(shí)現(xiàn)詳細(xì)部署步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03