Professional Documents
Culture Documents
Adv of DBMS:
K.V.R
Chebrolu Engineering College
2. Reduced inconsistency: By avoiding the redundancy we can eliminate
inconsistency.
3. Data independency
4. Data sharing
5. Efficient data access
6. Data integrity and security
7. Data administration
8. Concurrent access and crash recovery
9. Reduced application Development
1. External schema
2. Conceptual schema
3. Internal schema
K.V.R
Chebrolu Engineering College
External schema: All the users and user interfaces is at external schema. There
are three types of users.
1. End user
2. Application programmer
3. Database Administrator
End user: is a person who operate the database without any prior knowledge
Data Models: A data model will specify how the data is representing in the
database. The different data models are
1. Hierarchical model
2. Network model
3. Relational model
4. Object oriented model
Hierarchical model: In this model the data is represent like a tree structure. Each
record is called as a node. The top node is called as a root node. The bottom
nodes are called as leaf nodes. The intermediate nodes are called as internal
nodes.
K.V.R
Chebrolu Engineering College
Network model: In this model the data is represent in the form of linked list.
Each record is called as a node. The first node is called as header node. The last
node is called as end node.
Relational Model: In this model the data is represent in the form of tables or
relations. Each table is the combination of rows and cols .
This model will support one to one , one to many , many to one, many to many.
1. Presentation logic
K.V.R
Chebrolu Engineering College
2. Processing
3. Data storage
UG PG
Single tier architecture: in this architecture all the application logic is performed
in one system.
Two tier architecture: In two tier architecture all the systems are interconnected.
In this architecture the application logic is divided between client and server.
The client system is responsible for presentation. The server system is
responsible for processing and data storage.
K.V.R
Chebrolu Engineering College
Client-1 Client-2 Client-3
server
Three tier client server architecture: the application logic is divided between
client, application server and data server. The client is responsible for
presentation. The application server is responsible for processing and data
server is used to store the data.
Client-3
Client-1 Client-2
Application server
Data server
K.V.R
Chebrolu Engineering College
5. Database tuning
Unit-1
Unit-II
1. Strong entity
2. Weak entity
STUDENT
Weak entity: An entity which depends on some other entity type such an entity
is called as weak entity. A weak entity can be represented by double line
rectangle.
1. Key attribute
2. Derived attribute
3. Composite attribute
K.V.R
Chebrolu Engineering College
4. Non key attribute
5. Multi valued attribute
ROLL NO
Non key attribute: An attribute value which is not unique such an attribute is
called as non key attribute. It is represented by
NAME
Derive attribute: An attribute value which is derived from some other attributes
such an attribute is called as derived attribute. It is represented by doted ellipse.
Total Marks
NAME
Multi valued attribute: For each instance an attribute which contains multiple
values is called as multi valued attribute. It is represented by double line ellipse.
K.V.R
Chebrolu Engineering College
Mobile no
Strong relationship: The entity types of the relation is strong entities then it is
called as strong relationship. It is represented by
Weak relationship: When the entities are strong and weak in a relationship then
it is called as weak relationship. It is represented by
1. Unary relationship
2. Binary relationship
3. Ternary relationship
Unary relationship: A relationship which contains only one entity type is called
as unary relationship.
P_id
MARRY
PERSON
name
Ex:
student course
Join
K.V.R
Chebrolu Engineering College
Ex: vendor supply material and ship to warehouse
qty
M_id Desc
material
v-id name
w-id Loc
supply warehouse
vendor
K.V.R
Chebrolu Engineering College
One to many relationship: An entity instance of entity type –A is associated
with many of entity instances of entity type-B is called as one to many
relationship
K.V.R
Chebrolu Engineering College
Entity type-A Entity type-B
Ex: There are two entity types such as EMPLOYEE and STUDENT. The
properties of these two entity types is
Id
Name
EMPLOYEE
Name Fee
Id
STUDENT
The common properties from these two entity types is Id and Name.
K.V.R
Chebrolu Engineering College
The generalization of these two entity types is
Id Name
PERSON
IS a
Fee
EMPLOYEE STUDENT
Specialization: Identification of subtypes from a super type is called as
specialization. It is a top down approach.
Id Name
PERSON
Is a
Fee
K.V.R
Chebrolu Engineering College
EMPLOYEE STUDENT
Employee
manage
1. Selection
2. Projection
3. Cross product
4. Union
5. Intersection
6. Difference
K.V.R
Chebrolu Engineering College
7. Joins
8. Rename
9. Division
Ex:
sailors
Boats
Reserves
Π(Colum list)
Π(bname)boats
Bname
Xx
Yy
Zz
Aa
K.V.R
Chebrolu Engineering College
Ex: List colors of all the boats
Π(colors)boats
Colors
Red
Green
Red
Yellow
Π(names)sailors
Sname
Ravi
venkat
sai
Π(name,age)sailors
Output:
Sname age
Ravi 45
venkat 38
sai 42
Ex: List the details of the sailors whose age is less than 40
K.V.R
Chebrolu Engineering College
σ (age<40)sailors
σ(rating>=6)sailors
Ex: List the name and rating of sailors whose age is less than 40
Π(name,rating) σ (age<40)sailors
Sname Reating
venkat 6
Cross product: In this operation each row first table is joined with all the rows
of second table. It is also called as Cartesian product. It is represented by by × .
The syntax of this operation is
relation1 × relation2
Ex:
Table1
principle
1000
2000
3000
Table2
Time interest
12 3.5
15 4
Table1×Table2
K.V.R
Chebrolu Engineering College
Principle Time interest
1000 12 3.5
1000 15 4
2000 12 3.5
2000 15 4
3000 12 3.5
3000 15 4
Union: To perform this operation the two tables must have same cols. This
is called as union compatibility. The resultant table will contain the rows of
first table or second table or both.
Student1
Student2
Student1U student2
K.V.R
Chebrolu Engineering College
Intersection: The result of this operation is the rows which are common
under both the sets. To perform this operation the two tables must be union
compatible. The syntax of this operation is
Table1 ∩ table2
Difference: The resultant table which contains the element of first table but
not second table. The syntax of this operation is
Table1-table2
Ex:
Student1-student2
Ex-2:
Student2-student1
Joins: joins are used to retrieve the data from one or multiple tables. There
are two types of joins. They are
1. Inner join
2. Outer join
Inner join: This join retrieve the data from both the tables which satisfies
the condition. There are three types of inner joins. They are
K.V.R
Chebrolu Engineering College
1. Theta join
2. Equi join
3. Natural join
Theta join: This join retrieve data from both the tables which satisfies the
condition. The condition is either > ,>=,< or <=
Ex:
T1
Col-1 Col-2
1 5
2 4
3 8
T2
Col-1 Col-2
1 3
6 2
8 8
EX: Retrieve the data from the given tables where col-2 of T1 is greater
than col-2 value of T2
T1⋈T1.col-2>T2.col-2 (T2)
Col-1 Col-2 Col-1 Col-2
1 5 1 3
2 4 6 2
Ex: Retrieve the data from both the table where col-2 of T1 is equal to col-2
of T2.
T1⋈T1.col-2=T2.col-2 (T2)
Col-1 Col-2 Col-1 Col-2
K.V.R
Chebrolu Engineering College
3 8 8 8
T1
Num square
2 4
3 9
T2
Num cube
2 8
3 27
T1 ⋈T2
Outer joins: This join retrieve the rows of the table even there is no
matching value in another table. There are three outer joins. They are
Left outer join: In this join it produces all the rows of the table-1 even there
is no matching value in table-2. This is represented by
Ex:
T1
Num square
2 4
3 9
4 16
K.V.R
Chebrolu Engineering College
T2
Num cube
2 8
3 27
5 125
10 1000
T1 T2
T2 T1
Right outer join: It produces all the rows of second table and matching
rows of first table. It is represented by
Ex: T1 T2
K.V.R
Chebrolu Engineering College
Full outer join: It produces all the rows of both the tables. It is represented
by
Ex:
T1 T2
Rename: This operation is used to change the name of the table. It will be
represented by ->
DIVISION: Let us assume that there are two tables A and B. The table A
has two fields x and y. The table B has only one field y. The division A/B
will display all x values for every y value of B
Table: A
sno Pno
S1 P1
S1 P2
S1 P3
S1 P4
S2 P1
S2 P2
S3 P2
S4 P2
S4 P4
Table: B
pno
P2
P4
A/B
K.V.R
Chebrolu Engineering College
Sno
S1
S4
Table: B
pno
P1
P2
P4
A/B
SNO
S1
Sailors(sid,sname,rating,age)
Boats(bid,bname,color)
Reserves(sid,bid,day,)
Ex: List the details of the sailors whose rating is greater than 8
σ rating >8(Sailors)
Πsname,rating(Sailors)
Ex: List name and rating of all sailors whose rating is greater than 8
Π sname((σbid=103(Reserves) ⋈Sailors)
Ex: List the names of the sailors who reserve red boat
Π sname((σcolor=’red’(BOATS) ⋈Reserves⋈Sailors)
K.V.R
Chebrolu Engineering College
Ex: List the colors of the boats reserved by lubber
Π colors((σsname=’lubber’(sailors) ⋈Reserves⋈Boats)
Ex: List the names sailors who have reserve at least one boat
Π sname(Sailors⋈Reserves)
Ex: find the names of the sailors who reserved a red or green boat
Ex: Find the sids of sailors with age over 20 who have not reserved a red boat
K.V.R
Chebrolu Engineering College
Unit-III
1. DDL statements
2. DML statements
3. DCL statements
Datatypes:
Ex:
Student(rollno:number,name:character,gpa:number)
K.V.R
Chebrolu Engineering College
CREATE TABLE STUDENT(
ROLLNO NUMBER(3),
NAME VARCHAR2(30),
GPA NUMBER(2,1));
Employee(eno:number,ename:character,dob:date,mgr:number,deptid:nu
mber)
ENO NUMBER(3),
ENAME VARCHAR2(30),
DOB DATE,
MGR NUMBER(3),
DEPTID NUMBER(2));
K.V.R
Chebrolu Engineering College
DROP TABLE: This statement is used to delete a table. The syntax of this
statement is
DESC <tablename>;
DESC employee;
student
Insert : This statement is used to insert a record into the table. The syntax
of this statement is
Ex:
Ex:
K.V.R
Chebrolu Engineering College
Insert into student values(&rollno,’&name’,&gpa);
Insert rollno:501
Insert name:sai
Sql> /
Ex:
Update: This statement is used to modify the data. The syntax of this
statement is
Update tablename
Set col=value
[where condition];
When where clause is not specified all the rows are updated. When where
clause is specified only condition satisfied rows are updated.
Ex:
Update student
Set gpa=9.2
Where rollno=501;
Delete: This statement is used to delete the rows from a table. The syntax of
this statement is
K.V.R
Chebrolu Engineering College
Delete *
From tablename
[where condition]
When where clause is not specified all the rows of the table is deleted. when
where clause is specified only the condition satisfied rows are deleted.
Ex:
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Special operators
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
Mod Remainder
Relational operators: These operators are used to compare the values. The
relational operators are
Operator Meaning
< Lessthan
> Greaterthan
<= Less than or equal
>= Greater than or equal
= Equal
!= Not equal
K.V.R
Chebrolu Engineering College
Logical operators: These operators are used to combine two or more
conditions. The logical operators are
Operator Meaning
AND Logical AND
OR Logical OR
NOT Logical NOT
Operator
IN
BETWEEN
LIKE
EMP Table
DEPT Table
Select statement: This statement is used to retrieve the data from a tables.
The syntax of this statement is
SELECT columnlist
[WHERE condition]
K.V.R
Chebrolu Engineering College
[GROUP BY column]
[HAVING condition]
[ORDER BY column]
Ex-2: List employee no,name and salary for all the employees
Ex-3: List the details of the employees who are working in dept 10
Ex-5: List name,sal and date of join of the employees who are working in
dept 10 or dept 20
Ex-6: List details of the employees whose job is sales and salary is>25000
K.V.R
Chebrolu Engineering College
Ex-9: List deptname and loc of all the departments
Ex-13: List the details of the employees who joined before 2000
Ex-15: List names and salary of all employees in descending of the salary
Ex-16: List the details of the employees who are working in dept 10 in
ascending order of the salary
Ex-17: List the names of the employees whose employee numbers are
7369,7521,6789,4567,2345
(Or)
Ex-18: List the details of the employees does not belongs to depts 10,30,40
K.V.R
Chebrolu Engineering College
Ex-19:List the details of the employees whose salary in between 10000 to
15000
(Or)
Ex-21: List the employee names who are not eligible for commission
Ex-22: List the names of the employees who does not report to any body.
Ex-25: List the details of the employees whose salary is greater than 10000
and comm is null
Like operator: it is used for pattern matching. This operator has two wild
characters % and _.
Ex-26: List the names of the names of the employees whose name begins
with s
Ex-27: List the name of the employees who name begins with sai
K.V.R
Chebrolu Engineering College
Select ename from emp where ename like ‘sai%’;
Ex-28: List names of the employees whose name ends with lakshmi
Ex-29: List the names of the employees who name has exactly 5 characters
Ex-30: List names of the employees whose name second character is ‘e’
K.V.R
Chebrolu Engineering College
Ex-37: List no of employees in deptno 10
Ex-38: list no of employees, max sal,min sal,avg sal ,total salary of dept 10
employees
Group by: This clause is used to divide the rows of table into smaller
groups
Having clause is used to retrieve the rows from a group result with a
specific condition.
Ex-41: List jobs and no employees in each job. The result should be
descending order of no of employees
Ex-42: List total salary, maximum, minimum, avg salary of employees job
wise
Ex-44: List total salary, maximum salary, minimum salary, and average
salary of employees job wise for dept 20 only
K.V.R
Chebrolu Engineering College
Ex-45: List avg monthly salary for each job type with in department
Ex-46: List avg salary for all departments employees more than 5 people
Ex-47: list jobs of all the employees where maximum salary is greater than
5000
1. Union
2. Intersect
3. Minus
Union: This statement merges the outputs of two or more queries into a
single set of rows and columns. The syntax of this statement is
Select <statement-1>
Union
Select <statement-2>
[order by column]
Union
Ex-49: list ename, salary for the employees who are working in dept 20
and dept 30 order by descending order of salary.
K.V.R
Chebrolu Engineering College
Select ename,sal from emp where deptno=20
Union
Order by 2 desc;
Intersection: This statement returns all the rows which are common in
both the sets. The syntax of this statement is
Select <statement-1>
Intersect
Select <statement-2>
Ex-50: List the jobs which are common in dept 20 and dept 30
Intersect
Minus: This operators return the rows unique to first query. The syntax of
this operator is
Select <statement-1>
Minus
Select <statement-2>
[order by column]
Minus
K.V.R
Chebrolu Engineering College
Select job from emp where deptno=20
Minus
Sub queries: A query which contains another query is called as sub queries.
Sub queries are used to write the queries very efficiently. Sub queries are
very useful when the required information in one table and condition is in
another table.
Ex-53:
List the names of the employees who were working in CSE in department
Ex -54:
Ex-56:
List names of the employees who were drawing greater than average salary
Ex-57:
List the details of the employees whose salary is greater than avg salary
and hiredate is before 01-apr-2000
K.V.R
Chebrolu Engineering College
Select * from emp where sal>(select avg(sal) from emp where hiredate<’01-
apr-2020’)
Ex-58:
Select job,Avg(sal)
From emp
Group by job
Ex-59: List name of the employees who earn lowest salary in each dept
Ex-60:
List the employee numbers and names who earn salary greater than the
avg salary for their department
Select empno,ename,sal,deptno
From emp e
Ex-61:
List all employees who have at least one person reporting them
Select empno,ename,job,deptno
From emp e
K.V.R
Chebrolu Engineering College
Where exists( select empno from emp where emp.mgr=e.empno)
Order by empno;
Ex-62:
List the employee details if and only if more than 10 employees are present
in department 10
Group by deptno
Having count(*)>10);
1. Arithmetic functions
2. Character functions
3. Date functions
4. General functions
1. ABS(N)
2. CEIL(N)
3. FLOOR(N)
4. MOD(m,n)
5. POWER(m,n)
6. SIGN(n)
7. SQRT(n)
8. TRUNC(n)
9. ROUND(n)
10.EXP(n)
K.V.R
Chebrolu Engineering College
Output:65
CEIL(n): This function return the smallest integer greater than or equal to
n
Output: 89
Output: 88
Output:1
Output:125
Output: 1
Output:-1
Output:3
K.V.R
Chebrolu Engineering College
Ex: select trunc(123.456,1) from dual;
Output:123.4
Ex:
Output: 123.45
Output:
1234.8 1234
Output:54.59
Chr(x): This functions return the character for the given ascii value.
Output: d
Ex:
Output:
Goodmorning
K.V.R
Chebrolu Engineering College
LOWER(string): This function convert the string into lower case
Output:oodm
ASCII(char): This function return the ascii value for the given character
Output:97
Output:6
Date functions: These functions return date data type as a result. Some of
the date functions are
1. SYSDATE
2. ADD_MONTHS(d,n)
3. MONTHS_BETWEEN(d1,d2)
4. LAST_DAY(d)
5. NEXT_DAY(d)
6. TO_CHAR(d,f)
7. TO_DATE(char,f)
K.V.R
Chebrolu Engineering College
Ex:
Output: 4
Output:31-aug-2020
Next_day(date,day):
Output: Tuesday
OUTPUT: SEP
1. Unique
2. Not null
3. Primary key
4. Check
5. Default
6. Foreign key
K.V.R
Chebrolu Engineering College
Unique constraint: When we define unique constraint to a column then the
column value must be unique i.e the column value can’t be repeated. A
unique constraint allows null values.
Not null: When we define not null constraint to a column then the column
value can’t be null.
Primary key: primary key is key which is unique and not null.
Ex:
Foreign key: A foreign key is a non key attribute in one table and it is a
primary key in another table. It is used to establish the relationship
between two tables.
Ex:
Emp(eno,ename,salary,deptno)
Dept(deptno,dname,loc)
Here deptno is a foreign key. This key is a non key attribute in emp table
and primacy key in dept table.
K.V.R
Chebrolu Engineering College
Not null, salary number(7,2) not null, deptno number(3) references
Dept(deptno));
1. Entity integrity
2. Referential integrity
Entity integrity: The primary key value can’t be NULL. This is called as
entity integrity.
Ex:
Employee(eno,ename,salary)
Here eno is primary key. This value can’t be NULL. This is called as entity
integrity.
Referential integrity: Foreign key is a non key attribute in one relation and
it is a primary key in another relation. The value if the foreign key is either
null or match the primary key.
Ex:
Emp(eno,ename,salary,deptno)
Dept(deptno,dname,loc)
Here deptno is a foreign key. The value of the deptno in emp table is either
null or match with the deptno of dept table.
Self join: A table which join the table itself is called as self join. The syntax
of self join is
Select columnlist
Where t1.column=t2.column
Select t1.ename,t2.ename
Inner join: An inner join will display the rows which satisfies the
condition. The syntax of inner join is
Select columlist
From table1
Select columnlist
From table1
Select columnlist
From table1
Select columnlist
From table1
Trigger:
K.V.R
Chebrolu Engineering College
Ex:
Before
Insert
On student
Set student.total=student.sub1+student.sub2+student.sub3;
UNIT-IV (NORMALIZATION)
1. 1 NF(Normal Form)
2. 2 NF
3. 3 NF
4. BCNF
5. 4 NF
6. 5 NF
STUDENT
SID NAME MOBILENO
501 Ramya 9877889910
8988990011
502 Lakshmi 8899888800
K.V.R
Chebrolu Engineering College
9966330011
The above relation is not in first normal form because mobile no is a multi
valued attribute and some students has multiple mobile no. The above
relation can be modified by
Ex:
The above relation is not first normal form because the name column is not
atomic. The first normal form for the above relation is
Second normal form (2NF): A relation is said to be second normal form iff
it is in first normal form and every non key attribute is fully functional
dependent on primary key.
X Y.
K.V.R
Chebrolu Engineering College
Ex:
The above relation is in second normal form. The key attribute is sid and
non key attributes are name and marks. The attributes name and marks is
fully functional dependent on sid. The functional dependency for the above
relation is
SID NAME
SID MARKS
Ex:
CITY PINCODE
Third normal form (3NF): A relation is said to be third normal form iff it is
in second normal form and there is no transitive dependency.
Candidate key: A key which has unique property such a key is called as
candidate key.
Ex:
K.V.R
Chebrolu Engineering College
125 JAVA SAI 9.2
125 DBMS LAKSHMI 9.1
TEACHER SUBJECT
KVR CP
RAM JAVA
KIRAN CP
SAI JAVA
LAKSHMI DBMS
TEACHER SUBJECT
K.V.R
Chebrolu Engineering College
Subject Teacher textbook
DBMS Ram Korth
venkat Ramakrishna
date
Java Sai Dietel
Kiran ramchandrea
raj
SUBJECT TEACHER
DBMS ram
DBMS venkat
Java sai
Java kiran
Java raj
SUBJECT TEXTBOOK
DBMS Korth
DBMS ramakrishna
DBMS date
Java dietel
Java ramchandra
K.V.R
Chebrolu Engineering College
Fifth normal form (5NF): fifth normal form is a lossless join dependency. It
is also called as DKNF (Domain key normal form). This normal form is
used to join the relations.
K.V.R
Chebrolu Engineering College
3. Deletion anomalies: An error or inconsistency that is occur during data
deletion is called as deletion anomaly
4. Update anomalies: An error or inconsistency that is occurring during data
update is called as update anomaly.
Rating Hourly_wage
10 1500
8 1000
K.V.R
Chebrolu Engineering College
Let R be a relation and F be as set of FDs that held over R. The decomposition
of R into the relations with attribute sets R1 and R2 is loss less if and only if F+
contains either the FD R1∩R2 R1 or R1∩R2 R2.
For example
Employee(eno,name,rating,hours_wage,hours_worked)
R1(eno,name,rating,hours_worked)
R2(rating,hours_wage)
Rating Hourly_wage
10 1500
8 1000
K.V.R
Chebrolu Engineering College
combine the functional dependencies two relations is same as the functional
dependencies of original relation.
Ex:
Employee(eno,name,rating,hours_wage,hours_worked)
Eno name
Eno rating
Eno hours_worked
Rating hours_wage
The above relation is decomposed into two relations and the functional
dependencies are
Emp_work(eno,name,hours_worked)
Eno name
Eno rating
Eno hours_worked
Emp_wage(rating,hours_wage)
Rating hours_wage
When we union these functional dependencies then we get the original table
functional dependencies
B C . FABUFBC F
K.V.R
Chebrolu Engineering College
Unit-v
1. Atomic
2. Consistency
3. Isolation
4. Durability
Atomic: An execution of each transaction either all actions carried out or none.
User should not worry about incomplete transaction.
Durability: Once the transaction has completely successful its effects should be
persist even if the system crashes. This is called as durability.
Transaction log:
A DBMS uses a transaction log to keep track of all transactions that
update the database. The information stored in this log is used by the
DBMS for a recovery requirement triggered by a ROLLBACK statement, a
program’s abnormal termination, or a system failure such as a network
discrepancy or a disk crash. While the DBMS executes transactions that
modify the database, it also automatically updates the transaction log.
K.V.R
Chebrolu Engineering College
Transaction management using commit ,rollback and save point:
Concurrency Control
o In the concurrency control, the multiple transactions can be executed simultaneously.
o It may affect the transaction result. It is highly important to maintain the order of
execution of those transactions.
Several problems can occur when concurrent transactions are executed in an uncontrolled
manner. Following are the three problems in concurrency control.
1. Lost updates
2. Dirty read
3. Unrepeatable read
Example:
K.V.R
Chebrolu Engineering College
Here,
2. Dirty Read
o The dirty read occurs in the case when one transaction updates an item of the
database, and then the transaction fails for some reason. The updated database item is
accessed by another transaction before it is changed back to the original value.
o A transaction T1 updates a record which is read by T2. If T1 aborts then T2 now has
values which have never formed part of the stable database.
Example:
K.V.R
Chebrolu Engineering College
o So, Transaction-X now contains a value which has never become part of the stable
database.
o Such type of problem is known as Dirty Read Problem, as one transaction reads a
dirty value which has not been committed.
Example:
K.V.R
Chebrolu Engineering College
o Transaction-X is doing the sum of all balance while transaction-Y is transferring an
amount 50 from Account-1 to Account-3.
o Here, transaction-X produces the result of 550 which is incorrect. If we write this
produced result in the database, the database will become an inconsistent state
because the actual sum is 600.
o Here, transaction-X has seen an inconsistent state of the database.
In this type of protocol, any transaction cannot read or write data until it acquires an
appropriate lock on it. There are two types of lock:
1. Shared lock:
o It is also known as a Read-only lock. In a shared lock, the data item can only read by
the transaction.
o It can be shared between the transactions because when the transaction holds a lock,
then it can't update the data on the data item.
2. Exclusive lock:
o In the exclusive lock, the data item can be both reads as well as written by the
transaction.
o This lock is exclusive, and in this lock, multiple transactions do not modify the same
data simultaneously.
It is the simplest way of locking the data while transaction. Simplistic lock-based protocols
allow all the transactions to get the lock on the data before insert or delete or update on it. It
will unlock the data item after completing the transaction.
K.V.R
Chebrolu Engineering College
o Before initiating an execution of the transaction, it requests DBMS for all the lock on
all those data items.
o If all the locks are granted then this protocol allows the transaction to begin. When the
transaction is completed then it releases all the lock.
o If all the locks are not granted then this protocol allows the transaction to rolls back
and waits until all the locks are granted.
Growing phase: In the growing phase, a new lock on the data item may be acquired by the
transaction, but none can be released.
Shrinking phase: In the shrinking phase, existing lock held by the transaction may be
released, but no new locks can be acquired.
In the below example, if lock conversion is allowed then the following phase can happen:
K.V.R
Chebrolu Engineering College
Example:
The following way shows how unlocking and locking work with 2-PL.
Transaction T1:
Transaction T2:
K.V.R
Chebrolu Engineering College
Strict Two-phase locking (Strict-2PL): There are two rules in strict two phase locking
protocol. They are
If a transaction T wants to read an object it first request shared lock on that object. If the
transaction T wants to modify an object it first request exclusive lock on that object.
All the locks held by the transaction are released when the transaction is completed.
Example-1: Transaction T1 obtain an exclusive lock on object A then the transaction can perform
read and write operations on that object. When transaction T2 request a lock on A however this
request can’t be granted until T1 release exclusive lock on A
T1 T2
X(A)
R(A)
W(A)
Ex-2: Transaction T1 obtain exclusive lock on object A and B then the transaction can perform read
and write operations on the objects. The transaction T2 can process the objects after T1 release the
locks.
T1 T2
X(A)
R(A)
W(A)
X(B)
R(B)
W(B)
COMMIT
X(A)
R(A)
W(A)
X(B)
R(B)
W(B)
COMMIT
Ex-3: Transaction T1 obtain a shared lock on object A, Transaction T2 obtain shared lock on object A
and exclusive lock on object B. The transaction T1 obtain exclusive lock on object C.
K.V.R
Chebrolu Engineering College
T1 T2
S(A)
R(A)
S(A)
R(A)
X(B)
R(B)
W(B)
COMMIT
X(C )
R(C)
W(C)
COMMIT
Dead Lock: Let us assume that there are two transactions T1 and T2. Transaction T1 set
exclusive lock on object A and request exclusive lock on Object B. Transaction T2 set
exclusive lock on object B and request exclusive lock on object A. Now T1 waiting for T2 to
release its locks and T2 waiting for T1 to release its locks. Such a cycle of transaction waiting
is called as deadlock.
The DBMS keep track of the locks issued to the transaction is called as lock manager. The
lock manager maintains lock table. The lock table contains the number of transactions
currently holding a lock on the object, the nature of the lock (shared or exclusive) and pointer
to queue of lock requests.
Dead lock detection: Deadlocks can be detected by using wait-for graph. The nodes
represents the transactions and edges represents the request objects hold by other
transactions. For example transaction Ti Tj indicates that transaction Ti requesting
resources which is held by transaction Tj When there is cycle found in the graph indicates
there is a deadlock in the system otherwise no deadlock. Before issuing lock to the object the
lock manager will add the request to the graph and check weather there is any cycle or not.
When cycle is found the lock can’t be issued otherwise issue the lock to transaction for the
object.
T1 T2
T3 T4
K.V.R
Chebrolu Engineering College
For the above example there are four transactions T1, T2, T3 and T4. The transaction T1
requesting objects which is hold by transaction T2. The transaction T2 requesting objects
which is hold by Transaction T4. The transaction T4 requesting objects which are hold by
transaction T1. In the above graph the cycle is found and hence the system is in deadlock.
Deadlock Prevention
o Deadlock prevention method is suitable for a large database. If the resources are
allocated in such a way that deadlock never occurs, then the deadlock can be
prevented.
o The Database management system analyzes the operations of the transaction whether
they can create a deadlock situation or not. If they do, then the DBMS never allowed
that transaction to be executed.
Wait-Die scheme
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.
Let's assume there are two transactions Ti and Tj 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:
1. Check if TS(Ti) < TS(Tj) - If Ti is the older transaction and Tj has held some
resource, then Ti 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 resource until it is
available.
2. Check if TS(Ti) < TS(Tj) - If Ti is older transaction and has held some resource and if
Tj is waiting for it, then Tj is killed and restarted later with the random delay but with
the same timestamp.
Transaction Recovery: Transaction recovery is used to recover the data when the system is
crashed. The recovery manager of a DBMS is responsible for ensuring two important
properties of transactions. They are atomicity and durability. It ensures atomicity by undoing
the actions of the transactions that don’t commit and durability makes sure that all actions of
committed transactions survive system crashes and media failures.
K.V.R
Chebrolu Engineering College
ARIES is a recovery algorithm which is conceptually simple, works well with wide
range of concurrency control mechanism. The recovery manager is invoked after crash,
restart proceeds in three phases. They are
1. Analysis
2. Redo
3. Undo
Analysis: Identifies dirty pages in the buffer pool and active transactions at the time of crash.
Redo: Repeat all actions starting from appropriate point in the log and restores the database
state to what it was at the time of crash
Undo: undoes the actions of transactions that did not commit, so that the database reflects
only the actions of committed transactions.
Ex;
LSN LOG
10 update: T1 writes P5
20 update: T2 writes p3
30 T2 commit
40 T2 end
50 update: T3 writes p1
60 update: T3 write p3
Crash, Restart
When the system is crashed the analysis phase identifies transactions T1 and T3 are the active
transactions and P1,P3 and P5 are dirty pages. All updates for the transactions T1 and T3 are
reapplied in the order shown during the redo phase. Finally the actions of T1 and T3 are undo
in the reverse order during undo phase.
Write-Ahead logging: Any change in the database object is first recorded in the log. The
record in the log must be written to the stable storage before the change to the database object
is written to disk.
Repeating history during redo: When the system is crashed the algorithm retraces all actions
of the DBMS before the crash and brings the system back to the exact state that it was at the
time of crash. It undo all the actions of the transactions still active at the time of crash
K.V.R
Chebrolu Engineering College
Logging crash during undo: Changes made to the database while undoing a transaction are
logged to ensure such an action is not repeated in the event of repeated restarts.
K.V.R
Chebrolu Engineering College