ORACLE Trigger Hasan Dwi Cahyono.

Slides:



Advertisements
Presentasi serupa
Antonius Wahyu Sudrajat, S. Kom., M.T.I. Trigger.
Advertisements

SQL – DML.
MATA KULIAH : “LOGIKA DAN ALGORITMA”
KUMPULAN PERINTAH SQL PENDY.
Pertemuan 2 : Dasar-dasar SQL SBD C – Senin, Frank & Rudy
SISTEM BASIS DATA INTRO-5. •Setelah mengikuti perkuliahan ini diharapkan mahasiswa dapat mengerti : 1.Aplikasi perintah SQL ke MySql Server 2.Mengerti.
Error Handling (Exception) & INDEX
PERINTAH SQL.
Dasar-dasar SQL soesanto.
ORACLE Trigger Hasan Dwi Cahyono.
Database Create-Retrieve-Update-Delete (CRUD)
DDL & Aturan Referential
PEMROGRAMAN BASIS DATA
T R I G G E R.
SQL: Queries II, Constraints & Triggers (Chap. 5 – Ramakrishnan)
DML Lanjutan Pertemuan Minggu Ke-10.
SISTEM BASISDATA Lasmedi Afuan, ST.,M.Cs. SQL (S RUCTURE Q UERY L ANGUAGE ) Query/SQL : Bahasa standar yang digunakan untuk mengakses basisdata. Standar.
Manajemen Basis Data menggunakan SQL Server
TRIGGER.
Triggers & Stored Procedure The main reference of this presentation is from book: Peter Rob, Carlos Coronel, “Database systems: Design, Implementation.
CHANGE DATA CAPTURE.
DDL & Aturan Referential
Praktek Pemograman Client-Server
STORED PROCEDURE Achmad Yasid, SKom.
TRIGGER Siti Mukaromah, S.Kom.
TRIGGER.
Data Definition Language dan Data Manipulation Language
Review DDL Pertemuan ke 2.
Triggers & Stored Procedure The main reference of this presentation is from book: Peter Rob, Carlos Coronel, “Database systems: Design, Implementation.
Data Types Data Definition Language Referential Constraint SQL Query
SQL (Structure Query Language)
BAHASA QUERY TERAPAN OLEH : HARSITI, ST.
STRUCTURED QUERY LANGUAGE (SQL)
DATA DEFINITION LANGUAGE
Cursor MI2163 Dasar Pemrograman Basis Data. Introduction Cursor merupakan suatu variabel yang digunakan untuk menampung hasil query yang terdiri atas.
Dasar query basis data dengan SQLite
HANDLING EXCEPTIONS.
C U R S O R.
TRIGGER LANJUT.
TRIGGER (Statement & Row Trigger)
Pertemuan after UTS Structure Query Language (SQL)
TRIGGER SYAIFUL HUDA, S.Kom.
FUNGSI-FUNGSI AKSES MySql
DDL, DML.
SQL (Structured Query Language)
STORED PROCEDURE & TRIGGER
SQL.
Bahasa query terpan.
SQL (Structure Query Language)
View dan Trigger Materi 5
SQL (Structure Query Language)
Manajemen Basis Data menggunakan SQL Server
Data Manipulation Languange (DML) Perintah INSERT dan DELETE
Konsep Teknologi Informasi B
Stucture Query Language
SQL Basis Data.
Praktikum Berkas dan Basis Data
TRIGGER.
SQL OVERVIEW.
Created By Amir Ali,S.Kom.,M.Kom
BAB ENHANCED DATA MODELS FOR ADVANCED APPLICATIONS
CURSOR.
Oleh : FIRMAN JAYA, S.Pt, M.Kom
Stucture Query Language
Integritas Basis Data Triggers, Store Procedure
Referensi Bahasa MySQL
Stucture Query Language
12/6/2018 TRIGGER Pemrograman Basis Data Siti Mukaromah, S.Kom.
ORACLE PL/SQL TRIGGER Program Studi D3 Teknik Informatika -Fakultas Ilmu Terapan.
10/14/2019 TRIGGER Pemrograman Basis Data.
Transcript presentasi:

ORACLE Trigger Hasan Dwi Cahyono

Overviews Pengertian Trigger Schema Trigger Aplikasi Trigger Perancangan Basis Data - Trigger

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 - Trigger

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 - Trigger

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 - Trigger

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

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 - Trigger

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

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

Schema Trigger: Referencing Clause Perancangan Basis Data - Trigger

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 - Trigger

Aplikasi Trigger (Buat tabel contoh) CREATE TABLE Dept_tab ( Deptno NUMBER(2) NOT NULL, Dname VARCHAR2(14), Loc VARCHAR2(13), Mgr_no NUMBER, Dept_type NUMBER ); 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 Perancangan Basis Data - Trigger

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 + 500.00 WHERE deptno = 10; Apa yang terjadi? Perancangan Basis Data - Trigger

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 - Trigger

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; FROM Project_tab WHERE Project_tab.projno = :n.projno; INSERT INTO Project_tab (projno, prj_level) VALUES(:n.projno, :n.prj_level); UPDATE Project_tab SET Project_tab.prj_level = :n.prj_level END; Perancangan Basis Data - Trigger

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

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 + 1000.0 WHERE Deptno = 20; Perancangan Basis Data - Trigger

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 - Trigger

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 - Trigger

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 - Trigger

Latihan Menggunakan tabel Penjual(nama, minuman, harga) dan sebuah relasi unary, simpan daftar penjual yang menaikkan harga sebesar Rp.1000. 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 - Trigger

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

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 - Trigger

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); EMPNO ENAME DEPTNO SAL COMM ----------------------------------- 9005 ROBERS 30 3000 1200 9006 ALLEN 30 4500 1800 Perancangan Basis Data - Trigger

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 - Trigger

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 - Trigger

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 - Trigger

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 ------------------ -------------------- -------------------- 08-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) Perancangan Basis Data - Trigger

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 <trigger_name>; Disabling atau Enabling Triggers ALTER TRIGGER <trigger_name> {DISABLE|ENABLE}; Harus UpperCase? Perancangan Basis Data - Trigger

Sumber: Oracle.com http://webhome.cs.uvic.ca/~thomo/courses/csc370Spring2 009/triggers.ppt Perancangan Basis Data - Trigger