Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

Triggers & Stored Procedure The main reference of this presentation is from book: Peter Rob, Carlos Coronel, “Database systems: Design, Implementation.

Presentasi serupa


Presentasi berjudul: "Triggers & Stored Procedure The main reference of this presentation is from book: Peter Rob, Carlos Coronel, “Database systems: Design, Implementation."— Transcript presentasi:

1 Triggers & Stored Procedure The main reference of this presentation is from book: Peter Rob, Carlos Coronel, “Database systems: Design, Implementation and Management 4th ed”, Thomson Learning, Additional reference from Oracle course handout: Matthew P. Johnson CISDD, CUNY, January, 2005

2 Slide 5-2 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Outline Trigger Stored Procedure

3 Slide 5-3 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Triggers Trigger merupakan kode PL/SQL yang secara otomatis dijalankan oleh DBMS jika suatu event database terjadi Event tersebut bisa berupa operasi INSERT, UPDATE, DELETE

4 Slide 5-4 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Trigger …lanjut Sebuah trigger selalu dijalankan sebelum atau sesudah sebuah data row di-SELECT, di-INSERT atau di-UPDATE Khusus untuk oracle, trigger dapat diperlakukan bersamaan dengan statement. Sebuah trigger selalu berasosiasi dengan tabel pada basis data Setiap tabel bisa mempunyai satu atau lebih trigger Sebuah trigger dieksekusi sebagai bagian dari transaksi yang men-trigger trigger tsb.

5 Slide 5-5 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Kegunaan Trigger Trigger dapat digunakan untuk memaksakan constraint yang tidak dapat dilakukan pada perancangan dan implementasi DBMS Trigger dapat secara otomatis memberikan pesan warning jika terjadi gangguan pada IC. Penggunaan trigger yg umum adalah untuk meningkatkan referential IC Trigger dapat digunakan untuk update nilai pada tabel, insert tuple pada tabel, dan memanggil stored procedure yg lain

6 Slide 5-6 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Recommends Oracle… Oracle merekomendasikan trigger utk: Tujuan audit (membuat audit log) Secara otomatis men-generate nilai dari derived column Memaksakan constraint (business atau security) Membuat replikasi tabel untuk tujuan back-up

7 Slide 5-7 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Sintaks Trigger dalam Oracle CREATE [OR REPLACE] TRIGGER trigger name {BEFORE | AFTER | INSTEAD OF} {INSERT | DELETE | UPDATE | UPDATE OF column list} ON table name [FOR EACH ROW] [WHEN (...)] [DECLARE... ] BEGIN... executable statements... [EXCEPTION... ] END [trigger name]; CREATE [OR REPLACE] TRIGGER trigger name {BEFORE | AFTER | INSTEAD OF} {INSERT | DELETE | UPDATE | UPDATE OF column list} ON table name [FOR EACH ROW] [WHEN (...)] [DECLARE... ] BEGIN... executable statements... [EXCEPTION... ] END [trigger name];

8 Slide 5-8 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Penamaan pada Trigger Untuk tujuan dokumentasi, baiknya trigger diberikan nama yang merefleksikan tabel yang terkait dengan trigger tsb. Sebagai tambahan gunakan awalan ‘TRG’ di namanya: TRG_TABLE_NAME Contoh TRG_PRODUCT_REORDER, mengindikasikan bahwa tabel PRODUCT adalah source dari trigger REORDER

9 Slide 5-9 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 The PRODUCT Table

10 Slide 5-10 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 TRG_PRODUCT_REORDER Trigger TRG_PRODUCT_REORDER untuk mengevaluasi kuantitas product on-hand (P_ONHAND). Jika kuantitas on hand dibawah nilai minimum (P_MIN), maka nilai P_REORDER diset 1, yang artinya harus diorder.

11 Slide 5-11 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Verifying the TRG_PRODUCT_REORDER Trigger Execution

12 Slide 5-12 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 The P_REORDER Value Mismatch After Update of the P_MIN Attribute Jika kita mengubah P_MIN dari product “2232/QWE”, P_ONHAND < P_MIN, namun P_REORDER masih 0, Kenapa ?

13 Slide 5-13 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 TRG_PRODUCT_REORDER versi 2 CREATE OR REPLACE TRIGGER TRG_PRODUCT_REORDER AFTER INSERT OR UPDATE OF P_ONHAND, P_MIN on PRODUCT BEGIN UPDATE PRODUCT SET P_REORDER = 1 WHERE P_ONHAND <= P_MIN; END;

14 Slide 5-14 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Jika P_ONHAND pada “11QER/31” diupdate menjadi 29 sehingga nilainya lebih besar dari P_MIN, ternyata P_REORDER masih 1. Kenapa ?

15 Slide 5-15 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 TRG_PRODUCT_REORDER versi 3 CREATE OR REPLACE TRIGGER TRG_PRODUCT_REORDER AFTER INSERT OR UPDATE OF P_ONHAND, P_MIN on PRODUCT FOR EACH ROW BEGIN IF :NEW.P_ONHAND <= :NEW.P_MIN THEN :NEW.P_REORDER := 1 ELSE :NEW.P_REORDER := 0 END IF; END;

16 Slide 5-16 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Stored Procedure Sebuah stored procedure adalah kumpulan dari prosedur dan statement SQL yang terdapat pada DBMS. Dikenali dengan nama Dieksekusi sebagai sebuah sebuah kesatuan (unit) Di Oracle, dikenal dengan PL/SQL (Procedural Language/SQL)

17 Slide 5-17 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Keuntungan stored procedure Mengurangi network traffic dan meningkatkan performance Tidak ada transmisi individual SQL statements pada network

18 Slide 5-18 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Stored Procedure syntax CREATE OR REPLACE PROCEDURE procedure_name (argument IN/OUT data-type, etc) IS/AS BEGIN DECLARE variable name and data type PL/SQL or SQL statement END; DECLARE  digunakan untuk mendeklarasikan variabel yg digunakan dan tipe datanya Argumen menspesifikasikan parameter  IN (input) & OUT (output) Data type merupakan salah satu dari tipe data yang terdapat pada DBMS. Untuk mengeksekusinya: EXEC stored_procedure_name (parameter, parameter, …)

19 Slide 5-19 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Creating the PRC_PROD_DISCOUNT Stored Procedure

20 Slide 5-20 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Results of the PRC_PROD_DISCOUNT Stored Procedure

21 Slide 5-21 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 The PRC_CUS_ADD Stored Procedure

22 Slide 5-22 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 The PRC_INV_ADD and PRC_LINE_ADD Stored Procedures

23 Slide 5-23 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Testing the PRC_INV_ADD and PRC_LINE_ADD Procedures

24 Slide 5-24 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Stored Procedure diaktifkan secara eksplisit Trigger diaktifkan oleh event Source: adapted from Mullins, Trigger vs Stored Procedure


Download ppt "Triggers & Stored Procedure The main reference of this presentation is from book: Peter Rob, Carlos Coronel, “Database systems: Design, Implementation."

Presentasi serupa


Iklan oleh Google