You are on page 1of 23

DATABASE SYSTEM CONCEPTS

CHAPTER 8

APPLICATION DESIGN AND DEVELOPMENT


Prof. : Nitin Merh Submitted By : Prakash Singh Tanwar

WHAT IS TRIGGER ?u.


A trigger is a statement that is executed automatically by the system as a side effect of a modification to the database.

TO DESIGN A TRIGGER MECHANISM, WE MUST: : Specify the conditions under which the trigger is to be executed. : Specify the actions to be taken when the trigger executes.

NEED FOR TRIGGER


Suppose that instead of allowing negative account balances, the bank deals with overdrafts by : : Setting the account balance to zero. : Creating a loan in the amount of the overdraft. : Giving this loan a loan number identical to the account number of the overdrawn account.

**The condition for executing the trigger is an update to the account relation that results in a negative balance value.

TRIGGER EXAMPLE IN SQL


Create trigger overdraft trigger after update on account referencing new row as nrow for each row : when nrow. Balance < 0 Begin atomic insert into borrower (Select customer name, account number from depositor where nrow. Account-number = depositor. Account-number); Insert into loan values (n.row.account-number, nrow. Branch-name, nrow. Balance); update account set balance = 0 where account. account-number = nrow.accountnumber end

TRIGGER IN SQL
 Triggering event can be insert, delete or update.  Triggers on update can be restricted to specific attributes : E.g. create trigger overdraft trigger after update of balance on account.  Values of attributes before and after an update can be referenced : Referencing old row as : for deletes and updates : Referencing new row as : for inserts and updates  Triggers can be activated before an event, which can serve as extra constraints. E.g. convert blanks to null.

Contd.

TRIGGER IN SQL
 Instead of executing a separate action for each affected row, a single action can be executed for all rows affected by a transaction

: Use for each statement instead of for each row

: Use referencing old table or referencing new table to refer to temporary tables (called transition tables) containing the affected rows

: Can be more efficient when dealing with SQL statements that update a large number of rows

WHEN NOT TO USE TRIGGER


 Triggers were used earlier for tasks such as : Maintaining summary data (e.g. total salary of each department) : Replicating databases by recording changes to special relations (called change or delta relations) and having a separate process that applies the changes over to a replica.  There are better ways of doing these now: : Databases today provide built in materialized view facilities to maintain summary data. : Databases provide built-in support for replication.  Encapsulation facilities can be used instead of triggers in many cases : Define methods to update fields. : Carry out actions as part of the update methods instead of through a trigger.

 

Assume relational data model Replication : System maintains multiple copies of data, stored in different sites, for faster retrieval and fault tolerance.

Fragmentation

:Relation is partitioned into several fragments stored in distinct sites  Replication and fragmentation can be combined : Relation is partitioned into several fragments: system maintains several identical replicas of each such fragment.

DATA REPLICATION
A relation or fragment of a relation is replicated if it is stored redundantly in two or more sites.

Full replication of a relation is the case where the relation is stored at all sites.

Fully redundant databases are those in which every site contains a copy of the entire database.

ADVANTAGES AND DISADVANTAGES


Advantages of Replication Availability: Failure of site containing relation r does not result in unavailability of r is replicas exist. Parallelism: Queries on r may be processed by several nodes in parallel. Reduced data transfer: Relation r is available locally at each site containing a replica of r.

contd..

Disadvantages of Replication Increased cost of updates: each replica of relation r must be updated.

Increased complexity of concurrency control: concurrent updates to distinct replicas may lead to inconsistent data unless special concurrency control mechanisms are implemented.

DATA FRAGMENTATION
Division of relation r into fragments r1, r2, , rn which contain sufficient information to reconstruct relation r.  Horizontal fragmentation: each tuple of r is assigned to one or more fragments  Vertical fragmentation: the schema for relation r is split into several smaller schemas : All schemas must contain a common candidate key (or super key) to ensure lossless join property. : A special attribute, the tupleid attribute may be added to each schema to serve as a candidate key.

ADVANTAGE OF FRAGMENTATION
Horizontal: Allows parallel processing on fragments of a relation. Allows a relation to be split so that tuples are located where they are most frequently accessed. Vertical: Allows tuples to be split so that each part of the tuple is stored where it is most frequently accessed. Tuple-id attribute allows efficient joining of vertical fragments. Allows parallel processing on a relation.  Vertical and horizontal fragmentation can be mixed. Fragments may be successively fragmented to an arbitrary depth.

DATA TRANSPARENCY
Data transparency: Degree to which system user may remain unaware of the details of how and where the data items are stored in a distributed system.  Consider transparency issues in relation to : Fragmentation transparency Replication transparency Location transparency

Assumptions:
System has only reusable resources Only exclusive access to resources Only one copy of each resource States of a process: running or blocked Running state: process has all the resources Blocked state: waiting on one or more resource

DEADLOCKS
Resource Deadlocks
A process needs multiple resources for an activity. Deadlock occurs if each process in a set request resources held by another process in the same set, and it must receive all the requested resources to move further.

Communication Deadlocks
Processes wait to communicate with other processes in a set. Each process in the set is waiting on another processs message, and no process in the set initiates a message until it receives a message for which it is waiting.

GRAPH MODELS
Nodes of a graph are processes. Edges of a graph the pending requests or assignment of resources. Wait-for Graphs (WFG): P1 -> P2 implies P1 is waiting for a resource from P2. Transaction-wait-for Graphs (TWF): WFG in databases. Deadlock: directed cycle in the graph. Cycle example:

AND, OR MODELS
AND Model A process/transaction can simultaneously request for multiple resources. Remains blocked until it is granted all of the requested resources. OR Model A process/transaction can simultaneously request for multiple resources. Remains blocked till any one of the requested resource is granted.

DEADLOCK HANDLING STRATEGIES


Deadlock Prevention: difficult Deadlock Avoidance: before allocation, check for possible deadlocks. Difficult as it needs global state info in each site (that handles resources). Deadlock Detection: Find cycles. Focus of discussion. Deadlock detection algorithms must satisfy 2 conditions: No undetected deadlocks. No false deadlocks.

DISTRIBUTED DEADLOCKS
Centralized Control
A control site constructs wait-for graphs (WFGs) and checks for directed cycles. WFG can be maintained continuously (or) built on-demand by requesting WFGs from individual sites.

Distributed Control
WFG is spread over different sites. Any site can initiate the deadlock detection process.

Hierarchical Control
Sites are arranged in a hierarchy. A site checks for cycles only in descendents.

You might also like