Events and Active Concepts in Database Systems

Events and Active Concepts in Database Systems

EventsEvents andand activeactive conceptsconcepts inin databasedatabase systemssystems Trigger in SQL:1999 (Oracle) Active Database systems EventsEvents inin DBS:DBS: MotivationMotivation Assumption: Data stored in DB Architectures: New book 1.Alternative Amazon Web-interface ? DB DBMS Application ! 2.Alternative New book FU-Berlin, DBS I 2006, Amazon Web-interface DB DBMS Application Hinze / Scholz / Hinze ENS 7.2 EventsEvents inin DBS:DBS: MotivationMotivation Idea: Active Database System (ADBS) New book Application DB aDBMS FU-Berlin, DBS I 2006, Active database — Relational or object oriented data base — Triggers actions in reaction on (system)events Hinze / Scholz / Hinze — ECA-Rules specify event, condition, action 7.3 EventsEvents inin DBS:DBS: Conceivable events: — Database state transitions — Temporal events — Abstract or external events Examples: — Relational: (Sybase, Ingres) — Object-relational: Postgres — Object-oriented: Sentinel, Ode [agr89], Samos[dit00] FU-Berlin, DBS I 2006, Simple triggers also in SQL:1999 (Oracle) Hinze / Scholz / Hinze 7.4 EventsEvents inin DBS:DBS: Argumentation for aDBS: — central management of semantic in DB — optimization of processing — higher DBMS functionality — support of time-dependent requirements Typical applications: — monitoring of integrity constraints — access control — service for derived data within database (view update) FU-Berlin, DBS I 2006, — but also: trigger of external actions (notify administrator about weird actions on data) Hinze / Scholz / Hinze 7.5 EventsEvents inin DBS:DBS: RoadmapRoadmap Trigger in SQL:1999 — Standard —Example: Oracle Active Database Systems FU-Berlin, DBS I 2006, Hinze / Scholz / Hinze 7.6 Trigger in SQL1999: Introduction Simplified integrity rules Simple conditions CREATE TRIGGER <TriggerName> {before|after } {insert|delete|update} ON <RelationName> [referencing old as <OldName>, new as <NewName>] [WHEN <Condition>] [for each row|statement] <SQLStatement> FU-Berlin, DBS I 2006, — called on insert/update/delete on specified relation — references: binds variables to old/new tuples of a relation — for each row: activates action for all selected tuples Hinze / Scholz / Hinze — for each statement: activates action once for each condition —Only for single table 7.7 Trigger in SQL1999: Trigger Example Example: — count the number of inserted books createcreate triggertrigger BookCountPlusBookCountPlus onon insertinsert onon BookBook referencingreferencing oldold asas OldOld newnew asas NewNew updateupdate BookCountBookCount setset New.NumberNew.Number == Old.Number+1Old.Number+1 forfor eacheach rowrow createcreate triggertrigger BookCountMinusBookCountMinus FU-Berlin, DBS I 2006, ...... setset New.NumberNew.Number == Old.Number-1Old.Number-1 Hinze / Scholz / Hinze 7.8 TriggerTrigger inin SQL1999SQL1999:: TriggerTrigger ExampleExample Example: — check that the budget holds — just once per update statement createcreate triggertrigger BudgetTestBudgetTest afterafter updateupdate onon SalarySalary forfor eacheach statementstatement whenwhen (SumSalary(SumSalary > > 200.000)200.000) signalsignal „Budget„Budget overflow!“overflow!“ FU-Berlin, DBS I 2006, Note: SumSalary is a function that has been predefined and is just used here Hinze / Scholz / Hinze 7.9 TriggerTrigger inin SQL1999SQL1999:: TriggerTrigger inin OracleOracle — similar to stored procedures — written in PL/SQL, Java (stored internally), or C (stored externally) PL/SQL Block Structure: Stored procedure example: DECLARE – Optional DECLARE Variables, cursors, v_variable VARCHAR2(5); user-defined exceptions BEGIN BEGIN – Mandatory SELECT column_name SQL statements INTO v_variable PL/SQL statements FROM table_name; EXCEPTION – Optional EXCEPTION Actions to perform when WHEN exception_name THEN FU-Berlin, DBS I 2006, errors occur ... END; – Mandatory END; PL/SQL variable Hinze / Scholz / Hinze Bind variable: declared in host environment (e.g.SQL*Plus), 7.10 reference in PL/SQL: :variable TriggerTrigger inin SQL1999SQL1999:: TriggerTrigger inin OracleOracle Supported Events: — DML statements that modify data in a table (INSERT, UPDATE, or DELETE) — DDL statements — System events (startup, shutdown, and error messages) — User events such as logon and logoff Syntax: CREATECREATE [OR[OR REPLACE]REPLACE] TRIGGERTRIGGER <trigger_name><trigger_name> {BEFORE|AFTER|INSTEAD{BEFORE|AFTER|INSTEAD OF}OF} {INSERT|DELETE|UPDATE{INSERT|DELETE|UPDATE [OF[OF column_list]}column_list]} [[REFERENCING[[REFERENCING [NEW[NEW ASAS <new_row_name><new_row_name>]] FU-Berlin, DBS I 2006, [OLD[OLD ASAS <old_row_name><old_row_name>]]]] [FOR[FOR EACHEACH ROWROW [WHEN[WHEN ((condition)]]condition)]] DECLAREDECLARE <<declarations>declarations> BEGINBEGIN Hinze / Scholz / Hinze PL/SQLPL/SQL codecode END;END; 7.11 TriggerTrigger inin SQL1999SQL1999:: TriggerTrigger inin OracleOracle Trigger Applications: —Integrity Test — Referential Integrity Test — Event Logging —User Auditing — Maintain table replicas — Gather statistics — Modify tables according to DML statements against views FU-Berlin, DBS I 2006, — Security authorization — Publish events to applications Hinze / Scholz / Hinze 7.12 TriggerTrigger inin SQL1999SQL1999:: TriggerTrigger inin OracleOracle Examples of Entity Integrity Triggers CREATECREATE OROR REPLACEREPLACE TRIGGERTRIGGER CUSTOMER_GET_KEYCUSTOMER_GET_KEY BEFOREBEFORE INSERTINSERT ONON CUSTOMERCUSTOMER FORFOR EACHEACH ROWROW DECLAREDECLARE NEW_IDNEW_ID NUMBER;NUMBER; BEGINBEGIN SELECTSELECT MAX(CUSTOMER_ID_NO)MAX(CUSTOMER_ID_NO) INTOINTO NEW_IDNEW_ID FROMFROM CUSTOMER;CUSTOMER; NEW.CUSTOMER_ID_NONEW.CUSTOMER_ID_NO :=:= NEW_IDNEW_ID ++ 1;1; END;END; FU-Berlin, DBS I 2006, CREATECREATE OROR REPLACEREPLACE TRIGGERTRIGGER customer_name_uppercustomer_name_upper BEFOREBEFORE INSERTINSERT OROR UPDATEUPDATE OFOF namename ONON customercustomer FORFOR EACHEACH ROWROW BEGINBEGIN Hinze / Scholz / Hinze :new.name:new.name :=:= UPPER(:new.name)UPPER(:new.name) ;; END;END; 7.13 TriggerTrigger inin SQL1999SQL1999:: TriggerTrigger inin OracleOracle Example of a Referential Integrity Trigger CREATECREATE OROR REPLACEREPLACE TRIGTRIGGERGER CUSTOMER_DEL_CHECKCUSTOMER_DEL_CHECK BEFOREBEFORE DELETEDELETE ONON CUSTOMERCUSTOMER FORFOR EACHEACH ROWROW Non-PL/SQL variable DECLAREDECLARE CUSTOMER_COUNTCUSTOMER_COUNT NUMBER;NUMBER; BEGINBEGIN SELECTSELECT COUNT(CUSTOMER_ID_NO)COUNT(CUSTOMER_ID_NO) INTOINTO CUSTOMER_COUNTCUSTOMER_COUNT FROMFROM SALESALE WHEREWHERE CUSTOMER_ID_NOCUSTOMER_ID_NO == :OLD.CUSTOMER_ID_NO;:OLD.CUSTOMER_ID_NO; IFIF (CUSTOMER_COUNT(CUSTOMER_COUNT >> 0)0) THENTHEN FU-Berlin, DBS I 2006, RAISE_APPLICATION_ERROR(-20000,RAISE_APPLICATION_ERROR(-20000, 'Cannot'Cannot deletedelete customercustomer becausebecause itit hashas '||'|| TO_CHAR(CUSTOMER_COUNT,'99999')TO_CHAR(CUSTOMER_COUNT,'99999') |||| '' Sales.');Sales.'); ENDEND IF;IF; END;END; Hinze / Scholz / Hinze 7.14 TriggerTrigger inin SQL1999SQL1999:: TriggerTrigger inin OracleOracle Example of a Integrity Constraint Trigger (using exception!) CREATECREATE OROR REPLACEREPLACE TRIGGERTRIGGER pos_cust_balpos_cust_bal BEFOREBEFORE INSERTINSERT OROR UPDATEUPDATE ONON custcust FOR FOR EACHEACH ROWROW DECLAREDECLARE neg_bal_errorneg_bal_error EXCEPTION; EXCEPTION; BEGINBEGIN IFIF :new.balance:new.balance << 00 THENTHEN RAISERAISE neg_bal_errorneg_bal_error ; ; ENDEND IF;IF; EXCEPTIONEXCEPTION FU-Berlin, DBS I 2006, WHENWHEN neg_bal_errorneg_bal_error THEN THEN RAISE_APPLICATION_ERRORRAISE_APPLICATION_ERROR (-20001,(-20001, ‘Negative‘Negative BalBalanceance notnot allowed.’);allowed.’); END;END; Hinze / Scholz / Hinze 7.15 TriggerTrigger inin SQL1999SQL1999:: TriggerTrigger inin OracleOracle Triggers vs. integrity constraints: Oracle recommends to use triggers only to enforce: — referential integrity when child and parent tables are on different nodes of a distributed database — complex business rules that are not definable using integrity constraints Reason: FU-Berlin, DBS I 2006, — integrity constrains are “all SQL” - easier, less errors — triggers are more complex to evaluate — Integrity constraints have better performance due to better optimization Hinze / Scholz / Hinze 7.16 TriggerTrigger inin SQL1999SQL1999:: TriggerTrigger inin OracleOracle Example of User Auditing Trigger CREATECREATE OROR REPLACEREPLACE TRIGTRIGGERGER audit_cust_triggeraudit_cust_trigger BEFOREBEFORE UPDATEUPDATE ONON custcust FORFOR EACHEACH ROWROW WHENWHEN (new.balance(new.balance <><> old.balance)old.balance) BEGINBEGIN INSERTINSERT INTOINTO audit_custaudit_cust VALUESVALUES (:old.cust_no,(:old.cust_no, :old.name,:old.name, :old.address,:old.address, :old.balance,:old.balance, sysdate);sysdate); END;END; FU-Berlin, DBS I 2006, Hinze / Scholz / Hinze 7.17 TriggerTrigger inin SQL1999SQL1999:: TriggerTrigger inin OracleOracle Compiling Triggers: different to PL/SQL blocks PL/SQL block — compiled each time loaded into memory: h1.Syntax checking + parse tree generation h2.Semantic checking: Type checking etc. on the parse tree h3.Code generation(pcode) Trigger — fully compiled at creation time (pcode then stored in the data FU-Berlin, DBS I 2006, dictionary) — firing: no opening of cursor, but direct execution — errors during compilation do not stop trigger creation Hinze / Scholz / Hinze (trigger firing fails + calling action fails) 7.18 TriggerTrigger inin SQL1999SQL1999:: TriggerTrigger inin OracleOracle Modifying Triggers — replace with new definition, or — drop and rerun create trigger Enabling and Disabling Triggers — two modes: enabled (default) / disabled — Enabled: trigger executes

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    49 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us