2.1 Active Database and Triggerv2
2.1 Active Database and Triggerv2
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 direquest 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)
Event : kejadian-kejadian yang menjadi pemicu (trigger), biasanya berupa operasioperasi 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)
Notification
Monitoring system GPA alert and course prerequisites Salary of employee cant exceed that of manager Maintain the derived attribute, TOTAL_SAL
Business rule
STARBURST
SYNTAX CREATE 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 [(columnnames)]
STARBURST
SEMANTIC
While the conflict set is not empty 1. select one rule R from the conflict set among those rules at highest priority; Rs state becomes untriggered 2. Evaluate the condition of R 3. If the condition of R is TRUE, then execute the action of R.
Database states:
Employee Stefano Patrick Michael Sal 90 90 110
John
108
ORACLE
SYNTAX CREATE TRIGGER<trigger-name> {BEFORE|AFTER} <trigger-events> ON <tablename> [[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>
ORACLE
SEMANTIC 1. Execute the statement-level before triggers 2. For each row in the target table : 3. Perform statement-level referential integrity and assertion checking
a. b.
c.
Execute the row-level before-triggers Perform the modification of the row and row-level referential integrity and assertion checking Execute the row-level after-triggers
4.
TRIGGER
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
5102100020
TGL 3/2/09 7/2/09
B
KET UPDATE INSERT
TABLE PendingOrders
OrderQuantity TGL
UPDATE
TABEL INVENTORY
Part 1 PartOnHand 130 ReorderPoint 150 ReorderQuantity 100
2
3
780
450
500
400
200
120
TABLE PendingOrders
Part OrderQuantity TGL
UPDATE
TABEL INVENTORY
Part 1 2 3 PartOnHand 140 720 390 ReorderPoint 150 500 400 ReorderQuantity 100 200 120
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
DB2
SYNTAX CREATE 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>
DB2
SEMANTIC 1. Suspend the execution of A and save its working storage on a stack 2. Compute transition values (OLD and NEW) relative to event E 3. Consider and execute all before-triggers relative to event E, possibly changing the NEW transition values 4. Apply NEW transition value to the database, thus making the state change associated to event E effective 5. 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
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
Part
Cost 150 500 400 800 Distributor
Distributor
City State
Jones
Taylor HDD
Palo Alto
Minneapolis Atlanta
California
Minnesota Georgia
SUPPLIER RULE
CREATE TRIGGER OneSupplier BEFORE UPDATE OF Supplier ON Part REFERENCING NEW AS N FOR EACH ROW WHEN (N.Supplier IS NULL) SIGNAL SQLSTATE 70005 (Cannot change supplier into NULL)
*trigger di atas tidak menangani inputan data yg bernilai NULL pada Supplier
AUDIT RULE
CREATE TRIGGER AuditSupplier AFTER UPDATE OF Supplier ON Part REFERENCING OLD_TABLE AS OT FOR EACH STATEMENT INSERT INTO Audit VALUES (USER, CURRENT_DATE, (SELECT COUNT(*) FROM OT)
user Bill melakukan transaksi pada October 10, 1996: DELETE FROM Distributor WHERE State = California Audit
User CurrentDate UpdatedTuples
Bill
1996-10-10
CHIMERA
SYNTAX DEFINE 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
CHIMERA
SEMANTIC
While the conflict set is not empty do: 1. select one trigger T from the conflict set among those rules at highest priority; Ts state becomes untriggered 2. Evaluate the condition of T 3. If the condition of T is TRUE, then execute the action of T
Initial Schema
Employee Name: string Salary: integer Mgr: Employee
Class Employee:
{(o1,john,3500,o2),
(o2,tom,4500,null), (o3,bob,4300,o2)}
{} {(o4,toys,{o1,o2,o3}) }
Transaksi
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)
Ketika data dosen dihapus maka set nipwali mahasiswa menjadi null Syntax :
create trigger tr1 before delete on dosen for each row begin update mhs m set m.nipwali=null where m.nipwali= :old.nip; end;