A comprehensive database of active and potentially-active ...
2.1 Active Database and Triggerv2
Transcript of 2.1 Active Database and Triggerv2
ACTIVE DATABASE AND TRIGGER2009 © Umi Laili Yuhana, S.Kom, M.Sc., Dini Adni, S.Kom, Nurul Fajrin, S.Kom, Wijayanti, S.Kom, Ridho Rahman, S.Kom
BDL Genap 2008
Teknik Informatika - ITS
Topik Minggu Ini
Pertemuan Ke-
Materi
1 PENDAHULUAN
Penjelasan Silabus Perkuliahan Review tentang basis data, data modelling
(cdm+pdm) Trend tentang database
2 ACTIVE DATABASE• Active dan passive database concept• Trigger
3 APLIKASI ACTIVE DATABASE• Integrity management• Derived data maintenance• Business rule• Penugasan tugas analisa case study dan
penerapan active database
Outline
Konsep pasive database dan active database
Model umum untuk active database dan trigger
Aplikasi-aplikasi untuk active database Desain dan isu implementasi untuk active
database Contoh statement-level active rule dalam
STARBURST, Oracle dan DB2 dan Chimera
Pasive Database
DB digunakan sebagai tempat penyimpan data (repositoris)
Database tidak bisa melakukan action tertentu ketika suatu event tertentu terjadi
Perintah (command) seperti query, update dan delete diekseskusi oleh database ketika di-request oleh pengguna atau program aplikasi
Active Database
DB dapat bereaksi dan memberikan respon tertentu ketika suatu kejadian terjadi
Merupakan sistem database yang didukung oleh fitur automatic execution pada saat kondisi tertentu atau kejadian tertentu terjadi
Menggunakan active rule (trigger) yang disebut dengan ECA rule.
ECA rule terdiri dari 3 komponen utama yaitu ECA (Event, Condition, and Action)
Model Umum untuk Active DB Event : kejadian-kejadian yang menjadi
pemicu (trigger), biasanya berupa operasi-operasi yang ekplisit ada dalam database atau dapat juga berupa temporal atau eksternal event
Condition: menentukan apakah rule action harus dieksekusi atau tidak
Action : tindakan yang dilakukan, biasanya berupa urutan statement SQL atau bisa juga berupa program eksternal yang dieksekusi secara otomatis (stored procedure)
Contoh aplikasi potensial dari active rule Notification
Monitoring system Enforce integrity constraints
GPA alert and course prerequisites Business rule
Salary of employee can’t exceed that of manager
Maintenance of derived data Maintain the derived attribute,
TOTAL_SAL
Desain dan isu implementasi untuk active database STARBURST
SYNTAXCREATE RULE <rule-name> ON <table-name>WHEN <triggering-operations>[IF <SQL-predicate>]THEN <SQL-statements>[PRECEDES <rule-names>][FOLLOWS <rule-names>] <triggering-operations> ::= INSERTED|DELETED|
UPDATED [(column-names)]
Desain dan isu implementasi untuk active database STARBURST
SEMANTICWhile the conflict set is not empty
1. select one rule R from the conflict set among those rules at highest priority; R’s state becomes untriggered
2. Evaluate the condition of R3. If the condition of R is TRUE, then execute
the action of R.
Contoh Active Rules Executions di Starburst
Database states:
CREATE RULE SalaryControl ON EmpWHEN INSERTED, DELETED, UPDATED (Sal)IF (SELECT AVG (Sal) FROM Emp) > 100THEN UPDATE Emp
SET Sal = .9 * Sal
Employee Sal
Stefano 90
Patrick 90
Michael 110
Insert (Rick, 150)
Insert (John, 120)
Employee Sal
Stefano 81
Patrick 81
Michael 99
Rick 135
John 108
Desain dan isu implementasi untuk active database ORACLE
SYNTAXCREATE TRIGGER<trigger-name> {BEFORE|AFTER} <trigger-events> ON <table-
name>[[REFERENCING <references>]FOR EACH ROW[WHEN (<condition>)]]<PL/SQL BLOCK> <trigger-events> ::= INSERT|DELETE|UPDATE
[OF (column-names)]<reference> ::= OLD AS <old-value-tuple-name>|
NEW AS <new-value-tuple-name>
Desain dan isu implementasi untuk active database ORACLE
SEMANTIC1. Execute the statement-level before triggers2. For each row in the target table :3. Perform statement-level referential integrity
and assertion checkinga. Execute the row-level before-triggersb. Perform the modification of the row and row-level
referential integrity and assertion checkingc. Execute the row-level after-triggers
4. Execute the statement-level after-triggers
Desain dan isu implementasi untuk active database TIPE TRIGGER PADA ORACLE
STATEMENT TRIGGER Dieksekusi hanya SATU KALI sebelum atau
setelah (BEFORE/AFTER) suatu kejadian yang berupa INSERT, UPDATE dan DELETE
ROW TRIGGER Dieksekusi SATU KALI untuk setiap baris (FOR
EACH ROW) sebelum atau setelah (BEFORE/AFTER) suatu kejadian yang berupa INSERT, UPDATE dan DELETE
Contoh active rule execution di ORACLE(Statement TRIGGER)
CREATE TRIGGER LogUserNilaiAFTER INSERT ON FRSBEGIN
INSERT INTO LogNilai VALUES (user, sysdate,‘INSERT’);
END;
NRP NILAI
5102100180 A
NM TGL KET
ARI 3/2/09 UPDATE
INSERT INTO FRS (‘5102100020’,’B’)
NRP NILAI
5102100180 A
5102100020 B
NM TGL KET
ARI 3/2/09 UPDATE
ANI 7/2/09 INSERT
Contoh active rule execution di ORACLE (Row TRIGGER)CREATE TRIGGER ReorderAFTER UPDATE of PartOnHand ON InventoryWHEN (:new.PartOnHand<:New.ReorderPoint)FOR EACH ROWDECLARE NUMBER X;BEGINSELECT COUNT (*) INTO X FROM PendingOrders WHERE Part=:new.Part;IF X=0THENINSERT INTO PendingOrders VALUES (:new.Part,:new.OrderQuantity,SYSDATE)END IF;END;
Contoh active rule execution di ORACLE (Row TRIGGER)
Part PartOnHand
ReorderPoint ReorderQuantity
1 200 150 100
2 780 500 200
3 450 400 120
TABEL INVENTORY
Part
OrderQuantity
TGL
TABLE PendingOrders
UPDATE INVENTORY SET PartOnHand= PartOnHand-70 WHERE Part=1
Part
PartOnHand
ReorderPoint ReorderQuantity
1 130 150 100
2 780 500 200
3 450 400 120
Part
OrderQuantity
TGL
1 100 3/2/2009
TABLE PendingOrders
TABEL INVENTORY
Contoh active rule execution di ORACLE (Row TRIGGER)
Part
PartOnHand
ReorderPoint
ReorderQuantity
1 200 150 100
2 780 500 200
3 450 400 120
TABEL INVENTORY
Part
OrderQuantity
TGL
TABLE PendingOrders
Part PartOnHand
ReorderPoint
ReorderQuantity
1 140 150 100
2 720 500 200
3 390 400 120
Part OrderQuantity
TGL
1 100 3/2/2009
3 120 3/2/2009
TABLE PendingOrders
TABEL INVENTORY
UPDATE INVENTORY SET PartOnHand= PartOnHand-60 WHERE Part>=1
Desain dan isu implementasi untuk active database MUTATING TABLE
tabel yang sedang dimodifikasi menggunakan statemen UPDATE, DELETE, atau INSERT,
table yang mungkin harus diupdate karena efek dari deklarasi referential integrity constraint seperti DELETE CASCADE
Contoh active rule execution di ORACLE (Mutating Table) ORACLE
MUTATING TABLECREATE OR REPLACE TRIGGER mhs_count
AFTER DELETE ON mhs
FOR EACH ROW
DECLARE n INTEGER;
BEGIN
SELECT COUNT(*) INTO n FROM mhs;
DBMS_OUTPUT.PUT_LINE(' Jumlah mhs = ' || n ||' orang.');
END;
Desain dan isu implementasi untuk active database DB2
SYNTAXCREATE TRIGGER<trigger-name> {BEFORE|AFTER} <trigger-event> ON <table-name>[REFERENCING <references>]FOR EACH {ROW|STATEMENT}WHEN (<SQL-condition>)<SQL procedure-statements><trigger-events> ::= INSERT|DELETE|UPDATE
[OF (column-names)]<reference> ::= OLD AS <old-value-tuple-name>|
NEW AS <new-value-tuple-name>|OLD_TABLE AS <old-value-table-name>|NEW_TABLE AS <new-value-table-name>
Desain dan isu implementasi untuk active database DB2
SEMANTIC1. Suspend the execution of A and save its working storage
on a stack2. Compute transition values (OLD and NEW) relative to
event E3. Consider and execute all before-triggers relative to event
E, possibly changing the NEW transition values4. Apply NEW transition value to the database, thus making
the state change associated to event E effective5. Consider and execute all after-triggers relative to event E.
if any of them contains an action Ai that activates other triggers then invoke this processing procedure recursively for Ai
6. Pop from the stack the working storage for A and continue its evaluation
Desain dan isu implementasi untuk active database DB2
SEMANTIC (revised step 4)4. Apply NEW transition values to the database, thus
making the state change associated to event E effective. For each integrity constraint IC violated by the current state, consider the action Aj that compensates the integrity constraint IC.
a. Compute the transition values (OLD and NEW) relative to Aj
b. Execute the before-triggers relative to Aj, possibly changing the NEW transition values
c. Apply NEW transition values to the database, thus making the state change associated to Aj effective
d. Push all after-triggers relative to action Aj into queue of suspended triggers
Contoh Active Rules Executions di DB2 Tabel: Part, Distributor, Audit Part, DEFAULT = ‘HDD’
Part Distributor
FOREIGN KEY (Supplier) REFERENCES Distributor ON DELETE SET DEFAULT
Distributor City State
Jones Palo Alto California
Taylor Minneapolis
Minnesota
HDD Atlanta Georgia
PartNum Supplier Cost
1 Jones 150
2 Taylor 500
3 HDD 400
4 Jones 800
Contoh Active Rules Executions di DB2 SUPPLIER RULE
*trigger di atas tidak menangani inputan data yg bernilai NULL pada Supplier
CREATE TRIGGER OneSupplier BEFORE UPDATE OF Supplier ON PartREFERENCING NEW AS NFOR EACH ROWWHEN (N.Supplier IS NULL) SIGNAL SQLSTATE ‘70005’ (‘Cannot change supplier into NULL’)
Contoh Active Rules Executions di DB2 AUDIT RULE
CREATE TRIGGER AuditSupplier AFTER UPDATE OF Supplier ON PartREFERENCING OLD_TABLE AS OTFOR EACH STATEMENT INSERT INTO Audit VALUES (USER, CURRENT_DATE, (SELECT COUNT(*) FROM OT)
user Bill melakukan transaksi pada October 10, 1996:
DELETE FROM DistributorWHERE State = ‘California’
User CurrentDate UpdatedTuples
Bill 1996-10-10 2
Audit
Desain dan isu implementasi untuk active database CHIMERA
SYNTAXDEFINE TRIGGER<trigger-name> [FOR <class-name>]EVENTS < triggering-events>CONDITION <formula>ACTIONS <procedural-expression>[{BEFORE|AFTER} <trigger-names>]END <triggering-event> ::= CREATE|DELETE|MODIFY
[<attr-name>]<option> ::= [<consumption-opt>][<execution-
opt>]<consumption-opt> ::= event-consuming|event-preserving<execution-opt> ::= deferred|immediate
Desain dan isu implementasi untuk active database CHIMERA
SEMANTICWhile the conflict set is not empty do:
1. select one trigger T from the conflict set among those rules at highest priority; T’s state becomes untriggered
2. Evaluate the condition of T3. If the condition of T is TRUE, then execute
the action of T
Contoh Active Rule Execution di CHIMERA Initial Schema
Employee
Name: string
Salary: integer
Mgr: Employee
SpecialEmp
Name: string
Department
Name: string
Employees: set-of(Employee)
Class Employee:
{(o1,’john’,3500,o2),
(o2,’tom’,4500,null),
(o3,’bob’,4300,o2)}
Class SpecialEmp:
{ }
Class Department:
{(o4,’toys’,{o1,o2,o3}) }
Contoh Active Rule Execution di CHIMERA
Transaksi
define trigger AdjustSalary for Employeeevents create, modify(Salary)condition Self.Salary > Self.Mgr.Salaryactions modify (Employee.Salary, Self, Self.Mgr.Salary)
end;begin transaction;select(X where employee(X), X.name=‘tom’),
modify(Employee.Salary,X,5000);select(X where employee(X),X.name=‘john’),
modify(Employee.Salary,X,5300);commit;
(o1,’john’,5000)
Contoh Soal Trigger
Ketika data dosen dihapus maka set nipwali mahasiswa menjadi null
Syntax :create trigger tr1before delete on dosenfor each rowbegin update mhs m set m.nipwali=null where
m.nipwali= :old.nip; end;