Dbms
Dbms
• Consider the top part of Fig. 1.5.1. It shows application interfaces used by naïve users, application programs
created by application programmers, query tools used by sophisticated users and administration tools used by
database administrator.
• The lowest part of the architecture is for disk storage.
• The two important components of database architecture are - Query processor and storage manager.
Query processor:
• The interactive query processor helps the database system to simplify and facilitate access to data. It consists of
DDL interpreter, DML compiler and query evaluation engine.
• With the following components of query processor, various functionalities are performed -
i) DDL interpreter: This is basically a translator which interprets the DDL statements in data dictionaries.
ii) DML compiler: It translates DML statements query language into an evaluation plan. This plan consists of the
instructions which query evaluation engine understands.
iii) Query evaluation engine: It executes the low-level instructions generated by the DML compiler.
• When a user issues a query, the parsed query is presented to a query optimizer, which uses information about how
the data is stored to produce an efficient execution plan for evaluating the query. An execution plan is a blueprint for
evaluating a query. It is evaluated by query evaluation engine.
Storage manager:
• Storage manager is the component of database system that provides interface between the low level data stored
in the database and the application programs and queries submitted to the system.
• The storage manager is responsible for storing, retrieving, and updating datain the database. The storage manager
components include -
i) Authorization and integrity manager: Validates the users who want to access the data and tests for integrity
constraints.
ii) Transaction manager: Ensures that the database remains in consistent despite of system failures and concurrent
transaction execution proceeds without conflicting.
iii) File manager: Manages allocation of space on disk storage and representation of the information on disk.
iv) Buffer manager: Manages the fetching of data from disk storage into main memory. The buffer manager also
decides what data to cache in main memory. Buffer manager is a crucial part of database system.
• Storage manager implements several data structures such as -
i) Data files: Used for storing database itself.
ii) Data dictionary: Used for storing metadata, particularly schema of database.
iii) Indices: Indices are used to provide fast access to data items present in the database
Data Models
AU: Dec.-14, May-19, Marks 13
Definition: It is a collection of conceptual tools for describing data, relationships among data, semantics (meaning) of
data and constraints.
• Data model is a structure below the database.
• Data model provides a way to describe the design of database at physical, logical and view level.
• There are various data models used in database systems and these are as follows -
(1) Relational model:
• Relation model consists of collection of tables which stores data and also guilatxo represents the relationship
among the data.
• Table is also known as relation.
• The table contains one or more columns and each column has unique name.
• Each table contains record of particular type, and each record type defines a fixed number of fields or attributes.
• For example - Following figure shows the relational model by showing the relationship between Student and Result
database. For example - Student Ram lives in city Chennai and his marks are 78. Thus the relationship between these
two databases is maintained by the SeatNo. Column
Advantages:
(i) Structural Independence: Structural independence is an ability that allows us to make changes in one database
structure without affecting other. The relational levsiz model have structural independence. Hence making required
changes in thedatabase is convenient in relational database model.
(ii)Conceptual Simplicity: The relational model allows the designer to simply focus on logical design and not on
physical design. Hence relational models are conceptually simple to understand.
(iii) Query Capability: Using simple query language (such as SQL) user can get egile information from the database or
designer can manipulate the database structure.
(iv) Easy design,maintenance and usage: The relational models can be designed logically hence they are easy to
maintain and use.
Disadvantages:
(i) Relational model requires powerful hardware and large data storage devices.
(ii) May lead to slower processing time.
(iii) Poorly designed systems lead to poor implementation of database systems.
1) Entity relationship model:
• As the name suggests the entity relationship model uses collection of basic objects called entities and relationships.
• The entity is a thing or object in the real world.
• The entity relationship model is widely used in database design.
• For example - Following is a representation of Entity Relationship modelin which the relationship works_for is
between entities Employee and Department.
Advantages:
i) Simple: It is simple to draw ER diagram when we know entities and relationships.
ii) Easy to understand: The design of ER diagram is very logical and hence they are easy to design and understand.
iii) Effective: It is effective communication tool.
iv) Integrated: The ER model can be easily integrated with Relational model.
v) Easy conversion: ER model can be converted easily into other type of models.
Disadvantages:
i) Loss of information: While drawing ER model some information can be hidden or lost.
ii) Limited relationships: The ER model can represent limited relationships as compared to other models.
iii) No Representation for data manipulation: It is not possible to represent data manipulation in ER model.
iv) No industry standard: There is no industry standard for notations of ER diagram.
(3) Object Based Data Model:
• The object oriented languages like C++, Java, C# are becoming the
dominant in software development.
• This led to object based data model.
• To The object based data model combines object oriented features with relationaldata model.
Advantages:
i) Enriched modelling: The object based data model has capability of modelling the real world objects.
ii) Reusability: There are certain features of object oriented design such as inheritance, polymorphism which help in
reusability.
iii) Support for schema evolution: There is a tight coupling between data and b applications, hence there is strong
support for schema evolution.
iv)Improved performance: Using object based data model there can be significant improvement in performance
using object based data model.
Disadvantages:
i) Lack of universal data model: There is no universally agreed data model for an object based data model, and most
models lack a theoretical foundation.
ii) Lack of experience: In comparison with relational database management the use of object based data model is
limited. This model is more dependent on the skilled egi programmer.
iii) Complex: More functionalities present in object based data model make the design complex.
(4) Semi-structured data model:
• The semi-structured data model permits the specification of data where individual data items of same type may
have different sets of attributes.
• The Extensible Markup Language (XML) is widely used to represent semi- structured data model.
Advantages
i) Data is not constrained by fixed schema.
ii) It is flexible.
iii) It is portable.
Disadvantages
i) Queries are less efficient than other types of data model.
Normal Forms
• Normalization is the process of reorganizing data in a database so that it meets two basic requirements:
1) There is no redundancy of data (all data is stored in only one place), and
2) data dependencies are logical (all related data items are stored together)
3) The normalization is important because it allows database to take up less disk space.
iv) And the order in which data is stored, does not matter.
Student
As there are multiple values of phone number for sid 1 and 3, the above table is not in 1NF. We can make it in 1NF.
The conversion is as follows -
Before understanding the second normal form let us first discuss the concept of j functional dependency and prime
and non prime attributes.
Partial dependency means that a nonprime attribute is functionally dependent on part of a candidate key.
Hence {A,B) are prime attributes and {C,D) are non prime attribute. In A->C, the non prime attribute C is dependent
upon A which is actually a part of candidate key AB. Hence due to A->C we get partial functional dependency.
• Prime attribute: An attribute, which is a part of the candidate-key, is known as a prime attribute.
• Non-prime attribute: An attribute, which is not a part of the prime-key, is said to be a non-prime attribute.
• Example : Consider a Relation R ={A,B,C,D) and candidate key as AB, the Prime attributes: A, B
For a table to be in the Second Normal Form, following conditions must be followed
For example: Consider following table in which every information about a the Student is maintained in a table such
as student id(sid), student name(sname), course id(cid) and course name(cname).
Student_Course
This table is not in 2NF. For converting above table to 2NF we must follow the following steps -
Step 2: Here sname and sid are associated similarly cid and cname are associated with each other. Now if we delete
a record with sid=2, then automatically the course C++ will also get deleted. Thus,
sid->sname or cid->cname is a partial functional dependency, because {sid,cid} should be essentially a candidate key
for above table. Hence to bring the above table to 2NF we must decompose it as follows:
Student:
Course:
Thus now table is in 2NF as there is no partial functional dependency
Before understanding the third normal form let us first discuss the concept of transitive dependency, super key and
candidate key.
A functional dependency is said to be transitive if it is indirectly formed by two functional dependencies. For example
-
X->Y
Y->Z
Superkey: A super key is a set or one of more columns (attributes) to uniquely identify rows in a table.
Candidate key: The minimal set of attribute which can uniquely identify a tuple is known as candidate key. For
example consider following table:
Superkeys
• {RegID}
• {RegID, RollNo}
• {RegID,Sname}
• {RollNo,Sname}
• {RegID, RollNo,Sname}
Candidate Keys
• {RegID}
• {RollNo}
i) It is in the Second Normal form.(i.e. it does not have partial functional dependency)
Or in other words
In other words 3NF can be defined as: A table is in 3NF if it is in 2NF and for each functional dependency
X-> Y
Here
Candidate keys:{sid}
sid->sname
sid->zipcode
zipcode->cityname
cityname->state
The above denotes the transitive dependency. Hence above table is not in 3NF. We can convert it into 3NF as
follows:
Student
Zip
Example 2.11.1 Consider the relation R = {A, B, C, D, E, F, G, H, I, J} and the set of functional dependencies F= {{A,
B} →C, A →{D, E}, B→F, F→ {G, H}, D →{I, J} }
Solution: Let,
A→ DE (given)
A→ D, A→ E
AsD→IJ, A→IJ
A → DEIJ
As A→ A
we get A → ADEIJ
AB → ABDEIJ
But
AB→C (given)
AB →ABCDEIJ
B→ F (given) F→ GH .. B→ GH (transitivity)
AB → ABCDEFGHIJ
AB is a key
R1= (A, B, C)
R3= (B, F, G, H)
The above 2NF relations can be converted to 3NF as follows:
R1= (A, B, C)
R2= (A, D, E)
R1= (D, I, J)
R1= (B, E)
ii) One to many :When entity A is associated with more than one entities at a time then there is one to many
relation. For example - One customer places order at a time.
iii) Many to one : When more than one entities are associated with only one entity then there is is many to one
relation. For example – Many student take a ComputerSciCourse
iv) Many to many: When more than one entities are associated with more than one entities. For example -Many
teachers can teach many students.
Ternary Relationship
The relationship in which three entities are involved is called ternary relationship. For example -
Binary and Ternary Relationships
• Although binary relationships seem natural to most of us, in reality it is sometimes necessary to connect three or
more entities. If a relationship connects three entities, it is called ternary or "3-ary."
• Ternary relationships are required when binary relationships are not sufficient to accurately describe the semantics
of an association among three entities.
• For example - Suppose, you have a database for a company that contains the entities, PRODUCT, SUPPLIER, and
CUSTOMER. The usual relationships might be PRODUCT/ SUPPLIER where the company buys products from a
supplier - a normal binary relationship. The intersection attribute for PRODUCT/SUPPLIER is wholesale_price
• Now consider the CUSTOMER entity, and that the customer buys products. If all customers pay the same price for
a product, regardless of supplier, then you have a simple binary relationship between CUSTOMER and PRODUCT. For
the CUSTOMER/PRODUCT relationship, the intersection attribute is retail_price.
• Single ternary relation: Now consider a different scenario. Suppose the customer buys products but the price
depends not only on the product, but also on the supplier. Suppose you needed a customerID, a productID, and a
supplierID to identify a price. Now you have an attribute that depends on three things and hence you have a
relationship between three entities (a ternary relationship) that will have the intersection attribute, price.
ACID Properties
AU: May 14,18,19, Dec- 19,Mark 15
1) Atomicity:
This property states that each transaction must be considered as a single unit and must be completed fully or not
completed at all.
• No transaction in the database is left half completed.
• Database should be in a state either before the transaction execution or after the transaction execution. It should
not be in a state 'executing'.
• For example In above mentioned withdrawal of money transaction all the five steps must be completed fully or
none of the step is completed. Suppose if transaction gets failed after step 3, then the customer will get the money
but the balance will not be updated accordingly. The state of database should be either at before ATM withdrawal
(i.e customer without withdrawn money) or after ATM withdrawal (i.e. customer with money and account updated).
This will make the system in consistent state.
2) Consistency:
• The database must remain in consistent state after performing any transaction.
• For example: In ATM withdrawal operation, the balance must be updated appropriately after performing
transaction. Thus the database can be in consistent state.
3) Isolation:
• In a database system where more than one transaction are being executed simultaneously and in parallel, the
property of isolation states that all the transactions will be carried out and executed as if it is the only transaction in
the system.
• No transaction will affect the existence of any other transaction.
• For example: If a bank manager is checking the account balance of particular customer, then manager should see
the balance either before withdrawing the money or after withdrawing the money. This will make sure that each
individual transaction is completed and any other dependent transaction will get the consistent data out of it. Any
failure to any transaction will not affect other transaction in this case. Hence it makes all the transactions consistent.
4) Durability:
• The database should be strong enough to handle any system failure.
• If there is any set of insert /update, then it should be able to handle and commit to the database.
• If there is any failure, the database should be able to recover it to the consistent state.
• For example: In ATM withdrawal example, if the system failure happens after Customer getting the money then
the system should be strong enough to update Database with his new balance, after system recovers. For that
purpose the system has to keep the log of each transaction and its failure. So when the system recovers, it should be
able to know when a system has failed and if there is any pending transaction, then it should be updated to
Database.
Deadlock Handling
AU: May-09, Dec.-14,15,16,19, Marks 16
Deadlock is a specific concurrency problem in which two transactions depend on each other for something.
For example- Consider that transaction T1 holds a lock on some rows of table A and needs to update some rows in
the B table. Simultaneously, transaction T2 holds locks on some rows in the B table and needs to update the rows in
the A table held by Transaction T1.
Now, the main problem arises. Now Transaction T1 is waiting for T2 to release its lock and similarly, transaction T2 is
waiting for T1 to release its lock. All activities come to a halt state and remain at a standstill. This situation is called
deadlock in DBMS.
Definition: Deadlock can be formally defined as - " A system is in deadlock state if there exists a set of transactions
such that every transaction in the set is waiting for another transaction in the set. "
There are four conditions for a deadlock to occur
A deadlock may occur if all the following conditions holds true.
1. Mutual exclusion condition: There must be at least one resource that cannot be used by more than one process
at a time.
2. Hold and wait condition: A process that is holding a resource can request for vd's additional resources that are
being held by other processes in the system.
3. No preemption condition: A resource cannot be forcibly taken from a process. Only the process can release a
resource that is being held by it.
4. Circular wait condition: A condition where one process is waiting for a resource that is being held by second
process and second process is waiting for third process In the....so on and the last process is waiting for the first
process. Thus making a circular chain of waiting.
Deadlock can be handled using two techniques -
1. Deadlock Prevention
2. Deadlock Detection and deadlock recovery
1. Deadlock prevention :
For large database, deadlock prevention method is suitable. A deadlock can be prevented if the resources are
allocated in such a way that deadlock never occur. The DBMS analyzes the operations whether they can create
deadlock situation or not, If they do, that transaction is never allowed to be executed.
There are two techniques used for deadlock prevention -
(i) Wait-Die:
• In this scheme, if a transaction requests for a resource which is already held with a conflicting lock by another
transaction then the DBMS simply checks the timestamp of both transactions. It allows the older transaction to wait
until the resource is available for execution.
• Suppose there are two transactions T; and T, and let TS(T) is a timestamp of any transaction T. If T2 holds a lock by
some other transaction and T1 is requesting for resources held by T2 then the following actions are performed by
DBMS:
• Check if TS(T) < TS(T) - If T, is the older transaction and T, has held some resource, then T, is allowed to wait until
the data-item is available for execution. That means if the older transaction is waiting for a resource which is locked
by the younger transaction, then the older transaction is allowed to wait for estb n resource until it is available.
• Check if TS(T;) < TS(T;) - If T; is older transaction and has held some resource and if T, is waiting for it, then T, is
killed and restarted later with the random delay but with the same timestamp.
Timestamp is a way of assigning priorities to each transaction when it starts. If timestamp is lower then that
transaction has higher priority. That means oldest transaction has highest priority.
For example-
Let T1 is a transaction which requests the data item acquired by Transaction T2. Similarly T3 is a transaction which
requests the data item acquired by transaction T2.
Here TS(T1) i.e. Time stamp of T1 is less than TS(T3). In other words T1 is older than T3. Hence T1 is made to wait
while T3 is rolledback.
(ii) Wound - wait:
• In wound wait scheme, if the older transaction requests for a resource which is held by the younger transaction,
then older transaction forces younger one to not kill the transaction and release the resource. After some delay, the
younger bevomer transaction is restarted but with the same timestamp.
• If the older transaction has held a resource which is requested by the Younger transaction, then the younger
transaction is asked to wait until older releases it.
Suppose T1 needs a resource held by T2 and T3 also needs the resource held by T2, with TS(T1)=5, TS(T2)=8 and
TS(T3)=10, then T1 being older waits and T3 being younger dies. After the some delay, the younger transaction is
restarted but with the same timestamp.
This ultimately prevents a deadlock to occur.
To summarize
2. Deadlock detection:
• In deadlock detection mechanism, an algorithm that examines the state of the system is invoked periodically to
determine whether a deadlock has occurred or not. If deadlock is occurrence is detected, then the system must try
to recover from it.
• Deadlock detection is done using wait for graph method.
Wait for graph
• In this method, a graph is created based on the transaction and their lock. If the created graph has a cycle or closed
loop, then there is a deadlock.
• The wait for the graph is maintained by the system for every transaction which is waiting for some data held by the
others. The system keeps checking the graph if there is any cycle in the graph.
• This graph consists of a pair G = (V, E), where V is a set of vertices and E is a set of edges.
• The set of vertices consists of all the transactions in the system.
• When transaction Ti requests a data item currently being held by transaction T i, then the edge Ti → Tj is inserted in
the wait-for graph. This edge is removed only when transaction T j is no longer holding a data item needed by
transaction Ti.
• For example - Consider following transactions, We will draw a wait for graph for this scenario and check for
deadlock.
Step 2: We find the Read-Write pair from two different transactions reading from top to bottom. If such as pair is
found then we will add the edges between corresponding directions. For instance –
Step 3:
As cycle is detected in the wait-for graph there is no need to further process. The deadlock is present in this
transaction scenario.
Example 3.16.1 Give an example of a scenario where two phase locking leads to deadlock. AU: May-09, Marks 4
Solution: Following scenario of execution of transactions can result in deadlock.
In above scenario, transaction T1, makes an exclusive lock on data item B and then transaction T2 makes an exclusive
lock on data item A. Here unless and until T 1, does not give up the lock (i.e. unlock) on B; T 2 cannot read/write it.
Similarly unless and until T2 does not give up the lock on A; T1, cannot read or write on A.
This is a purely deadlock situation in two phase locking.
ARIES Algorithm
• When database crashes during some transaction processing, we have the logs that got saved to the disk.
1) Analysis:
• Scan the log from start to reconstruct the transaction and dirty page table. Dirtypages contain data that has been
changed but not yet written to disk.
• The active transactions which were present at the time of crash are identified.
• During analysis phase the log is scanned forward from the checkpoint record to construct snapshot of what system
looks like at the time of crash.
2) Redo :
3) Undo :
• The log is scanned backward and updates to corresponding active transactions are undone.
Advantages:
RAID
• RAID stands for Redundant Array of Independent Disks. This is a technology in which multiple secondary disks are
connected together to increase the performance, data redundancy or both.
• For achieving the data redundancy - in case of disk failure, if the same data is also backed up onto another disk, we
can retrieve the data and go on with the operation.
• It consists of an array of disks in which multiple disks are connected to achieve different goals.
• The main advantage of RAID, is the fact that, to the operating system the array of disks can be presented as a
single disk.
Need for RAID
• RAID is a technology that is used to increase the performance.
• It is used for increased reliability of data storage.
• An array of multiple disks accessed in parallel will give greater throughput than a single disk.
• With multiple disks and a suitable redundancy scheme, your system can stay up and running when a disk fails, and
even while the replacement disk is being installed and its data restored.
Features
(1) RAID is a technology that contains the set of physical disk drives.
(2) In this technology, the operating system views the separate disks as a single logical disk.
(3) The data is distributed across the physical drives of the array.
(4) In case of disk failure, the parity information can be helped to recover the data.
RAID Levels
Level: RAID 0
• In this level, data is broken down into blocks and these blocks are stored across all the disks.
• Thus striped array of disks is implemented in this level. For instance in the following figure blocks "A B" form a
stripe.
• There is no duplication of data in this level so once a block is lost then there is no int lovol diri way recover it.
• The main priority of this level is performance and not the reliability.
Level: RAID 1
• This level makes use of mirroring. That means all data in the drive is duplicated to another drive.
• This level provides 100% redundancy in case of failure.
• Only half space of the drive is used to store the data. The other half of drive is just a mirror to the already stored
data.
• The main advantage of this level is fault tolerance. If some disk fails then the other automatically takes care of lost
data.
Level: RAID 2
• This level makes use of mirroring as well as stores Error Correcting Codes (ECC) for its data striped on different
disks.
• The data is stored in separate set of disks and ECC is stored another set of disks.
• This level has a complex structure and high cost. Hence it is not used commercially.
Level: RAID 3
• This level consists of byte-level stripping with dedicated parity. In this level, the parity information is stored for
each disk section and written to a dedicated. parity drive.
• We can detect single errors with a parity bit. Parity is a technique that checks whether data has been lost or
written over when it is moved from one place in storage to another.
• In case of disk failure, the parity disk is accessed and data is reconstructed from the remaining devices. Once the
failed disk is replaced, the missing data can be restored on the new disk.
Level: RAID 4
• RAID 4 consists of block-level stripping with a parity disk.
• Note that level 3 uses byte-level striping, whereas level 4 uses block-level striping.
Level: RAID 5
• RAID 5 is a modification of RAID 4.
• RAID 5 writes whole data blocks onto different disks, but the parity bits generated for data block stripe are
distributed among all the data disks rather than storing them on a different dedicated disk.
Level: RAID 6
• RAID 6 is a extension of Level 5
• RAID 6 writes whole data blocks onto different disks, but the two independent parity bits generated for data block
stripe are distributed among all the data disks rather than storing them on a different dedicated disk.
• Two parities provide additional fault tolerance.
• This level requires at least four disks to implement RAID.
The factors to be taken into account in choosing a RAID level are :
Monetary cost of extra disk-storage requirements.
1. Performance requirements in terms of number of I/O operations.
2. Performance when a disk has failed.
3. Performance during rebuild
B+ Tree Index Files
AU: May-03,06,16,19, Dec.-17, Marks 16
• The B+ tree is similar to binary search tree. It is a balanced tree in which the internal nodes direct the search.
• The leaf nodes of B+ trees contain the data entries.
Structure of B+ Tree
• The typical node structure of B+ node is as follows –
Insertion Operation
Algorithm for insertion :
Step 1: Find correct leaf L.
Step 2: Put data entry onto L.
i) If L has enough space, done!
ii) Else, must split L (into L and a new node L2)
• Allocate new node
• Redistribute entries evenly
• Copy up middle key.
• Insert index entry pointing to L2 into parent of L.
Step 3: This can happen recursively
i) To split index node, redistribute entries evenly, but push up middle key. (Contrast with leaf splits.)
Step 4: Splits "grow" tree; root split increases height.
i) Tree growth: gets wider or one level taller at top.
Example 4.8.1 Construct B+ tree for following data. 30,31,23,32,22,28,24,29, where number of pointers that fit in
one node are 5.
Solution: In B+ tree each node is allowed to have the number of pointers to be 5. That means at the most 4 key
values are allowed in each node.
Step 1: Insert 30,31,23,32. We insert the key values in ascending order.
Step 2: Now if we insert 22, the sequence will be 22, 23, 30, 31, 32. The middle key 30, will go up.
Step 3: Insert 28,24. The insertion is in ascending order.
Step 4: Insert 29. The sequence becomes 22, 23, 24, 28, 29. The middle key 24 will go up. Thus we get the B+ tree.
Example 4.8.2 Construct B+ tree to insert the following (order of the tree is 3) 26,27,28,3,4,7,9,46,48,51,2,6
Solution:
Order means maximum number of children allowed by each node. Hence order 3 means at the most 2 key values are
allowed in each node.
Step 1: Insert 26, 27 in ascending order
Step 2: Now insert 28. The sequence becomes 26,27,28. As the capacity of the node is full, 27 will go up. The B+ tree
will be,
Step 4: Insert 4. The sequence becomes 3,4, 26. The 4 will go up. The partial B+ tree will be –
Step 5: Insert 7. The sequence becomes 4,7,26. The 7 will go up. Again from 4,7,27. the 7 will go up. The partial B+
Tree will be,
Step 6: Insert 9. By inserting 7,9, 26 will be the sequence. The 9 will go up. The partial B+ tree will be,
Step 7: Insert 46. The sequence becomes 27,28,46. The 28 will go up. Now the sequence becomes 9, 27, 28. The 27
will go up and join 7. The B+ Tree will be,
Step 8: Insert 48. The sequence becomes 28,46,48. The 46 will go up. The B+ Tree will become,
Step 9: Insert 51. The sequence becomes 46,48,51. The 48 will go up. Then the sequence becomes 28, 46, 48. Again
the 46 will go up. Now the sequence becomes 7,27, 46. Now the 27 will go up. Thus the B+ tree will be
Step 10: Insert 2. The insertion is simple. The B+ tree will be,
Step 11: Insert 6. The insertion can be made in a vacant node of 7(the leaf node). The final B+ tree will be,
Deletion Operation
Algorithm for deletion:
Step 1: Start at root, find leaf L with entry, if it exists.
Step 2: Remove the entry.
i) If L is at least half-full, done!
ii) If L has only d-1 entries,
• Try to re-distribute, borrowing keys from sibling.
(adjacent node with same parent as L).
• If redistribution fails, merge L and sibling.
Step 3: If merge occurred, must delete entry (pointing to L or sibling) from parent of L.
Step 4: Merge could propagate to root, decreasing height.
Example 4.8.3 Construct B+ Tree for the following set of key values
(2,3,5,7,11,17,19,23,29,31) Assume that the tree is initially empty and values are added in ascending order. Construct
B+ tree for the cases where the number of pointers that fit one node is four. After creation of B+ tree perform
following series of operations:
(a) Insert 9. (b) Insert 10. (c) Insert 8. (d) Delete 23. (e) Delete 19.
Solution: The number of pointers fitting in one node is four. That means each node contains at the most three key
values.
Step 1: Insert 2, 3, 5.
Step 2: If we insert 7, the sequence becomes 2, 3, 5, 7. Since each node can accommodate at the most three key, the
5 will go up, from the sequence 2, 3, 5, 7.
Step 4: Insert 17. The sequence becomes 5,7, 11,17. The element 11 will go up. Then the partial B+ tree becomes,
Step 8: Insert 31. The sequence becomes 19,23,29, 31. The 29 will go up. Then at the upper level the sequence
becomes 5,11,19,29. Hence again 19 will go up to maintain the capacity of node (it is four pointers three key values
at the most). Hence the complete B+ tree will be,
(a) Insertion of 9: It is very simple operation as the node containing 5,7 has one space vacant to accommodate. The
B+ tree will be,
(b) Insert 10: If we try to insert 10 then the sequence becomes 5,7,9,10. The 9 will go up. The B+ tree will then
become –
(c) Insert 8: Again insertion of 8 is simple. We have a vacant space at node 5,7. So we just insert the value over there.
The B+ tree will be-
(d) Delete 23: Just remove the key entry of 23 from the node 19,23. Then merge the sibling node to form a node
19,29,31. Get down the entry of 11 to the leaf node. Attach the node of 11,17 as a left child of 19.
(e) Delete 19: Just delete the entry of 19 from the node 19,29,31. Delete the internal node key 19. Copy the 29 up as
an internal node as it is an inorder successor node.
Search Operation
1. Perform a binary search on the records in the current node.
2. If a record with the search key is found, then return that record.
3. If the current node is a leaf node and the key is not found, then report an unsuccessful search.
4. Otherwise, follow the proper branch and repeat the process.
For example-
Example 4.9.1 Create B tree of order 3 for following data: 20,10,30,15,12,40,50. Solution: The B tree of order 3
means at the most two key values are allowed in each node of B-Tree.
Step 1: Insert 20,10 in ascending order
Step 2: If we insert the value 30. The sequence becomes 10,20,30. As only two key values are allowed in each node
(being order 3), the 20 will go up.
Step 4: Insert 12. The sequence becomes 10, 12, 15. The middle element 12 will go up.
Step 5: Insert 40
Step 6: Insert 50. The sequence becomes 30,40,50. The 40 will go up. But again it forms 12,20,40 sequence and then
20 will go up. Thus the final B Tree will be,
• That means, if we want to generate address for Stud_RollNo = 34789. Here if we use mod 10 hash function, it
always result in the same bucket address 9. There will not be any changes to the bucket address here.
• Hence number of data buckets in the memory for this static hashing remains constant throughout. In our example,
we will have ten data buckets in the memory used to store the data.
• If there is no space for some data entry then we can allocate new overflow page, put the data record onto that
page and add the page to overflow chain of the bucket. For example if we want to add the Stud_RollNo= 35111 in
above hash table then as there is no space for this entry and the hash address indicate to place this record at index
1, we create overflow chain as shown in Table 4.11.1.
Example 4.11.1 Why is hash structure not the best choice for a search key on which range of queries are likely ? AU:
May-06, Marks 8
Solution :
• A range query cannot be answered efficiently using a hash index, we will have to read all the buckets.
• This is because key values in the range do not occupy consecutive locations in the buckets, they are distributed
uniformly and randomly throughout all the buckets.
Advantages of Static Hashing
(1) It is simple to implement.MI
(2) It allows speedy data storage.
Disadvantages of Static Hashing
There are two major disadvantages of static hashing:
1) In static hashing, there are fixed number of buckets. This will create a problematic situation if the number of
records grow or shrink.
2) The ordered access on hash key makes it inefficient.
Open Hashing
The open hashing is a form of static hashing technique. When the collision occurs, that means if the hash key returns
the same address which is already allocated by some data record, then the next available data block is used to enter
new record instead of overwriting the old record. This technique is also called as linear probing. For example
Consider insertion of record 105 in the hash table below with the hash function h (key) mod 10.
The 105 is probed at next empty data block as follows -
Advantages:
1) It is faster technique.
2) It is simple to implement.
Disadvantages:
1) It forms clustering, as the record is just inserted to next free available slot.
2) If the hash table gets full then the next subsequent records can not be accommodated.
Dynamic Hashing
AU: May-04,07,18, Dec.-08,17, Marks 13
• The problem with static hashing is that it does not expand or shrink dynamically as the size of the database grows
or shrinks.
• Dynamic hashing provides a mechanism in which data buckets are added and removed dynamically and on-
demand.
• The most commonly used technique of dynamic hashing is extendible hashing.
Extendible Hashing
The extendible hashing is a dynamic hashing technique in which, if the bucket is overflow, then the number of
buckets are doubled and data entries in buckets are re- distributed.
Example of extendible hashing:
In extendible hashing technique the directory of pointers to bucket is used. Refer following Fig. 4.12.1
To locate a data entry, we apply a hash function to search the data we us last two digits of binary representation of
number. For instance binary representation of 32* = 10000000. The last two bits are 00. Hence we store 32*
accordingly.
Insertion operation :
• Suppose we want to insert 20* (binary 10100). But with 00, the bucket A is full. So we must split the bucket by
allocating new bucket and redistributing the contents, bellsp across the old bucket and its split image.
• For splitting, we consider last three bits of h(r).
• The redistribution while insertion of 20* is as shown in following Fig. 4.12.2.
The split image of bucket A i.e. A2 and old bucket A are based on last two bits i.e. 00. Here we need two data pages,
to adjacent additional data record. Therefore here it is necessary to double the directory using three bits instead of
two bits. Hence,
• There will be binary versions for buckets A and A2 as 000 and 100.
• In extendible hashing, last bits d is called global depth for directory and d is called local depth for data pages or
buckets. After insetion of 20*, the global depth becomes 3 as we consider last three bits and local depth of A and A2
buckets become 3 as we are considering last three bits for placing the data records. Refer Fig. 4.12.3.
(Note: Student should refer binary values given in Fig. 4.12.2, for understanding insertion operation)
• Suppose if we want to insert 11*, it belongs to bucket B, which is already full. Hence let us split bucket B into old
bucket B and split image of B as B2.
• The local depth of B and B2 now becomes 3.
• Now for bucket B, we get and 1=001
11 100011
• For bucket B2, we get
5=101
29 = 11101
and 21 =10101
After insertion of 11* we get the scenario as follows,
Example 4.12.1 The following key values are organized in an extendible hashing technique.
13589 12 17 28. Show the extendible hash structure for this file if the hash function is h(x) =
x mod 8 and buckets can hold three records. Show how extendable hash structure changes as the result of each of
the following steps:
Insert 2
Insert 24
Delete 5
Delete 12
Solution:
Step 1: Initially we assume the hash function based on last two bits, of result of hash function.
1 mod 8=1=001
3 mod 8=3 = 011
5 mod 8=5= 101
8 mod 8=0=000
9 mod 8=1=001
12 mod 8 = 4=100
17 mod 8=1=001
28 mod 84 = 100
The extendible hash table will be,
Hence we will extend the table by assuming the bit size as 3. The above indicated bucket A will split based on 001
and 101.
a) Insert 2 will be 2 mod 8 = 2 = 010. If we consider last two digits i.e. 10 then there is no bucket. So we get,
b) Insert 24 : 24 mod 8 = 0 = 000. The bucket in which 24 can be inserted is 8, 12, 28. But as this bucket is full we split
it in two buckets based on digits 000 100.
c) Delete 5: On deleting 5, we get one bucket pointed by 101 as empty. This will also result in reducing the local
depth of the bucket pointed by 001. Hence we get,
d) Delete 12: We will simply delete 12 from the corresponding bucket there can not be any merging of buckets on
deletion. The result of deletion is as given below
Difference between Static and Dynamic Hashing
• Discretionary Access Control allows each user or subject to control access to their own data.
• In DAC, owner of resource restricts access to the resources based on the identity of users.
• DAC is typically the default access control mechanism for most desktop operating doy systems.
• Each resource object on DAC based system has Account Control List (ACL) associated with it.
• An ACL contains a list of users and groups to which the user has permitted access together with the level of access
for each user or group.
• For example - The ACL is an object centered description of access rights as follows-
test1.doc: {Prajka: read}
test2.exe: {Ankita: execute}, {Prajkta: execute}
test3.com: (Ankita: execute, read}, {Prajkta: execute, read, write}
• Object access is determined during Access Control List (ACL) authorization and based on user identification and/or
group membership.
• Under DAC a user can only set access permissions for resources which they already own.
• Similarly a hypothetical user A cannot change the access control for a file that is owned by user B. User A can,
however, set access permissions on a file that he/she Jono owns.
• User may transfer object ownership to another user(s).
• User may determine the access type of other users.
• The DAC is easy to implement access control model.
Advantages:
(1) It is flexible.
(2) It has simple and efficient access right management.
(3) It is scalable. That means we can add more users without any complexity.
Disadvantages:
(1) It increases the risk that data will be made accessible to users that should not necessarily be given access.
(2) There is no control over information flow as one user can transfer ownership to another user.
istributed Databases
AU: Dec.-04,07,16,17,19, May-03,06,14,16,17,19, Marks 16
Definition of distributed databases:
• A distributed database system consists of loosely coupled sites (computer) that share no physical components and
each site is associated a database system.
• The software that maintains and manages the working of distributed databases is called distributed database
management system.
• The database system that runs on each site is independent of each other. Refer Fig. 5.1.1.
The transactions can access data at one or more sites.
Advantages of distributed database system
(1) There is fast data processing as several sites participate in request processing. 2.
(2) Reliability and availability of this system is high.
(3) It possess reduced operating cost.
(4) It is easier to expand the system by adding more sites.
(5) It has improved sharing ability and local autonomy.
Disadvantages of distributed database system
(1) The system becomes complex to manage and control.
(2) The security issues must be carefully managed.
(3) The system require deadlock handling during the transaction processing otherwise the entire system may be in
inconsistent state.
(4) There is need of some standardization for processing of distributed database system.
Difference between distributed DBMS and centralized DBMS
• In this system, all the sites are aware of the other sites present in the system and they all cooperate in processing
user's request.
• Each site present in the system, surrenders part of its autonomy in terms of right to change schemas or software.
• The homogeneous database system appears as a single system to the user.
(2) Heterogeneous databases
• The heterogeneous databases are kind of database systems in which different sites have different schema or
software. Refer Fig. 5.1.3.
• The participating sites are not aware of other sites present in the system.
• These sites provide limited facilities for cooperation in transaction processing.
Architecture
• Following is an architecture of distributed databases. In this architecture the local database is maintained by each
site.
• Each site is interconnected by communication network.
When user makes a request for particular data at site Si then it is first searched at the local database. If the data is
not present in the local database then the request for that data is passed to all the other sites via communication
network. Each site then searches for that data at its local database. When data is found at particular site say Sj then
it is transmitted to site Si via communication network.
Data Storage
There are two approaches of storing relation r in distributed database -
(1) Replication: System maintains multiple copies of data, stored in different sites, for grind faster retrieval and fault
tolerance.
(2) Fragmentation: Relation is partitioned into several fragments stored in distinct sites.
Data Replication
• Concept: Data replication means storing a copy or replica of a relation fragments in two or more sites.
• There are two methods of data replication replication. (1) Full replication (2) Partial replication
• Full replication: In this approach the entire relation is stored at all the sites. In this approach full redundant
databases are those in which every site contains a copy of entire database.
• Partial replication: In this approach only some fragments of relation are replicated on the sites.
Advantages:
(1) Availability: Data replication facilitates increased availability of data.
(2) Parallelism: Queries can be processed by several sites in parallel.
(3) Faster accessing: The relation r is locally available at each site, hence data accessing becomes faster.
Disadvantages:poi
(1) Increased cost of update: The major disadvantage of data replication is increased betcost of updated. That
means each replica of relation r must be updated from all the sites if user makes a request for some updates in
relation.
(2) Increased complexity in concurrency control: It becomes complex to implement the concurrency control
mechanism for all the sites containing replica.
Data Fragmentation
• Concept: Data fragmentation is a division of relation r into fragments r1,r2, r3,...,rn which contain sufficient
information to reconstruct relation r.
• There are two approaches of data fragmentation - (1) Horizontal fragmentation and (2) Vertical fragmentation.
• Horizontal fragmentation: In this approach, each tuple of r is assigned to one or more fragments. If relation R is
fragmented in r1 and r2 fragments, then to bring these fragments back to R we must use union operation. That
means R=r1ur2
• Vertical fragmentation: In this approach, the relation r is fragmented based on one or more columns. If relation R
is fragmented into r1 and r2 fragments using vertical fragmentation then to bring these fragments back to original
relation R we must use join operation. That means R= r1 r2
• For example - Consider following relation r
Student(RollNo, Marks, City)
The values in this schema are inserted as
Horizontal Fragmentation 1:
SELECT * FROM Student WHERE Marks >50 AND City='Pune'
We will get
Horizontal Fragmentation 2:
SELECT * FROM Student WHERE Marks >50 AND City="Mumbai'
We will get
Vertical Fragmentation 1 :
SELECT * FROM RollNo
Vertical Fragmentation 2:
SELECT * FROM city
Transaction Processing
Basic Concepts
In distributed system transaction initiated at one site can access or update data at other sites. Let us discuss various
basic concepts used during transaction processing in distributed systems -
• Local and global transactions :
Local transaction Ti is said to be local if it is initiated at site Si and can access or update data at site Si only.
Global transaction Ti initiated by site Si is said to be global if it can access or update data at site Si, Sj,Sk and so on.
• Coordinating and participating sites:
The site at which the transaction is initiated is called coordinating site. The participating sites are those sites at which
the sub-transactions are executing. For example - If site S1 initiates the transaction T1 then it is called coordinating
site. Now assume that transaction T1 (initiated at S1) can access site S2 and S3. Then sites S2 and S3 are called
participating sites.
To access the data on site S2, the transaction T1 needs another transaction T12 on site S2 similarly to access the data
on site S3, the transaction T2 needs some transaction say T13 on site S3. Then transactions T12 and T13 are called
sub-transactions. The above described scenario can be represented by following Fig. 5.1.6.
• Transaction manager :
The transaction manager manages the execution of those transactions (or subtransactions) that access data stored in
a local site.
(1) To maintain the log for recovery purpose.
(2) Participating in coordinating the concurrent execution of the transactions executing balls at that site.
• Transaction coordinator:
The transaction coordinator coordinates the execution of the various transactions (both local and global) initiated at
that site.
The tasks of Transaction coordinator are -
(1) Starting the execution of transactions that originate at the site.
(2) Distributing subtransactions at appropriate sites for execution
Let TC denotes the transaction coordinator and TM denotes the transaction manager, then the system architecture
can be represented as,
Failure Modes
There are four types of failure modes,
1. Failure of site
2. Loss of messages
3. Failure of communication link
4. Network partition
The most common type of failure in distributed system is loss or corruption of messages. The system uses
Transmission Control Protocol(TCP) to handle such error. This is a standard connection oriented protocol in which
message is transmitted from one end to another using wired connection.
• If two nodes are not directly connected, messages from one to another must be routed through sequence of
communication links. If the communication link fails, the messages are rerouted by alternative links.
• A system is partitioned if it has been split into two subsystems. This is called partitions. Lack of connection between
the subsystems also cause failure in distributed system.
Commit Protocols
Two Phase Commit Protocol
• The atomicity is an important property of any transaction processing. What is this atomicity property? This
property means either the transaction will execute completely or it won't execute at all.
• The commit protocol ensures the atomicity across the sites in following ways -
i) A transaction which executes at multiple sites must either be committed at all the sites, or aborted at all the sites.
ii) Not acceptable to have a transaction committed at one site and aborted at another.
• There are two types of important sites involving in this protocol -
• One Coordinating site
• One or more participating sites.
Two phase commit protocol
This protocol works in two phases - i) Voting phase and ii) Decision phase.
Phase 1: Obtaining decision or voting phase
Step 1: Coordinator site Ci asks all participants to prepare to commit transaction Ti.
• Ci adds the records <prepareT> to the log and writes the log to stable storage.
• It then sends prepare T messages to all participating sites at which T will get executed.
Step 2: Upon receiving message, transaction manager at participating site determines if it can commit the
transaction
• If not, add a record <no T> to the log and send abort T message to coordinating site Ci.
Failure of site
There are various cases at which failure may occur,
(1) Failure of participating sites
• If any of the participating sites gets failed then when participating site Si recovers, it examines the log entry made
by it to take the decision about executing transaction.
• If the log contains <commit T> record: participating site executes redo (T)
• If the log contains <abort T> record: participating site executes undo (T)
• If the log contains <ready T> record: participating site must consult Coordinating site to take decision about
execution of transaction T.
• If T committed, redo (T)
• If T aborted, undo (T)
• If the log of participating site contains no record then that means Si gets failed before responding to Prepare T
message from coordinating site. In this case it must abort T
(2) Failure of coordinator
• If coordinator fails while the commit protocol for T is executing then participating sites must take decision about
execution of transaction T:
i) If an active participating site contains a <commit T> record in its log, then T site must be committed.
ii) If an active participating site contains an <abort T> record in its log, then T must be aborted.
iii) If some active participating site does not contain a <ready T> record in its log, then the failed coordinator Ci
cannot have decided to commit T. Can therefore abort T.
iv) If none of the above cases holds, then all participating active sites must have a <ready T> record in their logs, but
no additional control records (such as <abort T> of <commit T>). In this case active sites must wait for coordinator
site Ci to recover, to find decision.
Two phase locking protocol has blocking problem.
What is blocking problem?
It is a stage at which active participating sites may have to wait for failed coordinator site to recover.
The solution to this problem is to use three phase locking protocol.
Three Phase Commit Protocol
• The three phase locking is an extension of two phase locking protocol in which eliminates the blocking problem.
• Various assumptions that are made for three phase commit protocol are -
• No network partitioning.
• At any point at least one site must be up.
• At the most k sites (participating as well as coordinating) can fail.
• Phase 1: This phase is similar to phase 1 of two phase protocol. That means Coordinator site Ci asks all participants
to prepare to commit transaction Ti. The coordinator then makes the decision about commit or abort based on the
response from all the participating sites.
• Phase 2: In phase 2 coordinator makes a decision as in 2 Phase Commit which is called the pre-commit decision
<Pre-commit, T>, and records it in multiple (at least K) participating sites.
• Phase 3: In phase 3, coordinator sends commit/abort message to all participating Brits sites.
• Under three phase protocol, the knowledge of pre-commit decision can be used to commit despite coordinator
site failure. That means if the coordinating site in case gets failed then one of the participating site becomes the
coordinating site and der consults other participating sites to know the Pre-commit message which they possess.
Thus using this pre-commit t message the decision about commit/abort is taken by this new coordinating site.
• This protocol avoids blocking problem as long as less than k sites fail.
Advantage of three phase commit protocol
(1) It avoid blocking problem.
Disadvantage of three phase commit protocol
(1) The overhead is increased.
4.10. QUERY PROCESSING OVERVIEW: A query expressed in a high-level query language such as SQL must first be
scanned, parsed, and validated. The scanner identifies the query tokens—such as SQL keywords, attribute names,
and relation names—that appear in the text of the query, whereas the parser checks the query syntax to determine
whether it is formulated according to the syntax rules (rules of grammar) of the query language. The query must
also be validated by checking that all attribute and relation names are valid and semantically meaningful names in
the schema of the particular database being queried. An internal representation of the query is then created,
usually as a tree data structure called a query tree. It is also possible to represent the query using a graph data
structure called a query graph. The DBMS must then devise an execution strategy or query plan for retrieving the
results of the query from the database files. A query typically has many possible execution strategies, and the
process of choosing a suitable one for processing a query is known as query optimization. Figure shows the
different steps of processing a high-level query. The query optimizer module has the task of producing a good
execution plan, and the code generator generates the code to execute that plan. The runtime database processor
has the task of running (executing) the query code, whether in compiled or interpreted mode, to produce the query
result. If a runtime error results, an error message is generated by the runtime database processor. Code can be:
Executed directly (interpreted mode) Stored and executed later whenever needed (compiled mode) Figure . Typical
steps when processing a high-level query. The term optimization is actually a inaccurate name because in some
cases the chosen execution plan is not the optimal (or absolute best) strategy—it is just a reasonably efficient
strategy for executing the query PP Finding the optimal strategy is usually too time-consuming—except for the
simplest of queries. In addition, trying to find the optimal query execution strategy may require detailed information
on how the files are implemented and even on the contents of the files—information that may not be fully available
in the DBMS catalog. Hence, planning of a good execution strategy may be a more accurate description than query
optimization. For lower-level navigational database languages in legacy systems—such as the network DML or the
hierarchical DL/1—the programmer must choose the query execution strategy while writing a database program. If
a DBMS provides only a navigational language, there is limited need or opportunity for extensive query optimization
by the DBMS; instead, the programmer is given the capability to choose the query execution strategy. On the other
hand, a high-level query language—such as SQL for relational DBMSs (RDBMSs) or OQL for object DBMSs (ODBMSs)
—is more declarative in nature because it specifies what the intended results of the query are, rather than
identifying the details of how the result should be obtained. Query optimization is thus necessary for queries that
are specified in a high-level query language. We will concentrate on describing query optimization in the context of
an RDBMS because many of the techniques we describe have also been adapted for other types of database
management systems, such as ODBMSs. A relational DBMS must systematically evaluate alternative query execution
strategies and choose a reasonably efficient or near-optimal strategy. Each DBMS typically has a number of general
database access algorithms that implement relational algebra operations such as SELECT or JOIN or combinations of
these operations. Only execution strategies that can be implemented by the DBMS access algorithms and that apply
to the particular query, as well as to the particular physical database design, can be considered by the query
optimization module. Translating SQL Queries into Relational Algebra: In practice, SQL is the query language that is
used in most commercial RDBMSs. An SQL query is first translated into an equivalent extended relational algebra
expression—represented as a query tree data structure—that is then optimized. Typically, SQL queries are
decomposed into query blocks, which form the basic units that can be translated into the algebraic operators and
optimized. A query block contains a single SELECT-FROM-WHERE expression, as well as GROUP BY and HAVING
clauses if these are part of the block. Hence, nested queries within a query are identified as separate query blocks.
Because SQL includes aggregate operators—such as MAX, MIN, SUM, and COUNT—these operators must also be
included in the extended algebra. Consider the following SQL query on the EMPLOYEE relation : SELECT Lname,
Fname FROM EMPLOYEE WHERE Salary > ( SELECT MAX (Salary) FROM EMPLOYEE WHERE Dno=5 ); This query
retrieves the names of employees (from any department in the company) who earn a salary that is greater than the
highest salary in department 5. The query includes a nested subquery and hence would be decomposed into two
blocks. The inner block is: SELECT MAX (Salary) FROM EMPLOYEE WHERE Dno=5 ) This retrieves the highest salary in
department 5. The outer query block is: SELECT Lname, Fname FROM EMPLOYEE WHERE Salary > c ; FROM where c
represents the result returned from the inner block. The inner block could be translated into the following extended
relational algebra expression: ℑMAX Salary(σDno=5(EMPLOYEE)) and the outer block into the expression: π
Lname,Fname(σSalary>c(EMPLOYEE)) The query optimizer would then choose an execution plan for each query
block. Notice that in the above example, the inner block needs to be evaluated only once to produce the maximum
salary of employees in department 5, which is then used—as the constant c—by the outer block. We called this a
nested query (without correlation with the outer query) in Section 5.1.2. It is much harder to optimize the more
com-plex correlated nested queries (see Section 5.1.3), where a tuple variable from the outer query block appears in
the WHERE-clause of the inner query block.