stikom

stikom

Selasa, 20 September 2011

Stored Procedures, Functions dan Trigger

NIM/NAMA    : 10410100215/TRIO AGUSTINO
Dosen               : Tan Amelia
Tugas                : Resume Stored Procedures, Functions dan Trigger

STRORED PROSEDURES

Adalah program yang disimpan dalam data base seperti halnya data. Hal ini sebenanya cukup tidak umum, karena kita mengharapkan yang disimpan dalam data base adalah data bukan nya program.

Kemampuan utama SQL Server berada pada Store Procedure dan Fungsi (Fungsi hanya terdapat pada SQL Server 2000)
Dengan adanya Store Procedure, maka program SQL yang telah kita buat :
  • dapat digunakan kapan pun
  • lebih cepat dan efisien karena bersifat Server Side
  • mudah dibuat dan dirawat karena kecil tapi ‘Power Full’

Dapat digunakan kapanpun

Seperti halnya pembuatan prosedur pada C++ / Pascal / Java atau pemrograman yang lain, apabila pembuatan program bersifat modular (dibuat kecil untuk setiap maksud/tujuan), akan lebih baik apabila pemrograman tesebut menggunakan banyak prosedur. Dengan dibuat terpisah, kapanpun diinginkan, hanya tinggal memanggil program tersebut. 


TRIGGERS 
A Trigger is a named database object which defines some action that the database should take when some databases related event occurs. Triggers are executed when you issues a data manipulation command like INSERT, DELETE, UPDATE on a table for which the trigger has been created. They are automatically executed and also transparent to the user. But for creating the trigger the user must have the CREATE TRIGGER privilege. In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them.
CREATE TRIGGER
The general syntax of CREATE TRIGGER is :
  CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement
By using above statement we can create the new trigger. The trigger can associate only with the table name and that must be refer to a permanent table. Trigger_time means trigger action time. It can be BEFORE or AFTER. It is used to define that the trigger fires before or after the statement that executed it. Trigger_event specifies the statement that executes the trigger. The trigger_event can be any of the DML Statement : INSERT, UPDATE, DELETE.

We can not have the two trigger for a given table, which have the same trigger action time and event. For Instance : we cannot have two BEFORE INSERT triggers for same table. But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table.

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGIN?END compound statement.
We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword. OLD.column_name is used to refer the column of an existing row before it is deleted or updated and NEW.column_name is used to refer the column of a new row that is inserted or after updated existing row.

In INSERT trigger we can use only NEW.column_name because there is no old row and in a DELETE trigger we can use only OLD.column_name because there is no new row. But in UPDATE trigger we can use both, OLD.column_name is used to refer the columns of a row before it is updated and NEW.Column_name is used to refer the column of the row after it is updated.
In the following example we are updating the Salary column of Employee table before inserting any record in Emp table.

exemple:
mysql> CREATE TABLE account (acct_num INT, amount DECIMAL(10,2));
Query OK, 0 rows affected (0.03 sec)

mysql> CREATE TRIGGER ins_sum BEFORE INSERT ON account
    -> FOR EACH ROW SET @sum = @sum + NEW.amount;
Query OK, 0 rows affected (0.06 sec)
 
 

What's a Stored Function

If procedural programming is new to you, you may be wondering what the difference is between a Stored Procedure and a Stored Function. Not too much really. A function always returns a result, and can be called inside an SQL statement just like ordinary SQL functions. A function parameter is the equivalent of the IN procedure parameter, as functions use the RETURN keyword to determine what is passed back. Stored functions also have slightly more limitations in what SQL statements they can run than stored procedures.

A Stored Function example

Here is an example of a stored function:
mysql> DELIMITER |
mysql>
     CREATE FUNCTION WEIGHTED_AVERAGE (n1 INT, n2 INT, n3 INT, n4 INT)
       RETURNS INT
         DETERMINISTIC
           BEGIN
              DECLARE avg INT;
              SET avg = (n1+n2+n3*2+n4*4)/8;
              RETURN avg;
          END|
     Query OK, 0 rows affected (0.00 sec) mysql> SELECT WEIGHTED_AVERAGE(70,65,65,60)\G *************************** 1. row ***************************     WEIGHTED_AVERAGE(70,65,65,60): 63
                                1 row in set (0.00 sec)
As mentioned in the first stored procedures tutorial, we declare the "|" symbol as a delimiter, so that our function body can use ordinary ";" characters. This function returns a weighted average, as could be used to determine an overall result for a subject. The third test score is weighted twice as heavily as the first and second scores, while the fourth score counts four times as much. We also make use of the DECLARE (declaring a variable) and DETERMINISTIC (telling MySQL that, given the same input, the function will always return the same result) statements, as discussed in earlier tutorials.