DBMS Theory Book
DBMS Theory Book
MANAGEMENT
SYSTEM
CONTENTS
DATABASE MANAGEMENT SYSTEM
CHAPTER - 1
Functions Dependency and Normalization
• Introduction …………………………………………………………………………. 1
• Relation Database Management System …………………………………………...... 2
• Functional Dependency …………………………………………………………….....4
• Properties of FD’s…………………………………………………………………….. 5
• Attribute Set Closure (X+)……………………………………………………………. 5
• Super Key (OR) Candidate Key……………………………………………………… 5
• Minimal Super-key is Candidate Key ………………………………………….......... 5
• Membership Test …………………………………………………………………….. 6
• Schema Refinement (Normalization)………………………………………………… 6
• Testing Condition to check whether lossy (or) lossless …………………………….. 9
▪ When to merge 2 table …………………………………………………………......... 5
• Dependency Preserving ……………………………………………………………… 6
• Normal Form ………………………………………………………………………. 11
• Possible way of non-trivial FD which suffer Redundancy………………………….. 12
• How to convert relation into BCNF ………………………………………………… 15
• Equality of FD’s Set …………………………………………………………………17
• Canonical Cover (OR) Minimal Cover …………………………………………….. 18
• Foreign Key ………………………………………………………………………… 20
CHAPTER - 2
E-R Model
2. E-R Model.................................................................................................. 37-48
• E-R model………………………………………………………………………….. 37
CHAPTER - 3
Query Language
CHAPTER - 4
Transaction and Concurrency Control
• Transaction …………………………………………………………………………. 86
• Classification of Schedule……………………………………………………………72
• Classification of schedule based on serializability…………………………………. 97
• Two types of schedule based on serializability ……………………………………. 97
• Conflict Serializable Schedule ………………………………………………………97
• Conflict Serializable Schedule……………………………………………………….99
• View Serializable Schedule ………………………………………………………..102
• Concurrency Control Protocol………………………………………………...……104
• Locking Protocol……………………………………………………………………104
• Basic Time-stamp Ordering ………………………………………………………..109
• Thomas’s Write Time Stamp Protocol……………………………………………...110
• Deadlock Prevention Protocol………………………………………………………111
CHAPTER - 5
INDEXING
5. Indexing ……………………………………................................................127-139
CONTENT
Functions Dependency and Normalization
• Introduction …………………………………………………………………………. 1
• Relation Database Management System …………………………………………...... 2
• Functional Dependency …………………………………………………………….....4
• Properties of FD’s…………………………………………………………………….. 5
• Attribute Set Closure (X+)……………………………………………………………. 5
• Super Key (OR) Candidate Key……………………………………………………… 5
• Minimal Super-key is Candidate Key ………………………………………….......... 5
• Membership Test …………………………………………………………………….. 6
• Schema Refinement (Normalization)………………………………………………… 6
• Testing Condition to check whether lossy (or) lossless …………………………….. 9
▪ When to merge 2 table …………………………………………………………......... 5
• Dependency Preserving ……………………………………………………………… 6
• Normal Form ………………………………………………………………………. 11
• Possible way of non-trivial FD which suffer Redundancy………………………….. 12
• How to convert relation into BCNF ………………………………………………… 15
• Equality of FD’s Set …………………………………………………………………17
• Canonical Cover (OR) Minimal Cover …………………………………………….. 18
• Foreign Key ………………………………………………………………………… 20
1 Functional Dependency and Normalization
Introduction
Database: - Collection of data (inter-related data OR logically related data)
DBMS: - collection of interrelated data and a set of programs to access those data.
Primary Goal
The primary goal of a DBMS is to provide a way to store and retrieve database information that is
both convenient and efficient.
Aim of DBMS
To provide data Independency: - Hide the physical details to the external user.
According to ‘CODD’, basic aim is to provide data independency, there should be at-least 2-level
of abstraction i.e., we need at least two level of interface between User and Database.
To provide this, 3-level of abstraction is used User.
External Schema-1 External Schema-2
Conceptual
DBMS
Schema
Physical
Schema
DB
Let R be the relation schema with n-attribute R(A1, A2,A3). Then how many super key are
possible.
(i) With only candidate key {A1}
Sol. {A1, A1A2 , A1A3 , A1A2A3} i.e. 4 super key possible.
Let R be the relation schema with n-attribute R(A1, A2….. An). Then how many super key are
possible with only candidate key {A1}
Sol. Total number of super key possible is 2n-1.
(i) with only candidate key {A1, A2}
Sol. When A1 is candidate key{ A1, A1A2 , A1A3 , A1A2A3}. When A2 is candidate key{ A2,
A1A2 , A2A3 , A1A2A3}.But A1A2,A1A2A3 are common in both set so count these type of
super key only once.So total super key possible is 6.
Let R be the relation schema with n-attribute R(A1, A2….. An). Then how many super key are
possible with only candidate key {A1,A2}
Sol. Total number of super key possible is 2n-1+2n-1 - 2n-2.
Functional Dependency
Functional Dependency is a constraint between two sets of attributes from the database. It tells
relationship between attribute OR constraints between set of attributes of DB.
Let R be the relational schema with same set of attribute X, Y. Let t1, t2 any tuple of R such that
X Y is Exist in R only if t1.X = t2.X then t1.Y = t2.Y.
X Y X Y X Y
X1 Y1 X1 Y1 X1 Y1
X1 Y1 X1 Y2 X1 Y1
X2 Y1 X2 Y2 X2 Y2
XY XY XY
R(ABCDE) FD ={AB C, C D, B E}
(AB)+ = {ABCDE}
So, AB is Super Key.
Test for C.K A+ = {A} B+ = {BE}
Let delete course C2, then we also delete tuple (t2, t3, t4).
So, we loss other data.
To Eliminate these anomalies, we delete redundancy.
Redundancy can be eliminated by Decomposition of Table.
Decomposition: Splitting relation into 2 or more sub-relation.
Sid Sname Sid Cid Cid Cname Fee
S1 A S1 C1 C1 DB 8K
S2 A S1 C2 C 2 Java 7K
S3 B S2 C2 PK: Cid
PK: Sid S3 C2
S3 C1
PK: SidCid
Properties of Decomposition:
(1) Lossless Decomposition
(2) Dependency preserving
(1) Lossless Join Decomposition:
Because of decomposition, after join again, it should not create any extra tuple.
i.e., R1 natural join R 2 R ,it is lossless decomposition
A B C
1 1 2
2 1 3
3 2 3
A B A C
1 1 1 2
2 1 2 3
3 2 3 3
A B C
1 1 2
2 1 3
3 2 3
R1 R2
FD set F1 FD set F2
R(ABCD)
FD = {AB, BC, CD, DA}
Decomposition = {AB. BC. CD}
R1 AB R 2 BC R 3 CD
FD’s are A B BC CD
BA CB DC
R(ABCD)
X Y
X1 Y2
X Y2
Redundant Copy 1 X Y where X is not a key
X2 Y3
X1 Y2
If non-trivial FD (XY) with X is a key, then X, Y attribute not suffers from redundancy.
X Y
X1 Y1
X2 Y1
X3 Y1 X is a key so no redundancy possible.
SidCname: CK
But, Sid Sname is a partial dependency.
Partial Dependency: Let R be a relational schema with X, Y, A are attribute set where X is
any primary key, Yis proper subset of primary key, A is Non-Prime attribute, then if FD Y
A exist, then it is partial dependency.
Third Normal Form (3NF): -
---------------------------- DATABASE MANAGEMENT SYSTEM ----------------------------
14 | DBMS : CS
Relation R is in 3NF iff every non-trivial FD(XY) satisfy the following condition:-
R should be in 2NF and
(1) X: Super-key OR
(2) Y: Prime Attribute (OR)
R(ABCD) FD={AB,BC,CD}
A B C D
A1 B1 C1 D1
A2 B2 C1 D1
A3 B3 C2 D2
A4 B1 C1 D1
A5 B2 C1 D1
Note: 3NF is more accurate normal form as BCNF not always dependency preserve.
(1) BINARY RELATION: Relation with 2-attribute
Example: R(AB), the possible FD with their key and highest normal form are:
(1) A B A:CK BCNF (HNF)
(2) B A B:CK BCNF (HNF)
(3) A B, B A A, B: CK BCNF (HNF)
(4) Non non-trivial AB: CK BCNF (HNF)
Note: If a relation with 2-Attribute, always in BCNF.
(2) Relation with only simple CK: If every primary key are simple, then partial dependency
not possible. So, a relation with simple primary key always in 2NF but may or may not in
3NF (OR) BCNF.
R ABCDE A B B AC C DE
2NF yes yes yes
3NF yes yes no
R(ABCDE)
{ABC, CD, DE, EA}
CK = AB, EB, DB, CB
3NF yes yes yes yes
BCNF no no no no
Minimal cover (OR) canonical cover means elimination of extraneous attribute (OR)
redundant FD’s.
Note: Minimal cover may not be unique, but all minimal cover are logically equivalent.
Possible Cases:
1 AB C, A B A C, A B
BisExtraneous Attribute
2 AB C, A C A C
BisExtraneous Attribute
3 A BC, B C A B, B C
CisExtraneous Attribute
4 AB CD, BC D AB C, BC D
DisExtraneous Attribute
Possible Case-1
{ABC, AB} {AB, AB}
Here, B is Extraneous Attribute
Proof: Let ‘A’ is Redundant attribute
Foreign Key
Foreign Key: are set of attribute references to primary key (OR) alternative key of the same
(OR) same other table.
* Used to relate data with one table to another table
* Used to maintain the consistency among tuples in the two relations
Referential integrity:- Referential integrity is a constraint between the two relational
schemas R1 and R2 that is specified in the criteria for a foreign key, which are provided
below. If the conditions are met, a set of attributes FK in relation schema R1 is a foreign
key of R1 that refers relation R2.
PRACTICE QUESTIONS
1. Let R is a relational schema with 5-attribute i.e. R(a1, a2,......a5.), and if a1 and a2 are candidate
key, then how many super key possible?
Min : 24.0
Max : 24.0
Answer 24
Sol. 24
251 251 252 24 24 23 32 8 24
2. Let R(ABCD) is a relation-chema with following dependencies, the total number of candidate
key possible is_________
FD's {A B, B C, C D, D A}
Min : 4.0
Max : 4.0
Answer 4
Sol. 4
A+ = ABCD, B+ = BCDA, C+ = CDAB, D + =DABC
3. Let a relational schema R( A, B, C. D) where AB and BC are candidate key, then total number
of super key possible is__________
Min : 6.0
Max : 6.0
Answer 6
Sol. 3
When AB is candidate key. then total number of super key is 24 2 22 4
When BC is candidate key then total number of super key is
24-2 = 22 = 4 but there exist some super key which are common.
So, Total number of super key is
242 242 243 22 22 21 4 2 6
4. Given a relation R(A, B, C) and set of functional dependencies FD = {A BC, B C, C
B} then what are the candidate key of R?
(A) A only (B) AB only
(C) AB, AC only (D) A, B, C
Sol. (A)
AT = ABC
B+ = BC
Min : 31.0
Max : 31.0
Answer 31
Sol. 31
2* – 1 = 31
10. Consider the following relational schema R(ABCDE) with following FD's
FD's = {AB CD, C D, D B, B A}
then total number of candidate key possible is_
(A) 0 (B) l (C) 2 (D)3
Sol. (D)
Following more the candidate key of R.
EB+ = BACDE
11. The following functional dependencies hold for relation R(A,B,C), S(B,D,E)
B A, A C
The relation R contains 500 tuples and relation S contain 100 tuples. What is the maximum
number of tuple possible in natural join of R S i.e._______
Min : 100.0
Max : 100.0
Answer 100
Sol. 100
As B is the candidate key of relation R(A, B, C). B is foreign key of S-relation S contain 100
tuples. So, after join operation, maximum number of tuple is 100.
12. Given a relation R(A,B,C) and FD set is {A BC, B C, C B} the highest normal form
of R is
(A) INF (B) 2NF (C) 3NF (D) BCNF
Sol. (B)
Candidate key of R is {A}
So, there exist no partial dependencies.
13. Let the relation R(A, B, C, D, E, F) and candidate key are (AB, AE), then number of superky
are_________
Min: 24.0
Max : 24.0
D A
R1(AD) has FD . So, {D, A} is candidate key of R1
A D
R2(BCD) has FD {BCD BCD}. So, BCD is candidate key.
So, decomposition is lossless but not dependency preserve.
15. Consider the following relations R(ABCDEF) with FD’s {A FC, C D, B E}. Then to
make relation into 3NF, minimum number of table required is ________
Min : 4.0
Max : 4.0
Answer 4
Sol. 4
Candidate key of R is {AB}
So, B E is a partial dependencies.
So, Total tables required is
A L
1 2
2 3
4 3
5 1
6 4
Sol. (A)
Every BCNF relation is also 3NF but not vice-versa
23. Consider a relation R(MNRQT) with FD set
F1 = {M TN, P QM}
F2 = {M N, P Q, P MT, MK T}
Which of the following is correct?
(a) F1 and F1 are equivalent (B) Fl and F2 are not equivalent
(C) F1 F2 (D) F2 F1
Sol. (A)
F1 covers F2 and F2 covers F1. So, both FD set are equivalent.
24. Consider the relation R(A, B, C, D, E, F) with FD’s {AB C, C D, D E, E F, F
B}
The maximum number of candidate key possible is__________
Min : 5.0
Max : 5.0
Answer 5
Sol. 5
(AB)+ = ABCDEF
(AC) + = ABCDFE
(AD)+ = ABCDEF
(AF) + = ABCDEF
(AE) + = ABCDEF
25. Consider the following relation
R(MNOPQR) with FD set
{M N, O R, Q R}. The minimum number of relation required to decompose relation
R into 2NF which satisfy lossless join and dependency preserving decomposition
is_________?
Min: 4.0
Max : 4.0
Answer 4
Sol. 4
Candidate key of R is {MOQ}
---------------------------- DATABASE MANAGEMENT SYSTEM ----------------------------
30 | DBMS : CS
So, partial dependencies are M N, O R, Q R
So, decompose relation R into
R1 (MN) R2 (OP) R3 (QR) R4 (MOQ)
26. Consider the following relation R(ABCDE) with functional dependencies set F = {AB C,
AB D, D A, BC D, BC E} what will be the highest satisfied
form satisfied by R?
(A) INF (B) 2NF (C) 3NF (D) BCNF
Sol. (C)
Candidate key of relation R is AB, BD, BC.
D A not follows the condition of BCNF
So, R not m BCNF
27. Consider the following two relation R1(ABC) and R2(ABC) with following FD set
respectively
FD1 = {A B, B C, A C}
FD2 = {A B, B C, AB C}
then what is the relationship between R1 and R1 ?
(A) R 1 R 2 (B) R 2 R 1 (C) R 2 R 1 (d) no relation
Sol. (C)
Both relation R1 and R2 are equivalent because f1 and f2 cover f1
28. Consider a following relation-schema R(ABCD) with following FD’s
FD = {A B, B C, C D, AB D}
then,
(a) A D is member of FD set
(b) BC D is member of FD set
Total number of statement which are true is_________
Min : 2.0
Max : 2.0
Answer 2
Sol. 2
find A+
A+ = ABCD i.e. A D
find (BC)+ = BCD i.e. BC D
So, both statement arc true
29. Consider the following set of functional dependencies on relational schema R(ABC)
---------------------------- DATABASE MANAGEMENT SYSTEM ----------------------------
Functional Dependency and Normalization | 31
{A BC, B C, A B, AB C}
The canonical cover for F is:-
(A) {A B, B C, A C (B) {A B, B C A, B C}
(C) {A B, B C} (D) none
Sol. (C)
Canonical cover means no attribute is extraneous and no FD's is extraneous.
i.e.,
= A B, A C, B C, A B, AB C
= A B, B C
30. Consider the following R(A, B, C, D) with FD set {AB C, C A, C D}. Total number
of candidate are___________
Min : 2.0
Max : 2.0
Answer 2
Sol. 2
(AB)+ = ABCD
(BC)+ = BCAD
31. Consider the following relation R (A B C D E) with FD set
F1 = [A EB4 C DA}
F2 = {A B, C D, C AE, AB E}
which of the following is True?
(A) F1 F2 (B) F1 F2
Sol. (C)
Check f1 comes f2
A B, A+ = AEB
C D, C+ = CDA
C AE, C+ = CDAE
AB E, AB+ = ABE
Check f2 comes f1
A EB A+ = ABCDE
C DA C+ = CAED
A B C D
1 b1 c1 d1
1 b1 c2 D2
2 b1 c3 d1
3 b2 c2 d3
which of the following functional dependencies will hold on the above relational instances?
(A) A B (B) B D
(C) C B (D) D A
Sol. (A)
If there exist x y FD, then for every unique x, there should be a unique y.
A B
1 b1
1 b2
2 b1
3 b2
EXERCISE QUESTIONS
Attribute: The descriptive features that each member of an entity collection possesses are known as
attributes. It is represented by ovel.
Multi-valued Attribute: Multi-valued attributes can take up and store more than one value at a time
for an entity instance from a set of possible values.It is represented by Double ovel.
Derived Attribute: Value of attribute is derived from another stored attribute.It is represented by
dotted ovel.
Age – is Derived attribute which is derived from current date and date of birth.
Date of Birth – is stored attribute
Relationship set: A group of relationships of the same type is known as a relationship set.It
is denoted by diamond .
Domain:-The value set (or domain of values) that corresponds to each simple attribute of an
entity type describes the range of values that may be provided to that attribute for each
particular entity.
Degree of Relationship Type: The degree of a relationship type is the number of
participating entity types.
Student Enroll
course
Here,2 entity are related with relationship set ‘enroll’ .So degree is 2.
Descriptive Attributes:- A relationship may also have attributes called descriptive attributes
Constraints on Binary Relationship Types:- The combinations of entities that may be
included in the corresponding relationship set are typically constrained by particular
relationship kinds.2 main types of constraints are:-
(1) Participation
(2) Cardinality
(1) PARTICIPATION: - The participation constraint states whether an entity's existence is
dependent on its relationship (via the relationship type) with another entity. The minimal
number of relationship instances in which each entity may participate is specified by
constraint.
Let constraints is one Employee manages multiple departments. But each department should
be managed by single employee.
Total Participation: Every entity of entity set relating with the another entity set via
relationship set. Total participation is also called existence dependency
Department
Denoted by Double lines
Partial Participation: Every entity of entity set is not relating with the another entity set via
relationship set.
Employee
Denoted by single line
(2) CARDINALITY: The maximum number of relationship instances in which an entity can
take part is indicated by the cardinality ratio for a binary relationship i.e each entity of first
set is related wit how many entity of other entity set.
4-type of Cardinality:
(1) 1:1 (2) 1:M (3) M:N
(1) 1:1:- An entity in A has a maximum of one entity in B as an association, and vice versa
for entity in B.
Course
Cid Cname
C1 DS
Enroll
Sid Cid
S1 C2
S2 C3
Key for enroll table is {sid,cid}
Student
Sid Sname
S1 A
S2 B
S3 A
(2) 1:M:- An entity in A is associated with any number (zero or more) of entities in B. An entity
in B, however, can be associated with at most one entity in A.
Student
Sid Sname
S1 A
S2 B
S3 A
Course
Cid Cname
C1 DS
C2 C
C3 C++
Enroll
Sid Cid
S1 C1
S1 C2
S2 C3
Key for enroll table is {cid}
Course
Cid Cname
C1 DS
C2 C
C3 C++
Enroll
Sid Cid
S1 C2
S1 C3
S2 C1
S3 C2
Key for enroll table is {sidcid}
Minimization of E-R Diagram:-
Case 1:- When cardinality is 1:M
EMP Sid, Ename Manages Sid, did, Since Dept did, dname
E1 E1 D1 D1
E2 E1 D2 D2
E3 E2 D3 D3
D4
P.K=Did
F.K.=Sid,did P.K=did
Here, one Table primary key referencing the primary key of others table. If such scenario
exist, then merge both table.( Manages & Dept. Table)
A A1 A B A B1
1 a1 1 12 11 b1
2 a1 2 13 12 b2
3 a2 13 b2
(OR) E1 R E2
Let Merge E1 R E2
A A1 B B B1
1 a1 12 11 b1
2 a1 13 12 b 2
3 a2 NULL 13 b 2
A: PK B: PK
B: Alternate Key
A A1 A B B b1
1 a1 1 12 11 b1
2 a1 2 13 12 b2
13 b3
C.K. = A, B
P.K = B
Alterante key = A
Minimum number of table required is one.
If both side have total participation, then we cannot get any NULL value in merging table.
Case 3:- When cardinality is M:N
Note:-Merging is not possible when cardinality is M:N.So,3 table required to represent the
information.
Note:
(1) 1:M = R and E2 can Merge
So, Minimum 2 Table required.
(2) += when atleast one side total participation then 1 table required otherwise 2 table
required.
(3) M:N= We cannot Merge
So, Minimum 3 Table required.
Denoted by
Weak entity type also called Chid entity (OR) Subordinate entity.
This relationship set is NOT strong because E1& E2 Cname is A but we can not differentiate
the A(i.e., which A is that).So, Relationship is WEAK.
So, We always merge weak relationship set and weak entity set.
SSn Cname age gender
E1 A 20 F
1. The minimum number of tables required to represent the following E-R diagram into relational midel
is__________
Mm : 5.0
Max : 5.0
Answer 5
Sol. 5
One for E]t one for E,, one for R, Two tables for multivalued attribute of Ej and E,
2. Consider a business rule i,e. "Each department is manged by atmost one employee".
What is the cordiality from employee to the department?
(A) 1:1 (B) 1: M
(C) M : 1 (D) M : N
Sol. (B)
E – R diagram for above constraints is
Let the cardinality of each relation is 1:1, then minimum number of table possible in relational model
to represent above E-R diagram (Let the participation of A.B is total)_______
Min : 3.0
Max : 3.0
Sol. 3
One table for AR1 B
One table for R2
One table for R3
R.A.
Informal (non-procedural) Q.L: - What to retrieve from DB
Example: TRC, SQL
SQL: - By default duplicate is not eliminated. So, SQL table is not a ‘set of tuples’ but it is a
multiset (bag) of tuples.
Keyword ‘DISTINCT’ is used to get only distinct values.
The basics clause in SQL is:
SELECT <attribute list>
FROM <table list>
WHERE <condition>
A B C D
1 X C1 D1
2 X C2 D2
3 Y C3 D3
4 Z C3 D4
5 K C4 D5
Note: A simple SQL query with a single relation name in the FROM clause is similar to a
SELECT-PROJECT pair of relational algebra operation.
Student
Sid Sname Dob Courseid Course
1 A 10/11 1 Cid Cname Fee
2 B 10/11 4 1 C 10K
3 A 10/12 1 2 C++ 15K
4 C 10/13 1 3 DB 20K
5 D 10/14 2 4 Java 30K
SELECT *
FROM employee, department
Sol. Results cross-product of Employee and department relation.
Note: ‘SELECT DISTINCT’ eliminates duplicate ‘SELECT ALL’ does not eliminate
duplicate. But if we are not mentioning neither DISTINCT nor ALL then it is equivalent to
SELECT ALL.
1
2
4
6
5
1
2
4
1
1
2
4
1
2
6
6
R-All S: Tuples in R but not in S
IS / IS NOT Clause:-
To check attribute value is ‘NULL’, SQL uses IS (OR) IS NOT
We can not use = (OR) <> ‘Not equal to’ operator because SQL considers each Null
value as being distinct from every other NULL values.
When join condition is specified, then tuples with NULL values for the join attribute
are not included in the result.
Branch
Bno. City Code
1 Jaipur XX
2 Chandigarh YY
3 Delhi ZZ
4 Jalandhar AA
5 Jairpur XXY
Staff: -
Sid Sname Branch no. Position
11 A 1 Manager
12 B 3 Assistant
13 C 3 Supervisor
14 D 2 Assistant
15 E 3 Manager
16 F 1 Assistant
Select course id from section as S where semester = fall and year = 2009 and
exists (select *
from section as T
where semester = spring and
year = 2010 and S. course id = T. course id)
Find all course tough in both the fall 2009 and spring 2010 semester.
NULL VALUES:-
Null values are considered as Unknown/Unexisted
If where clause evaluates to either FALSE or UNKNOWN for a tuple, the tuple is not
added to the result.
When a query uses the ‘select distinct’ clauses, duplicate tuples must be eliminated.
Aggregate Function:
Count: returns the no. of values in a specified column
Sum: returns the sum of the values in a specified column
AVG: returns the average the values in a specified column
MIN: returns the smallest values in a specified column
MAX: returns the largest value in a specified column
Note: Apart from ‘COUNT’ each function eliminates nulls first and operates only on the
remaining non-null values.
‘Count (*)’ is a special use of ‘COUNT’ which counts all the rows of a table, regardless of
whether nulls (OR) duplicates values occur.
Find all department where the total salary is greater than the average of the total salary at all
department.
Sol. With dept. total (depatname, value) as
(Select depatname, sum(salary)
from instructor
grouping departname)
dept total ave (value) as
Select T. course id
from course as T
where unique(Select R.courseid
from section as R
where T.courseid=R.courseid
and R.year=2009
---------------------------- DATABASE MANAGEMENT SYSTEM ----------------------------
58 | DBMS: CS
Select T.courseid
from course as T
where 1 < = (Select count(R.courseid)
from section as R
where T.courseid=R.courseid
and
R.year=2009
NULL Value
(1) The result of an arithmetic expression (+, -, *, /) is NULL if any of the input value is
NULL.
Example: r.A + 5 = null (if r.A value is null)
(2) SQL gives UNKNOWN the result of any comparison involving a null value.
(3) When comparing values of corresponding attribute from two tuples, the values are treated
as identical if either both are non-null and equal in value OR both are null.
(‘A’, null), (‘A’ null) are treated as identical, even if some of the attribute have a null value.
(4) Treatment of null above pointer is different from the way null are treated in predicate
where a comparison ‘null = null’ would return unknown.
Join Condition:
‘ON’ The ON predicate is written like WHERE clause predicate except for the use the
keyword ON rather than WHERE.
ON condition appears at the end of the join expression
Select *
from student join takes on st.id = takes.id
Join and Natural join difference is that result in ‘Join’, 1D attribute appears twice, Natural
join, 1D attribute once.
Equivalent query w/o ON
select *
from student, takes
where stu.id = takes.id
Relational Algebra
Relational Algebra is a formal languages(procedural language)
Note:- By default duplicate is eliminated.
List of operation in relational algebra are
SELECT,PROJECT, RENAME, SET operation(UNION, INTERSECTION, MINUS),
CARTESIAN PRODUCT, INNER JOIN(THETA, EQUI, NATURAL JOIN), OUTER JOIN(LEFT,
RIGHT, FULL), DIVISION
(1) SELECT: - It is a unary operation, which perform horizontal partition.
It isused to select the tuples from a reltion that satisfies a selection conditon.
Denoted by: <Selection Condition>(R)
Here, selection condition is a Boolean expression.
Selection condition format:
<attribute name>< Comparision op ><Constant value>
(OR)
<attribute name>< Comparision op ><attribute name>
The relation resulting from SELECT operation has the same atribute as R.
It applied to each tuple individuallly
The ‘Degree’ of the relation resulting from a SELECT operation is number of attributes i.e., the
same as the degree of R.
Numbers of tuples in the resulting relation is always less than (OR) equal to the number of tuples
in R i.e.,
|c(R)| |R|
The fraction of tuples selected by a selection condition is referred to as the selectivity of the
condition.
SELECT operation is commutative i.e.,
σ cond1 σ cond2 R =σ cond21 σ cond1 R
So, we can always combine a cascade of SELECT operation into a single SELECT operation with a
conjunctive (AND) condition i.e.,
σ cond1 σ cond21 = σ cond31 ....σ condn1 R .....
σ cond1 AND Cond2 AND.......... Condn R
PROJECT operation
It is a unary operation which perform vertical partition
selects column (attribute)
CSEBRANCH branch=CSE(Student)
RESULT Sname(CSEBRANCH)
C D
C1 D1
C2 D2
Then R X S=
A B C D
A1 B1 C1 D1
A1 B1 C2 D2
A2 B2 C1 D1
A2 B2 C2 D2
Join Operation
denote by ( )
used to combine related tuples from two relation into single tuples.
It is a binary operation.
Note: JOIN operation can be stated in terms of CARTESIAN PRODUCT followed by a SELECT
operation.
General form of a JOIN operation on two relation R(A1, A2,…..,An) and S(B1, B2,……,Bm) is
The result of JOIN is a relation (Q) with n+m attribute Q(A1, A2,…..,An, B1, B2,…….,Bm) in that
order.
General JOIN condition is of the form: -
<condition> AND <condition> AND………..AND<condition>
Where, each condition in the form Ai BJ
Ai is an attribute of R
Bj is an attribute of S
Ai and Bj have same domain
(5) If there is no JOIN condition, all combination of tuples qualify and JOIN degenerates into a
CARTESIAN PRODUCT, also called CROSS PRODUCT (OR) CROSS JOIN.
(6) These JOIN operation is also called INNER JOIN
(7) If No common attribute, then
Natural Join = Cross Product
(R S) = (R S)
1 ΠSid E ×C S1 C1
S2 C2
S3 C3
S1 C1
S1 C2
S1 C3
S2 C1
S2 C2
S2 C3
S3 C1
S3 C2
S3 C3
Note: Not enrolled every course Enrolled some course because it cover who enrolled in all
course also.
---------------------------- DATABASE MANAGEMENT SYSTEM ----------------------------
Query Language | 65
(2) (ΠSid E ×C) - E
1 C1 S1 C1
S1 C2 S1 C2
S1 C3 S1 C3
S2 C1 S2 C1
S2 C2 S2 C2
S2 C3 S3 C3
S3 C1
S3 C2
S3 C3
S2
(3) Result of Step-2 is
S3
i.e. return all student id who are not enrolled in all the courses.
i.e from all student id,subtract student id who are not enrolled in every course.
S1
Outer Join
When a user wants to retain all of the tuples in R, S, or both relations in the result of the JOIN
regardless of whether there are matching tuples in the other relation, a set of operations known as
outer joins.
3 types of outer join
1. Left outer join
2. Right outer join
3. Full outer join
RIGHT-OUTER JOIN: - R S
FULL-OUTER JOIN
Step-1:- Output of natural join is
A B C D
1 2 1 4
Step-2:- Add those tuples of R and S which are failed in join condition.
A B C D
1 2 1 4
2 2 2 NULL
NULL 4 2 1
s supplier
Bounded Variable: If tuple variable preceded by Quantifier Example: & supplier.
Note:(1) TRC, atmost are free variable can be used.
(2) Result of TRC should be free variable
sid col.id=p.id Catalog X col=red Parts
T c catalog p parts p.col=Red P.id=c.id T=c.sid
Result
Retrieve name of the all employee who work for the CSE department.
Solution: {t.name|Emplyee(t) AND (d) (Department (d) AND d.name=CSE AND
d.number=t.Dno}
Safe and unsafe Query
A safe expression in relational calculus is one that is guaranteed to yield a finite number of
tuples as its result
Unsafe Query: Quary which results infinite set of tuples.
{s| s supplier}
Tuples not belong to supplier.
i.e., all tuples in DB except supplier.
i.e., whenever we do complement of free variable, we get unsafe query.
Note: Expressive power of RA = Expressive power of 7RC
An similar expression can be obtained by transforming a universal quantifier into an
existential quantifier and vice versa i.e.
(x) (P(x)) NOT (x) (NOT (P(x)))
(x) (P(x)) NOT (x) (NOT (P(x)))
(x) (P(x) AND Q(x)) NOT (x) (NOT (P(x)) OR NOT (Q(x)))
(x) (P(x) OR Q(x)) NOT (x) (NOT (P(x)) AND NOT (Q(x)))
(x) (P(x)) OR Q(x)) NOT (x) (NOT (P(x)) AND NOT (Q(x)))
(x) (P(x) AND Q(x)) NOT (x) (NOT (P(x)) OR NOT (Q(x)))
Query 1) Get names of all employees in department 5 who work more than 10 hours/week
on the ProductX project.
Sol. { t.fname, t.minit, t.lname | employee(t) and (Exists w)(Exists p)(works_on(w) and
project(p) and t.ssn = w.essn and w.pno = p.pnumber and w.hours >= 10 and p.pname =
'ProductX') }
(Query 2) Get names of all employees who have a dependent with the same first name as
themselves.
Sol. { t.fname, t.minit, t.lname | employee(t) and (Exists d)(dependent(d) and t.ssn = d.essn
and t.fname = d.dependent_name) }
(Query 3) Get the names of all employees who are directly supervised by Franklin Wong.
---------------------------- DATABASE MANAGEMENT SYSTEM ----------------------------
Query Language | 69
Sol. t.fname, t.minit, t.lname | employee(t) and (Exists e)(employee(e) and t.superssn =
e.ssn and e.fname = 'Franklin' and e.lname = 'Wong') }
(Query 4) Get the names of all employees who work on every project.
Sol. { t.fname, t.minit, t.lname | employee(t) and (Forall p)(project(p) -> (Exists
w)(works_on(w) and w.essn = t.ssn and e.pno = p.pnumber)) }
(Query 5) Get the names of employees who do not work on any project.
Sol. { t.fname, t.minit, t.lname | employee(t) and not (Exists w)(works_on(w) and w.essn
= t.ssn) }
A B C A B A C
R 1 a S
10 2 a 1 NULL
2 b 20 3 b 2 10
3 c 30 6 c 3 20
4 d 40 6 d 4 NULL
5 e 50 7 E 5 NULL
NULL 6 30
NULL 6 40
NULL 7 50
then R B 1 S
A B
1 4
2 5
3 6
4 Null
3 4
6 8
9 77
10 14
(A) I & II (B) II & III (C) III only (D) I & III
Sol. (C)
In given input, A < B. So, A B select all rows and project A,B
S1 S2
P Q R T U V
1 2 3 1 2 3
5 10 4 2 4 2
2 4 3 1 2 4
3 6 5 3 6 2
1 2 5
A. Empty relation
B. {(2,4), (3,6)}
C. A relation with schema (P,Q) and tuples {(1,2)}
D. A relation with schema (P,Q) and tuples {(1,2), (2,4), (3,6)}
Ans. A
2. Using the relation instances given in the Question 24, find out how many tuples will be there
in the result of the following relational algebra expression
S1 S1.Q S2.V S2
A. 20 B. 9 C. 8 D. 16
Ans. D
3. Consider the following relations S1(P,Q,R) and S2(P,R) and the given instances. What is the
result of the relational algebra expression?
S1 S2
S1 S2
P Q R P R
1 2 2 1 2
1 4 2 2 4
2 4 4
1 6 2
2 2 4
A. Empty relation
P Q R
1 2 3
3 4 5
2 4 6
3 3 5
4 2 3
A. 5 B. 6 C. 7 D. 8
Ans. C
5. On the same relation given in Question 28, what is the size of the result of the following
relational algebra expression?
A. 9 B. 10 C. 11 D. 12
Ans. C
R1 R2
A B B C
4 8 6 3
3 6 3 6
2 4 2 4
• Transaction …………………………………………………………………………. 86
• Classification of Schedule……………………………………………………………72
• Classification of schedule based on serializability…………………………………. 97
• Two types of schedule based on serializability ……………………………………. 97
• Conflict Serializable Schedule ………………………………………………………97
• Conflict Serializable Schedule……………………………………………………….99
• View Serializable Schedule ………………………………………………………..102
• Concurrency Control Protocol………………………………………………...……104
• Locking Protocol……………………………………………………………………104
• Basic Time-stamp Ordering ………………………………………………………..109
• Thomas’s Write Time Stamp Protocol……………………………………………...110
• Deadlock Prevention Protocol………………………………………………………111
4 Transaction and Concurrency Control
Transaction
A transaction is a group of operations that together make up a single logical unit of work. One or
more database access operations, such as insertion, deletion, update, or retrieval activities, are
included in a transaction.
T.log
Add=1000
Anew=500
Bold=2000
Bnew=2500
(2) Durability: Rollback (Recovery) should possible under any case of failure . durability
property is the responsibility of the recovery subsystem of the DBMS
(3) Consistency: DB should be consistence before and after execution of transaction.
Taken care by use
* If RMC & CCMC both are success, then always consistency satisfied.
T1 T2
100 R(A)
0
W(A
500 )
200 R(B)
0
W(B R(A 500
250 ) )
0 250
R(B 0
)
T1 T2
R(A) 1000
R(B) 2000
1000 R(A)
500 W(A)
2000 R(B)
2500 W(B)
if(A>0)
if(A>0)
A=A–1
W(A)
W(A)
Final value of A after successful completion must be 8 after 2 book are issued. But according
to above code ,final value of A is 9 which results inconsistency.
(2) W.R. Problem (Read After Write) – Reading Uncommitted data or Temporary
Update problem
T1 T2
W A
R(A)
R A
R B
fail
R B
W B
. R A
Commit C
(OR)
Rollback
Transaction T2 read the data item which is updated by T1 and if T2 commit before C/R of T1.
(b) Recoverable Schedule:
If transaction T2 reads data-item which is modified by T1, then commit (or) rollback of T1
should before commit of T2.
T1 T2
W A
R A
C/R
C
Note: If a schedule is recoverable, it does not mean that is is always free from inconsistency.
* It may be inconsistence schedule.
T1 T2
R A
W A This scheduleis
R A Recoverable, but
R B Inconsistence schedule
R B
W B
C1
C2
T1 T2 T3
R(A)
W(A)
R(A)
W(A) Rollback
R(A)
*fail Rollback W(A) Rollback
Cascade less Rollback Schedule: If transaction (T1) updates the data item(A), then other
transaction (T2) is not allowed to read data-item(A) until commit (OR) rollback of T1
T1 T2
W A
C
R A
Strict Recoverable Schedule: If transaction (T1) performs write (A) operation, then other
transaction not allowed to read/write data-item (A) until commit (OR) rollback of T1.
T1 T2
W A
C
R A W A
So, W-R, W-W, lost update problem are not possible, but R-W may exist.
a T1 : R A T2 : R(A)
T1 T2 T1 T2
i.e., execution sequence S1 and S2 give same result. So, it is not conflict pair.
b T1 : R A T2 : W A
T1 T2 T1 T2
R A W A
W A R A
S1 S2
S1 S2
Conflict Pair
S1 S2
S1 S2
Conflict Pair
Because T2 read the updated value of A in S1, whereas in S2, it read initial value. So, it is a
conflict pair.
d T1 : R A T2 : R(A)
T1 T2 T1 T2
W A W A
W A W A
S1 S2
Because final updated value of A in S1 and S2 is not same. So, it is a conflict pair.
Conflict Equivalent Schedule: When two operations in a schedule access the same database
item, are part of different transactions, and are either both write_item operations or one
write_item and one read_item operation, respectively, they are said to conflict. The effects on
the database or the transactions in the schedule may change if two conflicting operations are
applied in two schedules in different orders, therefore the schedules are not conflict
equivalent.
So, if S2 results after swapping order of consecutive non-conflict pair of S1, then S1 and S2
are conflict equivalent schedule.
T1 T2 T1 T2
R A R A
W A W A
R A R A
R B R B
R B R B
W B non-conflict W B
pair
S1 S2
In above example,R1(B) and R2(B) are non conflict pair, so S1& S2 are Conflict Equivalent
Schedule
T1 T2
R A
W A
R A is Not CSS because output of schedule is not equivalent to any serial schedule.
R B
R B
W B
T1 T2 T1 T2
R A R A
W A W A
R A R B
R B R A
W B W B
R B R B
S1 S2
S1 S2 Conflict Equivalent Schedule because final output of schedule
is equivalent to serial schedule T1->T2
Note: If two schedule are conflict equivalent schedule, then it doesn’t mean that they are
conflict serializable schedule.
S1 S2 S3
S1& S2 are conflict Equivalent Schedule S3 is Not Conflict Equivalent Schedule because in
S3 ,non conflict pair (W3(A),W2(A) ) operation sequence is changed.
Precedence Graph
Precedence Graph is used to check whether schedule is conflict serializable schedule or not.
Let graph G: (V, E) V = Transaction of Schedule
E = Conflict pair precedence.
TiTj only if there exist conflict pair such that Ti precedence Tj
Ti : R A Tj: W A or
Ti : W A Tj: R A or
Ti : W A Tj: W A or
R2(A) W2(A) R3(C) W2(A) W3(A) W3(C) R1(A) R1(B) W1(A) W1(B)
Edge T2 to T1 as W2(B), R1(B) operation.
Edge T2 to T3 as W2(A) W3(A) operation.
Edge T3 to T1 as W3(A) R1(A) operation.
T1
T2
No Cycle. So, schedule is Conflict Serializable Schedule.
T3
T1
T2
T3
T1T2T3T4T5
(OR)
T1T2T4T3T5
Note: If Schedule is not conflict serializable schedule(CSS) then it does not mean that
schedule is always non-serializable.
Testing Condition
If (Acyclic Precedence G) then CSS Serializable – It is only sufficient condition for
serializability.
else
Not CSS may (OR) may not serialzable– It is not neccesary condition for Non-serlizability
So, CSS is sufficient but not necessary for serializability.
Note:- If schedule is Not CSS, then there should be blind write.
View Serializable Schedule
View Equivalent: Two schedules S1 and S2 are view equivalent if the following 3-condition holds.
(1) For each data-item x, if transaction Ti read the initial value of x in schedule S1, then
transaction Ti must also read the initial value of x in schedule S2.
(2) If Ti reads a value of x written by Tj in S1, then it must also read the value of x written by Tj
in S2.
(3) For each data-item x, the transaction (if any) that performs the final write on A in S1 must
also perform the final write on A in S2.
View serializable schedule(VSS): A schedule is view serializable if it is view equivalent to some
serial schedule.
Note: (1) Every CSS is VSS but not vice-versa.
(2) Any view serializable schedule that is not CSS contain a Blind Write.
Note: Initial Read means before read, no write operation performed on that data-item.
T1 T2 T3
R A
W A is NOT CSS but VSS (T1 T2 T3) i.e.,
W A
W A
Below schedule is Correct schedule but not serializable: Because addition and subtraction
operation are commutative, they can applied in any order i.e., it is possible to produce correct
schedules that are not serializable.
T1 T2
R(X) R(Y)
X = X-10 Y=Y-20
W(X) W(Y)
R(Y) R(X)
Y=Y+10 X=X+20
W(Y) W(X)
Let
R1 X
W1 X
R2 Y
W2 Y
R1 Y
W1 Y
R2 X
W2 X
(b) Exclusive lock (X): - If a transaction T want to perform read / write operation then exclusive-
mode lock (denoted by X) is used.
Example: T1
X(A)
R(A)
W(A)
Lock Compatable Table: - tells that whether Tj request is grant or denied if Ti already hold
a lock on that data item.
Request () /Hold() Shared Exclusive
Shared Allowed Not Allowed
Exclusive Not Allowed Not Allowed
T1 T2
X A
R A
W A
U A
S A
R A
U A
S B
R B
U B
X B
R B
W B
U B
T
Growing X A
(OR) S B
Locking S C
Phase X D
lock-point Position of after last lock (OR) position of before first unlock point
Shrinking U C
(OR) U A
Unlock U B
Phase U D
T1 T2
X A
R A
W A
X B
U A
SA
R A
S B requestNot allowed
R B
R B
W B
In above example ,Due to 2PL protocol,T2 request of S(B) is denied as T1 already hold a
X(B) on data item B.
Note:-2PL not allowed to execute the non–serializable schedule.(above schedule is non–
serializable)
T1 T2
X A
R A
W A
X B
T1 lock point U A
S A
R A
R B
W B
U B
S B
R B T2 lock point
U A
U B
Note: 1)If schedule is in 2PL, then schedule always conflict serilizable schedule (So, 2PL
always serializable)
T1 T2 T3
• T2 T3 T1
• serial schedule
•
Limitation of 2PL
(1) Not free from irrecoverable schedule.
T1 T2
X A
W A
X B
U A
S A
R A
W B
U B
S B
R B
Commit
Commit
Strict 2PL: - 2PL + transaction T does not release any of its exclusive (write) locks until after
it commits or aborts. Hence, no other transaction can read or write an item that is written by
T unless T has committed, leading to a strict schedule for recoverability
Advantage: Always ensure serializability and strict recoverability.
Disadvantage: Not free from Deadlock & Starvation.
T1 T2
X A
R A
W A
C/R
U A
S A X A
Regorous 2PL: Basic 2PL and transaction T does not release any of its locks (exclusive or
shared) until after it commits or aborts.
Advantage: Results strict recoverable schedule & serializable schedule
Equivalent serial schedule is based on order of ‘Commit’.
T1 T2 T3
C
T3 T1 T2
C
C
Disadvantage: Not free from Deadlock & starvation but, easy to implement than strict 2PL.
Conservative 2PL (or) Static 2PL:-
2PL + all lock should be hold before the transaction begins, by predeclaring its read-set
and write-set.
Disadvantage: Less concurrency may be difficult to identify the required lock.
May not free from starvation.
Advantage: Deadlock free.
---------------------------- DATABASE MANAGEMENT SYSTEM ----------------------------
Transaction and Concurrency control | 109
2)Time-Stamp Ordering Protocol
Note:-Time-Stamp Ordering Protocol always free from Deadlock.
Time-Stamp Value: - Unique value assigned by DBMS to every transaction in ascending
order.
10 20 30 40
T1 T2 T3 T4
Write Timestamp (A): Highest transaction timestamp value that has perform Write(A)
operation successfully.
10 20 30 40
T1 T2 T3 T4
R A
W A WTS_A = 0 10 30 40
W A
W A
W A
10 20
T1 T2
W(A)
R A
C
C
---------------------------- DATABASE MANAGEMENT SYSTEM ----------------------------
Transaction and Concurrency control | 111
* Basic Time Stamp ordering protocol.
* Thomas’s Write TS ordering protocol but not recoverable schedule.
To avoid recoverability problem, we use strict time stamp ordering protocol.
Strict Time Stamp ordering Protocol: To achieve the recoverability,foloow the rules of
Strict Recoverability i.e.
Basic TS Protocol (OR) Thomas’s Write TS Protocol and Strict Recoverability
It always ensure serializability, recoverability deadlock free-but, not free from starvation.
Deadlock Prevention Protocol
Timestamp values is used to prevent deadlock in locking protocol.
Dependency Graph.
T1 T2
T1
T2
X A
S A request
i.e.,T2 is depend on T1
(ii) If TJ required the resource i.e., holded by Ti, then Rollback TJ.
Ti Tj
Rollback TJ and restart with same TS value.
old younger
T1 T2
R(A)
R(A)
W(A
)
W(B W(A)
)
W(A
)
Graph contain cycle. So, schedule is not conflict serializable schedule. But initial read, final
update are same as T2 T1 T3 serial schedule So, schedule is View serializable
2. Which of the following scenario may lead to an irrecoverable error in database system?
(A) A transaction writes a data-item after it is read by an uncommitted transaction
(B) A transaction reads a data item after it is written by an uncommitted transaction
(C) A transaction reads a data item after it is read by an uncommitted transaction
(D) A transaction reads a data item after it is written by a committed transaction
Sol. (B)
Irrecoverable schedule is like
T1 T2
W(A) R(A)
C
Rolback*
3. Consider the following schedule
S: R1 (A) R2(A) W2 (A) W1 (A) C2 C1
(A) Schedule is irrecoverable
(B) Schedule is recoverable but not cascadeless
(C) Schedule is cascadeless but not strict
(D) Schedule is strict
Sol. (C)
Given schedule is
T1 T2
R(A)
R(A)
W(A)
C
W(A
)
C
T1 T2 T3
R(A)
R(B)
R(B)
W(A
) W(A)
R(C)
W(A
)
5. Consider the following schedule
S: R1(X) R2(X) R1(X) C1 W3(X) C2 R3 (X) C3
(A) Schedule is not view serializable but recoverable schedule
(B) Schedule is not conflict serializable but strict recoverable schedule
(C) Schedule is conflict serializable but strict recoverable schedule
(D) Schedule is conflict serializable but not strict recoverable schedule
Sol. (C)
T1 T2 T3
R(X)
R(X)
R(X)
C W(X
)
(C)
W(X
)
C
So, schedule is conflict serializable but not strict recoverable schedule because no transaction
perform read or write operation on uncommitted transaction.
6. Consider the following schedule
T1 T2
R(A)
W(A)
R(A)
W(A
) back
Roll
C
Schedule is
(A) Recoverable (B) Cascodeless
(C) Irrecoverable (D) Strict recoverable
Sol. (C)
As T1 reads a data item which is update by T2 and T1 commit first. So, recoverability is not
possible
5. Consider the following schedule
S: R1(x) R2(z) R1(z) R3(y) R4(x), W1(y) W3(y) R2 (y) W2(z) W2(y) then
(A) S is conflict serializable with T1 T2 T3
(B) S is conflict serializable with T2 T3 T1
(C) S is conflict serializable with T2 T1 T3
(D) Not conflict serializable
Sol. (D)
6. Match List-1 with List-II
List – I List – II
(a) W-R conflict 1) Unrepeatable rend
(b) R-W conflict 2) Phantom problem
(c) W-W conflict 3) Lost update
A B C
(A) 4 1 3
(B) 4 2 3
(C) 2 4 3
(D) 2 1 4
Sol. (A)
W – R conflict - Reading uncommitted data
R – W conflict - Unrepeatable read
W – W conflict - Over writing uncommitted data
7. Which of the following is correct?
(A) A schedule will be irrecoverable if a transaction reads a data item and commit after it
is written by an uncommitted transaction
(B) A schedule will be irrecoverable if a transaction writes a data item and commit after
it is read by an uncommitted transaction
(C) Both of these
(D) none of these
Sol. (A)
Since, the data item is written by an uncommitted transaction there are a chance that, it might
abort. Hence, if another transaction reads the data item and commit, the sechedule will be
irrecoverable.
8. Total number of serial schedule possible with 5 transaction is_________
Min : 120.0
Max : 120.0
Answer 120
Sol.1: ();
N! Serial schedule are possible with n transaction. So, S! = 120
9. Consider the following statement
(1) All serial schedule are always consistance schedule
(2) All concurrent schedule are always consistance schedule
Which of the following is FALSE?
(A) 1 only (B) 2 only (C) Both (D) none
Sol. (B)
Concurrent schedule may be inconsistence schedule
T1 T2
R(A)
W(A) R(A)
R(B)
R(B)
W(B)
is inconsistence schedule
10. Match the following
Table – I
(1) Atonieity
(2) Isolation
(3) Consistency
Table – II
(a) Concurrent execution of two or more transaction should be equal to more transaction
should be equal to any serial schedule
(b) Execute all operation or none
(c) Before and after execution of transaction, DB should be equal
(A) l – a, 2 – b, 3 – c (B) 1 – c, 2 – a, 3 – b
(C) l – b,2 – a,3 – c (D) l – b, 2 – c,3 – a
Sol. (C)
Atomicity means execute all operation or more isolation means concurrent execution of two
or more transaction should be equal to any serial schedule. Consistency means before and
after execution of transaction. DB should be equal
11. Consider the following schedule
R3(A) R1(A) W3(A) R2(A) W1(B) R2(B) W2(A) C3 C1C2
Which of the following is True?
(A) Schedule is not recoverable
(B) Schedule is recoverable but not cascaddess
(C) Schedule is recoverable and cascadless
(D) Schedule is strict recoverable
T1 T2 T3
R(A)
R(A)
W(A)
R(A)
W(B)
R(B)
W(A)
C3
C1
C2
Precedence graph of S2 is
W(A)
W(A)
15. Let T1 and T2 are two transaction with their time stamp value TS (T1) and TS(T2). Let TS(T1)
< TS (T2) and if T1 required the resource which is holded by T2 Then, according to wait-die
protocol, which of the following statement is true
(A) T2 allowed to wait (B) T2 is rollback,
(C) T1 allowed to wait (D) T1 is roolback
Sol. (C)
In wait - die protocol, when older transaction required the resource which is hold by younger
transaction then older transaction allowed to wait.
2) T1 T2 T4 T3 T5
T1 T2 T3
R(A)
W(A)
W(A)
W(A)
T1 T2 T3
R(x)
W(x)
W(x) R(x)
C
C
C
T1 T2 T3 T1
S1: R(A) S2 : R(A)
R(B) R(B)
R(C) R(C)
W(B)
W(C) W(B)
W(A) C1
C1 W(C)
C2 C2
C3 W(D)
C3
Both schedule S1 and S2 are not allowed in strict 2PL. Because in strict 2PL, all exclusive lock hold
until commit.
T1 T2
R(A)
W(A)
R(A)
R(B) R(B)
W(B)
T1
X(A)
R(A)
W(A)
CO(A)
U(A)
S(A)
R(A)
U(A)
S(B)
R(B)
U(B)
X(B)
R(B)
W(B)
U(B)
A. True
B. False
Ans. B
6. If all transactions follow 2PL protocol, deadlocks can be avoided
A. True
B. False
Ans. B
5. Indexing ……………………………………................................................127-139
INDEXING
An index is a data structure that organizes data records on disk to optimize certain kinds of retrieval
operations. An index allows us to efficiently retrieve all records that satisfy search conditions on the
search key fields of the index.
No Memory Leaks/wastage
I/O cost is more because to access the record R3,2 block need to transfer from secondary memory
to main memory.
(2) Unspanned organization:
Records not allowed to spanned between blocks.
Entire record belongs to single block.
Let the relational schema Emp(Eid,Ename,Esalary) .Then retrieve the eid from Emp table
whose Eid is ‘y’.
1000
= =20 block
50
OR
Type of Index:
Condition: (1) Search key used in index file should be used to physically ordered the DB
record (i.e., ordered file)
Condition: (1) Search key used in index file should be used to physically ordered the DB
records (i.e., ordered file)
Note: Clustering Index is sparse because it has an entry for every distinct value.
Note: For a DB, we cannot make both Primary index and Clustering index.
(3) Secondary Index: Secondary way of accessing the data even if either primary (OR)
clustering index is exist.
Note: Secondary Index is always Dense. More than one secondary Index is possible.
Let secondary index on a Non-key field of a file. We can implement this type of index as
follows.
(a) Include several index entries with the same key one for each record. So, it is a dense index.
(b) Use variable length records for the index entries with a repeating field for the pointer (one
pointer for each block)
(c) Create an extra level of indirection to handle multiple pointers. So, it is sparse index.
In this pointer in the index entry points to a block of record pointer and each record pointer
in the block points to one of database record.
If record pointer cannot fit in a single disk block, a cluster (OR) linked list of blocks is used.
* It requires one (OR) more additional block access because of extra level.
Multilevel Index:
Index is index until last level become Binary block.
P – Block Pointer
P-1 Key
(2) Structure of External Node (leaf Node)
If there are N records in the file, the path is no longer than log n/2 N
n-1
* Each node must have at least key atmost (n-1) key.
2
1, 2, ………….10
Note:(1) If given keys are in ascending order,
In B+-tree: Right Biasing gives more no. of node split than left biasing.
(2) If given biasing descending left biasing more
B+-Tree
Let n = 3 Deletion
Initial:
1.
Delete-5: No Problem
Delete-18 - No Problem
Delete-22 Problem
Delete-7
4. The order of an internal node is ET tree index is the maximum number of children it can have
Suppose that a child pointer takes 3B. the search Field value takes 17B and the block size is
1024B. The order of internal node is
556
level 2 56 nodes
10
56
level 3 6 nodes
10
6
level 4 1nodes
10
For the B+ tree given above, the minimum number of nodes of tree (including the root node)
that must be fetched in order to obtain result of the following query: “Get all records with a
search key greater than or equal to 14 and less than or equal to 20” is
A. 4 B. 5 C. 6 D. 7
Ans. A
6. Consider the B+ tree given in Question 69 with order =3 and order leaf = 2. If we insert the
element 15, the number of internal nodes and leaf nodes in the resulting tree, respectively, are
A. 4,6 B. 4, 7 C. 5, 6 D. 5, 7
Ans. B