Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

ORACLE Trigger Hasan Dwi Cahyono. Overviews  Pengertian Trigger  Schema Trigger  Aplikasi Trigger Perancangan Basis Data - Trigger2.

Presentasi serupa


Presentasi berjudul: "ORACLE Trigger Hasan Dwi Cahyono. Overviews  Pengertian Trigger  Schema Trigger  Aplikasi Trigger Perancangan Basis Data - Trigger2."— Transcript presentasi:

1 ORACLE Trigger Hasan Dwi Cahyono

2 Overviews  Pengertian Trigger  Schema Trigger  Aplikasi Trigger Perancangan Basis Data - Trigger2

3 Pengertian Trigger  Database triggers adalah prosedur yang tersimpan dalam database dan di aktifkan ketika kondisi tertentu terjadi  Dapat digunakan untuk menambah kapabilitas agar database mampu dikostumasi secara sistematis.  Sebagai contoh, membuat Trigger yang dapat membatasi perintah DML tertentu pada sebuah tabel dan mengijinkan perintah tertentu pada waktu tertentu juga. Perancangan Basis Data - Trigger3

4 Pengertian Trigger (Lanjutan)  Database triggers dapat digunakan dalam sebuah table, schema, atau database.  Cara memicunya ketika:  Perintah DML dieksekusi (INSERT, UPDATE, DELETE) terhadap tabel tertentu.  DDL tertentu dieksekusi (contohnya: ALTER, CREATE, DROP) pada sebuah objek pada database atau schema.  Event tertentu terjadi pada database (contohnya: STARTUP, SHUTDOWN, SERVERERROR) Perancangan Basis Data - Trigger4

5 Schema Trigger: Create Trigger CREATE [ OR REPLACE ] TRIGGER [ schema. ]trigger { BEFORE | AFTER | INSTEAD OF } { dml_event_clause | { ddl_event [ OR ddl_event ]... | database_event [ OR database_event ]... } ON { [ schema. ]SCHEMA | DATABASE } [ WHEN (condition) ] { pl/sql_block | call_procedure_statement } ; Perancangan Basis Data - Trigger5

6 Schema Trigger: Create Trigger (Lanj.) Perancangan Basis Data - Trigger6

7 Schema Trigger: DML Event Clause { DELETE | INSERT | UPDATE [ OF column [, column ]... ] } [ OR { DELETE | INSERT | UPDATE [ OF column [, column]... ] } ]... ON { [ schema. ]table | [ NESTED TABLE nested_table_column OF ] [ schema. ] view } [ referencing_clause ] [ FOR EACH ROW ] Perancangan Basis Data - Trigger7

8 Schema Trigger: DML Event Clause (Lanj.) Perancangan Basis Data - Trigger8

9 Schema Trigger: Referencing Clause REFERENCING { OLD [ AS ] old | NEW [ AS ] new | PARENT [ AS ] parent } [ OLD [ AS ] old | NEW [ AS ] new | PARENT [ AS ] parent ]... Perancangan Basis Data - Trigger9

10 Schema Trigger: Referencing Clause Perancangan Basis Data - Trigger10

11 Options: Event-nya  AFTER atau bisa juga BEFORE.  UPDATE ON bisa juga DELETE ON atau INSERT ON.  Dan UPDATE ON dapat juga UPDATE …OF… ON Perancangan Basis Data - Trigger11

12 Aplikasi Trigger (Buat tabel contoh) CREATE TABLE Project_tab ( Prj_level NUMBER, Projno NUMBER, Resp_dept NUMBER ); CREATE TABLE Emp_tab ( Empno NUMBER NOT NULL, Ename VARCHAR2(10), Job VARCHAR2(9), Mgr NUMBER(4), Hiredate DATE, Sal NUMBER(7,2), Comm NUMBER(7,2), Deptno NUMBER(2) NOT NULL ); CREATE TABLE Dept_tab ( Deptno NUMBER(2) NOT NULL, Dname VARCHAR2(14), Loc VARCHAR2(13), Mgr_no NUMBER, Dept_type NUMBER ); Perancangan Basis Data - Trigger12

13 Contoh Trigger Pada Tabel  Terjadi Ketika Insert, Update, dan Delete CREATE OR REPLACE TRIGGER Print_salary_changes BEFORE DELETE OR INSERT OR UPDATE ON Emp_tab FOR EACH ROW WHEN (new.Empno > 0) DECLARE sal_diff number; BEGIN sal_diff := :new.sal - :old.sal; dbms_output.put('Old salary: ' || :old.sal); dbms_output.put(' New salary: ' || :new.sal); dbms_output.put_line(' Difference ' || sal_diff); END; /  Lakukan Perintah: UPDATE Emp_tab SET sal = sal WHERE deptno = 10;  Apa yang terjadi? Perancangan Basis Data - Trigger13

14 Contoh Trigger Pada View CREATE OR REPLACE VIEW manager_info AS SELECT e.ename, e.empno, d.dept_type, d.deptno, p.prj_level, p.projno FROM Emp_tab e, Dept_tab d, Project_tab p WHERE e.empno = d.mgr_no AND d.deptno = p.resp_dept; Perancangan Basis Data - Trigger14

15 Contoh Trigger Pada View (Lanjutan) CREATE OR REPLACE TRIGGER manager_info_insert INSTEAD OF INSERT ON manager_info REFERENCING NEW AS n -- new manager information FOR EACH ROW DECLARE rowcnt number; BEGIN SELECT COUNT(*) INTO rowcnt FROM Emp_tab WHERE empno = :n.empno; IF rowcnt = 0 THEN INSERT INTO Emp_tab (empno,ename) VALUES (:n.empno, :n.ename); ELSE UPDATE Emp_tab SET Emp_tab.ename = :n.ename WHERE Emp_tab.empno = :n.empno; END IF; SELECT COUNT(*) INTO rowcnt FROM Dept_tab WHERE deptno = :n.deptno; IF rowcnt = 0 THEN INSERT INTO Dept_tab (deptno, dept_type) VALUES(:n.deptno, :n.dept_type); ELSE UPDATE Dept_tab SET Dept_tab.dept_type = :n.dept_type WHERE Dept_tab.deptno = :n.deptno; END IF; SELECT COUNT(*) INTO rowcnt FROM Project_tab WHERE Project_tab.projno = :n.projno; IF rowcnt = 0 THEN INSERT INTO Project_tab (projno, prj_level) VALUES(:n.projno, :n.prj_level); ELSE UPDATE Project_tab SET Project_tab.prj_level = :n.prj_level WHERE Project_tab.projno = :n.projno; END IF; END; Perancangan Basis Data - Trigger15

16 Contoh Trigger Pada View (Lanjutan) Perancangan Basis Data - Trigger16  Lakukan Perintah: INSERT INTO manager_info (ename, empno, dept_type, deptno, prj_level, projno) VALUES(‘Andi’, 101, 1, 1, 1,1);  Apa yang terjadi?

17 Firing Triggers Sekali atau Berkali-kali (FOR EACH ROW Option) CREATE TABLE Emp_log ( Emp_id NUMBER, Log_date DATE, New_salary NUMBER, Action VARCHAR2(20) ); CREATE OR REPLACE TRIGGER Log_salary_increase AFTER UPDATE ON Emp_tab FOR EACH ROW WHEN (new.Sal > 1000) BEGIN INSERT INTO Emp_log (Emp_id, Log_date, New_salary, Action) VALUES (:new.Empno, SYSDATE, :new.SAL, 'NEW SAL'); END; Lakukan perintah: UPDATE Emp_tab SET Sal = Sal WHERE Deptno = 20; Perancangan Basis Data - Trigger17

18 Trigger Ini Hanya Dijalankan Sekali CREATE OR REPLACE TRIGGER Log_emp_update AFTER UPDATE ON Emp_tab BEGIN INSERT INTO Emp_log (Log_date, Action) VALUES (SYSDATE, 'Emp_tab COMMISSIONS CHANGED'); END; Perancangan Basis Data - Trigger18

19 Memanggil Java Procedure dari Trigger CREATE OR REPLACE PROCEDURE Before_delete (Id IN NUMBER, Ename VARCHAR2) IS language Java name 'thjvTriggers.beforeDelete (oracle.sql.NUMBER, oracle.sql.CHAR)'; CREATE OR REPLACE TRIGGER Pre_del_trigger BEFORE DELETE ON Tab FOR EACH ROW CALL Before_delete (:old.Id, :old.Ename) / Perancangan Basis Data - Trigger19

20 thjvTriggers.java import java.sql.* import java.io.* import oracle.sql.* import oracle.oracore.* public class thjvTriggers { public state void beforeDelete (NUMBER old_id, CHAR old_name) Throws SQLException, CoreException { Connection conn = JDBCConnection.defaultConnection(); Statement stmt = conn.CreateStatement(); String sql = “insert into logtab values (“ + old_id.intValue() + ", '“ + old_ename.toString() + ", BEFORE DELETE'); stmt.executeUpdate (sql); stmt.close(); return; } Perancangan Basis Data - Trigger20

21 Latihan  Menggunakan tabel Penjual(nama, minuman, harga) dan sebuah relasi unary, simpan daftar penjual yang menaikkan harga sebesar Rp  Misal relasi Unary-nya kepada: KumpulanPenjual(nama). CREATE TABLE Penjual( minuman VARCHAR(10), nama VARCHAR(13), harga FLOAT ); CREATE TABLE KumpulanPenjual( nama VARCHAR(13) ); Perancangan Basis Data - Trigger21

22 Trigger CREATE OR REPLACE TRIGGER PriceTrig AFTER UPDATE OF price ON Penjual FOR EACH ROW WHEN (new.harga > old.harga ) BEGIN INSERT INTO KumpulanPenjual VALUES(:new.nama); END; / Perancangan Basis Data - Trigger22

23 Contoh Lainnya: CREATE TABLE emp ( empno INT, ename VARCHAR(30), deptno INT, sal FLOAT, comm FLOAT ); CREATE OR REPLACE TRIGGER emp_comm_trig BEFORE INSERT ON emp FOR EACH ROW BEGIN IF :NEW.deptno = 30 THEN :NEW.comm := :NEW.sal *.4; END IF; END; / Perancangan Basis Data - Trigger23

24 Latihan: INSERT INTO emp VALUES (9005,'ROBERS',30, 3000,NULL); INSERT INTO emp VALUES (9006,'ALLEN',30, 4500,NULL); SELECT * FROM emp WHERE empno IN (9005, 9006); EMPNOENAME DEPTNOSALCOMM ROBERS ALLEN Perancangan Basis Data - Trigger24

25 Trigger:Membatalkan Perintah dengan Error  Trigger dapat digunakan untuk melukan pengecekan contrain.  Perintah-nya: RAISE_APPLICATION_ERROR.  Perintah yang mengaktifkan Trigger (insert, update, atau delete) akan dibatalkan.  Contohnya, perintah berikut ini memaksa nilai input harus Person.age >= 0: CREATE TABLE Person (age INT); CREATE TRIGGER PersonCheckAge AFTER INSERT OR UPDATE OF age ON Person FOR EACH ROW BEGIN IF (:new.age < 0) THEN RAISE_APPLICATION_ERROR(-20000, 'no negative age allowed'); END IF; END; / Perancangan Basis Data - Trigger25

26 Statement-Level Trigger  Ketika operasi insert, update, atau delete operation terjadi pada tabel emp, sebuah record akan ditambahkan ke tabel empauditlog yang mencatat date, user, dan action.  Sebelumnya, buat tabel empauditlog table: CREATE TABLE empauditlog ( audit_date DATE, audit_user VARCHAR2(20), audit_desc VARCHAR2(20) ); Perancangan Basis Data - Trigger26

27 Trigger CREATE OR REPLACE TRIGGER emp_audit_trig AFTER INSERT OR UPDATE OR DELETE ON emp DECLARE v_action VARCHAR2(20); BEGIN IF INSERTING THEN v_action := 'Added employee(s)'; ELSEIF UPDATING THEN v_action := 'Updated employee(s)'; ELSEIF DELETING THEN v_action := 'Deleted employee(s)'; END IF; INSERT INTO empauditlog VALUES (SYSDATE, USER, v_action); END; / Perancangan Basis Data - Trigger27

28 Sekarang jalankan: INSERT INTO emp (empno, ename, deptno) VALUES (9001,'SMITH',50); INSERT INTO emp (empno, ename, deptno) VALUES (9002,'JONES',50); UPDATE emp SET ename = 'SMITH BROWN' WHERE empno=9001; DELETE FROM emp WHERE empno IN (9001, 9002); SELECT TO_CHAR(AUDIT_DATE,'DD-MON-YY HH24:MI:SS') AS "AUDIT DATE", audit_user, audit_desc FROM empauditlog ORDER BY 1 ASC; AUDIT DATE AUDIT_USER AUDIT_DESC FEB-08 09:43:02 THOMO Added employee(s) 08-FEB-08 09:43:02 THOMO Deleted employee(s) 08-FEB-08 09:43:02 THOMO Updated employee(s) 08-FEB-08 09:43:02 THOMO Added employee(s) Perancangan Basis Data - Trigger28

29 Hal-hal Lain dalam Trigger  Viewing Defined Triggers  Untuk melihat Trigger yang tersimpan, gunakan perintah: SELECT * FROM USER_TRIGGERS[/DBA_TRIGGERS ] WHERE TABLE_NAME = ‘MY_TABLE’;  Melihat isi (code) Trigger: SELECT text FROM user_source WHERE name = 'PRICETRIG' ORDER BY line;  Menghapus Triggers DROP TRIGGER ;  Disabling atau Enabling Triggers ALTER TRIGGER {DISABLE|ENABLE}; Harus UpperCase? Perancangan Basis Data - Trigger29

30 Sumber: Perancangan Basis Data - Trigger30  Oracle.com  009/triggers.ppt


Download ppt "ORACLE Trigger Hasan Dwi Cahyono. Overviews  Pengertian Trigger  Schema Trigger  Aplikasi Trigger Perancangan Basis Data - Trigger2."

Presentasi serupa


Iklan oleh Google