CS 240A: Databases and Knowledge Bases Introduction to Active Databases Carlo Zaniolo Department of Computer Science University of California, Los Angeles.

Slides:



Advertisements
Similar presentations
DB glossary (focus on typical SQL RDBMS, not XQuery or SPARQL)
Advertisements

Active database concepts
1 Constraints, Triggers and Active Databases Chapter 9.
Database Administration, Integrity and Performance.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
SQL Constraints and Triggers
Triggers The different types of integrity constraints discussed so far provide a declarative mechanism to associate “simple” conditions with a table such.
Chapter 7 Triggers and Active Databases. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Trigger Overview Element of the database schema.
The SQL Query Language DML1 The SQL Query Language DML Odds and Ends.
CS 240A: Databases and Knowledge Bases Analysis of Active Databases Carlo Zaniolo Department of Computer Science University of California, Los Angeles.
 Data creation and destruction  Inserting into a table  Deleting from a table  Modifying values in a table  Other commonly used features  Views 
CS240A: Databases and Knowledge Bases Applications of Active Database Carlo Zaniolo Department of Computer Science University of California, Los Angeles.
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5 (cont.)
Triggers.
Cs3431 Triggers vs Constraints Section 7.5. cs3431 Triggers (Make DB Active) Trigger: A procedure that starts automatically if specified changes occur.
Chapter 7 Constraints and Triggers Spring 2011 Instructor: Hassan Khosravi.
Chapter 6: Integrity and Security Thomas Nikl 19 October, 2004 CS157B.
The Relational Model These slides are based on the slides of your text book.
CS 240A: Databases and Knowledge Bases Introduction to Active Databases Carlo Zaniolo Department of Computer Science University of California, Los Angeles.
1 ICS 184: Introduction to Data Management Lecture Note 11: Assertions, Triggers, and Index.
Triggers and Stored Procedures in DB 1. Objectives Learn what triggers and stored procedures are Learn the benefits of using them Learn how DB2 implements.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
ICS 321 Fall 2011 Constraints, Triggers, Views & Indexes Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Triggers. Why Triggers ? Suppose a warehouse wishes to maintain a minimum inventory of each item. Number of items kept in items table Items(name, number,...)
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Integrity Constraints. 421B: Database Systems - Integrity Constraints 2 Integrity Constraints (Review) q An IC describes conditions that every legal.
Fall 2001Database Systems1 Triggers Assertions –Assertions describe rules that should hold for a given database. –An assertion is checked anytime a table.
Advanced SQL: Triggers & Assertions
1 Chapter 7 Triggers and Active Databases. 2 Trigger Overview Element of the database schema General form: ON IF THEN –Event- request to execute database.
1 Chapter 7 Triggers and Active Databases. 2 Trigger Overview Element of the database schema General form: ON IF THEN –Event- request to execute database.
Oracle 11g: SQL Chapter 4 Constraints.
7 1 Constraints & Triggers Chapter Constraints and triggers? Constraints: Certain properties that the DBMS is required to enforce –E.g. primary.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Constraints and Triggers. What’s IC? Integrity Constraints define the valid states of SQL-data by constraining the values in the base tables. –Restrictions.
Objectives Database triggers and syntax
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 9 Database Triggers.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E) 1.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
1 SQL: Structured Query Language Chapter 5 (cont.)  Constraints  Triggers.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
CS 240A: Databases and Knowledge Bases Analysis of Active Databases Carlo Zaniolo Department of Computer Science University of California, Los Angeles.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts - 6 th Edition Triggers Chapter 5.
Database Management COP4540, SCS, FIU Database Trigger.
Integrity Prof. Yin-Fu Huang CSIE, NYUST Chapter 9.
IMS 4212: Constraints & Triggers 1 Dr. Lawrence West, Management Dept., University of Central Florida Stored Procedures in SQL Server.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
SQL Triggers, Functions & Stored Procedures Programming Operations.
Murali Mani Constraints. Murali Mani Keys: Primary keys and unique CREATE TABLE Student ( sNum int, sName varchar (20), dept char (2), CONSTRAINT key.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
Copyright © 2004 Pearson Education, Inc.. Chapter 24 Enhanced Data Models for Advanced Applications.
1 Constraints and Triggers in SQL. 2 Constraints are conditions that must hold on all valid relation instances SQL2 provides a variety of techniques for.
Creating Database Triggers
Constraints and Triggers
Active Database Concepts
SQL Stored Triggers Presented by: Dr. Samir Tartir
Introduction to Triggers
Trigger Overview Element of the database schema
Advanced SQL: Views & Triggers
Unit I-2.
Triggers and Active Databases
Introduction to Triggers
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Prof. Arfaoui. COM390 Chapter 9
-Transactions in SQL -Constraints and Triggers
Responding to Data Manipulation Via Triggers
Presentation transcript:

CS 240A: Databases and Knowledge Bases Introduction to Active Databases Carlo Zaniolo Department of Computer Science University of California, Los Angeles Notes From Chapter 2 of Advanced Database Systems by Zaniolo, Ceri, Faloutsos, Snodgrass, Subrahmanian and Zicari Morgan Kaufmann, 1997

Active Database Systems  An integrated facility for creating and executing production rules from within a database system.  A typical database production rule: when event if condition then action

Active Database Systems (Cont’)  Powerful and uniform mechanism for: - Constraint enforcement - Derived data maintenance - Alerting - Authorization checking - Version management - Resource management - Knowledge management

Outline of Slides  Chapter 2: Syntax and Semantics - A Relational Prototype: Starburst - Two Relational Systems: Oracle and DB2 - Features and Products Overview

Outline of Slides (Cont’)  Chapter 3: Applications - Applications of Active Rules - Deriving Active Rules for Constraint Management - Deriving Active Rules for View Maintenance - Rules for Replication - Rules for Workflow Management - Business Rules  View Maintenance design using deductive rules

Outline of Slides (Cont’)  Chapter 4: Design Principles - Properties of Active Rules and Rule Analysis - Rule Modularization - Rule Debugging and Monitoring - Rule Design: IDEA Methodology (pointer) - Conclusion

A Relational Example: Starburst  Designed and built at IBM Almaden  Chief Engineer: Jennifer Widom  Syntax based on SQL  Semantics is set-oriented - Triggering based on (arbitrary) sets of changes - Actions perform (arbitrary) sets of changes - Conditions and actions can refer to sets of changes  Instructions: create, drop, alter, deactivate, activate

Rule Creation ::= CREATE RULE ON WHEN [ IF ] THEN SQL-statements [ PRECEDES ] [ FOLLOWS ] := INSERTED | DELETED | UPDATED [( column-names>) ]

Rule Creation (Cont’)  Triggering operations: - inserted, deleted, updated, updated(c 1,..,c n )  Condition: arbitrary SQL predicate  Actions: any database operations insert, delete, update, select, rollback, create table, etc.  Precedes and Follows: for rule ordering

Example Rules Salary control rule: CREATE RULE SalaryControl ON Emp WHEN INSERTED, DELETED, UPDATED (Sal) IF (SELECT AVG (Sal) FROM Emp ) >100 THEN UPDATE Emp SET Sal =.9 * Sal

Example Rules (Cont’)  High paid rule: CREATE RULE HighPaid ON Emp WHEN INSERTED IF EXISTS (SELECT * FROM INSERTED WHERE Sal > 100) THEN INSERT INTO HighPaidEmp (SELECT * FROM INSERTED WHERE Sal > 100) FOLLOWS SalaryControl ____________________________________ Errata: ADS book has “FOLLOWS AvgSal”

Transition Tables  Logical tables storing changes that triggered rule  Can appear anywhere in condition and action  References restricted to triggering operations: inserted deleted new-updated old-updated

Rule Execution Semantics (1)  Rules processed at commit point of each transaction  Transaction's changes are initial triggering transition  Rules create additional transitions which may trigger other rules or themselves  Each rule looks at set of changes since last considered  When multiple rules triggered, pick one based on partial ordering

Example of Rule Execution  Initial state: Employee Sal Stefano Patrick Michael

Example of Rule Execution (Cont’) Transaction inserts tuples: (Rick, 150) and (John, 120) Employee Sal Stefano Patrick Michael Rick John Employee Sal Stefano Patrick Michael

Example of Rule Execution (Cont’)  Rule SalaryControl runs: Employee Sal Stefano Patrick Michael Rick John Employee Sal Stefano Patrick Michael Rick John

Rule Execution Semantics (2)  Rule SalaryControl runs again: Employee Sal Stefano Patrick Michael Rick John Employee Sal Stefano Patrick Michael Rick John

Rule Execution Semantics (2) (Cont’) Rule Rule HighPaid runs eventually, and inserts into HighPaid only one tuple: Employee Sal Rick 121

Oracle  Supports general-purpose triggers, developed according to preliminary documents on the SQL3 standard.  Actions contain arbitrary PL / SQL code.  Two granularities: row-level and statement-level.  Two types of immediate consideration: before and after.  Therefore: 4 Combinations: BEFORE ROW BEFORE STATEMENT AFTER ROW AFTER STATEMENT

Trigger Processing 1. Execute the BEFORE STATEMENT trigger. 2. For each row affected: (a) Execute the BEFORE ROW trigger. (b) Lock and change the row. (c) Perform row-level referential integrity and assertion checking. (d) Execute the AFTER ROW trigger. 3. Perform statement-level referential integrity and assertion checking. 4. Execute the AFTER STATEMENT trigger.

Syntax ::= CREATE TRIGGER { BEFORE | AFTER } ON [[ REFERENCING ] FOR EACH ROW [ WHEN ( ) ]] ::= INSERT | DELETE | UPDATE [ OF ] ::= OLD AS | NEW AS

Example Trigger in Oracle: Reorder Rule CREATE TRIGGER Reorder AFTER UPDATE OF PartOnHand ON Inventory WHEN (New.PartOnHand < New.ReorderPoint FOR EACH ROW DECLARE NUMBER X BEGIN SELECT COUNT(*) INTO X FROM PendingOrders WHERE Part = New.Part; IF X=0 THEN INSERT INTO PendingOrders VALUES (New.Part, New.OrderQuantity, SYSDATE) END IF; END; PL/SQL

Example of Execution  Initial state of Inventory:  PendingOrders is initially empty  Transaction(executed on October 10, 1996): T 1 : UPDATE Inventory SET PartOnHand = PartOnHand - 70 WHERE Part = 1 PartOnHand for Part 1 is changed to 130 < ReorderPoint, thus Reorder is triggered: it finds PendingOrders empty and thus inserts there the tuple (1,100, ) PartPartOnHandReorderPointReorderQuantity

Example of execution (Cont’)  Another transaction (executed on the same day) T 2 : UPDATE Inventory SET PartOnHand = PartOnHand - 60 WHERE Part >= 1  The trigger is executed upon all the tuples, and the condition holds for parts 1 and 3, but a new order is issued for part 3 only—this causes the insertion into PendingOrders of tuple (3,120, ) PartPartOnHandReorderPointReorderQuantity

DB2  Triggers for DB2 Common Servers defined at the IBM Almaden Research center in  Influential on the SQL3 standard.  As in Oracle: either BEFORE or AFTER their event, and either a row- or a statement-level granularity.

DB2 : Syntax :: = CREATE TRIGGER {BEFORE | AFTER} ON [ REFERENCING ] FOR EACH {ROW | STATEMENT} WHEN ( ) :: = INSERT | DELETE | UPDATE [ ON ] :: = OLD AS | NEW AS new-value-tuple-name | OLDTABLE AS old-value-table-name | NEWTABLE AS new-value-table-name

The Before Trigger CREATE TRIGGER emp_trig NO CASCADE BEFORE UPDATE OF salary ON emp REFERENCING OLD AS oldrow NEW AS newrow FOR EACH ROW WHEN (newrow.salary >1.5*oldrow.salary) SET newrow.salary = 1.5*oldrow.salary; Only three kinds of statement are allowed with before rules: 1. SET to modify the values of NEW 2. SIGNAL will nullify the action but not the transaction 3. VALUES used to call a function. For instance instead of SET we could have : BEGIN ATOMIC VALUES(log(‘excessive raise’, CURRENT TIMESTAMP, oldrow.empno)); SIGNAL SQLSTATE ‘70011’ (‘excessive raise for oldrow.empno’) END ATOMIC

Semantics of DB2 Triggers  Before-triggers:  Used to detect error conditions and to condition input values (assign values to NEW transition variables).  Read the database state prior to any modification made by the event.  Cannot modify the database by using UPDATE, DELETE, and INSERT statements (so they do not activate other triggers).  Several triggers (with either row- or statement-level granularity) can monitor the same event.  A system-determined total order takes into account the triggers’ definition time; row- and statement-level triggers are intertwined in the total order.

Semantics of DB2 Triggers (Cont’)  General trigger processing algorithm after statement A : 1. Suspend the execution of A, and save its working storage on a stack. 2. Compute transition values (OLD and NEW) relative to event. 3. Consider and execute all before-triggers relative to event E, possibly changing the NEW transition values. 4. Apply NEW transition values 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 A I that activates other triggers, then invoke this processing procedure recursively for A I. 6. Pop from the stack the working storage for A and continue its evaluation.

Semantics of DB2 Triggers (Cont’) Revised trigger processing with integrity checking: 4 Apply the 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 change, let A j be the compensating action (if any) specified with IC, then [a.] Compute the transition values (OLD and NEW) relative to A j. [b.] Execute the before-triggers relative to A j,, possibly changing the NEW transition values. [c.] Apply NEW transition values to the database, thus making the state change associated to A j effective. [d.] Push all after-triggers relative to action into a queue of suspended triggers. Until a quiescent point is reached where all the integrity constraints violated in the course of the computation are compensated.

Examples of Triggers on Part(PartNum, Supplier, Cost) 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 to NULL') Audit rule: CREATE TRIGGER Audit AFTER UPDATE ON Part REFERENCING OLDTABLE AS OT FOR EACH STATEMENT INSERT INTO AuditSupplier VALUES(USER, CURRENTDATE, (SELECT COUNT(*) FROM OT))

Example (cont.) Part PartNo SupplierCost 1Jones150 2Taylor500 3 HDD 400 4Jones800 Distributor Supplier City State JonesPalo AltoCA TaylorNewarkNJ HDDAtlanta GE Say that the default value is HDD … FOREIGN KEY (Supplier) REFERENCES Distributor ON DELETE SET DEFAULT  SIGNAL: rolls back the effect of a statement, while letting the transaction proceed.  SIGNAL and SET are often used with before triggers.

Part PartNo SupplierCost 1Jones150 2Taylor500 3 HDD 400 4Jones800 Distributor Supplier City State JonesPalo AltoCA TaylorNewarkNJ HDDAtlanta GE FOREIGN KEY (Supplier) REFERENCES Distributor ON DELETE SET DEFAULT (i.e. HDD ) CREATE TRIGGER OneSupplier BEFORE UPDATE OF Supplier ON Part REFERENCING NEW AS N FOR EACH ROW WHEN (N.Supplier IS NULL) SIGNAL SQLSTATE ‘70005 (…) CREATE TRIGGER Audit AFTER UPDATE ON Part REFERENCING OLDTABLE AS OT FOR EACH STATEMENT INSERT INTO AuditSupplier VALUES(USER, CURRENTDATE, (SELECT COUNT(*) FROM OT)) DELETE FROM Distributor WHERE State =`CA ’ Example (cont.)

Execution 1. Event:DELETE FROM Distributor WHERE State =`CA’ 2. No rule is specified for this event, but the FK constraint updates Jones with HDD in the first and last tuple of Part the OLD and NEW tables for Part are set accordingly. 3. The before rule is evaluated on the updates of point 2, but it is not fired since the WHEN condition fails. 4. The updates of point 2 are written onto the actual table Distributor (well, the in-memory copy, that will be then written to disk at transaction commit time). 5. It is now the turn of the after rules. The AuditSupplier rule is triggered, which adds to the Audit table the tuple: (Bill, , 2)

Question--homework What happens if we change the FK corrective action to: FOREIGN KEY (Supplier) REFERENCES Distributor ON DELETE SET NULL

Part PartNo SupplierCost 1Jones150 2Taylor500 3 HDD 400 4Jones800 Distributor Supplier City State JonesPalo AltoCA TaylorNewarkNJ HDDAtlanta GE FOREIGN KEY (Supplier) REFERENCES Distributor ON DELETE SET NULL CREATE TRIGGER OneSupplier BEFORE UPDATE OF Supplier ON Part REFERENCING NEW AS N FOR EACH ROW WHEN (N.Supplier IS NULL) SIGNAL SQLSTATE … CREATE TRIGGER Audit AFTER UPDATE ON Part REFERENCING OLDTABLE AS OT FOR EACH STATEMENT INSERT INTO AuditSupplier VALUES(USER, CURRENTDATE, (SELECT COUNT(*) FROM OT)) Given the DB below, What happen when the user issues the following: DELETE FROM Distributor WHERE State =`CA ’

Taxonomy of ECAs  Events and Conditions: DB changes, Retrievals, Time related events, composite events, application defined …  Actions: DB changes, messages, stored procedures, grant privileges, activate rules, arbitrary programs  Consideration and Execution:  Immediate Before  Immediate After  Deferred (till the end of transaction)  Detached (in another transaction)  Granularity: row or statement  SQL:1999 attempts to put a limit on these many alternatives. Still semantics is very complex.

Logical Complexity  The XCON experience: large rules sets supporting a complex application---unmanageable.  Use ECAs only in rather simple and well understood applications (Ch 3)  Static analysis to predict and limit dynamic interactions between rules and recursive triggering (Ch 4).