MySQL在5.0版後增強了資料庫stored procedure(5.0)與trigger(5.0.2)的功能, 根據MySQL自己的定義:

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database).

Correctly speaking, MySQL supports "routines" and there are two kinds of routines: stored procedures or functions.

也就是MySQL支援routines: stored procedure與functions, 而針對table的INSERT/UPDATE/DELETE動作,更加入了trigger

這幾個更新功能讓MySQL更具備大型資料庫的架勢, 以下就來談談如何測試這幾個功能:

(1)Stored Procedure

使用MySQL的stored procedure有幾個方式: dos command/phpmyadmin/EMS的MySQL Manager

首先以DOS command進入MySQL



接著可以打入 show procedure status; 來看看有無既存的stored procedures



現在我們進入使用test這個database (MySQL預設已經有的資料庫)

use test;
show tables;
desc tbl;

看到了tbl這個表格的結構 ...



現在我們建一個stored procedure

先使用宣告 delimiter 來告訴MySQL結束字元是 //

然後打入以下

Create procedure mytest1()
update tbl set f3=concat(f1,f2);



這樣去呼叫mytest1後, 就把f1,f2兩個字串串起來存到f3了 !

再來建立一個傳變數的stored procedure



(2)Trigger

首先看看表格結構: tbl與tbl2



再來建立trigger, 並看看效果
當有資料插入tbl時, 把f1欄位也插入到tbl2的f1欄位



看看我們剛建入的trigger




因此只要對於SQL有認識, 使用stored procedure與trigger應該不成問題...
但是頭痛的是...MySQL與MS SQL的語法又略有差異, 因此下次也來談談MS SQL, 以及為何要使用stored procedure及trigger ... 待續了


http://whateverusay.pixnet.net/blog/post/12731385

arrow
arrow
    全站熱搜

    天秤女~佳佳 發表在 痞客邦 留言(0) 人氣()