CS6005 Advanced Database System UNIT II
CS6005 Advanced Database System UNIT II
SC
Outline of Slides
Chapter 3: Applications
{ Applications of Active Rules
{ Deriving Active Rules for Constraint Management
{ Deriving Active Rules for View Maintenance
{ Rules for Replication
{ Rules for Work
ow Management
{ Business Rules
Rule Creation
Triggering operations:
{ inserted, deleted, updated,
updated(c1,..,cn )
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
Transition Tables
Initial state:
Employee Sal
Stefano 90
Patrick 90
Michael 110
Transaction inserts tuples (Rick,150) and (John,120)
Employee Sal
Stefano 90
Patrick 90
Michael 110
Rick 150
John 120
Rule SalaryControl runs:
Employee Sal
Stefano 81
Patrick 81
Michael 99
Rick 135
John 108
Oracle
Syntax
Trigger Processing
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;
Example of execution
DB2
Triggers for DB2 Common Servers dened at the IBM Al-
maden Research center in 1996.
In
uential on the SQL3 standard.
As in Oracle: either BEFORE or AFTER their event, and
either a row- or a statement-level granularity.
Syntax:
<DB2-trigger> ::= CREATE TRIGGER <trigger-name>
f BEFORE j AFTER g <trigger-event>
ON <table-name>
[ REFERENCING <references> ]
FOR EACH f ROW j STATEMENT g
WHEN ( <SQL-condition> )
<SQL-procedure-statements>
6. Pop from the stack the working storage for A and con-
tinue its evaluation.
Revised trigger processing with integrity checking:
Examples of triggers
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 Parts
REFERENCING OLD TABLE AS OT
FOR EACH STATEMENT
INSERT INTO AuditSupplier
VALUES(USER, CURRENT DATE,
(SELECT COUNT(*) FROM OT))
Trigger's Syntax
Trigger options:
{ immediate / deferred
{ consuming / event-preserving
Events:
{ create, create tmp, delete,
make persistent, generalize,
modify, specialize, query
Condition:
{ An arbitrary declarative formula of the language
Actions:
{ Update primitives, operations, display queries, externally
dened procedures, rollback command
Trigger Processing
Execution Modes
Event Formulas
Syntax: occurred(event,variable).
Semantics: binds OIDs of objects which were aected by the
event, according to several modes.
Every binding represents an \event instance".
Examples of triggers
Adjust salary rule
dene trigger AdjustSalary for Employee
events create, modify(Salary)
condition Self.Salary > Self.Mgr.Salary
actions modify(Employee.Salary,Self,Self.Mgr.Salary)
end;
Events
{ Database state changes
{ Retrievals
{ Time-related
{ Application-dened (external)
Condition
{ Database predicate
{ Database query
{ Application procedure (external?)
Actions
{ Database modications
{ Database retrieval operations
{ Other database commands
{ Application procedures
Consideration
Immediate:
Individual update primitives of the
Data Manipulation Language
(e.g.: Insert, Delete, Update)
{ Before
{ After
{ Instead of
Deferred:
Transition Values
{ Single tuple's OLD and NEW values
{ Collective changes:
INSERTED, DELETED
{ Updates treated:
Explicitly OLD/NEW UPDATED
As deleted followed by inserts
Prioritization
{ None
{ Partial Order
{ Total Order
<reference> ::=
OLD AS <old value tuple name> |
NEW AS <new value tuple name>
Available in Knowledge
Management Extension.
Semantics: AFTER, tuple-level.
Syntax:
<reference> ::=
OLD AS <old value tuple name> |
NEW AS <new value tuple name>
Semantics:
{ Both BEFORE and AFTER
{ Both Statement and Tuple Level
Syntax:
<reference> ::=
OLD AS <old value tuple name> |
NEW AS <new value tuple name>
CREATE TRIGGER
code cascade update
BEFORE UPDATE OF code
ON work status
REFERENCING OLD AS old work status
NEW AS new work status
(UPDATE employee E
SET E.code = NEW.code
WHERE E.code = OLD.code)
Variant:
<Sybase trigger2> ::=
CREATE TRIGGER <trigger name>
ON <table name>
FOR <trigger restricted-events>
AS [IF <trig-upd-exp>] <SQL statements>
Feature Comparison
(see: Widom-Ceri's book)
Approach
{ User species application at
declarative (high) level
{ System derives low-level rules
that implement it (automatically
or semi-automatically)
Framework
Integrity-Preserving Rules
Constraint: condition C
Rules(s):
when operations that could make
C become false
if C is false
then make C true
or abort transaction
Example:
C = every employee's department exists
Operations = insert into emp,
delete from dept,
update to emp.deptno,
update to dept.dno
Condition:
{ There is some employee violating C (due to above ops)
Action: make C true
{ Rollback insertion of emp
{ Rollback deletion of dept
{ Put emp into a dummy dept
View Maintenance
Virtual Views
Materialized Views
View: V = query Q
Rules(s): when operations that can change
the result of Q
then modify V
How to generate rule(s) from view?
Generate triggering operations by analyzing Q
View-Maintaining Rules
Example
Relational view selecting departments with one employee who
earns more than 50,000
DEFINE VIEW HighPaidDept AS
(SELECT DISTINCT Dept.Name
FROM Dept, Emp
WHERE Dept.Dno = Emp.Deptno
AND Emp.Sal > 50K)
Critical events
1. insertions into Emp
2. insertions into Dept
3. deletions from Emp
4. deletions from Dept
5. updates to Emp.Deptno
6. updates to Emp.Sal
7. updates to Dept.Dno
Replication
Work ow Management
Business Rules
Active Rules
R1: If a new user requires power, connect it to the closest node
R2: If a user or a node requires less power, change the power of the
user or node and propagate the change to its input branch
R3: If a branch requires less power, change the power of the branch
and propagate the change to its input node
R4: If the power required from a distributor is decreased, change its
output power accordingly
R5: If a user or node requires more power, change the power of the
user or node and propagate the change to its input branch
R6: If a branch requires more power, change the power of the branch
and propagate the change to its input node
R7: If the power required from a distributor is increased, change its
output power accordingly
R8: If a distributor's power exceeds its maximum, rollback the
entire transaction
R9: If a branch's power is changed, change the power of some of its
wires accordingly
R10: If a wire's power is above its threshold, change wire type
R11: If there's no suitable wire type, add another wire to the branch
R12: If a wire is not included into a tube, add a tube around it
R13: If a tube is too small to fit all its wires, change it into a
larger tube
R14: If a wire inside a tube is high voltage and the tube is not
protected, change it into a protected tube
R15: If there's no suitable tube, split the branch into two branches
Termination:
For any legal transaction, the subsequent rule execution ter-
minates.
Con
uence:
For any legal transaction, the subsequent rule execution ter-
minates in the same nal state.
Identical observable behavior:
For any legal transaction, the subsequent rule execution is
con
uent and produces the same output sequence.
Analysis of Termination
Cyclic Rule
With termination:
CREATE RULE SalaryControl ON Emp
WHEN INSERTED, DELETED, UPDATED (Sal)
IF (SELECT AVG (Sal) FROM Emp ) > 100
THEN UPDATE Emp
SET Sal = .9 * Sal
Without termination:
CREATE RULE SalaryControl2 ON Emp
WHEN INSERTED, DELETED, UPDATED (Sal)
IF (SELECT AVG (Sal) FROM Emp ) > 100
THEN UPDATE Emp
SET Sal = 1.1 * Sal
Con uence
Set-oriented Rules
{ Total ordering and termination imply con
uence.
Tuple-oriented Rules
{ Con
uence is much harder to ensure; it requires that the
nal state does not depend on the system's controlled
order in which tuples are accessed.
{ Con
uence is not necessary or desirable in many cases.
{ Mutating table exception, when a table that is currently
being updated also needs to be changed by a rule; may
reveal lack of con
uence.
Sucient condition for con
uence: Commutativity of two
rules ri and rj if for any database state, considering rule ri
and then rule rj produces the same database as considering
rule rj and then rule ri.
Commutative Rules
create trigger T1 for C1
events modify(A1)
condition C1(X), X.A1=7
actions modify(C1.A2,X,A2+1)
end;
Observable Determinism
Modularization Techniques
for Active Rule Design
Stratication:
partitioning of rules into disjoint strata
such that each strata includes \uniform" rules.
Consequence:
Rule behavior can be abstracted by:
{ Reasoning \locally" on each individual stratum
{ Reasoning \globally" of the behavior across strata
A key design principle for providing rule modularization
and control.
Intuition:
{ For each stratum S , introduce
a metric mS (integer nite function).
{ mS measures the distance from
the current database state to a
stratum xpoint, the quiescent
state produced by rules of a
stratum running in isolation.
{ Strata are ordered by priority.
{ The execution of rules belonging to
lower priority strata does not
perturbate the metric of strata
at higher priority.
Local Convergence,
Metric Conservation
Applications Suggested
Stratication
Constraint Assertional
Internal maintenance stratication
applications Derived data Event-based
maintenance stratication
Replication Assertional
management stratication
Version Event-based
Extended management stratication
applications Event-based
Work
ow stratication and
management assertional
stratication
External Business Behavioral
applications rules stratication
Stratum 1
R11: If a new user requires power, connect it to the
closest node
Stratum 2
R21: If a user or a node requires less power, change
the power of the user or node and propagate the change
to its input branch
R22: If a branch requires less power, change the power of the
branch and propagate the change to its input node
R23: If the power required from a distributor is decreased,
change its output power accordingly
R24: If a user or node requires more power, change the power
of the user or node and propagate the change to its
input branch
R25: If a branch requires more power, change the power of
the branch and propagate the change to its input node
R26: If the power required from a distributor is increased,
change its output power accordingly
R27: If a distributor's power exceeds its maximum, rollback
the entire transaction
Stratum 4
R41: If a wire is not included into a tube, add a tube
around it
R42: If a tube is too small to fit all its wires, change
it into a larger tube
R43: If a wire inside a tube is high voltage and the tube
is not protected, change it into a protected tube
R44: If there's no suitable tube, split the branch into
two branches
Priorities:
1. S1<S2 (S2<S1 excluded:
S1 may generate a user
with unmatched power)
2. S2<S3 (S3<S2 excluded:
S2 may generate wrong
wire types)
3. S3<S4 (S4<S3 excluded:
S3 may generate a wire
not included into tube)
Debugging in Chimera
A debugging environment is started before rule processing or
activated by spy points in the rules.
Interactive execution:
{ At the rule step level (halt+inspection after each rule
execution)
{ At the intra-rule step level (halt+inspection after each
step of triggering, condition evaluation, and action execu-
tion).
Available functionalities (in both modes):
{ Information on rules (including their source code, trigger-
ing time, and event consumption mode)
{ Commands for rule activation and deactivation
{ Inspection of the con
ict set (immediate or deferred)
{ Inspection of the trace
{ Information on occurred events (event type, OIDs of the
objects aected by the event)
Available functionalities only in intra-rule step mode:
{ Display of the processing status
{ Detection of the next executable rule
{ Dynamic modication of priorities
{ Information on bindings produced by the condition
IDEA Methodology
Methodology: www.elet.polimi.it/idea
(More than 250 slides available in HTML)
Web-Laboratory: www.txt.it/idea
(Java-based implementations of IADE, ARACHNE, and PAN-
DORA)
Conclusions