最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MySQL存儲過程概念、原理與常見用法詳解

 更新時間:2019年07月05日 11:32:11   作者:webbc  
這篇文章主要介紹了MySQL存儲過程概念、原理與常見用法,結(jié)合實例形式詳細分析了mysql存儲過程的概念、原理、創(chuàng)建、刪除、調(diào)用等各種常用技巧與相關(guān)注意事項,需要的朋友可以參考下

本文實例講述了MySQL存儲過程概念、原理與常見用法。分享給大家供大家參考,具體如下:

1、存儲過程的概念

在一些語言中,如pascal,有一個概念叫“過程”procedure,和“函數(shù)”function,在php中,沒有過程,只有函數(shù)。

過程:封裝了若干條語句,調(diào)用時,這些封裝體執(zhí)行
函數(shù):是一個有返回值的“過程”
總結(jié):過程是一個沒有返回值的函數(shù)

在MySQL中:

我們把若干條sql封裝起來,起個名字 —— 過程
把此過程存儲在數(shù)據(jù)庫中 —— 存儲過程

2、創(chuàng)建存儲過程

create procedure procedureName()
begin
  //--sql 語句
end$

3、查看已有的存儲過程

show procedure status

4、刪除存儲過程

drop procedure procedureName;

5、調(diào)用存儲過程

call procedureName();

6、第一個存儲過程

注意:我這里已經(jīng)將MySQL的結(jié)束標識符改為$,如果要知道怎么設(shè)置為$,請參考我的另一篇文章:MySQL觸發(fā)器。

create procedure p1()
begin
  select 2+3;
end$

調(diào)用:

call p1();

顯示結(jié)果:

這里寫圖片描述

7、引入變量

存儲過程是可以編程的,意味著可以使用變量,表達式,控制結(jié)構(gòu)來完成復(fù)雜的功能,在存儲過程中,用declare聲明變量:

declare 變量名 變量類型 [default 默認值]

使用:

create procedure p2()
begin
  declare age int default 18;
  declare height int default 180;
  select concat('年齡:',age,'身高:',height);
end$

顯示結(jié)果:

這里寫圖片描述

8、引入表達式

存儲過程中,變量可以在sql語句中進行合法的運算,如+-*/。變量的賦值形式:

set 變量名:= expression

使用:

create procedure p3()
begin
  declare age int default 18;
  set age := age + 20;
  select concat('20年后年齡:',age);
end$

顯示結(jié)果:

這里寫圖片描述

9、引入選擇控制結(jié)構(gòu)

格式:

if condition then
  statement
elseif
  statement
else
  statement
end if;

使用:

create procedure p4()
begin
  declare age int default 18;
  if age >= 18 then
  select '已成年';
  else
  select '未成年';
  end if;
end$

顯示結(jié)果:

這里寫圖片描述

10、給存儲過程傳參

在定義存儲過程的括號中,可以聲明參數(shù),語法:

[in/out/inout] 參數(shù)名 參數(shù)類型

使用:

create procedure p5(width int,height int)
begin
  select concat('你的面積是:',width * height) as area;
  if width > height then
    select '你比較胖';
  elseif width < height then
    select '你比較瘦';
  else
  select '你比較方';
  end if;
end$

顯示結(jié)果:

這里寫圖片描述

11、使用while循環(huán)結(jié)構(gòu)

需求:從1加到100

使用:

create procedure p6()
begin
  declare total int default 0;
  declare num int default 0;
  while num <= 100 do
    set total := total + num;
    set num := num + 1;
  end while;
  select total;
end$

顯示結(jié)果:

這里寫圖片描述

12、存儲過程參數(shù)的輸入輸出類型

主要有in、out、inout三種類型
需求:從1加到N
輸入型的數(shù)據(jù)是我們給出值,輸出型是我們給出變量名,用于乘裝輸出的變量值。

(1)in型,此時in為輸入行參數(shù),它能接受到我們的輸入

create procedure p7(in n int)
begin
  declare total int default 0;
  declare num int default 0;
  while num <= n do
    set total := total + num;
    set num := num + 1;
  end while;
  select total;
end$

調(diào)用:

call p7(100);

輸出結(jié)果:

這里寫圖片描述

(2)out類型的參數(shù)

create procedure p8(in n int,out total int)
begin
  declare num int default 0;
  set total := 0;
  while num <= n do
    set total := total + num;
    set num := num + 1;
  end while;
end$

調(diào)用:

call p8(100,@total); --100為輸入型參數(shù),而@total為輸出型變量
select @total; --輸出@total變量

輸出結(jié)果:

這里寫圖片描述

(3)inout類型的參數(shù)

create procedure p9(inout age int)
begin
  set age := age+20;
end$

調(diào)用:

set @age = 18; --設(shè)置@age變量為18
call p9(@age); --調(diào)用p9存儲過程,@age變量為實參
select @age; --顯示@age變量

輸出結(jié)果:

這里寫圖片描述

inout型變量的實參也是一個變量名,這個變量在存儲過程中既作為輸入變量,又作為輸出變量。

13、case結(jié)構(gòu)的用法

使用:

create procedure p10()
begin
  declare pos int default 0;
  set pos := floor(5*rand());
  case pos
  when 1 then select '仍然在飛';
  when 2 then select '落在海里';
  when 3 then select '落在陸上';
  else select '我不知道在哪里';
  end case;
end$

輸出結(jié)果:

這里寫圖片描述

14、repeat循環(huán)結(jié)構(gòu)

格式:

[begin_label:] REPEAT
  statement_list
UNTIL search_condition
END REPEAT [end_label]

需求:從1加到100

create procedure p11()
begin
  declare total int default 0;
  declare num int default 0;
  r:repeat
    set total:= total + num;
  set num:=num + 1;
  until num > 100
  end repeat r;
  select total;
end$

輸出結(jié)果:

這里寫圖片描述

更多關(guān)于MySQL相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《MySQL存儲過程技巧大全》、《MySQL常用函數(shù)大匯總》、《MySQL日志操作技巧大全》、《MySQL事務(wù)操作技巧匯總》及《MySQL數(shù)據(jù)庫鎖相關(guān)技巧匯總

希望本文所述對大家MySQL數(shù)據(jù)庫計有所幫助。

相關(guān)文章

  • mysql中使用UDF自動同步memcached效率筆記

    mysql中使用UDF自動同步memcached效率筆記

    接上篇:mysql使用mysql-udf-http效率測試筆記 ,這次不使用rest架構(gòu),而是使用:libmemcached和memcached_functions_mysql
    2011-08-08
  • mysql臨時表插入數(shù)據(jù)方式

    mysql臨時表插入數(shù)據(jù)方式

    這篇文章主要介紹了mysql臨時表插入數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • mysql中null(IFNULL,COALESCE和NULLIF)相關(guān)知識點總結(jié)

    mysql中null(IFNULL,COALESCE和NULLIF)相關(guān)知識點總結(jié)

    這篇文章主要介紹了mysql中null(IFNULL,COALESCE和NULLIF)相關(guān)知識點,結(jié)合實例形式總結(jié)分析了mysql中關(guān)于null的判斷、使用相關(guān)操作技巧與注意事項,需要的朋友可以參考下
    2019-12-12
  • Mysql報錯too many connections的原因及解決方案

    Mysql報錯too many connections的原因及解決方案

    這篇文章主要給大家介紹了關(guān)于Mysql報錯too many connections原因及解決方案,文中通過實例代碼以及圖文介紹的非常詳細,需要的朋友可以參考下
    2023-09-09
  • 最新評論

    呼和浩特市| 绥化市| 宣城市| 台南市| 镇原县| 南召县| 宝丰县| 长子县| 西畴县| 探索| 双柏县| 贺兰县| 涡阳县| 兴和县| 旬阳县| 蒙山县| 麻城市| 潜山县| 安新县| 沙田区| 宜兰市| 长子县| 吴旗县| 中山市| 九江县| 股票| 文登市| 兴文县| 辽阳县| 哈密市| 正宁县| 靖江市| 大方县| 伊春市| 万源市| 敖汉旗| 靖远县| 曲麻莱县| 鄂州市| 岳普湖县| 云梦县|