mysql存儲過程原理與使用方法詳解
本文實(shí)例講述了mysql存儲過程原理與使用方法。分享給大家供大家參考,具體如下:
存儲過程包含了一系列可執(zhí)行的sql語句,存儲過程存放于MySQL中,通過調(diào)用它的名字可以執(zhí)行其內(nèi)部的一堆sql
存儲過程的優(yōu)點(diǎn)
#1. 用于替代程序?qū)懙腟QL語句,實(shí)現(xiàn)程序與sql解耦
#2. 可以通過直接修改存儲過程的方式修改業(yè)務(wù)邏輯(或bug),而不用重啟服務(wù)器
#3. 執(zhí)行速度快,存儲過程經(jīng)過編譯之后會比單獨(dú)一條一條執(zhí)行要快
#4. 減少網(wǎng)絡(luò)傳輸,尤其是在高并發(fā)情況下這點(diǎn)優(yōu)勢大,存儲過程直接就在數(shù)據(jù)庫服務(wù)器上跑,所有的數(shù)據(jù)訪問都在服務(wù)器內(nèi)部進(jìn)行,不需要傳輸數(shù)據(jù)到其它終端。
存儲過程的缺點(diǎn)
1.SQL本身是一種結(jié)構(gòu)化查詢語言,加上了一些控制(賦值、循環(huán)和異常處理等),但不是OO的,本質(zhì)上還是過程化的,面對復(fù)雜的業(yè)務(wù)邏輯,過程化的處理會很吃力。這一點(diǎn)算致命傷,即只能應(yīng)用在邏輯簡單的業(yè)務(wù)上。
2.不便于調(diào)試?;旧蠜]有較好的調(diào)試器,很多時候是用print來調(diào)試,但用這種方法調(diào)試長達(dá)數(shù)百行的存儲過程簡直是噩夢。好吧,這一點(diǎn)不算啥,C#/java一樣能寫出噩夢般的代碼。
3.沒辦法應(yīng)用緩存。雖然有全局臨時表之類的方法可以做緩存,但同樣加重了數(shù)據(jù)庫的負(fù)擔(dān)。如果緩存并發(fā)嚴(yán)重,經(jīng)常要加鎖,那效率實(shí)在堪憂。
4.無法適應(yīng)數(shù)據(jù)庫的切割(水平或垂直切割)。數(shù)據(jù)庫切割之后,存儲過程并不清楚數(shù)據(jù)存儲在哪個數(shù)據(jù)庫中。
無參的存儲過程
delimiter //
create procedure p1()
BEGIN
select * from blog;
INSERT into blog(name,sub_time) values("xxx",now());
END //
delimiter ;
#在mysql中調(diào)用 call p1()
#在python中基于pymysql調(diào)用
cursor.callproc('p1')
print(cursor.fetchall())
有參的存儲過程
對于存儲過程,可以接收參數(shù),其參數(shù)有三類:
#in 僅用于傳入?yún)?shù)用
#out 僅用于返回值用
#inout 既可以傳入又可以當(dāng)作返回值
帶in的存儲過程
mysql> select * from emp; +----+----------+-----+--------+ | id | name | age | dep_id | +----+----------+-----+--------+ | 1 | zhangsan | 18 | 1 | | 2 | lisi | 19 | 1 | | 3 | egon | 20 | 2 | | 5 | alex | 18 | 2 | +----+----------+-----+--------+ 4 rows in set (0.30 sec) mysql> delimiter // mysql> create procedure p2(in n1 int, in n2 int) -> begin -> select * from emp where id >n1 and id <n2; -> end // Query OK, 0 rows affected (0.28 sec) mysql> delimiter ; mysql> call p2(1,3) -> ; +----+------+-----+--------+ | id | name | age | dep_id | +----+------+-----+--------+ | 2 | lisi | 19 | 1 | +----+------+-----+--------+ 1 row in set (0.07 sec) Query OK, 0 rows affected (0.07 sec)
#在python中基于pymysql調(diào)用
cursor.callproc('p2',(1,3))
print(cursor.fetchall())
帶有out
mysql> delimiter // mysql> create procedure p3( in n1 int, out res int) -> begin -> select * from emp where id >n1; -> set res=1; -> end // Query OK, 0 rows affected (0.28 sec) mysql> delimiter ; mysql> set @res=0; Query OK, 0 rows affected (0.00 sec) mysql> call p3(3,@res); +----+------+-----+--------+ | id | name | age | dep_id | +----+------+-----+--------+ | 5 | alex | 18 | 2 | +----+------+-----+--------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.01 sec) mysql> select @res; +------+ | @res | +------+ | 1 | +------+ 1 row in set (0.00 sec)
#在python中基于pymysql調(diào)用
cursor.callproc('p3',(3,0)) #0相當(dāng)于set @res=0
print(cursor.fetchall()) #查詢select的查詢結(jié)果
cursor.execute('select @_p3_0,@_p3_1;') #@p3_0代表第一個參數(shù),@p3_1代表第二個參數(shù),即返回值
print(cursor.fetchall())
帶有inout的例子
delimiter // create procedure p4( inout n1 int ) BEGIN select * from blog where id > n1; set n1 = 1; END // delimiter ;
#在mysql中調(diào)用 set @x=3; call p4(@x); select @x;
#在python中基于pymysql調(diào)用
cursor.callproc('p4',(3,))
print(cursor.fetchall()) #查詢select的查詢結(jié)果
cursor.execute('select @_p4_0;')
print(cursor.fetchall())
事務(wù)
#介紹
delimiter //
create procedure p4(
out status int
)
BEGIN
1. 聲明如果出現(xiàn)異常則執(zhí)行{
set status = 1;
rollback;
}
開始事務(wù)
-- 由秦兵賬戶減去100
-- 方少偉賬戶加90
-- 張根賬戶加10
commit;
結(jié)束
set status = 2;
END //
delimiter ;
#實(shí)現(xiàn)
delimiter //
create PROCEDURE p5(
OUT p_return_code tinyint
)
BEGIN
DECLARE exit handler for sqlexception
BEGIN
-- ERROR
set p_return_code = 1;
rollback;
END;
DECLARE exit handler for sqlwarning
BEGIN
-- WARNING
set p_return_code = 2;
rollback;
END;
START TRANSACTION;
DELETE from tb1; #執(zhí)行失敗
insert into blog(name,sub_time) values('yyy',now());
COMMIT;
-- SUCCESS
set p_return_code = 0; #0代表執(zhí)行成功
END //
delimiter ;
#在mysql中調(diào)用存儲過程 set @res=123; call p5(@res); select @res;
#在python中基于pymysql調(diào)用存儲過程
cursor.callproc('p5',(123,))
print(cursor.fetchall()) #查詢select的查詢結(jié)果
cursor.execute('select @_p5_0;')
print(cursor.fetchall())
存儲過程的執(zhí)行
mysql中執(zhí)行
-- 無參數(shù) call proc_name() -- 有參數(shù),全in call proc_name(1,2) -- 有參數(shù),有in,out,inout set @t1=0; set @t2=3; call proc_name(1,2,@t1,@t2)
pymsql中執(zhí)行
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import pymysql
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
# 執(zhí)行存儲過程
cursor.callproc('p1', args=(1, 22, 3, 4))
# 獲取執(zhí)行完存儲的參數(shù)
cursor.execute("select @_p1_0,@_p1_1,@_p1_2,@_p1_3")
result = cursor.fetchall()
conn.commit()
cursor.close()
conn.close()
print(result)
刪除存儲過程
drop procedure proc_name;
更多關(guān)于MySQL相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《MySQL存儲過程技巧大全》、《MySQL常用函數(shù)大匯總》、《MySQL日志操作技巧大全》、《MySQL事務(wù)操作技巧匯總》及《MySQL數(shù)據(jù)庫鎖相關(guān)技巧匯總》
希望本文所述對大家MySQL數(shù)據(jù)庫計有所幫助。
- MySQL中的if和case語句使用總結(jié)
- mysql存儲過程之游標(biāo)(DECLARE)原理與用法詳解
- mysql存儲過程之返回多個值的方法示例
- mysql存儲過程之創(chuàng)建(CREATE PROCEDURE)和調(diào)用(CALL)及變量創(chuàng)建(DECLARE)和賦值(SET)操作方法
- mysql存儲過程之引發(fā)存儲過程中的錯誤條件(SIGNAL和RESIGNAL語句)實(shí)例分析
- mysql存儲過程之錯誤處理實(shí)例詳解
- mysql 存儲過程中變量的定義與賦值操作
- mysql存儲過程 游標(biāo) 循環(huán)使用介紹
- MySQL存儲過程例子(包含事務(wù),輸出參數(shù),嵌套調(diào)用)
- MySql存儲過程與函數(shù)詳解
- mysql存儲過程之if語句用法實(shí)例詳解
相關(guān)文章
MySQL如何導(dǎo)入SQL數(shù)據(jù)庫的實(shí)戰(zhàn)舉例
在使用mysql數(shù)據(jù)庫是,經(jīng)常需要備份或者恢復(fù)數(shù)據(jù)庫數(shù)據(jù),最便捷的方式就是通過導(dǎo)出sql文件備份和直接執(zhí)行sql文件恢復(fù),下面這篇文章主要給大家介紹了關(guān)于MySQL如何導(dǎo)入SQL數(shù)據(jù)庫的相關(guān)資料,需要的朋友可以參考下2023-05-05
MySQL實(shí)戰(zhàn)窗口函數(shù)SQL分析班級學(xué)生考試成績及生活消費(fèi)
這篇文章主要為大家介紹了MySQL實(shí)戰(zhàn),利用窗口函數(shù)SQL來分析班級學(xué)生的考試成績及生活消費(fèi)的示例過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10
學(xué)習(xí)mysql之后的一點(diǎn)總結(jié)(基礎(chǔ))
學(xué)習(xí)mysql之后的一點(diǎn)總結(jié),比較適合新手朋友2012-05-05
基于mysql實(shí)現(xiàn)group by取各分組最新一條數(shù)據(jù)
這篇文章主要介紹了基于mysql實(shí)現(xiàn)group by取各分組最新一條數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09

