Professional Documents
Culture Documents
Basis Data II
Active Database
• Conventional database management systems are passive.
• Commands are executed by the database (query, update, delete, insert) and
and when requested by the user or application program
• It has passive update principle. That is, client controls DBMS updates. •
• However, some situations cannot be effectively modeled by this
pattern.
Problem with Traditional Database Systems:
• Example:
• Consider a railway database where data are stored about trains,
timetables, seats, fares, and so on, which is accessed by different
terminals.
• In some circumstances (e.g., public holidays, cultural events) it may
be beneficial to add additional coaches to specific trains if the
number of spare seats a month in advance is below a threshold value.
• How to administer this situation in the passive database?
Resolution to the Situation
• Two options are possible:
• Add the additional monitoring functionality to all booking programs
so that the preceding situation is checked each time a seat is sold.
• However, this approach leads to the semantics of the monitoring task
being distributed, replicated, and hidden among different application
programs
Other Examples
• Some more examples of real world problem not well suited for
passive update principle:
• Inventory control - reordering items when quantity in stock falls below
threshold.
• Travel waiting list - book ticket as soon as right kind is available
• Stock market - Buy/sell stocks when price below/above threshold
Active Database
• An Active Database Management System means:
• Allows users to specify actions to be taken automatically, without user
intervention, when certain conditions arise.
• Allowing the specification and implementation of reactive behaviour
• ADBMS extend “passive” DBMS with the possibility to specify reactive
behaviour
Active Databases
• General Idea:
• Active DBMS provides: Regular DBMS primitives + definition of application-
defined situations + triggering of application-defined reactions
• It means: ‣ being able to react automatically to situations in the database ‣
allowing the specification and implementation of reactive behavior
• Active DBMSs
• Embed situation-action rules in database
• Support many functionalities: E.g. Integrity control, derived data, change
notification
• Active DBMS functionality commercially available in SQL:99 as triggers
Active Databases
• Recognize predefined situations in database
• Trigger predefined actions when situations occur
• Actions are usually database updates
• Active Rules – rules that are automatically triggered by events in the
database.
Triggers
• Triggers is a concept that is technique for specifying certain types of
active rules in the database.
• A data base that has a set of associated triggers is called an active
data base.
• Trigger is like a procedure that is automatically invoked by the DBMS
in response to specified changes to data base.
• Trigger is like a ‘Daemon that monitors a data base, and is executed
when the data base is modified in a way that matches the event
specification
Event-Condition-Action (ECA)
• Event(s) that triggers the rule
• update operations (adding new rows, deleting rows)
• temporal events
• Condition that determines whether the rule action should be
executed
• SQL condition
• Action to be taken
• SQL + procedures
• All data actions performed by the trigger execute within the same transaction
in which the trigger fires
Event
• An event is something that happens at a point in time
• Possible alternatives:
• Structure operations (insert, update, access)
• Behavior invocation (the message display is sent to an object of type widget)
• Transaction (abort, commit, begin-transaction)
• Abstract or user-defined (response to some information entered by user)
• Exception (an attempt to access some data without appropriate
authorization)
• Clock (the first day of very month)
• External (the temperature reading goes above 30 degrees)
Event Granularity
• Indicates whether an event is defined:
• For every object in a set (e.g. every instance of a class)
• For given subset (e.g., all staff members except professors)
• For specific members of the set (e.g. to prevent unauthorized access to
specific instances)
Condition
• The condition indicates whether rule action should be executed.
• In ECA-rules, the condition is generally optional.
• Once the triggering event has occurred , the condition may be
evaluated. If condition evaluates to be true, the rule action will be
executed.
• If no condition is specified, the action will be executed once the event
occurs.
Action
• The range of tasks that can be performed if the rule condition is
evaluated to be true.
• It is usually a sequence of SQL statements.
• But actions may:
• perform some behavior invocation within the database or an external call,
• inform the user or system administrator of some situation,
• abort a transaction,
• take some alternative course of action using do-instead
Syntax
<trigger> ::= CREATE TRIGGER <trigger name>
( AFTER I BEFORE ) <triggering events> ON <table name>
[ FOR EACH ROW ]
[ WHEN <condition> ]
<trigger actions> ;
<triggering events> ::= <trigger event> {OR <trigger event> }
<trigger event> ::= INSERT I DELETE I UPDATE [ OF <column name> { , <column
name> } ]
<trigger action> ::= <PL/SQL block>
• We assume that NULL is allowed for Dno in table EMPLOYEE
• Total_sal attribute is sum of the salaries of all employees who are assigned
to the particular department
• Events that may cause a change in the value of Total_sal, which are as
follows:
1. Inserting (one or more) new employee tuples
2. Changing the salary of (one or more) existing employees
3. Changing the assignment of existing employees from one department to another
4. Deleting (one or more) employee tuples
Event 1: Inserting (one or more) new
employee tuples
• recompute Total_sal if the new employee is immediately assigned to a
department.
• if the value of the Dno attribute for the new employee tuple is not NULL,
this is the condition to be checked
• update the value of total_sal, this is the action