You are on page 1of 71

File system and its disadvantages:

COBOL: COMMON BUSINESS ORIENTED LANGUAGE

File: collection of record

Record: A collection of fields

Use: to store information on disks

Disadvantages of file system:

1. Data redundancy or Data duplication:


2. Data inconsistency
3. Lengthy program development
4. Data dependency
5. No data sharing
6. No data security

Data Base Management System:

Data: Known facts or raw facts of an organization

Information: Data which is processed is called as information

Database: collect of interrelated data

Database management system: collection of interrelated data and some set of


programs which is used to access the data very efficiently.

Adv of DBMS:

1. Minimum data redundancy

Htno Name Mobile no Address


1 Yy 8888888888 guntur
2 Zz 8787878787 nrt

Htno Marks Emailid


1 45 Sddddd.gmt
2 47 Ddddd.d.d.d.d

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

Three schema architecture of DBMS: The overall Design of a database is called


a schema. A database system has three schema. They are

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

Application programmers: persons who develop the application programs

Database administrators: skilled person capable to manage all the resource of


the database

Conceptual schema: All application programs at conceptual schema. A


conceptual schema will specify what data is to be stored in the data base

Internal schema: how the data is stored in the database

The purpose of schema architecture is to achieve the data independency. There


are two types of data independency.

1. Logical data independency


2. Physical data independency

Logical data independency: the ability to modify external schema without


affecting the conceptual schema is called as logical data independency

Physical data independency: The ability to modify conceptual schema without


affecting internal schema is called as physical data independency.

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 .

Htno Name Marks


501 Ravi 56
502 Kkk 45
503 Rrr 89
504 Jjj 45
505 Ttt 34

This model will support one to one , one to many , many to one, many to many.

Each row is called as tuple . Each col is called as domain.

Oracle is a relational datamodel

Object oriented model: An object is a runtime entity type or an instance of type


class. Each object will contain data and methods. The properties are called as
data. Methods will operate on data.

Client/server architecture: Application logic mainly contains 3 components.


They are

1. Presentation logic

K.V.R
Chebrolu Engineering College
2. Processing
3. Data storage

Presentation logic: presentation logic is responsible for input and output.

Processing: the procedure to solve the problems

Data storage : it is used to store the data.

UG PG

SCIENCE ENGI M.TECH MBA

There are 3 types of client server architecture. They are

1. Single tier architecture


2. Two tier architecture
3. Three tier architecture

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

Role Database Administrators: Database Administrator is a skilled person who


is responsible for all the critical issues in the database. The role of DBA is

1. Design of conceptual schema


2. Design of physical schema
3. Data security and authentication
4. Data availability and recovery from failure

K.V.R
Chebrolu Engineering College
5. Database tuning

Unit-1

1. Disadvantages of file system


2. Advantages of dbms
3. Data models
4. Three schema architecture of DBMS
5. Data independency
6. Role of DBA

Unit-II

ER diagrams(Entity Relationship diagram):

Entity: An entity is an object. An entity is represented in a rectangle. Ex:


student, customer, car. There are two types of entities. They are

1. Strong entity
2. Weak entity

Strong Entity: An entity which can be identified independently such an entity is


called as strong entity. The strong entity can be represented by

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.

Attributes: The properties or characteristics of an entity type are called as an


attribute. The differ attributes are

1. Key attribute
2. Derived attribute
3. Composite attribute

K.V.R
Chebrolu Engineering College
4. Non key attribute
5. Multi valued attribute

Key attribute: An attribute value which is unique such an attribute is called as


key attribute. The representation of key attribute is

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

Composite attribute: An attribute which can be further divided such an attribute


is called as composite attribute.

NAME

FIRST NAME MIDDLE LAST NAME


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

Relationship: An association between entity type is called as relationship. There


are two types of relationships. They are

1. Strong relation ship


2. Weak relation ship

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

Ex: student join in a course

C_ID C_NAME DURATION


ROLL NO NAME MOBILE

STUDENT JOIN COURSE


K.V.R
Chebrolu Engineering College
Degree of a ER diagram: The number of entities that are participated in a
relationship is called as degree of a relationship. There are 3 types of
relationships. They are

1. Unary relationship
2. Binary relationship
3. Ternary relationship

Unary relationship: A relationship which contains only one entity type is called
as unary relationship.

Ex: person marry a person

P_id

MARRY
PERSON

name

Binary relationship: A relationship which contains two entity types such a


relationship is called as binary relationship.

Ex:

Student join course

student course
Join

Ternary relationship: A relationship which contains 3 entity types is called as


ternary relationship.

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

Types of relationships: An entity instance of one entity type that can be


associated with an entity instance of other entity type is called as type of
relationship. There are 4 types of relationships. They are

1. One to one relationship


2. One to many relationship
3. Many to one relationship
4. Many to many relationship

One to one relationship: An entity instance of Entity type-A is associated with


an only one entity instance of entity type –B is called as one to one relationship.

Entity type-A Entity type-B

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

Entity type-A Entity type-B

Many to one relationship: Many entity instances of entity type-A is associated


with only one entity instance of entity type –B is called as many to one
relationship.

Entity type-A Entity type-B

Many to many relationship: Many entity instances of entity type-A is associated


with many entity instances of entity type –B is called as many to many
relationship.

K.V.R
Chebrolu Engineering College
Entity type-A Entity type-B

Generalization: Extraction of common properties from a set of entity types is


called as generalization. It is a bottom up approach. (Or) It is a process of
creation of super type from set of sub type entity types.

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.

Ex: The persons in an organization are either student or employee. The


specialization for the above example is

Id Name

PERSON

Is a

Fee

K.V.R
Chebrolu Engineering College
EMPLOYEE STUDENT

Aggregation: A relationship that associated with another relationship is called as


aggregation. (or) A relationship which act as an entity for another relationship is
called as aggregation.

Ex: project sponsor departments which is managed by employees

Employee

manage

pid pname pbudget deptid


budget
pbudget

project sponsor Department

Relational Algebra: It is a query language which is used to retrieve the data


from a relational model. The different operations that are performed on
relational model is

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

Sid Sname Rating age


11 Ravi 5 45
12 venkat 6 38
13 sai 7 42

Boats

Bid Bname Color


101 xx Red
102 yy Green
103 zz Red
104 aa yellow

Reserves

Sid Bid day


11 103 21/7/2020
12 101 20/6/2020

Projection: it is a vertical subset of a relation. It is represented by π. The syntax


of projection is

Π(Colum list)

Ex: List names of all the boats

Π(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

Ex: List the names of all the sailors

Π(names)sailors

Sname
Ravi
venkat
sai

Ex-4: list name and age of all the sailors

Π(name,age)sailors

Output:

Sname age
Ravi 45
venkat 38
sai 42

Selection: It is a horizontal subset of a relation. It is represented by sigma σ

Sid Sname Rating age


12 venkat 6 38
13 sai 7 42

Ex: List the details of the sailors whose age is less than 40

K.V.R
Chebrolu Engineering College
σ (age<40)sailors

Sid Sname Reating age


12 venkat 6 38

Ex: List details of the sailors whose rating is >=6

σ(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

Sid Name Marks


1 Sai 56
2 Venkat 67
3 Raju 89

Student2

Sid Name Marks


10 Rajani 89
11 Vyshnavi 45
3 Raju 89
2 Venkat 67
12 Lakshmi 45

Student1U student2

Sid Name Marks


1 Sai 56
2 Venkat 67
3 Raju 89
10 Rajani 89
11 Vyshnavi 45
12 Lakshmi 45

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

Ex: student1 ∩ student2

Sid Name Marks


2 Venkat 67
3 Raju 89

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

Sid Name marks


1 Sai 56

Ex-2:

Student2-student1

Sid Name marks


10 Rajani 89
11 Vyshnavi 45
12 Lakshmi 45

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

Equi join: In this join the condition is equal.

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

Natural join: In this join we can’t use any condition.

T1

Num square
2 4
3 9

T2

Num cube
2 8
3 27

T1 ⋈T2

Num Square Cube


2 4 8
3 9 27

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

1. Left outer join


2. Right outer join
3. Full outer join

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

NUM Square cube


2 4 8
3 9 27
4 16

T2 T1

Num Cube Square


2 8 4
3 27 9
5 125
10 1000

It produces all the rows of T1 and matching rows of T2

Right outer join: It produces all the rows of second table and matching
rows of first table. It is represented by

Ex: T1 T2

NUM Square Cube


2 4 8
3 9 27
5 125
10 1000

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

Num Square Cube


2 4 8
3 9 27
4 16
5 125
10 1000

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)

Ex-2: List name and rating of all sailors

Πsname,rating(Sailors)

Ex: List name and rating of all sailors whose rating is greater than 8

Π sname,rating(σ rating >8(Sailors))


Ex: find the names of the sailors who have reserve boat 103

Π 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

Π sname(σcolor=’red’˅ color=’green’(BOATS) ⋈Reserves⋈Sailors)

Ex: Find the sids of sailors with age over 20 who have not reserved a red boat

Πsid(σage>20(sailors))- Πsid((σcolor=’red’(Boats) ⋈Reserves⋈Sailors)

K.V.R
Chebrolu Engineering College
Unit-III

SQL: structured query language. It is a non procedural language. The


statements in SQL are divided into three types. They are

1. DDL statements
2. DML statements
3. DCL statements

DDL statements: DDL means Data Definition Language. These statements


are used to create, alter and delete database objects such as tables, views,
procedures, etc.

Creation of a table: The syntax to create a table is

Create table <tablename>

(column-1 datatype(size), column-2 datatype(size) , ………… column-n


datatype(size));

Datatypes:

Char---fixed length of character maximum of 256 characters

Varchar2 --- variable length of characters maximum of 2000

Number – it is used to store either integer or floating point numbers.

An integer can be defined as number(scale)

A floating point number can be defined as number(s,p)

Date : it is used to store date

Long: it is also used to store characters. It allows maximum of 2GB

RAW: It is used to store binary information

BLOB: Binary large objects

CLOB: character large objects.

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)

CREATE TABLE EMPLOYEE(

ENO NUMBER(3),

ENAME VARCHAR2(30),

DOB DATE,

MGR NUMBER(3),

DEPTID NUMBER(2));

Alter table: This command is used to add a column, modify a column or


delete a column. The syntax of this statement is

ALTER TABLE <tablename>

([ADD column_name datatype(size)]

[MODIFY column_name datatype(size)]

[DROP COLUMN column_name]);

Ex: Add salary column to employee table

ALTER TABLE EMPLOYEE(ADD salary number(6,2));

Ex: change the size of employee name from 30 characters to 40 characters

ALTER TABLE EMPLOYEE(MODIFY ename varchar(40));

Ex: Delete the column mgr from employee table

ALTER TABLE EMPLOYEE(DROP COLUMN mgr);

K.V.R
Chebrolu Engineering College
DROP TABLE: This statement is used to delete a table. The syntax of this
statement is

DROP TABLE <tablename>;

Ex: Delete table student

DROP TABLE student;

Describe the table structure:

DESC <tablename>;

DESC employee;

DML statements: Data Manipulation Language. These statements are used


to insert, modify, read and delete the data.

student

Rollno Name gpa

Insert : This statement is used to insert a record into the table. The syntax
of this statement is

1) Insert into <tablename> values(value-1,value-2,value-3,…..,value-n);

When the value is a string or character it must be enclosed in quotes. This


syntax is used to insert entire record.

Ex:

Insert into student values(501,’sai’,8.9);

Insert into student values(502,’lakshmi’,7.8);

Insert into student values(503,’bhanu’,6.8);

2) Insert into <tablename>values(&column-1,&column-2,…..,&column-


n);
Interactive method to insert a record

Ex:

K.V.R
Chebrolu Engineering College
Insert into student values(&rollno,’&name’,&gpa);

Insert rollno:501

Insert name:sai

Insert gpa: 8.7

One record is inserted

Sql> /

To enter another record

3) Insert into <tablename>(col-1,col-2,…col-n) values(value-1,value-


2,…,value-n);

Ex:

Insert into student(rollno)values(504);

Insert into student(rollno)values(505);

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:

Delete * from student where rollno=501;

Operators: An operator is a symbol which is used to perform a specific


operation. There are 4 types of operators in SQL. They are

1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Special operators

Arithmetic operators: These operators are used to perform arithmetic


operations such as addition, subtraction, multiplication and division.

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

Special operators: The special operators are

Operator
IN
BETWEEN
LIKE

EMP Table

EMPN ENAM JOB MG HIREDAT SAL COM DEPTN


O E R E M O
101 Sai Sale 01-aug- 5600 10
s 1998 0

DEPT Table

DEPTNO DNAME LOC

Select statement: This statement is used to retrieve the data from a tables.
The syntax of this statement is

SELECT columnlist

FROM table name

[WHERE condition]
K.V.R
Chebrolu Engineering College
[GROUP BY column]

[HAVING condition]

[ORDER BY column]

Where clause is used to display only condition satisfied rows

Group by clause is used to display rows in group

Having clause is used to display values based on condition

Order by clause is used to display the values in either ascending or


descending order

Ex-1: List the details of all the employees

Select * from emp;

Ex-2: List employee no,name and salary for all the employees

Select empno,ename,sal from emp;

Ex-3: List the details of the employees who are working in dept 10

Select * from emp where deptno=10;

Ex-4: List name and salary of employees who are in sales;

Select ename,sal from emp where job=’sales’;

Ex-5: List name,sal and date of join of the employees who are working in
dept 10 or dept 20

Select ename,sal,hiredate from emp where deptno=10 or deptno=20;

Ex-6: List details of the employees whose job is sales and salary is>25000

Select * from emp where job=’sales’ and salary >25000;

Ex-7: List the details of all the departments

Select * from dept;

Ex-8: List the details of the departments whose loc is Guntur

Select * from dept where loc=’guntur’;

K.V.R
Chebrolu Engineering College
Ex-9: List deptname and loc of all the departments

Select dname,loc from dept;

Ex-10: List names of clerks who are working in dept 20;

Select ename from emp where job=’clerk’ and deptno=20;

Ex-11: List name of salespersons and analyst persons

Select ename from emp where job=’sales’ or job=’analyst’;

Ex-12: List details of the employees except managers

Select * from emp where job!=’manager’;

Ex-13: List the details of the employees who joined before 2000

Select * from emp where hiredate<’1-jan-2000’;

Ex-14: List the names of employees in ascending order.

Select ename from emp order by ename;

Ex-15: List names and salary of all employees in descending of the salary

Select ename,sal from emp order by sal desc;

Ex-16: List the details of the employees who are working in dept 10 in
ascending order of the salary

Select * from emp where deptno=10 order by sal;

Ex-17: List the names of the employees whose employee numbers are
7369,7521,6789,4567,2345

Select ename from emp where empno=7369 or empno=7521 or


empno=6789 or empno=4567 or empno=2345;

(Or)

Select ename from emp where empno in(7369,7521,6789,4567,2345);

Ex-18: List the details of the employees does not belongs to depts 10,30,40

Select * from emp where deptno not in(10,30,40)

K.V.R
Chebrolu Engineering College
Ex-19:List the details of the employees whose salary in between 10000 to
15000

Select * from emp where sal>=10000 and sal<=15000

(Or)

Select * from emp where sal between 10000 and 15000;

Ex-20: List the details of the employees who joined in 2010

Select * from emp where hiredate between ‘1-jan-2010’ and ’31-dec-2010’;

Ex-21: List the employee names who are not eligible for commission

Select ename from emp where comm Is NULL;

Ex-22: List the names of the employees who does not report to any body.

Select ename from emp where mgr is NULL;

Ex-23: List the employees does not assigned to any dept

Select * from emp where deptno is NULL;

Ex-24: list the employees who are eligible for commission

Select * from emp where comm Is not null

Ex-25: List the details of the employees whose salary is greater than 10000
and comm is null

Select * from emp where sal>10000 and comm Is NULL

Like operator: it is used for pattern matching. This operator has two wild
characters % and _.

% is used for Set of characters

_ is used for one character

Ex-26: List the names of the names of the employees whose name begins
with s

Select ename from emp where ename like ‘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

Select ename from emp where ename like ‘%lakshmi’;

Ex-29: List the names of the employees who name has exactly 5 characters

Select ename from emp where ename like ‘-----‘;

Ex-30: List names of the employees whose name second character is ‘e’

Select ename from emp where ename like ‘-e%’;

Aggregate functions: These functions produce single value from a group of


values. These functions are

1. COUNT ----Determine the number of rows


2. SUM---------- It returns sum of all the selected rows
3. MAX---------- It returns the maximum among all the selected rows
4. MIN----------- It returns the minimum among all the selected rows
5. AVG---------- It returns the average of all the selected rows

Ex-31: List total number of employees in the organization

Select count(*) from emp;

Ex-32: list the maximum salary among all the employees

Select max(sal) from emp;

Ex-33: List the minimum salary among all the employees

Select min(sal) from emp;

Ex-34: list maximum salary ,minimum salary and average salary

Select max(sal),min(sal),avg(sal) from emp;

Ex-35: List the avg salary of dept 20

Select avg(sal) from emp where deptno=20;

Ex-36: List maximum salary of a clerk

Select max(sal) from emp where job=’clerk’;

K.V.R
Chebrolu Engineering College
Ex-37: List no of employees in deptno 10

Select count(*) from emp where deptno=10;

Ex-38: list no of employees, max sal,min sal,avg sal ,total salary of dept 10
employees

Select count(*),sum(sal),max(sal),min(sal),avg(sal) from emp where


deptno=10;

Group by: This clause is used to divide the rows of table into smaller
groups

This clause is used with select statement

Having clause is used to retrieve the rows from a group result with a
specific condition.

Ex-39: List no of employees in each department

Select deptno, count(*) from emp group by deptno;

Ex-40: list dept no and total salary of each dept

Select deptno,sum(sal) from emp group by deptno;

Ex-41: List jobs and no employees in each job. The result should be
descending order of no of employees

Select job,count(*) from emp group by job order by 2 desc;

Ex-42: List total salary, maximum, minimum, avg salary of employees job
wise

Select sum(sal),max(sal),min(sal),avg(sal) from emp group by job;

Ex-43: List average salary for each job excluding manager

Select job, avg(sal) from emp where job!=’manager’ group by job;

Ex-44: List total salary, maximum salary, minimum salary, and average
salary of employees job wise for dept 20 only

Select job,sum(sal),max(sal),min(sal),avg(sal) from emp where deptno=20


group by job;

K.V.R
Chebrolu Engineering College
Ex-45: List avg monthly salary for each job type with in department

Select deptno,job,avg(sal) from emp group by deptno,job;

Ex-46: List avg salary for all departments employees more than 5 people

Select deptno,avg(sal) from emp group by dept having count(*)>5;

Ex-47: list jobs of all the employees where maximum salary is greater than
5000

Select job,max(sal) from emp group by job having max(sal)>5000;

Set operators: These operators are used to combine information of similar


type from one or more tables. There are 3 set operators in SQL. They are

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]

Ex-48: list the names of employees in dept 20 and 30

Select ename from emp where deptno=20

Union

Select ename from emp where deptno=30;

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

Select ename,sal from emp where deptno=30

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>

[order by column name]

Ex-50: List the jobs which are common in dept 20 and dept 30

Select job from emp where deptno=20

Intersect

Select job from emp where deptno=30;

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]

Ex-51: List the jobs which are unique to deptno 20

Select job from emp where deptno=20

Minus

Select job from emp where deptno=10;

Ex-52: find the jobs which are unique to dept 20

K.V.R
Chebrolu Engineering College
Select job from emp where deptno=20

Minus

Select job from emp where deptno in(10,30);

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

Select ename from emp

Where deptno=(select deptno from dept where dname=’CSE’);

Ex -54:

List the names of the who is drawing highest salary

Select ename from emp where sal=(select max(sal) from emp);

Ex-55: List name of the who is drawing min salary

Select ename from emp where sal=(select min(sal) from emp);

Ex-56:

List names of the employees who were drawing greater than average salary

Select ename from emp where sal>(select avg(sal) from emp);

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:

List the jobs with highest avg salary

Select job,Avg(sal)

From emp

Group by job

Having avg(sal)=(select max(avg(sal)) from emp group by job);

Ex-59: List name of the employees who earn lowest salary in each dept

Select ename, sal, deptno from emp

Where sal in( select min(sal) from emp group by deptno);

Correlated queries: A correlated query is also a nested query which is


executed once for each candidate row. There must be correlation column in
main query and subquery.

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

Where sal >(select avg(sal) from emp where deptno=e.deptno);

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

Select * from emp

Where deptno=10 and exists(select count(*) from emp where deptno=10

Group by deptno

Having count(*)>10);

Functions in SQL: The different functions in SQL is

1. Arithmetic functions
2. Character functions
3. Date functions
4. General functions

Arithmetic functions: These functions are used to perform arithmetic


operations. The various arithmetic functions are

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)

ABS(n): This function will return absolute value of the column

Select abs(-65) from dual;

K.V.R
Chebrolu Engineering College
Output:65

CEIL(n): This function return the smallest integer greater than or equal to
n

Select ceil(88.9) from dual;

Output: 89

Floor: This function return the largest integer greater than n

Select floor(88.9) from dual;

Output: 88

MOD(m,n): This function will return the remainder when m is divided by


n

Select mod(5,2) from dual;

Output:1

Power(m,n): This function returns m raised to the power n

Select power(5,3) from dual;

Output:125

Sign(n): This function returns -1 when the number is negative . It return 1


when the number is positive.

Select sign(123) from dual;

Output: 1

Select sign(-123) from dual;

Output:-1

Sqrt(n): This function will return the square root of a number

Select sqrt(9) from dual;

Output:3

Trunc(m,n): This function truncates the m with n decimal positions

K.V.R
Chebrolu Engineering College
Ex: select trunc(123.456,1) from dual;

Output:123.4

Ex:

Select trunc(123.4567,2) from dual;

Output: 123.45

Round(m,n): This function rounds the column to n decimal places. If n is


not specified then the decimal places is truncated

Select round(1234.78,1),round(1234.678) from dual;

Output:

1234.8 1234

Exp(n): This function will return exponent value of the value n

Select exp(4) from dual;

Output:54.59

Character functions: These functions are used to perform operations on


strings or characters. The different character functions are

Chr(x): This functions return the character for the given ascii value.

Ex: select chr(100) from dual;

Output: d

Concat(string1,string2): This function concat two strings

Ex:

Select concat(‘good’,’morning’) from dual;

Output:

Goodmorning

INITCAP(string): This function capitalize the first character of a string

Ex: select INITCAP(ename) from emp;

K.V.R
Chebrolu Engineering College
LOWER(string): This function convert the string into lower case

Ex: select LOWER(ename) from emp;

UPPER(string): This function convert the string into upper case

Ex: select UPPER(ename) from emp;

SUBSTR(string,m,n): This function return the substring starts from m th


position.

Select substr(‘goodmoring’,2,4) from dual;

Output:oodm

ASCII(char): This function return the ascii value for the given character

Select ascii(‘A’) from dual;

Output:97

LENGTH(): This function returns the length of the string.

Ex: select length(‘venkat’) from dual;

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)

Sysdate: This function returns the system date

Select sysdate from sys.dual;

ADD_MONTHS(d,n): This function add the number of months to the date.

K.V.R
Chebrolu Engineering College
Ex:

Select hiredate,add_months(hiredate,4) from emp;

MONTHS_BETWEEN(d1,d2): This function return the number of months


between two dates.

Select MONTHS_BETWEEN(‘4-AUG-2020’,’4-DEC-2020’) FROM dual;

Output: 4

Last_day(d): It returns the last day of the month

Select last_day(sysdate) from dual;

Output:31-aug-2020

Next_day(date,day):

Select next_day(sysdate,’MONDAY’) FROM dual;

TO_CHAR(d,f): This function converts the date into character format

Select sysdate,TO_CHAR(sysdate,’day’) from dual;

Output: Tuesday

TO_DATE: This function converts the character string into specific


format.

Select TO_DATE(SYSDATE,’MON’) FROM DUAL;

OUTPUT: SEP

CONSTRAINTS: A constraint is a condition which is used to maintain


good quality of data. The different constraints in SQL is

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.

Check constraint: This constraint is used to validate the column value

Default constraint: This constraint is used to set the default value of a


column.

Ex:

Create table student ( Regd_no number(3) primary key, name


varchar2(30) not null, marks number(3) check marks>=0 and
marks<=100);

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.

Create table Dept( deptno number(3) primary key, dname varchar2(20)


not null, loc varchar2(10));

Create table emp(eno number(3) primary key, ename varchar2(20)

K.V.R
Chebrolu Engineering College
Not null, salary number(7,2) not null, deptno number(3) references
Dept(deptno));

Integrity constraints: Constraints are used to maintain good quality of


data. There are two integrity constraints in DBMS. They are

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

From table t1,table t2

Where t1.column=t2.column

Ex: list the names of the employees and its manager

Select t1.ename,t2.ename

From emp t1,emp t2


K.V.R
Chebrolu Engineering College
Where t1.mgr=t2.empno;

Inner join: An inner join will display the rows which satisfies the
condition. The syntax of inner join is

Select columlist

From table1

Inner join on table2 condition;

Left Outer join:

Select columnlist

From table1

Left join on table2 condition;

Right outer join

Select columnlist

From table1

Right join on table2 condition;

Full outer join:

Select columnlist

From table1

Full join on table2 conditon;

Trigger:

A trigger is a stored procedure that will be executed automatically when


the event is occured. The syntax of trigger is
create trigger [trigger_name]
[before | after]
{insert | update | delete}
on [table_name]
[for each row]
[trigger_body]

K.V.R
Chebrolu Engineering College
Ex:

Create trigger stu_total

Before

Insert

On student

For each row

Set student.total=student.sub1+student.sub2+student.sub3;

UNIT-IV (NORMALIZATION)

Normalization: The disadvantages of redundancy are data inconsistency


and wastage of storage. Normalization is used to avoid redundancy.
Normalization is the process of decomposing a relation into smaller and
well defined relations. A well defined relation is a relation with no
anomalies during insertion, deletion and update. The different normal
forms are

1. 1 NF(Normal Form)
2. 2 NF
3. 3 NF
4. BCNF
5. 4 NF
6. 5 NF

FIRST NORMAL FORM (1 NF): A relation is said to be first normal


form iff all attributes must be atomic (which can’t be decomposable) and
there is no multi valued attributes.

STUDENT
SID NAME MOBILENO
501 Ramya 9877889910
8988990011
502 Lakshmi 8899888800

503 Sai 8877554430

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

SID NAME MOBILENO


501 Ramya 9977889910
501 Ramya 8988990011
502 Lakshmi 8899888800
503 Sai 9966330011
503 Sai 8877554430
The above relation is in first normal however it has redundancy. Sid and
name will appear multiple numbers of time.

Ex:

SID NAME MOBILE


firstname lastname
501 Kenn sai 9997776660
502 bommala Rakesh 8899669900
503 Ch Srivani 7778889990

The above relation is not first normal form because the name column is not
atomic. The first normal form for the above relation is

Sid First name Last name mobile


501 Kenn Sai 9997776660
502 Bommala Rakesh 8899669900
503 Ch Srivani 7778889990

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.

Functional dependency: Let us assume that x and y are the attributes in a


relation R, an attribute y is functional dependent on x iff for each value of x
there is a unique value of y. The functional dependency is represented by

X Y.

K.V.R
Chebrolu Engineering College
Ex:

SID NAME MARKS


501 Sai lakshmi 78
502 Vyshnavi 67
503 vyshnavi 69
504 Rajya lakshmi 78

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:

EID NAME SALARY CITY PINCODE


101 Raju 89000 Guntur 522001
102 Sai 78000 Guntur 522001
103 Vyshu 77000 Guntur 522001
104 Lakshmi 67000 Nrt 523090
The above relation is first normal form but not second form because
pincode dependent on city. The key attribute is ‘EID’. The non key
attributes are name, salary, city and pincode. For the above relation one
non key attribute dependents on another non key attribute. So this relation
is not in second normal form. The functional dependencies for the above
relation is

EID NAME, SALARY, CITY, PINCODE

CITY PINCODE

The above relation is decomposed into two relations.

EID NAME SALARY CITY


101 Raju 89000 Guntur
102 Sai 78000 Guntur
103 Vyshu 77000 Guntur
K.V.R
Chebrolu Engineering College
104 Lakshmi 67000 Nrt
CITY PINCODE
Guntur 522001
Nrt 523090

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.

Transitive dependency: A non key attribute which is functional dependent


on some other non key attribute then it is called as transitive dependency.

EID NAME SALARY CITY


101 Raju 89000 Guntur
102 Sai 78000 Guntur
103 Vyshu 77000 Guntur
104 Lakshmi 67000 Nrt

The functional dependencies for the above relation is

EID NAME, SALARY, CITY

The above relation is 3 rd normal form because it is in second normal form


and no transitive dependency. The key attribute is ‘EID’ and non key
attributes are name, salary and city.

BCNF(Boyce codd normal form): A relation is said to be BCNF iff it is in 3


NF and every determinant is a candidate key.

Candidate key: A key which has unique property such a key is called as
candidate key.

Determinant: An attribute which appear left side of the functional


dependency such attribute is called as determinant.

Ex:

SID SUBJECT TEACHER GPA


123 CP KVR 8.9
123 JAVA RAM 9.0
124 CP KIRAN 8.8

K.V.R
Chebrolu Engineering College
125 JAVA SAI 9.2
125 DBMS LAKSHMI 9.1

SID, SUBJECT TEACHER, GPA

This relation is in 3NF but not BCNF because determinant is not a


candidate key. Here subject is determinant and it is not a candidate key.

SID SUBJECT TEACHER GPA

The above relation is decomposed into two relations.

SID TEACHER GPA


123 KVR 8.9
123 RAM 9.0
124 KIRAN 8.8
125 SAI 9.2
125 LAKSHMI 9.1

SID, TEACHER GPA

TEACHER SUBJECT
KVR CP
RAM JAVA
KIRAN CP
SAI JAVA
LAKSHMI DBMS

TEACHER SUBJECT

Fourth Normal Form (4NF): A relation is said to be 4NF iff there is no


multi valued dependences.

Multi valued dependency: A multi valued dependency is exist when the


relation will contains at least three attributes. Let use assume ‘A’, ‘B’ and
‘C’ are the attributes in the relation R, for each value of A there is set of
values of B and Set of values of C.

K.V.R
Chebrolu Engineering College
Subject Teacher textbook
DBMS Ram Korth
venkat Ramakrishna
date
Java Sai Dietel
Kiran ramchandrea
raj

The normalized relation for the above example is

SUBJECT TEACHER TEXTBOOK


DBMS Ram korth
DBMS Ram ramakrishna
DBMS Ram date
DBMS venkat korth
DBMS venkat ramakrishna
DBMS venkat date
java sai dietel
java Sai ramchandra
java kiran dietel
java kiran ramchandra
java raj dietel
java raj ramchandra

The above relation is not in 4NF because it contains multi valued


dependencies. This relation is decomposed into two relations.

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.

Subject teacher Subject textbook


DBMS ram DBMS Korth
DBMS venkat DBMS ramakrishna
Java sai DBMS date
Java kiran Java dietel
Java raj Java ramchandra

SUBJECT TEACHER TEXTBOOK


DBMS Ram korth
DBMS Ram ramakrishna
DBMS Ram date
DBMS venkat korth
DBMS venkat ramakrishna
DBMS venkat date
java sai dietel
java Sai ramchandra
java kiran dietel
java kiran ramchandra
java raj dietel
java Raj ramchandra

Schema Refinement: The overall design of a database is called as schema. A


schema refinement is intended to address and refinement approach based on
decomposition. Redundant storage is the root cause of these problems. The
problems for redundant storage is

1. Redundant storage: some information is stored repeatedly. This is called


as redundant storage. It leads wastage of storage.
2. Insertion anomalies: An error or inconsistency that is occurring during the
data insertion is called as insertion anomaly.

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.

Eno Name Rating Hourly_wage Hours_worked


101 Ravi 10 1500 40
102 Kiran 8 1000 45
103 Sai 10 1500 48
104 Ram 8 1000 42
105 Sasi 10 1500 48
For the above example hourly_wage depends on rating. Hourly wage is a
redundant storage. When hourly_wage is increased then it is necessary to update
all the rows. This is called as update anomaly. To avoid this problem the
relation is decomposed into two relations.

Eno Name Rating Hours_worked


101 Ravi 10 40
102 Kiran 8 45
103 Sai 10 48
104 Ram 8 42
105 Sasi 10 48

Rating Hourly_wage
10 1500
8 1000

The problems related to decomposition are when to decompose a relation and


what problems does a given decomposition cause. A relation is decomposed
when there is a transitive dependency. The decomposition must be loss less join
property.

Properties of decomposition: Decomposition is tool that allows us to eliminate


redundancy. There are two properties of decomposition. They are

1. Loss less join decomposition


2. Dependency preserving decomposition

Loss less join decomposition: All decompositions used to eliminate redundancy


must be loss less.

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)

This relation is decomposed into two relations

R1(eno,name,rating,hours_worked)

R2(rating,hours_wage)

Eno Name Rating Hourly_wage Hours_worked


101 Ravi 10 1500 40
102 Kiran 8 1000 45
103 Sai 10 1500 48
104 Ram 8 1000 42
105 Sasi 10 1500 48

The above relation is decomposed into two relations. They are

Eno Name Rating Hours_worked


101 Ravi 10 40
102 Kiran 8 45
103 Sai 10 48
104 Ram 8 42
105 Sasi 10 48

Rating Hourly_wage
10 1500
8 1000

This decomposition is loss less decomposition because when we join the


relations then we get new get the existing relation.

Dependency preserving decomposition: The decomposition of a relation schema


R with FDs into schema with attribute set X and Y is dependency preserving
if(FxUFy)+=F+ . i.e when a relation is decomposed with two relations ,when we

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)

The functional dependences are

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

Ex-2: Let R be a relation with attributes A,B and C. The functional


dependencies of the relation is A B, B C, C A. The above
relation is decomposed into two relations with attributes AB and BC. Then the
functional dependencies on FAB is A B, functional dependencies on FBCis

B C . FABUFBC F

Surrogate key: surrogate key is unique key generated by database management


system. It is a primary key and it can’t be derived from other attributes.

K.V.R
Chebrolu Engineering College
Unit-v

Transaction: A transaction is an execution of user program. It is a series of read


and write operations. A transaction has four properties to maintain data in
concurrent access and system failures. The four properties of a transaction is

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.

Consistency: Each transaction run by itself with no concurrent execution of


other transactions must preserve the consistency of data base. The DBMS
assumes that consistency holds for each transaction.

Isolation: Transactions are isolated or protected from the effects of concurrent


scheduling other transactions.

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.

The transaction log stores:

[1] A record for the beginning of the transaction.


[2] For each transaction component (SQL statement):
[3] The type of operation being performed (update, delete, insert).
[4] The names of the objects affected by the transaction (the name of
the table).
[5] The “before” and “after” values for the fields being updated.
[6] Pointers to the previous and next transaction log entries for the
same transaction.
[7] The ending (COMMIT) of the transaction.

K.V.R
Chebrolu Engineering College
Transaction management using commit ,rollback and save point:

The COMMIT Command


The COMMIT command is the transactional command used to save changes invoked by a transaction to the
database. The COMMIT command saves all the transactions to the database since the last COMMIT or
ROLLBACK command.
Ex:
SQL> delete * from emp where eno=101;
SQL>COMMIT
To Make changes permanent it is necessary to apply commit otherwise the changes will not be permanent
ROLLBACK
The ROLLBACK command is the transactional command used to undo transactions that have not already been
saved to the database. This command can only be used to undo transactions since the last COMMIT or
ROLLBACK command was issued.

SQL>select * from emp where eno=101;


SQL>ROLLBACK
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.

Problems of concurrency control

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

1. Lost update problem


o When two transactions that access the same database items contain their operations in
a way that makes the value of some database item incorrect, then the lost update
problem occurs.
o If two transactions T1 and T2 read a record and then update it, then the effect of
updating of the first record will be overwritten by the second update.

Example:

K.V.R
Chebrolu Engineering College
Here,

o At time t2, transaction-X reads A's value.


o At time t3, Transaction-Y reads A's value.
o At time t4, Transactions-X writes A's value on the basis of the value seen at time t2.
o At time t5, Transactions-Y writes A's value on the basis of the value seen at time t3.
o So at time T5, the update of Transaction-X is lost because Transaction y overwrites it
without looking at its current value.
o Such type of problem is known as Lost Update Problem as update made by one
transaction is lost 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:

o At time t2, transaction-Y writes A's value.


o At time t3, Transaction-X reads A's value.
o At time t4, Transactions-Y rollbacks. So, it changes A's value back to that of prior to
t1.

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.

3. Inconsistent Retrievals Problem


o Inconsistent Retrievals Problem is also known as unrepeatable read. When a
transaction calculates some summary function over a set of data while the other
transactions are updating the data, then the Inconsistent Retrievals Problem occurs.
o A transaction T1 reads a record and then does some other processing during which the
transaction T2 updates the record. Now when the transaction T1 reads the record, then
the new value will be inconsistent with the previous value.

Example:

Suppose two transactions operate on three accounts.

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.

Concurrency Control Protocol

Concurrency control protocols ensure atomicity, isolation, and serializability of concurrent


transactions. The concurrency control protocol can be divided into three categories:

1. Lock based protocol


2. Time-stamp protocol
3. Validation based protocol

Concurrency control protocols:

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.

There are four types of lock protocols available:

1. Simplistic lock protocol

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.

2. Pre-claiming Lock Protocol


o Pre-claiming Lock Protocols evaluate the transaction to list all the data items on
which they need locks.

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.

3. Two-phase locking (2PL)


o The two-phase locking protocol divides the execution phase of the transaction into
three parts.
o In the first part, when the execution of the transaction starts, it seeks permission for
the lock it requires.
o In the second part, the transaction acquires all the locks. The third phase is started as
soon as the transaction releases its first lock.
o In the third phase, the transaction cannot demand any new locks. It only releases the
acquired locks.

There are two phases of 2PL:

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:

1. Upgrading of lock (from S(a) to X (a)) is allowed in growing phase.


2. Downgrading of lock (from X(a) to S(a)) must be done in shrinking phase.

K.V.R
Chebrolu Engineering College
Example:

The following way shows how unlocking and locking work with 2-PL.

Transaction T1:

o Growing phase: from step 0-3


o Shrinking phase: from step 4-7
o Lock point: at 3

Transaction T2:

o Growing phase: from step 1-5


o Shrinking phase: from step 7-8
o Lock point: at 6

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.

Wound wait scheme


o 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 kill the
transaction and release the resource. After the minute delay, the younger transaction is
restarted but with the same timestamp.
o 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.

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.

The main three principle of ARIES algorithm is

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

You might also like