MySQL存儲函數(shù)以及觸發(fā)器詳解
MySQL是一個關(guān)系型數(shù)據(jù)庫管理系統(tǒng),由瑞典MySQL AB 公司開發(fā),屬于 Oracle 旗下產(chǎn)品。MySQL 是最流行的關(guān)系型數(shù)據(jù)庫管理系統(tǒng)之一,在 WEB 應用方面,MySQL是最好的 RDBMS (Relational Database Management System,關(guān)系數(shù)據(jù)庫管理系統(tǒng)) 應用軟件之一。MySQL是一種關(guān)系型數(shù)據(jù)庫管理系統(tǒng),關(guān)系數(shù)據(jù)庫將數(shù)據(jù)保存在不同的表中,而不是將所有數(shù)據(jù)放在一個大倉庫內(nèi),這樣就增加了速度并提高了靈活性。MySQL所使用的 SQL 語言是用于訪問數(shù)據(jù)庫的最常用標準化語言。MySQL 軟件采用了雙授權(quán)政策,分為社區(qū)版和商業(yè)版,由于其體積小、速度快、總體擁有成本低,尤其是開放源碼這一特點,一般中小型和大型網(wǎng)站的開發(fā)都選擇 MySQL 作為網(wǎng)站數(shù)據(jù)庫。
SQL結(jié)構(gòu)化查詢語言(Structured Query Language)簡稱SQL,是一種特殊目的的編程語言,是一種數(shù)據(jù)庫查詢和程序設(shè)計語言,用于存取數(shù)據(jù)以及查詢、更新和管理關(guān)系數(shù)據(jù)庫系統(tǒng)。結(jié)構(gòu)化查詢語言是高級的非過程化編程語言,允許用戶在高層數(shù)據(jù)結(jié)構(gòu)上工作。它不要求用戶指定對數(shù)據(jù)的存放方法,也不需要用戶了解具體的數(shù)據(jù)存放方式,所以具有完全不同底層結(jié)構(gòu)的不同數(shù)據(jù)庫系統(tǒng), 可以使用相同的結(jié)構(gòu)化查詢語言作為數(shù)據(jù)輸入與管理的接口。結(jié)構(gòu)化查詢語言語句可以嵌套,這使它具有極大的靈活性和強大的功能。
存儲函數(shù)1). 介紹存儲函數(shù)是有返回值的存儲過程,存儲函數(shù)的參數(shù)只能是 IN 類型的。具體語法如下:
CREATE FUNCTION 存儲函數(shù)名稱 ([ 參數(shù)列表 ])RETURNS type [characteristic ...]BEGIN-- SQL語句RETURN ...;END ;characteristic 說明
DETERMINISTIC :相同的輸入?yún)?shù)總是產(chǎn)生相同的結(jié)果
NO SQL :不包含 SQL 語句。
READS SQL DATA :包含讀取數(shù)據(jù)的語句,但不包含寫入數(shù)據(jù)的語句。
2). 案例計算從 1 累加到 n 的值, n 為傳入的參數(shù)值。
create function fun1(n int)returns int deterministicbegindeclare total int default 0;while n>0 doset total := total + n;set n := n - 1;end while;return total;end;select fun1(50);在 mysql8.0 版本中 binlog 默認是開啟的,一旦開啟了, mysql 就要求在定義存儲過程時,需要指定characteristic 特性,否則就會報如下錯誤:
觸發(fā)器是與表有關(guān)的數(shù)據(jù)庫對象,指在 insert/update/delete 之前 (BEFORE) 或之后 (AFTER) ,觸發(fā)并執(zhí)行觸發(fā)器中定義的 SQL 語句集合。觸發(fā)器的這種特性可以協(xié)助應用在數(shù)據(jù)庫端確保數(shù)據(jù)的完整性, 日志記錄 , 數(shù)據(jù)校驗等操作 。
使用別名 OLD 和 NEW 來引用觸發(fā)器中發(fā)生變化的記錄內(nèi)容,這與其他的數(shù)據(jù)庫是相似的。現(xiàn)在觸發(fā)器還只支持行級觸發(fā),不支持語句級觸發(fā)。
通過觸發(fā)器記錄 tb_user 表的數(shù)據(jù)變更日志,將變更日志插入到日志表 user_logs 中 , 包含增加 ,
修改 , 刪除 ;
表結(jié)構(gòu)準備 :
-- 準備工作 : 日志表 user_logscreate table user_logs(id int(11) not null auto_increment,operation varchar(20) not null comment '操作類型, insert/update/delete',operate_time datetime not null comment '操作時間',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_triggerafter insert on tb_user for each rowbegininsert 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;測試:-- 查看show triggers ;-- 插入數(shù)據(jù)到tb_userinsert into tb_user(id, name, phone, email, profession, age, gender, status,createtime) VALUES (26,'三皇子','18809091212','[email protected]','軟件工程',23,'1','1',now());測試完畢之后,檢查日志表中的數(shù)據(jù)是否可以正常插入,以及插入數(shù)據(jù)的正確性。
B. 修改數(shù)據(jù)觸發(fā)器create trigger tb_user_update_triggerafter update on tb_user for each rowbegininsert 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;測試:-- 查看show triggers ;-- 更新update tb_user set profession = '會計' where id = 23;update tb_user set profession = '會計' where id <= 5;測試完畢之后,檢查日志表中的數(shù)據(jù)是否可以正常插入,以及插入數(shù)據(jù)的正確性。
C. 刪除數(shù)據(jù)觸發(fā)器create trigger tb_user_delete_triggerafter delete on tb_user for each rowbegininsert 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;測試:-- 查看show triggers ;-- 刪除數(shù)據(jù)delete from tb_user where id = 26;測試完畢之后,檢查日志表中的數(shù)據(jù)是否可以正常插入,以及插入數(shù)據(jù)的正確性。
到此這篇關(guān)于MySQL存儲函數(shù)以及觸發(fā)器詳解的文章就介紹到這了,更多相關(guān)MySQL存儲函數(shù)及觸發(fā)器內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
