MySQL開(kāi)發(fā)中存儲(chǔ)函數(shù)與觸發(fā)器使用示例
1、存儲(chǔ)函數(shù)
1.1、介紹
存儲(chǔ)函數(shù)是有返回值的存儲(chǔ)過(guò)程,存儲(chǔ)函數(shù)的參數(shù)只能是IN類(lèi)型的。具體語(yǔ)法如下:
CREATE FUNCTION 存儲(chǔ)函數(shù)名稱(chēng) ([ 參數(shù)列表 ]) RETURNS type [characteristic ...] BEGIN -- SQL語(yǔ)句 RETURN ...; END ;
characteristic說(shuō)明:
- DETERMINISTIC:相同的輸入?yún)?shù)總是產(chǎn)生相同的結(jié)果
- NO SQL :不包含 SQL 語(yǔ)句。
- READS SQL DATA:包含讀取數(shù)據(jù)的語(yǔ)句,但不包含寫(xiě)入數(shù)據(jù)的語(yǔ)句。
1.2、案例
計(jì)算從1累加到n的值,n為傳入的參數(shù)值。
create function fun1(n int) returns int deterministic begin declare total int default 0; while n>0 do set total := total + n; set n := n - 1; end while; return total; end; select fun1(50);
在mysql8.0版本中binlog默認(rèn)是開(kāi)啟的,一旦開(kāi)啟了,mysql就要求在定義存儲(chǔ)過(guò)程時(shí),需要指定 characteristic特性,否則就會(huì)報(bào)如下錯(cuò)誤:
2、觸發(fā)器
2.1、介紹
觸發(fā)器是與表有關(guān)的數(shù)據(jù)庫(kù)對(duì)象,指在insert/update/delete之前(BEFORE)或之后(AFTER),觸 發(fā)并執(zhí)行觸發(fā)器中定義的SQL語(yǔ)句集合。觸發(fā)器的這種特性可以協(xié)助應(yīng)用在數(shù)據(jù)庫(kù)端確保數(shù)據(jù)的完整性 , 日志記錄 , 數(shù)據(jù)校驗(yàn)等操作 。 使用別名OLD和NEW來(lái)引用觸發(fā)器中發(fā)生變化的記錄內(nèi)容,這與其他的數(shù)據(jù)庫(kù)是相似的?,F(xiàn)在觸發(fā)器還 只支持行級(jí)觸發(fā),不支持語(yǔ)句級(jí)觸發(fā)。
2.2、語(yǔ)法
觸發(fā)器類(lèi)型 | NEW 和 OLD |
INSERT 型觸發(fā)器 | NEW 表示將要或者已經(jīng)新增的數(shù)據(jù) |
UPDATE 型觸發(fā)器 | OLD 表示修改之前的數(shù)據(jù) , NEW 表示將要或已經(jīng)修改后的數(shù)據(jù) |
DELETE 型觸發(fā)器 | OLD 表示將要或者已經(jīng)刪除的數(shù)據(jù) |
1). 創(chuàng)建
CREATE TRIGGER trigger_name BEFORE/AFTER INSERT/UPDATE/DELETE ON tbl_name FOR EACH ROW -- 行級(jí)觸發(fā)器 BEGIN trigger_stmt ; END;
2). 查看
SHOW TRIGGERS ;
3). 刪除
DROP TRIGGER [schema_name.]trigger_name ; -- 如果沒(méi)有指定 schema_name,默認(rèn)為當(dāng)前數(shù)據(jù)庫(kù) 。
2.3、案例
通過(guò)觸發(fā)器記錄 tb_user 表的數(shù)據(jù)變更日志,將變更日志插入到日志表user_logs中, 包含增加, 修改 , 刪除 ; 表結(jié)構(gòu)準(zhǔn)備:
-- 準(zhǔn)備工作 : 日志表 user_logs create table user_logs( id int(11) not null auto_increment, operation varchar(20) not null comment '操作類(lèi)型, insert/update/delete', operate_time datetime not null comment '操作時(shí)間', operate_id int(11) not null comment '操作的ID', operate_params varchar(500) comment '操作參數(shù)', primary key(`id`) )engine=innodb default charset=utf8;
A. 插入數(shù)據(jù)觸發(fā)器
create trigger tb_user_insert_trigger after insert on tb_user for each row begin insert into user_logs(id, operation, operate_time, operate_id, operate_params) VALUES (null, 'insert', now(), new.id, concat('插入的數(shù)據(jù)內(nèi)容為: id=',new.id,',name=',new.name, ', phone=', NEW.phone, ', email=', NEW.email, ', profession=', NEW.profession)); end;
測(cè)試:
-- 查看 show triggers ; -- 插入數(shù)據(jù)到tb_user insert into tb_user(id, name, phone, email, profession, age, gender, status, createtime) VALUES (26,'三皇子','18809091212','erhuangzi@163.com','軟件工 程',23,'1','1',now());
測(cè)試完畢之后,檢查日志表中的數(shù)據(jù)是否可以正常插入,以及插入數(shù)據(jù)的正確性。 B. 修改數(shù)據(jù)觸發(fā)器
create trigger tb_user_update_trigger after update on tb_user for each row begin insert into user_logs(id, operation, operate_time, operate_id, operate_params) VALUES (null, 'update', now(), new.id, concat('更新之前的數(shù)據(jù): id=',old.id,',name=',old.name, ', phone=', old.phone, ', email=', old.email, ', profession=', old.profession, ' | 更新之后的數(shù)據(jù): id=',new.id,',name=',new.name, ', phone=', NEW.phone, ', email=', NEW.email, ', profession=', NEW.profession)); end;
測(cè)試:
-- 查看 show triggers ; -- 更新 update tb_user set profession = '會(huì)計(jì)' where id = 23; update tb_user set profession = '會(huì)計(jì)' where id <= 5;
測(cè)試完畢之后,檢查日志表中的數(shù)據(jù)是否可以正常插入,以及插入數(shù)據(jù)的正確性。 C. 刪除數(shù)據(jù)觸發(fā)器
create trigger tb_user_delete_trigger after delete on tb_user for each row begin insert into user_logs(id, operation, operate_time, operate_id, operate_params) VALUES (null, 'delete', now(), old.id, concat('刪除之前的數(shù)據(jù): id=',old.id,',name=',old.name, ', phone=', old.phone, ', email=', old.email, ', profession=', old.profession)); end;
測(cè)試:
-- 查看 show triggers ; -- 刪除數(shù)據(jù) delete from tb_user where id = 26;
測(cè)試完畢之后,檢查日志表中的數(shù)據(jù)是否可以正常插入,以及插入數(shù)據(jù)的正確性。
到此這篇關(guān)于MySQL開(kāi)發(fā)中存儲(chǔ)函數(shù)與觸發(fā)器使用示例的文章就介紹到這了,更多相關(guān)MySQL存儲(chǔ)函數(shù)與觸發(fā)器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mysql制作外鍵出現(xiàn)duplicate?key?name錯(cuò)誤問(wèn)題及解決
這篇文章主要介紹了mysql制作外鍵出現(xiàn)duplicate?key?name錯(cuò)誤問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02Mysql limit 優(yōu)化,百萬(wàn)至千萬(wàn)級(jí)快速分頁(yè) 復(fù)合索引的引用并應(yīng)用于輕量級(jí)框架
MySql 性能到底能有多高?用了php半年多,真正如此深入的去思考這個(gè)問(wèn)題還是從前天開(kāi)始。有過(guò)痛苦有過(guò)絕望,到現(xiàn)在充滿(mǎn)信心!2011-06-06MySQL中NULLIF?、IFNULL、IF的用法和區(qū)別舉例詳解
這篇文章主要給大家介紹了關(guān)于MySQL中NULLIF?、IFNULL、IF的用法和區(qū)別的相關(guān)資料,nullif和ifnull都是MySQL中用于處理NULL值的函數(shù),但它們的用法和作用略有不同,下面給大家詳細(xì)介紹下,需要的朋友可以參考下2024-05-05Mysql?innoDB修改自增id起始數(shù)的方法步驟
本文主要介紹了Mysql?innoDB修改自增id起始數(shù)的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧<BR>2023-03-03AlmaLinux 9 安裝 MySQL 8.0.32的詳細(xì)過(guò)程
這篇文章主要介紹了AlmaLinux 9 安裝 MySQL 8.0.32的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04Windows下MySQL 5.7無(wú)法啟動(dòng)的解決方法
從網(wǎng)上下了5.7 的MySQL,在bin目錄下執(zhí)行 start mysqld ,彈出個(gè)cmd窗口一閃就沒(méi)了,也看不清是什么報(bào)錯(cuò)。mysqld --install安裝了服務(wù),也啟動(dòng)不了,下面通過(guò)本文給大家分享下解決辦法2016-12-12