0% found this document useful (0 votes)
13 views

Dbms Notes

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Dbms Notes

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 147

Database Management Systems. Seshadripuram College, Tumkur.

Introduction to DBMS
Chapter 1
Data:
It is a collection or representation of facts, figures and statistics etc which does not have
particular meaning. Data is in the form of numbers, characters, symbols, pictures also.

Symbols
Numbers
Data

Characters Pictures

Information:
It is a processed data or collection of data which is having perfect meaning.
Data [raw facts]-> data Transformation->Information.

Database:
Database is an organized collection of related data for one or more purposes.
The symbol to identify database in DBMS is

Field:
It is a single piece of information across vertical manner.
Record:
Collection of fields is known as one record.
File:
Collection of records is known as one file.

DBMS:
It is a system software or package. It allows access to the data in the database.
It also deletes the data from the database and done any type of the modification.
Department of BCA. 1|Page
Database Management Systems. Seshadripuram College, Tumkur.

Traditional File Processing System


In this approach each application will maintain separate master file. This approach has
duplication of data is the main theme.
Limitation of this file based approach is that the program will be dependent on the files. It
will lead inconsistency.

Merits/Advantages:
1. No need of highly technical person to handle the database.
2. Processing speed is high compared to the DBMS approach.
3. No need of external storage.

Demerits/Disadvantages:

Let us take an Example of Hospital Management System.

File Information
Dname,
Doctor
Experience,
specialization,etc.

Pname, Disease,
Patient
Dname, ward
number etc.

Ward Ward number,How


many patients,
dname etc.

Department of BCA. 2|Page


Database Management Systems. Seshadripuram College, Tumkur.

In the above example 3 components they are doctor, patient and ward files. They will maintain
their own files to store their information.

1. Duplication of data: Redundancy:


It means the information will be repeated one or more times.
In the above figure doctor’s details will be present not only in the doctor’s file but also in
patient and ward files. This mechanism will applied on both patient and wards files. Hence
same data will be repeated again and again.
2. Data dependence:
Due to the redundancy it will leads to the data dependence. It means one file
information is totally depends upon another file information.
In the above figure without knowing doctor’s details we can’t do anything on the patient
and ward files. The same mechanism will be applicable on both patient and ward files.
Hence data will be depends on each other.
3. Data inconsistency:
Due to the redundancy it will leads to the data inconsistency. Means data is not
an ordered.
4. Limited data sharing:
Each application as its own private files. In this users having little opportunity to
share the Data from outside applications.
5. Lack of security:
In this approach Unable to protect data from unauthorized persons. Because this
approach does not have any mechanism to check the identification of the users who are
authorized to that application.
6. Lengthy development time:
It will take more time to develop several numbers of applications. It will take more
time.

Department of BCA. 3|Page


Database Management Systems. Seshadripuram College, Tumkur.

7. Concurrent access anomalies:


In this approach number of users can not access the system at the same time. Means it
does not support multiple users at a time.

These draw backs are overcome by using database approach.

Data base Approach


It is a single storage of data it is maintain for so many users. That storage is known as Repository.

Dname,
Experience,
specialization,etc.

Pname, Disease,
DBMS Did
Dname, ward
number etc. Pid

Wno

Ward number,How
many patients,
dname etc.

As shown in the above figure the DBMS is central system which provides a common interface
between the data and end users.

Department of BCA. 4|Page


Database Management Systems. Seshadripuram College, Tumkur.

Advantages:
1. Minimum data redundancy:
In this approach we will maintain the database for storing different (unique) details of
different users. Hence data present in one file need not be duplicated in other. It will reduce the
repentance of data.

2. Data Consistency:
Reduced data redundancy leads to better data consistency.
3. Data Sharing:
Related data can be shared through dbms. Means doctor share his data with other
doctor through database.
4. Data Independence:
Here doctor details are not depends upon the patient, patient does not depends upon
the ward and so on. Because each component must be know others data.

5. Data Integrity:
In this approach data integrity (security) is more compared to TFS.
6. Maintenance is easy due to centralized database system.

Disadvantages:

1. Cost of hardware and software.


2. Need for external database.
3. Database damage.
4. Highly dependent on DBMS operation.
5. Need technical staff to handle database.

Department of BCA. 5|Page


Database Management Systems. Seshadripuram College, Tumkur.

Difference between TFS and Database approach:


In TFS approach each user defines and implements their own files for a specific application to
run where as database approach maintains a single repository to store multiple users’ data.

Applications of DBMS:
• Storage and reproduction of graphic images, video and audio.
• Building of the virtual company office.
• Building of internet shops and distributed information systems.
• Creation of web sites, allotted to unlimited opportunities.

Characteristics of DBMS:

• Self describing nature of a database system: This is the fundamental characteristic


of database. A DBMS catalog stores the description of a particular database.
• Data abstraction:
The characteristic that allows program-data independence and program-
operation independence is called as data abstraction.
• Support of multiple views of the data:
Each user may see a different view of the database, which describes only
the data interest to that user. View may be a subset of the database or it contains
virtual data.
• Sharing of data and multi-user transaction processing:
It allows a set of concurrent users to retrieve from and to update the
database.
• Insulation between programs and data:
In TFS the structures of data files is embedded in the accessing programs,
so many changes to the structure of a file may require changing all programs that
access this file.

Department of BCA. 6|Page


Database Management Systems. Seshadripuram College, Tumkur.

DBMS Users
Users may be divided into 2 types. They are
• Actors
• Workers

Those who actually use and control the content called Actors.

Those who enable the database to be developed and designed the DBMS software and
implemented called Workers.

The overall aim of the database is to implement the concept of data abstraction and data
independence. For this we need some data base users. They are

1. Application programmers:
This type of users has a complete knowledge about the existing database. These
users can operate on the database.

2. Sophisticated users:
These users interact with the system using query based language. These people
having knowledge about database design.
Ex: scientists, engineers, system Analyst etc.
3. Stand alone End users:
This type of users who are specialized to access personal databases and also small
applications.
4. Naïve end users or Parametric Users:
These type of users cannot have the alteration ideas but they focus to get the
output of existing application to satisfy customer needs. These people frequently access
the data base.

Department of BCA. 7|Page


Database Management Systems. Seshadripuram College, Tumkur.

5. Casual end users:


These type of users can have little bit knowledge about the operations. These
people occasionally access the data base. These users are also bothered about only output
but not the designing part.
Database Administrator [DBA]
The person who is responsible for controlling and coordinating the system is called DBA.
Functions or roles of DBA:
• Evaluate oracle features and related products.
• Installation configuration and upgrading of oracle software.
• Establish and maintain backup and recovery.
• DBA is responsible for modifications such as inserting data, delete and update data.
• DBA also creates a set of definition which are responsible for actual storage structure.
• The DBA responsible for creation of user groups it means various view level schemas.
• Providing data security.

Roles and responsibilities of DBA

1. Selection of Hardware and Software:


• Keep up with current technological trends.
• Predict future changes.
• Establish self products.
2. Managing data Security:
• Establishment of user requirements.
• Firewalls for network security.
• Protection of data against accidental or intentional or miss use.
3. Managing Data Integrity:
• Integrity controls, protect data from unauthorized users.
• Maintains data consistency.
• Maintaining data relationship.

Department of BCA. 8|Page


Database Management Systems. Seshadripuram College, Tumkur.

4. Data Back up:


• DBA must assume what type of data should be backed up more frequently.
• There are several back up facilities done by DBA they are Automatic dump,
periodic back up etc.
5. Database Recovery:
• Re installation of database after crash. Recovery includes back up, check point
these are done by recovery manager.
6. DBA is responsible for improving query processing performance and maintain
documentation standards.
7. He is responsible for version control and technical support.
8. Take care of database design and implementation.
9. Checking memory and CPU usage.

When not to use a DBMS:


• Main cost of using a DBMS, it means high initial investment in hardware, software etc.
• When a DBMS may be unnecessary, if database and applications may not be met because
of DBMS overhead.
• If the database users need special operations not supported by the DBMS.

Department of BCA. 9|Page


Database Management Systems. Seshadripuram College, Tumkur.

Database system concepts and Architecture


CHAPTER 2
Definition:
Data model is a set of data structure and conceptual tools used to describe the structure of
a database.

Classification of Data models


Data Model

High level/ representation/ Low level/


Object based record based physical model

ER model object oriented Relational Network Hierarchical

High level or object based data model:


This model is used to describe the data. In this model we do not bother about the storage of
data.“The main aim of this model is only the relationship between the various objects”.
There are two types
1. E-R Model 2. Object Model

E-R Model: This is entity relationship model.


“It is a graphical or symbolical or pictorial notation for data base”.

Department of BCA. 10 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

The main components of this model are


• Entity.
• Relationship.
• Attribute.

Entity:
It is the real world object/concept like place, person or thing.
Entity can be classified into 2 types. They are
• Strong entity
• Weak entity
Strong entity is an entity which will not depends on the other entity. It will be represented by
Weak entity is an entity which will depends on the other entity. It will be represented by

Representation/ Record Based model


There are 3 types of record based models. They are

1. Hierarchical database model


2. Network database model
3. Relational database model

Relational database model


❖ This model was developed by E. F CODD in 1960’s.
❖ In this model all the data is represented (or) organized in the form of two-dimensional
format that is table.
❖ Table is a combination of unnamed rows and Named columns (or) is a collection of
records.
❖ Rows are also called as tuples or records.
❖ Columns are also called as fields or attributes.

Department of BCA. 11 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Cols (or) fields (or) attributes

SID SNAME COURSE FEE SEM


1 Aaa Bca 1000 2nd
Body of
2 Bbb Bsc 2000 2nd
relation
3 Bbb bca 1000 2nd

Terminology:
Domain:

A set of unique values in a particular column is referred as domain.

Ex: in the above relation SID column is the domain rest of the columns have duplicate values.

Cardinality of Relation:

“Number of rows presented in that particular relation is referred as cardinality of relation”.

Ex: cardinality of above relation is 3.

Degree of Relation:

“Number of columns presented in that particular relation is referred as degree of relation”.

Ex: degree of above relation is 5.

ADVANTAGES:

• Compared to network and hierarchical models creation of relation will be easy.


• In this model changes in the structure do not affect the data access.
• It is a very powerful and flexible model for every user.

DISADVANTAGES:

• In this we hide the implementation complexity and data storage details from the users.
• Using this model we cannot implement reference concept.
• It is poorly designed database system means every user can implement the model.

Department of BCA. 12 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Hierarchical database model


.
Root A

B C D

G H
E F

❖ In this model data will be represent in the form of inverted tree like structure.
❖ This structure allows representing information using parent-child relationship.
❖ Each parent can have many children. But each child have only one parent. Hence, this
relationship is called as one to many relationships

This model is used to describe the data and maintain the logical and view level
At the top of the hierarchy, there is one entity, which is called the root.

ADVANTAGES:

• In this model DBMS provides lot of security.


• There is always link between the parent and child.
• Proper ordering of a tree result is easier and faster.

DISADVANTAGES:

• Although it is a simple to design but complex to implement.


• If you make any changes in the database structure then you must make changes in the
entire application in the DBMS.
• This model suffers from the insert, delete and update anomalies.

Department of BCA. 13 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Network database model


❖ It is a flexible of representing objects and their relationships.
❖ In this data can be representing in the form of relationship through the pointers.
❖ This model allows each record have multiple parent and child records, hence this is
referred as many to many relationship.
❖ It is as same as the hierarchical model but need not necessary to use a downward tree like
structure to represent the data.

In the network model, the entities are organized in a graph, in which some entities can be
accessed through several paths as shown in figure below.

ADVANTAGES:

• Data must be tree like structure because of we are using the pointers.
• Data manipulation can be done easily.
• It is possible to represent many to many relationship

DISADVANTAGES:

• It is very complex to implement the pointer concept.


• It is very difficult to make the structural changes in the database.

Department of BCA. 14 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Data base Schema and Instance

Schema:
Schema is a overall structure or description of a database, which does not have any
content in it.

Sno Sname Course Sem

Instance:
The actual content of database at a particular point at a time is referred as an Instance.

Sno Sname Course Sem


1 Aaa bca 3rd
Instance
2 Bbb bsc 2nd

DBMS Architecture
There are 2 different ways to look at the architecture of DBMS they are

1. Logical Architecture
2. Physical Architecture.

Logical DBMS Architecture (or) 3-Schema Architecture (or) 3 level


Architecture
This architecture describes how the data transfers from database to users through the
DBMS.
The aim of this architecture is separation of user application from the database.
This Architecture divided into 3 levels hence it is referred as 3 schema. Those
levels are
▪ Internal or physical or low level

Department of BCA. 15 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

▪ Conceptual or logical or middle level


▪ View or External or high level

All these levels are shown in the following figure.

Internal level:

▪ It is the lowest level of data abstraction that deals with the physical representation of
the database on the computer and thus, is also known as physical level.
▪ It describes how the data is physically stored and organized on the storage medium.
▪ In this level users can create only empty structure of the database.

Department of BCA. 16 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Conceptual level:

▪ This level of abstraction deals with the logical structure of the entire database and
thus, is also known as logical level.
▪ It describes what data is stored in the database, the relationships among the data and
complete view of the user’s requirements without any concern for the physical
implementation.
▪ That is, it hides the complexity of physical storage structures. The conceptual view is
the overall view of the database and it includes all types of operations like insertion,
deletion and modifications.

External level:

▪ It is the highest level of abstraction that deals with the user’s view of the database and
thus, is also known as view level.
▪ In general, most of the users and application programs do not require the entire data
stored in the database.
▪ It permits users to access data in a way that is customized according to their needs, so
that the same data can be seen by different users in different ways, at the same time.

The process of transforming the requests and results between various levels of DBMS architecture
is known as mapping.

There are 2 ways of mapping used in 3 schema architecture. They are

▪ Internal mapping:

The mapping between internal and conceptual level and it gives the
correspondence between structure and stored data.

▪ External mapping:

Department of BCA. 17 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

The mapping between external and conceptual level and it defines the
correspondence between particular records and fields.

The main advantage of three-schema architecture is that it provides data independence.

Data independence

“It is the ability to change the schema at one level of the database system without having
to change the schema at the other levels. “

Data independence is of two types, namely,

▪ logical data independence


▪ Physical data independence.

Logical data independence:

It is the ability to change the conceptual schema without affecting the external
schemas or application programs.

Physical data independence:

It is the ability to change the internal schema without affecting the conceptual or
external schema. An internal schema may be changed due to several reasons such as for creating
additional access structure, changing the storage structure, etc.

DBMS LANGUAGES
DBMS provides mainly 3 types of languages. They are

• Data definition language (DDL)


These commands are used to define the logical schema. Users can define, alter and
also drop the table by using these commands.
o Create
o Alter
o Drop
o truncate

Department of BCA. 18 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

• Data manipulation language (DML)


These commands are used to insert data into table, delete data from the table and
also update the content in the table.
o Insert
o Delete
o Update
o Select
• Data Control language (DCL)
These commands are used to control the database.
o Grant
o Revoke
o Commit
o rollback

Basic Client/Server Architectures

The client/server architecture was developed to deal with computer environment in which a large
number of PCs, workstation, file server…

 A client is the requesting machine and the server is the supplying machine. Both are
connected through network is referred as client Server Architecture.

There are 2 types of dbms Client-Server Architecture. They are


• Two-tier client server architecture
• Three-tier client server architecture

Department of BCA. 19 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

This is called two-tire architectures because the software components are distributed over two
systems: client and server

o In this establish a connection between client and server that is ODBC (open database
connectivity) once this connection is formed then the client will communicate with the
server.
o Query requests are sent from the client to the server and server processors then it sends
results to the client.

Advantages:
1. Clients are not powerful.
2. Reduces the data traffic on the network.
3. Improve data integrity.

Three-tier architecture
The emergence of the Web changed the roles of client and server, leading to the three-tier
architecture

The intermediate layer or middle layer is sometimes called the application server or Web
server Three-tier Architecture Can Enhance Security:

1. Database server only accessible via middle tier

2. Clients cannot directly access database server

Department of BCA. 20 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

The presentation layer displays information to the user

 The business logic layer handles intermediate rules and constrains before data is passed up
to the user or down to the DBMS

 If the bottom layer is split into two layers (a web server and a database server), then it is a
4-tier architecture (possible to the n-tier)

Advantages:
1. Technological flexibility.
2. Long term cost reduction.
3. Reduced risk.
4. Improve customer services.

DBMS Interfaces

Interfaces are the programs which convert system language to user understandable language;
hence it is as same as the translator.

The user friendly interfaces provided by DBMS are

1. Menu-Based Interfaces:

These interfaces present the user with lists of options, called menus that lead the
user through the formulation of a request. By using these menus users form a request
through query processing.

2. Forms-Based Interfaces :
A forms-based interface displays a form to each user. Users can fill out all of the
form entries to insert new data, or they fill out only certain entries, in which case the
DBMS will retrieve matching data for the remaining entries.

3. Graphical User Interfaces :


A graphical interface (CUI) typically displays a schema to the user in
diagrammatic form. The user can then specify a query by manipulating the diagram.

4. Interfaces for Parametric c Users :


Parametric users, such as bank tellers, often have a small set of operations that
they must perform repeatedly. Usually, a small set of abbreviated commands is included,
with the goal of minimizing the number of keystrokes required for each request.

Department of BCA. 21 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

5. Interfaces for the DBA: Most database systems contain privileged commands that can be
used only by the DBA's staff. These include commands for creating accounts, setting
system parameters, granting account authorization, changing a schema, and reorganizing
the storage structures of a database.

Meta data:
It is data about data, it includes data of the data types and size etc.

Meta data stores in a file that is repository or data dictionary.

Ex: student

Sno Sname course


1 A Bca
2 B Bsc

Meta data of above data is

Sno sid Number


Sname name Char
course scourse char

Classification of DBMS
DBMS can be classified into different categories on the basis of several criteria such as

1. Data models:
In this which type of data model can be used to design or define the data base.
2. Number of users support:
In this how many numbers of users and it supports which type of operating system
like single or multiuser.
3. Number of sites:
Here users are used to store data in a single site or distributed site.
4. Cost:
Whether it is low end system or high end system.
5. Purpose:
Whether it may be used for general purpose or special purpose software system

Department of BCA. 22 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Data modeling using E-R model


CHAPTER 3

Entity:

An entity is an object or thing in the real world with an independent existence.

Ex: place, person, thing etc.

Entity can be classified into two types they are

• Strong Entity: It is an entity which doesn’t depend upon on another entity. It will be
represented by symbol.

• Weak Entity: It is an entity which will depends upon the key entity. It will be represented
by symbol.

Entity set:

It is a collection of related entities of same type.

Entity type:

Is a collection or set of entities that have same attributes.

Relationship:

It is an association between two or more entities is referred as relationship.

It can be represented by symbol.

Department of BCA. 23 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Attribute:

It is a property or characteristic that described an entity or relationship and both.

It is represented by symbol.

Types of attributes: There are different types of attributes

1. Simple Attribute:
It is an Attribute which cannot be divided into sub parts. It consists of single
atomic value.
Ex: Regno, age etc.
2. Composite Attribute:
It is an attribute which can be further divided into subparts. This value not atomic.
Ex: Name, Address etc.
3. Single valued Attribute:
This attribute can have only a single value for particular entity.
Ex: A person can have only one date of birth, Regno etc
4. Multi Valued Attribute:
This Attribute can have multiple values.
It is represented by
Ex: skill, phone number etc.
5. Derived Attribute:
It is an attribute derived from another attribute.

It is represented by “Dotted Oval”

Ex: age, experience etc.


6. Null Attribute:
An entity may not have an applicable value for an attribute.
7. Stored Attribute:
It stores value of the related attribute.

Department of BCA. 24 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

8. Key Attribute:
It is an attribute it cannot be allow duplicate values; it can allow only unique
values.
It is represented by

Notation for E-R diagram


E-R Diagram:

The graphical representation of entities and relationship is known as ER Diagram.

Symbols Meaning

it represents entity

weak entity

Relationship

Identifying weak
relationship

Attribute

key attribute

Multivalued attribute

Department of BCA. 25 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Mapping cardinalities (or) types of relationships between entity sets

Express the number of entities to which another entity can be associated via a relationship set.

On the other hand, it is the cardinality of relationship that defines the number of instances of one
entity as it relates to the number of instances of the other entity.

Based on the different combinations between two entities we can have either

• One-to- One
• One- to- Many
• Many- to- One
• Many -to- Many

One to One Relationship:


One instance of one entity can be associates with one instance of another entity.

Ex: one man can marry with one woman.


This can be represented in the form of both set and E-R notation.

Can WOMAN
MAN
marry

One to Many Relationships:


One instance of one entity can be associates with many instances of another entity.

Ex: one teacher can teach many students at a time.


This can be represented in the form of both set and E-R notation.

Department of BCA. 26 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Can STUDENTS
TEACHER
teaches

Many to One Relationship:


Many instances of one entity can be associates with one instance of another entity.

Ex: many students can learn one course at a time.


This can be represented in the form of both set and E-R notation.

TEACHER Can STUDENTS


teachs

Many to Many Relationships:


Many instances of one entity can be associates many instances of another entity.

Ex: many teachers can teach many students at a time.


This can be represented in the form of both set and E-R notation.

Department of BCA. 27 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Can STUDENTS
TEACHERS
teaches

Degrees of relationship

It is process of counting number of participating entities in an E-R diagram.

There are 3 types of degree. They are

 Binary relationship degree.


 Ternary relationship degree.
 N-Array relationship degree.

Binary relationship degree:

If there are two entities involved in relationship then it is referred to as binary relationship.

This type of degree is most common type of degree used in E-R diagram.

eid ename location id


name

Works DEPARTMENT
EMPLOYEE
in

sal
desg

Department of BCA. 28 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Ternary relationship degree:

If there are three entities involved then it is called as ternary relationship.

BRANCH

location

N-Array relationship degree:


If there are more than 3 entities participated in E- R diagram is called as N-array
relationship degree.

Data Abstraction

The process of abstraction hides the data or information from the users.

It reduces the complexity and increases efficiency.

There are 3 main aspects of abstraction. They are

• Generalization
• Specialization
• Aggregation

Department of BCA. 29 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Example of Generalization.

(a) Two entity types CAR and TRUCK.

(b) Generalizing CAR and TRUCK into VEHICLE.

Department of BCA. 30 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Ex: An attribute-defined specialization on the Job Type attribute of EMPLOYEE.

Department of BCA. 31 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

In the above figure “offer” is a relationship between center and course entities and acts as an
entity for “enquires” relationship. Hence offer is called as associative entity because it acts as both
relationship and entity.

Department of BCA. 32 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

E-R diagram for University database

Department of BCA. 33 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

E-R diagram for bank database

Department of BCA. 34 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

E-R diagram for company database

Department of BCA. 35 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Record Storage and Primary File Organization


CHAPTER 8

Storage Device:
The hardware that writes data to or reads data from a storage medium is called a storage
device. (or)

The collection of data in a database must be physically on some computer storage


medium.

Storage devices are 2 main categories. They are

1. Primary Storage
2. Secondary Storage

Primary Storage:

This category includes storage media that can be operated on directly by the computer
CPU. Primary storage is a volatile and temporary. The advantage is that it provides fast access to
data. The disadvantage is that it is of limited storage capacity.

Secondary Storage:

Data in this storage devices cannot be processed directly by CPU, first copied into primary
storage. It is permanent storage device until changed by user.

Memory Hierarchies
3 key characteristics increase for a memory hierarchy. They are

• Access time
• Storage capacity
• Cost

The memory hierarchy with six levels. At the top there are CPU registers, which can be accessed
at full CPU speed.

Next cache memory which is currently on order of 32KB to a few MB. Cache memory is used to
speed up execution of programs.

Department of BCA. 36 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Next main memory with size currently ranging from 16 MB for entry level systems to tens of giga
bytes.

After the magnetic disks, the current work is permanent storage.

At the secondary storage level, magnetic disks and CD-ROM [Compact Disk Read Only
Memory] devices and magnetic tapes are included. Large permanent databases are in secondary
storage.

Secondary Storage Devices


Secondary storage technology refers to storage devices and storage media that are not always
directly accessible by a computer. This differs from primary storage technology.

Ex: external hard drives, USB flash drives, floppy disk, hard etc.

Department of BCA. 37 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Hard disk:

Internal architecture of hard disk as shown fig below.

Hard disk has high capacity, low cost per bit. Its capacity will be 100mb to gb.

Hard disk contains concentric tracks. Tracks are divided into sectors.

A sector is the smallest addressable unit in a disk.

A block is physical data records separated on the medium from other blocks by inter blocks.

A cylinder is the set of matched tracks at a given radius of a disk space.

Department of BCA. 38 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Floppy Disk:

Floppy disk contains a single flat piece of circular coated with metal oxide and enclosed with in a
cover.

Floppy disk is small and portable.

The 3 common sizes of floppies are 3 ½ inches, 5 ¼ inches, 8 inches in diameter.

The most commonly used floppy disk is 3 ½ inches with storage capacity of 1.44mb

Floppy disk was useful for transferring data between computer for keeping back- up of small
files.

Magnetic Tape:
Magnetic tapes are sequential access devices.

Magnetic disks are thin circular plastic plates on which some magnetic material is coated.

A recording medium consisting of a thin tape with a coating of a fine magnetic material used for
recording analogue or digital data.

Data is stored in frames across the width of the tape. these frames are grouped into blocks or
records which are separated from other blocks.

Magnetic tape is a serial access medium.

However large amounts of information can be stored within magnetic tape.

Department of BCA. 39 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Modern magnetic tape is most commonly performs actual writing or reading of data is a tape
drive.

The access time for a sector has 3 main components. They are

• Seek Time
• Rotational Delay or Latency
• Block Transfer Time

Seek Time:
“The time required to move the arm is called seek time.”

The seek time depends on the previous position of the head and speed that the arm moves across
the surface.

Rotational delay (or) Rotational Latency:

“The time taken to rotate and position the beginning of the desired block under the
read/write head.”

It depends on the rpm (rotation per minute) of the disk.

Block Transfer Time:


“The time requires to transfers the data is called Block Transfer Time.”

Total time needed to locate and transfer an arbitrary block given its address is the sum of seek
time, rotation delay and Block Transfer Time.

Department of BCA. 40 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Buffering of blocks
When several blocks need to be transferred from disk to main memory and all the block addresses
are known, several buffers can be reserved in main memory to speed up the transfer.

While one buffer is being read or write, the CPU can process data in the other buffer.

A A

B C

t1 t2 t3 t4

Interleaved concurrency of A and B Parallel concurrency of C and D

The above mentioned representation

Consider 2 processes A and B that run concurrently (simultaneously) in interleaved fashion.

Consider other 2 processes C and D that run concurrently in a parallel fashion.

Double Buffering
The CPU can start processing a block, once its transfer to main memory is completed, at
the same time the disk I/O processor can be reading and transferring the next block into a
different buffer. This mechanism is known as “double buffering”.

This can be used to speed up the transfer of continuous disk blocks.

Department of BCA. 41 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Placing a File Records on Disk


Record:

Record is a collection of related data values or items.

Record Type:

A collection of field names and their corresponding data types forms a record type or
record format definition.

Data type:

A data type associated with each field specifies the types of values a field can take.

2 types of record types are there. They are

Fixed length record type:

• Each field is of fixed length.


• The numbers of records, the length of each field are available in file header.

Variable length record type:

• Each field is of its own length.


• The numbers of records, the separator symbols used are recorded in the file header.

Department of BCA. 42 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Files
A file is a collection of similar records, where each record is a collection of data values or data
items.

A file descriptor or file header includes information that describes the file, such as the field
names and their data types and the addresses of the blocks on disk.

A file can have 2 types of records. They are

• Fixed length records


• Variable length records.

Fixed length records:

A file where all the records are of the same length is said to be fixed length.

Advantage: Access is fast because computer knows where each record starts.

Disadvantage: Using this the records are usually larger and therefore need more storage space and
are slower to transfer.

Variable length records:

One or more fields of different lengths in each record called variable length records.

Advantage: Records will be smaller and will need less storage space.

Disadvantage: Computer unable to determine where each record starts.

Allocating file blocks on disk


An allocation method refers to how disk blocks are allocated for files.

One main problem in file management is how to allocate space for files so that disk space is
utilized effectively and files can be accessed quickly. Three major methods of allocating disk
space are

• Contiguous allocation method


• Linked allocation method
• Indexed allocation method

Each method has its advantages and disadvantages.

Department of BCA. 43 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Contiguous Allocation:

• The contiguous allocation method requires each file to occupy a set of contiguous address
on the disk.
• Disk addresses define a linear ordering on the disk.
• Each file occupies a set of contiguous blocks on the disk.
• Simple – only starting location (block #) and length (number of blocks) are required.
• Wasteful of space (dynamic storage-allocation problem).
• Files cannot grow.
• When the file is created, the total amount of space it will need must be known and
allocated.

The difficulty with contiguous allocation is finding space for a new file.
If the file to be created is n blocks long, then the OS must search for n free contiguous
blocks.
Another problem with contiguous allocation is determining how much disk space is
needed for a file.

Department of BCA. 44 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Linked Allocation:

In linked allocation, each file is a linked list of disk blocks. . Each block contains a pointer to the
next block and the last block contains a NIL pointer.

These requirements can be avoided by using linked allocation.

• Each file is a linked list of disk blocks:

• Simple – need only starting address.


• Free-space management system –
• no waste of space
• Solves all the problems of contiguous allocation.
• Each file is a linked list of disk blocks, blocks may be scattered anywhere on the disk.
• Each data block contains the block address of the next block in the file.
• No external fragmentation.

This pointer is initialized to nil (the end-of-list pointer value) to signify an empty file.

Department of BCA. 45 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Linked allocation, does have disadvantages, however.

The major problem is that it is inefficient to support direct-access;


It is effective only for sequential-access files.
The effect of which could be picking up a wrong pointer and linking it to a free block or
into another file.

Indexed Allocation:

The indexed allocation method is the solution to the problem of both contiguous and
linked allocation.

It supports direct access by bringing all the pointers together into the index block.

In indexed allocation, each file has its own index block, which is an array of disk sector of
addresses. The ith entry in the index block points to the ith sector of the file.

Indexed allocation supports direct access, without suffering from external fragmentation. Any free
block anywhere on the disk may satisfy a request for more space.

Logical view

Department of BCA. 46 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Record Block:

The records of a file must be allocated to disk blocks because a block is a unit of data
transfer between disk and memory is known as “record block”.

Disk Block:

A block is unit of data in a disk is called as disk block.

Operations on Files
File operations are grouped into 4 types. They are

• General operations
• Retrieval operations
• Update operations
• Combined operations

The OS provides systems calls to create, write, read, reset, and delete files.

General operations:

Open: It prepares the file for read or write. It sets the file pointer to the beginning of the file.

Resetting a file. The directory is searched for the appropriate entry, and the current file position is
reset to the beginning of the file.

Close: This operation will perform closing the file from buffer.

Creating a file: The directory entry records the name of the file and the location in the file
system.

Retrieval Operations:

These types of operations do not change any data in a file but only locate certain records.

Find or locate: This operation searches for the particular (first) record satisfying a search
condition.

Reading a file. : This operation is used to read the content in the file.

Find Next: This operation search for next record in the file that satisfies the search condition.

Department of BCA. 47 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Update Operations:

These are changes the file by insertion or deletion or by modification values.

Writing a file: Using this operation we can able to perform any modifications on that file.

Deleting a file: If we want to release the space in memory for a file then we need to use this
operation.

Insert: this operation is used to add another record in existing file.

Append: this operation is used to add extra information to the existing file.

Combined Operations:

These are also known as set-at-time operations. They are

Find all: This operation locates all the records in the file that satisfies search condition.

Find ordered: It retrieves all the records in the file in some specified order.

Reorganization: It starts the reorganization process.

Parallelizing Disk Access Using RAID Technology


RAID stands for

“Redundant Array of Inexpensive or Independent Disks”.

The Aim of this technology is “to improve performance and reliability”.

Data Striping:

Distributes data transparently over multiple disks to make them appear as a single large,
fast disk. This improves disk performance. Is known as Data Striping.

Disk 0 Disk 1 Disk 2 Disk 3

A1 A2 A3 A4
File A

Department of BCA. 48 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Data Striping. File A is Striped across 4 disks.

On most situations you will be using one of the following eight levels of RAIDs.

• RAID 0

• RAID 1

• RAID 2

• RAID 3

• RAID 4

• RAID 5

• RAID 6

• RAID 10 (also known as RAID 1+0)

In all the diagrams mentioned below:

• A, B, C, D, E and F – represents blocks

• p1, p2, and p3 – represents parity

RAID Levels

RAID Level 0 : [ Blocks Striped with no fault tolerance ]

Department of BCA. 49 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Following are the key points to remember for RAID level0.

• Minimum 2 disks to require.

• Excellent performance (as blocks are striped ).

• No redundancy ( no mirror) and no parity.

• Don’t use this for any critical system.

Advantages

• No parity calculation overhead is involved


• Very simple design
• Easy to implement

Disadvantages

• The failure of just one disk will result in all data in the disk being lost.
• Should never be used in mission critical environments.

RAID Level 1: [Mirroring and Duplexing]

Following are the key points to remember for RAID level 1.

• Requires a minimum of 2 drives to implement


• No striping and No parity.
• Good performance (no striping, no parity).

Department of BCA. 50 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

• Excellent redundancy ( as blocks are mirrored ).

In this data is stored twice by writing them to both the data disk and mirrored disk. If a disk fails,
the controller uses either the data drive or mirror drive for data recovery and continues operation.

Advantages:

• In case a disk fails, data do not have to be rebuilding, they just have to be copied to the
replacement disk.
• RAID 1 is a very simple technology.

Disadvantages:

• The main problem is that the effective storage capacity is only half of the total disk
capacity because all data get written twice.

RAID Level2: [ Hamming code ECC ]

Following are the key points to remember for RAID level 2.

• It can do Error detection and Correction.


• In this use parity bits and Hamming codes used to identify and correct errors.
• Parity bits are used to verify data integrity and signify oddness of data bits.
• For all these we use ECC [Error Correction Code].

Department of BCA. 51 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

• This uses bit level striping. i.e., Instead of striping the blocks across the disks, it stripes the
bits across the disks.

• In the above diagram b1, b2, b3 are bits. E1, E2, E3 are error correction codes.

• You need two groups of disks. One group of disks is used to write the data, another group
is used to write the error correction codes.

• This is not used anymore. This is expensive and implementing it in a RAID controller is
complex, and ECC is redundant now-a-days, as the hard disk themselves can do this.

RAID level 3: [parallel transfer with parity]

This uses byte level striping. i.e Instead of striping the blocks across the disks, it stripes the bits
across the disks.

Following are the key points to remember for RAID level 3.

• RAID Level 3 requires a minimum of 3 drives to implement

• In the above diagram B1, B2, B3 are bytes. p1, p2, p3 are parities.

• Uses multiple data disks, and a dedicated disk to store parity.

• Sequential read and write will have good performance.

• Random read and write will have worst performance.


Department of BCA. 52 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

• This is not commonly used.

Advantages:

• RAID 3 provides high performance for large data transfers.


• Disk failures do not significantly slow down.

Disadvantages:

• This technology is fairly complex controller design.


• Transaction rate slowed by parity disk.

RAID level 4:[ Independent data disks with shared parity disk ]

Following are the key points to remember for RAID level 4.

• RAID Level 4 requires a minimum of 3 drives to implement


• This uses block level striping.
• Minimum of 3 disks (2 disks for data and 1 for parity)
• It is somewhat similar to RAID 3 and 5, but little different.
• This is just like RAID 3 in having the dedicated parity disk, but this stripes blocks.

Department of BCA. 53 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

• This is just like RAID 5 in striping the blocks across the data disks, but this has only one
parity disk.
• This is not commonly used.

Advantages:

• Very high read rates.


• Multiple files read at a time.
• This technology uses in web servers etc.

Disadvantages:

• Very slow write rates.


• In efficient data recovery.

RAID level 5: [Independent data disks with distributed parity blocks]


RAID 5 is the most common secure level.

Following are the key points to remember for RAID level 5.

• RAID Level 5 requires a minimum of 3 drives to implement

• Good performance ( as blocks are striped ).

Department of BCA. 54 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

• Good redundancy ( distributed parity ).

• Best cost effective option providing both performance and redundancy. Use this for DB
that is heavily read oriented. Write operations will be slow.

Advantages:

• Read data structures are very fast while write data transaction are somewhat slower.

Disadvantages:

• Most complex controller design.


• Medium write data transaction rate.

RAID level 6: [Independent data disks with dual parity schemes]


Two independent parity computations must be used in order to provide protection against double disk
failure.

Following are the key points to remember for RAID level 6.

• RAID Level 6 requires a minimum of 4 drives to implement

• Just like RAID 5, this does block level striping. However, it uses dual parity.

• In the above diagram A, B, C are blocks. p1, p2, p3 are parities.


Department of BCA. 55 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

• This creates two parity blocks for each data block.

• Can handle two disk failure.

• This RAID configuration is complex to implement in a RAID controller, as it has to


calculate two parity data for each data block.

RAID level 10: [Very high reliability combined with high performance]
This level has the combination of 0th level and 1st level of RAID. That is Data Striping +
Data Mirroring

Following are the key points to remember for RAID level 10.

• RAID Level 10 requires a minimum of 4 drives to implement

• This is also called as “stripe of mirrors”.

• Excellent redundancy ( as blocks are mirrored )

• Excellent performance ( as blocks are striped )

• If you can afford the dollar, this is the BEST option for any mission critical applications
(especially databases).

Department of BCA. 56 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

File Organization

Definition:
“The physical arrangement of data in a file into records and pages on the disk. It is
known as File Organization.”

There are 3 types of File Organization. They are

1. Unordered or Heap files


2. Ordered or Sequential files
3. Hash files

Each and every file organization technique has 3 types of operations. They are

1. Inserting a new record.


2. Searching for a record.
3. Delete a record.

Files of Unordered Records or Heap Files


Records are placed in this organization was unordered. Those types of files are known as “Heap
files”. These are also known as “pile files”.

Here 3 operations are performed on the files.

Inserting:

• Here insertion will be efficient. Because here insertion will takes place at the end of the
file.
• Takes the last block into a buffer.
• Adds the new record to it.
• Rewrites the block back to the disk.

Searching:

• Searching a record is not efficient. In this we are using “linear search” technique.
• So it needs to search record by record it needs more time to search that record.
• Slow because of linear search.
Department of BCA. 57 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Deleting:

• Find the corresponding block.


• Copy the block into a buffer.
• Delete the record from the buffer.
• Slow because the record to be searched first.

Files of ordered Records or Sequential Files


Records are placed in this organization was ordered. Those types of files are known as
“Sequential files”.

Here 3 operations are performed on the files.

Inserting:

• Here insertion will be in efficient. Because here insertion will takes place at the first or
middle position of the file.
• Adds the new record to it.
• Slow.

Searching:

• Searching a record is efficient. In this we are using “binary search” technique.


• In this search begins from the middle record in file.
• Update the ordering field.
• Must be content be ordered.
• Fast because of binary search.

Deleting:

• Deletion can be done efficiently.


• Find the record.
• Delete it and mark the place.
• Periodically reorganize the file.

Department of BCA. 58 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Hash Files or Hashing Techniques

Hashing technique
Hashing techniques is a very efficient method in the searching the exact item in very short time.

Hashing is the transformation of a string of characters into a usually shorter fixed length values.

Or

Hashing is the process in which we place the each data item at the index of the memory location.

Hash Organization

• Bucket: Hash file stores data in bucket format. Bucket is considered a unit of storage.
Bucket typically stores one complete disk block, which in turn can store one or more
records.
• Hash Function: A hash function h, is a mapping function that maps all set of search-keys
K to the address where actual records are placed. It is a function from search keys to
bucket addresses.

For example we are creating as index on the table, we cannot view that index. But using this
hashing functions are display that index values.

The primary file organization based on hashing techniques provides very fast access to records.
The file organization based on hashing is called hash or direct file.

The idea behind hashing is to provide a function called a hashing function or randomizing
function.

Advantages of hashing:

1> Hash tables over other data structures table is speed when the number of entries is large.
2> Hash tables are particularly effective when maximum number of entries can be allowed.

Disadvantages of hashing:

1> Hash tables can be more difficult to implement than self balancing trees.

2> Hash tables are not effective when the number of entries are very small.

3> Hash tables are less efficient for string processing application like spell checking etc.

There are 2 types of Hashing techniques

Department of BCA. 59 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

• Internal hashing
• External hashing

Internal hashing:

Hashing for internal files is known as internal hashing.

Internal hashing is typically implemented through the use of an array of records. Using this
hash field value such as names can be transformed into an integer (ASCII). Suppose that the array
index range from 0 to m-1, then we have m-1 slots whose address corresponds to array indexes.

Chaining: In this method a pointer field is added to each record location. A collection is
resolved by placing the new record in an unused overflow location and setting the pointer of
the occupied hash address location to the address of that overflow location.

External hashing:

Hashing for a disk file is called external hashing . to suit the characteristics of disk
storage, the target address space is made up of buckets, each of which holds multiple records.

A bucket is either one disk block or continuous blocks.

In this 2 types of techniques again they are

Static hashing:

In this fixed amount of space allocated to the file.

Department of BCA. 60 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Suppose if we allocate M buckets for the address space and N be the maximum number of
records that can fit in one bucket, then at most M*n records will fit in the allocated space.

Bucket Overflow

 The condition of bucket-overflow is known as collision. This is a fatal state for any static
hash function. In this case, overflow chaining can be used.

If number of records increases to more than m*n, then overflow error will be occurred.

In this we are difficult to expand one file size at run time.

Dynamic hashing:

• The problem with static hashing is that it does not expand or shrink dynamically as the
size of the database grows or shrinks.
• Dynamic hashing provides a mechanism in which data buckets are added and removed
dynamically and on-demand.
• Dynamic hashing is also known as extended hashing.
• Hash function, in dynamic hashing, is made to produce a large number of values and only
a few are used initially.

Department of BCA. 61 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

• In this hashing file size will be expand or shrink dynamically. The number of buckets are
not fixed and file size was also not fixed.
• If no of buckets are increased then the file size will be expanded. Suppose no of buckets
are less than the file size will be shrink.
• Once the bucket is full, and a new record is inserted. The buckets overflow and are split
up into two buckets. The records are distributed into the two buckets.

Disk:

Disk s is used for storing large amounts of data. The most basic unit of data on the disk is a single
bit of information.

Disk Packs:

To increase storage capacity, disks are assembled into a disk pack, which may include many disks
and hence many surfaces.

Disk pack is the core component of a hard disk drive.

Department of BCA. 62 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Transaction Processing Systems and Concurrency Control Techniques


CHAPTER 9
Transaction:
A transaction can be defined as a group of tasks.

Ex:

Begin transaction End transaction

Execution of transaction

Database may be temporarily in an

In consistent state during execution

Ex:

Let’s take an example of a simple transaction.

Suppose a bank employee transfers Rs 500 from A's account to B's account.

This very simple and small transaction involves several low-level tasks.

A’s Account

Open_Account(A)

Old_Balance = A.balance

New_Balance = Old_Balance - 500

A.balance = New_Balance

Close_Account(A)

B’s Account

Open_Account(B)

Old_Balance = B.balance

New_Balance = Old_Balance + 500


Department of BCA. 63 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

B.balance = New_Balance

Close_Account(B)

Transaction Properties: (or) Rules


DBMS has to maintain the following properties of transactions (ACID Properties)

ACID

A- Atomicity

C-Consistency

I - Isolation

D- Durability

A:Atomicity
States that every Transaction should be atomic in nature. Thus by Atomic Transaction it
means “all or none”. Either all SQL statements/steps execute successfully in a transaction, or fail
as a single unit and none of them should be treated as executed.

For example:

If account-A & account-B both having $2000 balance, you have to transfer $1000 from
account-A to account-B, this will involves 2 steps. First withdrawal from account-A, and Second
deposit in account-B. Thus, both the steps should be treated as single or atomic unit and at the end
account-A should have $1000 & account-B should have $3000 balance. If in case after First step
the system fails or any error occurs then first step should also be rolled-back and $1000
withdrawn from account-A should be re-deposited to it, maintaining $2000 back in both the
accounts.

Department of BCA. 64 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

C: Consistency
States that any Transaction happened in a database will take it from one consistent state to
another consistent state. The data finally recorded in the database must be valid according to the
defined Rules, Constraints, Cascades, Triggers, etc. If in case of any failure to these rules the
changes made by any transaction should be rolled-back, this will put the system in earlier
consistent state.

For example: If the money deposit process has any Trigger built on top of it. And at the time of
money transfer any of the Trigger fails or any database node, the system should automatically
Rollback the complete transaction and switches back the system to its previous consistent state
before the transaction was started. Or if everything executes successfully then the system is
committed to a new consistent state.

I: Isolation
Means Transactions performing same functions should run in Isolation and not in parallel
to provide more concurrency to the data and avoiding dirty reads & writes. One needs to use
proper Transaction Isolation levels and locking in order to prevent this.

For example:

If two people accessing a joint-account with $5000 balance from 2 terminals to withdraw
money. Let’s say at same time John & Marry apply to withdraw $4000 from two different ATMs.
If both the Transactions do not run in Isolation and run in parallel then both John & Marry will be
able to withdraw $4000 each i.e. $8000 total from their account. To make sure this won’t happen
Transactions should be not allowed to run in parallel, by setting Transaction Isolations and/or
locking methods on the database objects.

D: Durability
A transaction should be durable by storing the data permanently and making it available in
case of power failure, recovery from system failure, crash, any error, etc.

Department of BCA. 65 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Transaction States
A transaction is any one execution of a user program in a DBMS, a transaction moves through
various types of execution states. Because a failure occurs, transactions are broken up into several
states. They are

1. Active state
2. Partially Committed State
3. Failed State
4. Aborted State
5. Committed State
6. Terminated State

These are represented a shown below.

Active –

In this state, the transaction is being executed. This is the initial state of every transaction.

Partially Committed –

When a transaction executes its final operation, it is said to be in a partially committed


state.

Department of BCA. 66 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Failed –

A transaction is said to be in a failed state if any of the checks made by the database
recovery system fails. A failed transaction can no longer proceed further.

Aborted –

If any of the checks fails and the transaction has reached a failed state, then the recovery
manager rolls back all its write operations on the database to bring the database back to its
original state where it was prior to the execution of the transaction. Transactions in this state are
called aborted. The database recovery module can select one of the two operations after a
transaction aborts −

Re-start the transaction

Kill the transaction

Committed –

If a transaction executes all its operations successfully, it is said to be committed. All its
effects are now permanently established on the database system.

Single User and Multi User Systems

Single user:
A DBMS is single-user means one user can use the system at a time.

computer Presentation application DBMS

Services services

Presentation services display forms, handles flow of information to/ from screen.

Application services implements user request, interacts with DBMS.

Department of BCA. 67 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Multi- user:
A DBMS is multi-user means many users can use the system at a time or concurrently.

• Dumb terminals connected to main frame means application and presentation services on
main frame.
• ACID properties required

It means isolation: In DBMS sees an interleaved schedule.

Atomicity and durability implemented by DBMS, Provides ACID properties.

computer Presentation application

Services services

DBMS

computer Presentation application

Services services

Dumb terminals user module

Why recovery is needed


• Transaction submitted for execution.
• DBMS is responsible for making sure that either
❖ All operations in transaction are completed successfully and the changes is recorded
permanently in the database.
❖ The DBMS must not permit some operations of a transaction T to be applied to the Data
base while others of T are not.
❖ It will cause Inconsistency.

Department of BCA. 68 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Why Concurrency Control is needed


Several problems can occur when concurrent transactions execute in an uncontrolled manner.

• The Lost Update Problem.


• The temporary Update or Dirty Read Problem
• The incorrect Summery Problem

The Lost Update Problem: This occurs when 2 transactions that access the same database
items have their operations interleaved in a way that makes the value of some database item
incorrect.

Ex:

T1 T2

Read_item (X);

X:=X-N;

Read_item(X);

X:=X+M;

Write_item (X);

Read_item (Y); item X has incorrect value

Y:=Y+N;

Write_item(X);

Write_item(Y);

In this, transaction X has an incorrect value; because it is updated by T1 is lost (overwritten).

The Temporary Update or Dirty Read problem:


This occurs when one transaction updates a database item and then the transaction fails for
some reason. The updates item is accessed by another transaction before it is changed back to its
original value. Ex:

T1 T2

Department of BCA. 69 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Read_item (X);

X:=X-N;

Write_item (X);

Read_item(X);

X:=X+M;

Write_item(X);

Read_item (Y);

In this transaction T1 fails and must change the value of X back to its old value.

Transaction T2 has read the temporary incorrect value of X. the value of item X is read by T2 is
called “Dirty data”, because it has been created by a transaction that has not completed and
committed yet.

The Incorrect Summary Problem:


If one transaction is calculating an aggregate summary function on a number of records
while other transaction are updating some of these records, the aggregate function may calculate
some values before they are updated and others after they are updated.

Ex:

T1 T3

Sum:=0;

Read_item(A);

Sum:=sum+A;

Read_item (X);

X:=X-N; T3 reads X after N is subtracted and


reads Y before N is added

Write_item (X);

Read_item(X);

Department of BCA. 70 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Sum:=sum+x;

Read_item(Y);

Sum:=sum+Y;

Read_item (Y);

Y:=Y+N;

Write_item(Y);

A transaction reads items twice with 2 different values because it was changed by another
transaction between 2 reads.

Database Failures
Database failures are generally classified into 3 types. They are

• Transaction failure
• System failure
• Media failure

System Log:
Log or Journal keeps track of all transaction operations that affect the values of database
items.

Serializability:
Serializability of a schedule means equivalence to a serial schedule with the same
transactions.

Concurrency Control Techniques


Concurrency:
It refers to when multiple programs executing or running at the same time. It is also
referred as multiprogramming or multitasking.

Department of BCA. 71 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Time stamp:
Time stamp is a unique identifier created by the DBMS to identify the transaction. The
time stamp values are assigned in the order in which the transactions are submitted to the system.

Time stamp has 2 values. They are


1. Read_TS(X)
2. Write_TS(X)

Lock:
A lock is a variable associated with a data item that describes the status of the item with
respect to possible operations.

Types of Locks:
• Binary Lock
• Shared Lock or Exclusive Lock

Binary Lock:

A binary Lock can have 2 states or values those are locked and unlocked (1 and 0).

In binary locking system, almost one transaction can hold a lock on a given item.

Suppose we should allow several transactions to access the same item X, if they all access X for
reading purposes only. However, if a transaction is to write an item X, it must have exclusive
access to X. it cannot be possible by binary lock instead of this we are using Exclusive lock.

Shared or exclusive lock:

In this we are using 3 locking operations they are

• Read _lock(X)
• Write lock(X)
• Unlock(X)

In the above a read locked item is known as shared locked because outer transactions are allowed
to read the item.

Write locked item is called exclusive locked because a single transaction exclusively holds the
lock on the item.

Department of BCA. 72 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Two Phase Locking (2PL)


This locking protocol divides the execution phase of a transaction into three parts.

❖ In the first part, when the transaction starts executing, it seeks permission for the locks it
requires.
❖ The second part is where the transaction acquires all the locks. As soon as the transaction
releases its first lock,
❖ The third phase starts. In this phase, the transaction cannot demand any new locks; it only
releases the acquired locks.

Two-phase locking has two phases, one is growing, where all the locks are being acquired by the
transaction; and the second phase is shrinking, where the locks held by the transaction are being
released.

Strict Two-Phase Locking


The first phase of Strict-2PL is same as 2PL. After acquiring all the locks in the first phase, the
transaction continues to execute normally. But in contrast to 2PL, Strict-2PL does not release a
lock after using it. Strict-2PL holds all the locks until the commit point and releases all the locks
at a time.

Department of BCA. 73 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Relational Database Language


CHAPTER 4

Introduction: SQL
SQL stands Structural Query Language the name is derived from the word of
SEQUEL (sequential English query language). This was designed and implemented at IBM
research center. It is a standard relational database language which has several parts.

RDBMS:

It is a DBMS in which data is stored in the form of tables and relationship among the
data was also stored in the form of tables.

Query:
Retrieving or extracting required information from the database by using DML commands
is known as query.

Department of BCA. 74 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Differences between DBMS and RDBMS

DBMS RDBMS
In DBMS no relationship concept. It is used to establish the relationship.
It supports single user only. It supports multiple users.
It treats data as files internally. It treats data as tables internally.
It supports 3 rules E.F.CODD. It supports minimum 6 rules out of 12.
It requires low software and hardware
requirements It requires high software and hardware requirements.
Normalization process will not be used. Normalization process will be present.
It does not support any constraints. It supports all types constraints.

Basic terminologies used in RDBMS


Tuple or Row:

A tuple is a collection of attribute values. (or) A single row in the table is called as tuple.

It is also known as record.

Cardinality of relation:

The total number of rows in the table is called as cardinality of that relation.

Degree of relation:

The total number of columns in the table is called as degree of that relation.

Attribute or Column:

A column is a single piece of information on vertical side of table. It is also known as


field.

Cell:

Intersection of rows and columns is referred as cell.

Relation:

Relation is nothing but a table. It is in 2 dimensional formats.

Combination of unnamed rows and named columns is known as table or relation.

Department of BCA. 75 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

SQL Data Types

When we creating tables, we must specify a data type for each column.

The data types in SQL can be classified into 5 major categories. They are

❖ Character ( char, varchar2, text)


❖ Numeric (number)
❖ Binary (raw, long raw, BLOB etc.)
❖ Date and time (date, timestamp etc.)
❖ Row id (rowid etc.)

Most frequently used data types in SQL are

• Text (or) char (or) varchar2


• number
• Date/Time types.

CHAR (size):

Holds a fixed length string (can contain letters and special characters).
The fixed size is specified in parenthesis. Can store up to 255 characters. If we give more
letters then it will display error message or it will take only fixed size letters.

VARCHAR2 (size):

Holds a variable length string (can contain letters, numbers, and special characters). The
maximum size is specified in parenthesis. Can store up to 255 characters.

Note: If you put a greater value than 255 it will be converted to a TEXT type.

TEXT:

Holds a string with a maximum length of 65,535 characters. It does not have size.

BLOB:

For BLOBs (Binary Large Objects). The maximum size of this data type is 4 giga bytes of
data.

Department of BCA. 76 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

CLOB:

For CLOBs (character Large Objects). The maximum size of this data type is 4 giga bytes
of data.

Numeric (p, s):

This data type is used to store numeric data.


The p parameter indicates the maximum total number of digits that can be stored left side
of decimal point. The s parameter indicates the maximum number of digits stored to the right of
the decimal point.
Integer:

The INTEGER data type accepts a 64-bit signed integer value with an implied scale of
zero. It does not have size.

Date:

The DATE data type accepts date values, consisting of year, month, and day. No
parameters are required when declaring a DATE data type. Date values should be specified in the
form: DD-MMM-YYYY.

Types of SQL Statements

All most all relational database management systems use SQL for data manipulation and retrieval.

SQL is a non-procedural language, here we need to concentrate on what you want, not how to get.
You need not to concentrates on procedural concepts.

SQL Statements are divided into 4 categories. They are

❖ DDL (Data Definition Language)


❖ DML (Data Manipulation Language)
❖ DCL (Data Control Language)
❖ TCL (Transaction Control Language)
Department of BCA. 77 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Data Definition Language (DDL)


This part of SQL provides commands for defining relational schemas like tables, views, index etc.
It also provides commands for deleting and modifying the relational scheme.

DDL Commands are

❖ Create
❖ Alter
❖ Drop
❖ Truncate

• Create Command:
It is used to create the definition of a new relation.

Syntax: Create Table Table Name

Column name1 DataType1 (Size),

Column name2 DataType2 (Size),

Column name n Data Type n (Size)

);

• Drop Command:
This command is used to delete unwanted tables from the database.

Syntax: Drop Table Table Name;

• Alter Command:
The alter command can be used on a table to modify the table.

Syntax: Alter Table Table Name Add/modify/drop/rename( NewColumnName


DataType(Size));

Department of BCA. 78 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

It has several options.

o Add option:
Using this we can add column or primary key to the existing table.

Syntax: alter table table name add (new colname datatype(size));

Syntax: alter table table name add primary key (colname);

o Drop option:
Using this we can drop a particular column from the table.

Syntax: alter table table name drop column colname;

o Modify option:
Using this option we can modify data types of particular column and also we can
do increase or decrease sizes of data type.

Syntax: alter table table name modify(colname data type(size));

o Rename option:
Using this we can rename the table name as well as column name in a table.

If we want to rename the table then we need to follow the syntax.

Syntax:

Alter table existing table name rename to new table name;

If we want to rename the column then we need to follow the syntax.

Syntax:

Alter table table name rename column existing colname to new colname;

• Truncate Command:
This command is used to deletes the data in the table. But not the structure.

Syntax: truncate table table name;

Department of BCA. 79 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Data Manipulation Language (DML)


This part of SQL provides commands for manipulating adding and deleting data in various tables
of the databases.

Some of the DML commands are

❖ Insert
❖ Delete
❖ Update
❖ Select

• Insert Command:
It is used to add one or more tuples into an existing table.
Using this we can insert one tuple at a time.

Syntax1:

Insert into table Name (colName1, ColName 2, ….) values (value1, value2);

(or)

Insert into table name values ( value1, ‘value2’, ……. Value n);

Using this we can insert multiple tuples at a time.

Syntax 2:

Insert into table name values (&colname1,’&colname2’, …. &colname n);

Those colnames are having varchar2 or char or text or date data types must be enclosed
within the single quotes.

• Delete Command:
It is used to remove the tuples or records which are existing in a relation relation,
deletes entire table temporarily and also delete one particular record from table.

Syntax1:

delete table name ;

Department of BCA. 80 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Using this we can delete entire tuples but not the structure.

Syntax 2:

delete table name where condition;

Using this delete particular tuple if it is matched that condition.

• Select Command:
This command is used extract information from a particular table of a database. This
command performs a query on the table and extracts data from it. Query is a request for
data.

Syntax:

Select ColName1, ColName2, from Table name 1, TableName2…

Where <Condition>;

• Update Command:
This command is used update or modifies the contents of a table or relation.

Syntax:

Update table name set colname=value1, colname=value2… from table name

where condition ;

Transaction Control Language (TCL)


This part of SQL provides commands for controlling the transactions. Some TCL commands are

• Commit:
This command is used to save the changes made by the transactions on

The databases from the last commit point.

Syntax: Commit;

• Rollback:

Department of BCA. 81 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

This command is used to undo the changes made by the transactions on the
database before it is committed.

Syntax: Rollback;

Data Control Language (DCL)


This part of SQL provides commands for the DBA to either give or take away rights from various
users in controlling the data. Some DCL Commands are

• Grant :
Used to give privileges (authority) to specified users. This command is used only
by the DBA.

• Revoke :
This command is used to take away some or all privileges from the specified user.

SQL Constraints

SQL constraints are used to specify additional rules applied on the data in a table.

Constraints can be specified when the table is created (inside the CREATE TABLE statement) or
after the table is created (inside the ALTER TABLE statement).

General syntax to give constraint to the table:

CREATE TABLE table_name


(
column_name1 data_type(size) constraint_name,
column_name2 data_type(size) constraint_name,
column_name3 data_type(size) constraint_name,
....
);

SQL NOT NULL Constraint

• By default each and every column can hold NULL value in a table.

Department of BCA. 82 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

• If we do not want to allow NULL value to a column then we need to add this constraint to
the column.
• A table can have number of not null constraints.
• The NOT NULL constraint enforces a column to NOT accept NULL values.
• The following SQL enforces the "PId" column and the "LastName" column to not accept
NULL values:
• It will accepts duplicate but not accept NULL values.

Syntax:
Create table tablename
(
Colname1 datatype(size) not null,
Colname2 datatype(size),
:
Colname n datatype n
);

Example:

CREATE TABLE Persons


(
PId number(10) NOT NULL,
LastName varchar2(25),
FirstName varchar2(25),
Address varchar2(25),
City varchar2(25)
);

After creating a table then we need to check whether not null constraint will applied or not on this
table then we need to insert values into this table.

SQL UNIQUE Constraint

• The UNIQUE constraint uniquely identifies each record in a database table.

Department of BCA. 83 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

• The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness
for a column or set of columns.
• Note that you can have many UNIQUE constraints per table, but only one PRIMARY
KEY constraint per table.
• This will be applicable on both numeric and non numeric data.

Syntax:

Create table tablename


(
Colname1 datatype(size) unique,
Colname2 datatype(size) unique,
:
Colname n datatype n
);

Example:
CREATE TABLE Persons
(
PId number(10) unique,
LastName varchar2(25) unique,
FirstName varchar2(25),
Address varchar2(25),
City varchar2(25) unique
);

SQL UNIQUE Constraint on ALTER TABLE

ALTER TABLE table name ADD UNIQUE (colname);

After creating a table then we need to check whether unique constraint will applied or not on this
table then we need to insert values into this table.

Department of BCA. 84 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

SQL PRIMARY KEY Constraint

• The PRIMARY KEY constraint uniquely identifies each record in a database table.
• Primary keys must contain unique values.
• A primary key column cannot contain NULL values.
• Each table should have a primary key, and each table can have only ONE primary key.

SQL PRIMARY KEY Constraint on CREATE TABLE

Syntax:
Create table tablename
(
Colname1 datatype(size) primary key,
Colname2 datatype(size),
:
Colname n datatype n
);

The following SQL creates a PRIMARY KEY on the "PId" column when the "Persons" table is
created:

CREATE TABLE Persons


(
PId number(10) primary key,
LastName varchar2(25),
FirstName varchar2(25),
Address varchar2(25),
City varchar2(25)
);

SQL PRIMARY KEY Constraint on ALTER TABLE

ALTER TABLE tablename ADD PRIMARY KEY (colname);

After creating a table then we need to check whether primary key constraint will be applied or not
on this table then we need to insert values into this table.

Department of BCA. 85 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

SQL FOREIGN KEY Constraint

• A FOREIGN KEY in one which points to a PRIMARY KEY on another table.


• It allows values from primary key table and also maintains relationship between the parent
and the child tables.

Syntax:
Create table tablename
(
Colname1 datatype(size) ,
Colname2 datatype(size),
:
Colname n datatype (size) references parent tablename;
);

Example:
CREATE TABLE Persons
( PId number(10) references persons,

Result varchar2(25) );

SQL FOREIGN KEY Constraint on ALTER TABLE

ALTER TABLE TABLENAME ADD FOREIGN KEY (COLNAME) REFERENCES


PARENT TABLENAME (COLNAME);
After creating a table then we need to check whether foreign key constraint will be applied or not
on this table then we need to insert values into this table.

SQL CHECK Constraint

• The CHECK constraint is ensures that all values in a column satisfy certain condition.
• Based on that condition only we can insert values into the table.

Department of BCA. 86 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

• If you define a CHECK constraint on a single column it allows only certain values for this
column.
• It will be applicable only on numeric data.

Syntax:
Create table tablename
(
Colname1 datatype(size) ,
Colname2 datatype(size),
:
Colname n datatype (size) check (condition);
);

Example:

CREATE TABLE Persons


(
PId number(10) check (pid>2),
LastName varchar2(25),
FirstName varchar2(25),
Address varchar2(25),
City varchar2(25)
);

After creating a table then we need to check whether check constraint will be applied or not on
this table then we need to insert values into this table.

SQL DEFAULT Constraint

• The DEFAULT constraint provides a default value to a column when the insert into
statement does not provide a specific value.
• The default value will be added to all new records, if no other value is specified.

SQL DEFAULT Constraint on CREATE TABLE

Syntax:

Department of BCA. 87 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Create table tablename


(
Colname1 datatype(size) ,
Colname2 datatype(size),
:
Colname n datatype (size) default value;
);
Example:

CREATE TABLE student


(
sid number(10),
Name varchar2(25),
marks number(10) default 80
);

When we insert values into the table

Insert into student (sid, name) values (1,’ aaa’);

Then we will display by using select statement.

SQL DUAL TABLE

The dual table is a temporary table which consists only one row and one column (dummy). The
value of row is X and column name is D. the dual table can be accessed by every user.

When we check the content of the dual table is

Select * from dual;

SQL Functions

Functions are very powerful feature of SQL. These are 2 types. They are

FUNCTIONS

Department of BCA. 88 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

SINGLE ROW FUNCTIONS MULTI ROW FUNCTIONS

Single row functions:


These are accepting one row and it returns one result.

Again it has many types. They are

• Character functions
• Number functions
• Date functions
• Conversion functions etc.

Character functions

These functions accept character data as input and can return both character and numeric values.
These functions can be divided into 2 categories. They are

o Case manipulation functions (LOWER, UPPER, INITCAP)


o Character manipulation functions (CONCAT, SUBSTR, LENGTH, INSTR, LPAD, TRIM
& REPLACE )

Case manipulation functions

LOWER ( ) FUNCTION:

It converts upper case alphabet letters to lower case letters.

Syntax: select lower ( ‘ VIDYA VAHINI ‘) from dual;

UPPER ( ) FUNCTION:

Department of BCA. 89 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

It converts lower case alphabet letters to upper case letters.

Syntax: select upper (‘vidya vahini ‘) from dual;

INITCAP ( ) FUNCTION:

It converts character values to upper case for the first letter of each word rest of the letters
are in lowercase.

Syntax: select initcap (‘vidya vahini ‘) from dual;

Character manipulation functions


CONCAT ( ) FUNCTION:

This function is used to combine two words or strings.

Syntax: select concat(‘ vidya’,’ vahini’) from dual;

SUBSTR ( ) FUNCTION:

This function is used to display characters from specified positions.

Syntax: select substr ( vidya vahini’, 1, 3) from dual;

LENGTH ( ) FUNCTION:

This function is used to display number of characters in a given string.

Syntax: select length(‘ vidya vahini’) from dual;

INSTR ( ) FUNCTION:

This function is used to display length of the specified string.

Syntax: select instr(‘ vidya vahini’, ‘vidya’) from dual;

LPAD ( ) FUNCTION:

This function is used to display characters from left justification.

Syntax: select lpad (‘ vidya vahini’, 4) from dual;

TRIM ( ) FUNCTION:

This function is used to reduce the space in a given string.

Department of BCA. 90 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Syntax: select trim (‘ vidya vahini ‘) from dual;

REPLACE ( ) FUNCTION:

This function is used to replace a new word in the place of existing word in a given string.

Syntax: select replace (‘skyward publishers’,’ward’,’world’) from dual;

Number Functions
ROUND ( ) FUNCTION:

It will display the output based on decimal point. If decimal point is zero then it will
display only integer part of the number, if it is positive then it will display corresponding decimal
digit value and if it is negative then it will display only the integer part.

Syntax: select round(100.123,0), round(100.123, 2), round(100.123, -1) from dual;

TRUNC ( ) FUNCTION:

It will also display the output based on decimal point. If it is not mentioned or negative
then it will display only integer part or it is positive It will display corresponding decimal digit
value.

Syntax: select trunc(100.123), trunc(100.123, 1), trunc(100.123, -1) from dual;

MOD ( ) FUNCTION:

It is used to display remainder of given value.

Syntax: select mod(100,2), mod(50,15), mod(1000,800) from dual;

CEIL ( ) FUNCTION:

It is used to display positive value to be incremented by 1 and negative value display only
integer part.

Syntax: select ceil(100.567), ceil(-100.567) from dual;

Department of BCA. 91 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

FLOOR ( ) FUNCTION:

It is used to display negative value to be incremented by 1 and positive value display only
integer part.

Syntax: select floor(100.567), floor(-100.567) from dual;

SQRT ( ) FUNCTION:

It is used to display square root of a given number.

Syntax: select sqrt(64), sqrt(144) from dual;

Date functions

SYSDATE () function:

It returns the present data base server date and time.

Ex: we can display the current date by selecting Sysdate function from dual table is

Select sysdate from dual;

Ex: we can perform arthematical operations on sysdate function

Select syadate+1, sysdate-1, (sysdate+5) - sysdate from dual;

SQL Aggregate Functions (or) Multi row functions (or) Group functions

SQL aggregate functions operate on sets of rows to give one result per group of rows. These
functions are applicable on numeric data.

General syntax for group function is

Select [column1,….] group function(column name) from table;

Department of BCA. 92 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

There are several group functions supported by SQL. They are

• MAX( ) :

This function is used to Returns the largest value.

Select max (colname) from table name;

• MIN( ) :

This function is used to Returns the smallest value.

Select min (colname) from table name;

• AVG() :

This function is used to Returns the average value.

Select avg (colname) from table name;

• SUM() :

This is used to Returns the sum.

Select sum (colname) from table name;

• COUNT() :

This function is used to Returns the numbers of rows present in the table.

Select count (*) from table name;

• FIRST() :

This function is used to Returns the first value.

Select first (colname) from table name;

• LAST() :

This function is used to Returns the last value.

Select last (colname) from table name;

Department of BCA. 93 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

SQL Aliases

SQL aliases names are temporary names. A table or column can be given another name by using
this alias.

Table alias names can be up to 30 characters length, these are useful if a table or column names
are long or complex. Usually these names are short.

SQL Alias Syntax for Columns

SELECT column name AS alias name FROM table name;

SQL Alias Syntax for Tables

SELECT column name(s) FROM table name AS alias name;

Querying on the Database


❖ Select statement retrieves data from one or more tables.
❖ An asterisk (*) can also be used to specify should return all columns of the tables.
❖ Select is the most complex statement in SQL.
❖ The where clause includes a conditional retrieval of rows along with the select statement.
❖ Select clause can also be used with distinct to eliminate duplicate values.
❖ Along with select statement group by clause also used. The where clause is applied
before the group by clause.
❖ Along with select statement having clause also used.
❖ Along with select statement order by clause also used. It identifies which columns are
used to sort the data.
❖ The from clause which indicates the table from which data is to be retrieved.

Select with WHERE Clause:

The WHERE clause is used to extract only those records that fulfill a specified criterion
(or) condition.

Department of BCA. 94 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

SQL WHERE Syntax

SELECT column name1, column name2…. FROM table name


WHERE column name operator value;

Select with ORDER BY clause:

The ORDER BY keyword is used to sort the data by one or more columns.

The ORDER BY keyword sorts the records in ascending order by default. To sort the records in a
descending order, you can use the DESC keyword.

If we use the order by clause, it must be the last clause of the SQL statement.

SQL ORDER BY Syntax

SELECT column name1, column name2 …. FROM table name Where condition
ORDER BY column name1, column name2… ASC|DESC;

Select with GROUP BY Statement:

We can divide the table of information into smaller groups using the GROUP BY
clause.

SQL GROUP BY Syntax

SELECT column name, aggregate function (column name) FROM table name
WHERE condition

GROUP BY column name

ORDER BY column name;

Department of BCA. 95 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Select with HAVING Clause:

The HAVING clause was added to SQL because the WHERE keyword could not be used
with aggregate functions.

When we use the having clause, the oracle restricts groups

• Rows are grouped.


• Group function is applied.

SQL HAVING Syntax

SELECT column name, aggregate function (column name) FROM table name
WHERE condition
GROUP BY column name
HAVING aggregate function(column name) operator value;

SELECT with DISTINCT clause:

In a table, a column may contain many duplicate values; and sometimes you only want to list
the unique (distinct) values.

The DISTINCT keyword can be used to return only distinct (different or unique) values.

SQL Select DISTINCT Syntax

SELECT DISTINCT column name1, column name2 …… FROM table name;

SET operators
Set operators combine the results of 2 queries into a single result. Queries contains set operators
are called compound queries.

The set operators in SQL are

❖ Union
❖ Intersect
Department of BCA. 96 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

❖ Minus
❖ Cartesian product.

1. Union:
Union returns all distinct (unique) tuples selected by both the queries.

The union operator selects only unique values by default. To allow duplicates values use
“union all” operator instead of union operator.

Syntax 1:

Select column names from table1 union select column names from table2;

Syntax 2:

Select column names from table1 union all select column names from table2;

2. Intersect:
This operation returns only those rows which are common in both the queries.
Syntax:
Select column names from table1 intersect select column names from table2;

3. Minus:
This operation returns only those rows that are distinct in first table but not present
in second table.
Syntax:
Select column names from table1 minus select column names from table2;

JOINS
An SQL JOIN clause is used to combine rows from two or more tables, based on a common field
between them.

Let us consider 2 tables’ employee and department.

SQL> select *from employee;

Department of BCA. 97 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

EMPNO ENAME DEPTNO

--------------- - -------------- --------------

1000 aaa 10

2000 bbb 10

5000 eee 30

3000 ccc 20

4000 ddd 20

SQL> select *from depart;

DEPTNO DNAME

---------- ---------------

10 marketting

20 finance

30 hr

40 payroll

SQL contains different types of Joins we will see each concept with example by using above
tables.

Types of Joins

1) Cross Join
2) Equi Join
3) Non-Equi Join
4) Outer Join
5) Self Join

Department of BCA. 98 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Cross Join

• We can combine each row from first table combine with every row from second table.
• In order to this we need not mention where condition here.

A cross join that produces Cartesian product of the tables that involved in the join.

Syntax1 for Cross Join

SELECT * FROM table_name1 CROSS JOIN table_name2;


Or we can write it in another way also

Syntax2 for Cross Join

SELECT * FROM table_name1,table_name2;

Now check the below query for Cross join

Example

SELECT * FROM employee CROSS JOIN depart;


Or

SELECT * FROM employee, depart;

Once we run that query our output will be like this

Here employee table has 5 rows and depart table has 4 rows.thr result is 5 X 4=20 rows.

SQL> select *from employee, depart;

EMPNO ENAME DEPTNO DEPTNO DNAME


---------- --------------- ---------- ---------- ---------------
1000 aaa 10 10 marketting
2000 bbb 10 10 marketting
3000 ccc 20 10 marketting
4000 ddd 20 10 marketting
5000 eee 30 10 marketting
1000 aaa 10 20 finance

Department of BCA. 99 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

2000 bbb 10 20 finance


3000 ccc 20 20 finance
4000 ddd 20 20 finance
5000 eee 30 20 finance
1000 aaa 10 30 hr

EMPNO ENAME DEPTNO DEPTNO DNAME


---------- --------------- ---------- - --------- ---------------
2000 bbb 10 30 hr
3000 ccc 20 30 hr
4000 ddd 20 30 hr
5000 eee 30 30 hr
1000 aaa 10 40 payroll
2000 bbb 10 40 payroll
3000 ccc 20 40 payroll
4000 ddd 20 40 payroll
5000 eee 30 40 payroll

20 rows selected.

Equi Join
The Equi join is used to display all the matched records from the joined tables and also display
redundant values.
In this join we need to use * sign to join the table.
In this we use The comparison operator is = (assignment operator) sign.

Syntax1 for Equi Join:

SELECT * FROM table_name1 t1 INNER JOIN table_name2 t2


ON t1.column_name=t2.column_name

Syntax2 for Equi Join:

Select aliasname.colname, aliasname.colname ……… from tablename1 alias name, tablename2


alias name where aliasname.tablename1= aliasname.tablename2;

Now check the below query for Equi join

Department of BCA. 100 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

Example for Syntax1:

SELECT * FROM employee e INNER JOIN depart d ON e.deptno= d.deptno ;

EMPNO ENAME DEPTNO DEPTNO DNAME


---------- --------------- ---------- ---------- ---------------
1000 aaa 10 10 marketting
2000 bbb 10 10 marketting
3000 ccc 20 20 finance
4000 ddd 20 20 finance
5000 eee 30 30 hr

Example for Syntax2:

select e.empno,e.ename,d.dname from employee e, depart d


where e.deptno=d.deptno;

Once we run above query our output will be like this

EMPNO ENAME DNAME


---------- --------------- ---------------
1000 aaa marketting
2000 bbb marketting
3000 ccc finance
4000 ddd finance
5000 eee hr

Non Equi Join


This join is similar to the equi-join operator except that comparisons operator in the join condition
is “not equals”.

We include <, >, <=, >=, like, in and between operators along with it.

Example:

select e.empno,e.ename,d.dname from employee e, depart d


where e.deptno>d.deptno;
Department of BCA. 101 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Natural Join
The Natural join is same as our Equi join but only the difference is it will restrict to display
redundant values.

Syntax for Natural Join

SELECT * FROM table_name1 t1 NATURAL JOIN table_name2 t2 ;

Example

SELECT * FROM employee NATURAL JOIN depart ;

Once we run above query our output will be like this

DEPTNO EMPNO ENAME DNAME


---------- ---------- - -------------- ---------------
10 1000 aaa marketting
10 2000 bbb marketting
20 3000 ccc finance
20 4000 ddd finance
30 5000 eee hr

Outer Join

Sometimes matching and non-matching rows are required return from the tables. In such acses we
need to use this outer join.

A join that return all the rows that satisfy the condition and unmatched rows in the joined table is
an Outer Join.

If there are any values in one table that do not have corresponding values in the other table that
will not be selected in equi join. Whereas in outer join those type of data forcefully selected by
using (+) symbol. The corresponding columns for that row will have NULL.

Department of BCA. 102 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

We are having three types of Outer Joins

Left Outer Join


Right Outer Join
Full Outer Join

Left Outer Join

The left outer join displays all the rows from the left table even if there are no matches in the right
table.

Syntax1 for Left Outer Join:

SELECT Column_List FROM table_name1 t1


LEFT OUTER JOIN table_name2 t2
ON t1.column_name=t2.column_name

Syntax2 for Left Outer Join:

Select aliasname.colname, aliasname.colname ……… from tablename1 alias name, tablename2


alias name where aliasname.tablename1 (+) = aliasname.tablename2;

Example

select e.empno,e.ename,d.deptno from employee e, depart d


where e.deptno(+)=d.deptno;

Once we run that query our output will be like this

EMPNO ENAME DEPTNO


---------- --------------- ----------
1000 aaa 10
2000 bbb 10
3000 ccc 20
4000 ddd 20
5000 eee 30
40

Right Outer Join

Department of BCA. 103 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

The right outer join displays all the rows from the right table even if there are no matches in the
left table.

Syntax1 for right Outer Join:

SELECT Column_List FROM table_name1 t1


RIGHT OUTER JOIN table_name2 t2
ON t1.column_name=t2.column_name

Syntax2 for right Outer Join:

Select aliasname.colname, aliasname.colname ……… from tablename1 alias name, tablename2


alias name where aliasname.tablename1= aliasname.tablename2(+);

Example:

select e.empno,e.ename,d.deptno from employee e, depart d


where e.deptno=d.deptno (+);

Full Outer Join

Full Outer Join displays all the matching and non matching rows from both the tables.

Method of achieving full outer join is

LEFT OUTER JOIN UNION RIGHT OUTER JOIN

Syntax1 for Full Outer Join

SELECT Column_List FROM table_name1 t1 FULL OUTER JOIN table_name2 t2


ON t1.column_name=t2.column_name

Syntax2 for Full Outer Join

Select aliasname.colname, aliasname.colname ……… from tablename1 alias name, tablename2


alias name where aliasname.tablename1 (+) = aliasname.tablename2

Union

Department of BCA. 104 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

Select aliasname.colname, aliasname.colname ……… from tablename1 alias name, tablename2


alias name where aliasname.tablename1= aliasname.tablename2(+);

Example

select e.empno,e.ename,d.deptno from employee e, depart d where e.deptno(+)=d.deptno

union

select e.empno,e.ename,d.deptno from employee e, depart d where e.deptno=d.deptno (+);

Self Join
Joining the table itself called self join.

It means that each row of the table is combined with itself and with every other row of the same
table.

The self join can be viewed as a join of two copies of the same table.

Here we need to use aliases for the same table to set a self join between single table and retrieve
records satisfying the condition in where clause.

Syntax:

Select aliasname1.colname, aliasname2.colname ……… from tablename alias name1, tablename


alias name2 where aliasname1.col name = aliasname2.col name ;

Example:

SQL> select e1.empno,e1.ename, e1.sal from emp e1,emp e2


where e2.ename='adams' and e1.sal>e2.sal;

Department of BCA. 105 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

SQL INDEX

Definition:

Indexes are optional structures associated with tables to speed query execution and
provide uniqueness.

Use of indexes:

Creating simple indexes improves query performances on frequently queried columns.

The CREATE INDEX statement is used to create indexes in tables.

An index can be created in a table to find data more quickly and efficiently.

The users cannot see the indexes; they are just used to speed up searches/queries.

SQL CREATE INDEX Syntax

Creates an index on a table. Duplicate values are allowed:

Syntax:

CREATE INDEX index name ON table name (column name);

SQL CREATE UNIQUE INDEX Syntax

Creates a unique index on a table. Duplicate values are not allowed:

Syntax:

CREATE UNIQUE INDEX index name ON table name (column name) ;

We can drop (or) remove the index from the data base.

Syntax:

drop index index name;

Department of BCA. 106 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

SQL Views

A view is a virtual or imaginary table. It does not contain data, deriving its data from base tables.

A view contains rows and columns, just like a real table. The fields in a view are fields from one
or more real tables in the database.

SQL CREATE VIEW Syntax

CREATE VIEW view name(col names) AS


SELECT (column name(s)) FROM table name;

Example:
Create view empview(empid,empname) as select (empno,ename) from employee;

If we wants display view then

Select *from empview;

Once we insert data into employee table automatically updates the view also.

Advantages of views:

• Views can be used to implement secure the data.


• Views can join and simplify multiple tables into a single table.
• View cannot affect the content of the base table only it replaces the headings.
• Views occupy little space to store in database.

Sub Queries

It is also referred as inner query or nested query. Sub queries are alternate way of returning data
from multiple tables.

In this output of one query is depends on another query.

Always sub queries are encloses within the braces.

First evaluate the sub query and then run the main query.

Department of BCA. 107 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

Syntax: outer query

Select column names from table name1 where condition

(Select column name from table name2 where condition);

inner or sub query

Questions:
1. What is super key?
2. Explain Constraints in relational model.
3. Write the syntax of using distinct.
4. Discuss the 3 DDL commands in SQL.
5. Write a short note on different SQL functions.
6. Write the syntax of create command in SQL.
7. What is view?
8. What is equi join? Explain.
9. Explain different data types used in SQL.
10. Explain the advantage of SQL.
11. Explain INSERT and SELECT commands in SQL with an example.

Department of BCA. 108 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

PL/SQL
CHAPTER-5
What is PL/SQL?

PL/SQL is oracle’s procedural language extension to SQL. PL/SQL allows to mix SQL
statements with procedural statements.

PL/SQL is superset of SQL.

Differences between SQL and PL/SQL:


SQL:

• It is a data oriented language for selecting and manipulating data.


• It is a non-procedural language.
• It does not support programming part.
• SQL engine executes only one statement at a time.
• We can embed SQL statements in a PL/SQL program.
• It is used to frame the queries by using the commands.
• If we made a mistake SQL engine will display its own error messages.

PL/SQL:

• PL/SQL is Oracle’s procedural language to create applications.


• PL/SQL is executed block of code.
• PL/SQL is the superset of SQL.
• We cannot embed PL/SQL statements within a SQL.
• PL/SQL allows you to mix SQL statements with procedural statements like IF statement,
Looping structures etc.
• It uses SQL for data retrieval and manipulation and uses its own statements for data
processing.
• It will display user friendly errors.

Advantages (or) features of PL/SQL

Block structure:
PL/SQL consists of a block of code which can be nested within each other. Each program
written in PL/SQL is written as a block. It is easy to understand and access.

Department of BCA. 109 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

Procedural Language capability:


PL/SQL consists of procedural language constructs such as conditional statements and
looping statements.

Better performance:
PL/SQL engine processes multiple SQL statements simultaneously as a single block. it
improves performance.

Exception (or) error handling:


PL/SQL allows errors, called as exceptions, to be detected and handled. Whenever there is
a predefined error PL/SQL raises an exception automatically. These exceptions can be handled to
recover from errors.

Modularity:
PL/SQL allows process to be divided into different modules. Subprograms called as
procedures and functions can be defined and invoked using the name. These subprograms can
also take parameters.

Portability:
Applications written in PL/SQL are portable to any platform on which Oracle runs. Once
you write a program in PL/SQL, it can be used in any environment without any change at all.

PL/SQL BLOCKS

PL/SQL blocks can be generally categorized as follows:

• Named blocks
• Anonymous (or) Unnamed blocks

Department of BCA. 110 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

Named blocks: are the blocks these are having names like procedures, functions, packages,
cursors and triggers.

Unnamed blocks: are the blocks which are not having names.
These are
Declare
Begin
Exception
End

PL/SQL block Structure


PL/SQL programs are written as blocks. Block allows you to group logically related statements
and declarations.

PL/SQL block is consisting of the following three parts:

❖ Declarative part (optional)


❖ Executable part (mandatory)
❖ Exception-handling part (optional)

The following is the syntax of PL/SQL block.

DECLARE
Declaration of variable
BEGIN
Executable commands
EXCEPTION
Exception handlers
END;
Department of BCA. 111 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

Declarative Part:
This section is optional and is used to declare variables, constants, records and cursors etc.
The declaration section of a PL/SQL block starts with the keyword “declare”.
The following is the example of declarative part.
First variable is of type NUMBER(5). As we have seen before PL/SQL supports the data types of
SQL.
The second variable is initialized to 0.
declare
v_rollno number(5);
v_count number(2) := 0;

Executable part:
The execution section of a PL/SQL block starts with the keyword begin and ends with end
keyword.
This is the only mandatory part of the entire block, where the program logic is written to perform
any task.
Begin
Statements
End;

Exception-handling part:
The exception section of a PL/SQL block starts with the keyword “exception”.
This section is optional. Any errors in the program can be handled in this section. Exception
handlers handle exceptions.
Exception
Statements;
End section:
It is the last section of PL/SQL block. It indicates end of program.
End;

Department of BCA. 112 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

PL/SQL Architecture

• Every PL/SQL block is first executed by PL/SQL engine.


• This is the engine that compiles and executes PL/SQL blocks.
• PL/SQL engine executes all procedural statements of a PL/SQL of the block, but sends
SQL command to SQL statements executor in the Oracle RDBMS.
• That means PL/SQL separates SQL commands from PL/SQL commands and executes
PL/SQL commands using Procedural statement executor, which is a part of PL/SQL
engine.
• PL/SQL engine locates both client side as well as server side also.
• All SQL statements that are embedded within the PL/SQL block sent to the oracle server.

Comments in PL/SQL

You can give comments in PL/SQL block in two ways.


• First way is by preceding the comment with two hyphens (- -).
Example: -- this is single line comment

Department of BCA. 113 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

• Second way is by starting the comment with /* and ending it with */.
Example: /* this comment can be of multiple lines */

PL/SQL Data types

PL/SQL provides a variety of predefined data types, which can be divided into three categories:

• Scalar represents a single value.


• Composite Is a collection of components
Reference is a pointer that points to another item.
• LOB holds a large object locator.

The following are the data types in various categories

Scalar: NUMBER, CHAR, VARCHAR2, DATE, BOOLEAN


Composite: RECORD, TABLE and VARRAY.
LOB: BFILE, BLOB, CLOB, and NCLOB.

Scope and visibility of variables


Scope of the variable refers to the region of the program in which the variable can be used. A
variable is said to be visible when it can be referred without any qualifier.

Examine the following examples to understand scope and visibility of variables.

Department of BCA. 114 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

Variable NUM1 can be accessed from the point of declaration to the end of the outer block.
Variable NUM2 can be accessed from the point of declaration to the end of inner block.

Assignment Operator ( := )

Assignment operator allows a value to be stored in a variable.


Syntax: variable := expression;

The following are examples of assignment operator:


count := 1;
name := ‘Srikanth’;

Displaying output from PL/SQL

In order to display output from a PL/SQL block, we have to use DBMS_OUTPUT package.
A package is a collection of procedures and functions.

Department of BCA. 115 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

PUT and PUT_LINE procedures both these procedures are used to display a NUMBER,
VARCHAR2 or DATE type value.
PUT allows you to put multiple pieces that make up a line. PUT_LINE puts the given data
followed by end-of-line character.

In order to see the output sent using these procedures, the following must satisfy:
_ The program unit from where they are called must be completed
_ SERVEROUPUT variable of SQL*PLUS must be set to ON by using the command.

Set serveroutput on;

PL/SQL Execution

• Create a program in text editor or SQL window.


• After writing a program then type / at the SQL prompt then enter to execute.

PL/SQL Control Statements


There are 3 types of control structures supported by PL/SQL. They are
• Sequential control statements
• Conditional or selection control statements
• Looping statements

Conditional Statements in PL/SQL

IF - THEN statement:
The IF statement associates a condition with a sequence of statements enclosed by the keywords
THEN (starting of IF) and END IF; (ending of IF).
If the condition is TRUE, the statements get executed, and if the condition is FALSE or NULL,
then the IF statement display nothing.

Department of BCA. 116 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

Syntax for IF-THEN statement is:


IF condition
THEN
Statements;
END IF;

Flow chart:

Example of an IF-THEN statement is:


Declare
Age number:=19;
Begin
IF (a <= 20)
THEN
Dbms_output.put_line (‘voting is eligible’);
END IF;
End;

IF-THEN-ELSE statement
Department of BCA. 117 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

In this user executes true block as well as false block statements.

Syntax for the IF-THEN-ELSE statement is:

IF condition
THEN
S1;
ELSE
S2;
END IF;

Where, S1 and S2 are different sequence of statements. In the IF-THEN-ELSE statements, when
the test condition is TRUE, the statement S1 is executed and S2 is skipped; when the test
condition is FALSE, then statement S2 is executed.

Flow Chart:

For example:
Declare
A number:=10;

Department of BCA. 118 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

B number:=20;
Begin
IF (a < b)
THEN
Dbms_output.put_line (‘a is big’||a);
Else
Dbms_output.put_line (‘b is big’||b);
END IF;
End;

IF-THEN-ELSIF statement
When we mention 2 or more conditions on one program we use this syntax.

The syntax of an IF-THEN-ELSIF Statement in PL/SQL programming language is:

IF (condition 1)
THEN
S1; -- Executes when the condition 1 is true
ELSIF (condition 2)
THEN
S2; -- Executes when the condition 2 is true
ELSIF (condition 3)
THEN
S3; -- Executes when the condition 3 is true
ELSE
S4; -- executes when the none of the above condition is true
END IF;

Example:

Department of BCA. 119 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

DECLARE
a number(3) := 100;
BEGIN
IF ( a := 10 ) THEN
dbms_output.put_line ('Value of a is 10’);
ELSIF (a: = 20) THEN
dbms_output.put_line ('Value of a is 20' );
ELSIF ( a := 30 ) THEN
dbms_output.put_line('Value of a is 30' );
ELSE
dbms_output.put_line('None of the values is matching');
END IF;
dbms_output.put_line('Exact value of a is: '|| a );
END;

Nested IF-THEN-ELSE
It is always legal in PL/SQL programming to nest IF-ELSE statements, which means you can use
one IF or ELSE IF statement inside another IF or ELSE IF statement(s).
Syntax:
IF( condition 1)
THEN
IF(condition 2)
THEN
sequence-of-statements;
END IF;
ELSE
else-statements;
END IF;

Department of BCA. 120 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

Example:
DECLARE
a number(3) := 100;
b number(3) := 200;
BEGIN
IF( a := 100 )
THEN
IF( b := 200 )
THEN
dbms_output.put_line('Value of a is 100 and b is 200' );
END IF;
END IF;
dbms_output.put_line ('Exact value of a is : ' || a );
dbms_output.put_line ('Exact value of b is : ' || b );
END;
Case statement
Like the IF statement, the CASE statement selects one sequence of statements to execute. This
statement will select any one of several alternatives.

The syntax for case statement in PL/SQL is:

CASE selector
WHEN 'value1' THEN Statement1;
WHEN 'value2' THEN Statement2;
WHEN 'value3' THEN Statement3;
...
ELSE Statement n; -- default case

END CASE;

Department of BCA. 121 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

Example:

DECLARE
Grade char(1) := 'A';
BEGIN
CASE grade
when 'A' then dbms_output.put_line('Excellent');
when 'B' then dbms_output.put_line('Very good');
when 'C' then dbms_output.put_line('Well done');
when 'D' then dbms_output.put_line('You passed');
when 'F' then dbms_output.put_line('Better try again');
else dbms_output.put_line('No such grade');
END CASE;
END;

.Looping Control Statements

PL/SQL Basic LOOP


Basic loop structure encloses sequence of statements in between the LOOP and END
LOOP statements. With each iteration, the sequence of statements is executed and then control
resumes at the top of the loop.

The syntax of a basic loop in PL/SQL programming language is:

LOOP
Sequence of statements;
END LOOP;

Department of BCA. 122 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

Here, sequence of statement(s) may be a single statement or a block of statements. An EXIT


statement or an EXIT WHEN statement is required to break the loop.

Example:
DECLARE
N number:=1;
Begin
Loop
Dbms_output.put_line(n);
N:=n+1;
Exit when n>5;
End loop;
End;

PL/SQL WHILE LOOP


A WHILE LOOP statement in PL/SQL programming language repeatedly executes a target
statement as long as a given condition is true.
Syntax:
WHILE condition
LOOP
sequence_of_statements
END LOOP;

Example:
DECLARE
a number(2) := 1;
BEGIN
WHILE a <=5
LOOP
dbms_output.put_line( a);
Department of BCA. 123 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.

a := a + 1;
END LOOP;
END;

PL/SQL FOR LOOP


If number of times body of loop will be executed in such case we need to use this loop.

Syntax:
FOR variable in condition
LOOP
sequence_of_statements;
END LOOP;
Example:
DECLARE
a number(2);
BEGIN
FOR a in 10 .. 20
LOOP
dbms_output.put_line('value of a: ' || a);
END LOOP;
END;

Reverse FOR LOOP Statement


By default, iteration proceeds from the initial value to the final value, generally upward from the
lower bound to the higher bound. You can reverse this order by using the REVERSE keyword.
Example:
DECLARE
a number(2) ;

Department of BCA. 124 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

BEGIN
FOR a IN REVERSE 10 .. 20
LOOP
dbms_output.put_line('value of a: ' || a);
END LOOP;
END;

When the above code is executed at SQL prompt, it produces the following result:
Value of a: 20
Value of a: 19
Value of a: 18
Value of a: 17
Value of a: 16
Value of a: 15
Value of a: 14
Value of a: 13
Value of a: 12
Value of a: 11
Value of a: 10
Nested loops in PL/SQL

PL/SQL allows using one loop inside another loop.

The syntax for a nested basic LOOP statement in PL/SQL is as follows:


LOOP
Sequence of statements1
LOOP
Sequence of statements2
END LOOP;

Department of BCA. 125 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

END LOOP;

The syntax for a nested FOR LOOP statement in PL/SQL is as follows:

FOR variable in condition1


LOOP
sequence_of_statements
FOR variable in condition2
LOOP
sequence_of_statements;
END LOOP;
END LOOP;

The syntax for a nested WHILE LOOP statement in Pascal is as follows:

WHILE condition1
LOOP
sequence_of_statements1
WHILE condition2
LOOP
sequence_of_statements2
END LOOP;
END LOOP;
Labeling a PL/SQL Loop
PL/SQL loops can be labeled. The label should be enclosed by double angle brackets (<< and >>)
and appear at the beginning of the LOOP statement. The label name can also appear at the end of
the LOOP statement.

The following program illustrates the concept:


<<outer>>

Department of BCA. 126 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

DECLARE
n number:=10;
BEGIN
DECLARE
n number:=20;
begin
dbms_output.put_line(‘value of n in outer block is:’||outer.n);
dbms_output.put_line(‘value of n in inner block is:’|| n);
END;
END;
Sample Programs
1. Program to illustrate exit statement.

Example:

DECLARE
a number(2) := 10;
BEGIN
WHILE a < 20
LOOP
dbms_output.put_line ('value of a: ' || a);
a := a + 1;
IF a > 15 THEN
EXIT;
END IF;
END LOOP;
END;

2. Program to illustrate continue statement.

Department of BCA. 127 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

Example:

DECLARE
a number(2) := 10;
BEGIN
WHILE a < 20
LOOP
dbms_output.put_line ('value of a: ' || a);
a := a + 1;
IF a = 15 THEN
a := a + 1;
CONTINUE;
END IF;
END LOOP;
END;

3. Program to find factorial of a given number using while loop.


DECLARE
i number := 1;
n number:=6;
fact number:=1;
BEGIN
WHILE i<=6
LOOP
Fact:=fact*i;
I:=i+1;
END LOOP;
dbms_output.put_line (‘factorial of ‘||n||’ is ||fact);
END;

Department of BCA. 128 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

4. Program to find factorial of a given number using for loop.


DECLARE
i number := 1;
n number:=6;
fact number:=1;
BEGIN
For I in 1 . . 6
LOOP
Fact:=fact*i;
I:=i+1;
END LOOP;
dbms_output.put_line (‘factorial of ‘||n||’ is ||fact);
END;

5. Create a table circle with the field radius and area write a PL/SQL program to find
area for given radius.
Create table circle ( r number(5),
Area number(10,2));
DECLARE
Pi constant number:=3.14;
r number(6);
area number(10,2);
BEGIN
For r in 5 . . 10
LOOP
Area:=pi * power(r,2);
Insert into circle values(r, area);
END LOOP;
END;

Department of BCA. 129 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

Select *from circle;


6. Program to find sum of first 10 natural numbers.

DECLARE
i number := 1;
sum1 number:=1;
BEGIN
LOOP
I:=i+1;
If i>10 then
Exit;
End if;
Sum1:=sum1+I;
END LOOP;
dbms_output.put_line (‘sum of 10 natural numbers is’ || sum1);
END;

7. Program to add 2 numbers


declare
a number;
b number;
c number;
begin
a:=&a;
b:=&b;
c:=a+b;
dbms_output.put_line('sum of numbers is'||c);
end;
8. Program to find sum of first 100 numbers.

Department of BCA. 130 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

declare
a number;
s1 number default 0;
begin
a:=1;
loop
s1:=s1+a;
exit when (a=100);
a:=a+1;
end loop;
dbms_output.put_line('sum between 1 to 100 is'||s1);
end;

9. Program to generate Fibonacci series.


declare
n number:=&n;
n1 number:=0;
n2 number:=1;
n3 number;
begin
dbms_output.put_line(n1);
dbms_output.put_line(n2);
for i in 3..n
loop
n3:=n1+n2;
dbms_output.put_line(n3);
n1:=n2;
n2:=n3;

Department of BCA. 131 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

end loop;
end;
Named Blocks
There are several named blocks in PL/SQL. They are
 Procedure
 Function
 Package
 Cursors
 Triggers

Procedure
Definition:
“Procedure is a subprogram is a program unit/module that performs a particular action”.
Procedure consists of 2 parts. They are
o Specification
o Body of procedure

Creating a Procedure

A procedure is created with the CREATE OR REPLACE PROCEDURE statement.

CREATE OR REPLACE PROCEDURE procedure name <parameters> is /as


BEGIN
Statements;
END procedure name;

Where,
procedure-name specifies the name of the procedure.

Department of BCA. 132 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

REPLACE keyword used to overwrite the procedure with the same name was already present.

Parameter list contains 3 parameters. They are IN ,OUT and INOUT. IN parameter is used to
send valued to stored procedure. OUT parameter is used to get values from stored procedure.

procedure-body contains the executable part.

The AS keyword is used instead of the IS keyword for creating a standalone procedure.

Example 1: Program to illustrate creation and execution of a procedure.

CREATE OR REPLACE PROCEDURE greetings AS


BEGIN
dbms_output.put_line('Hello World!');
END;
/
When above code is executed using SQL prompt.

SQL> EXECUTE greetings;


Output:
Hello World
PL/SQL procedure successfully completed.

Deleting a Standalone Procedure


A standalone procedure is deleted with the DROP PROCEDURE statement.
Syntax for deleting a procedure is:
DROP PROCEDURE procedure-name;

So you can drop greetings procedure by using the following statement:


BEGIN

Department of BCA. 133 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

DROP PROCEDURE greetings;


END;

Example 2: Create a procedure to add 2 numbers.


create or replace procedure add1(fn in number,sn in number,res out number) as
begin
res:=fn+sn;
end;
/
procedure created.
Now we will write the definition of the procedure.

declare
c number;
begin
add1(10,20,c);
dbms_output.put_line ('sum is' ||c);
end;

Example 3: IN & OUT Mode

This program finds the minimum of two values, here procedure takes two numbers using IN
mode and returns their minimum using OUT parameters.

Department of BCA. 134 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

DECLARE
a number;
b number;
c number;
PROCEDURE findMin(x IN number, y IN number, z OUT number) IS
BEGIN
IF x < y THEN
z:= x;
ELSE
z:= y;
END IF;
END;
BEGIN
a:= 23;
b:= 45;
findMin(a, b, c);
dbms_output.put_line(' Minimum of (23, 45) : ' || c);
END;
/
When the above code is executed at SQL prompt, it produces the following result:
Minimum of (23, 45) : 23
PL/SQL procedure successfully completed.

Functions
Definition:
“Function is a named PL/SQL block it is set of statements carrying a particular task.”
Major difference between Procedure and function is
A PL/SQL Procedure may or may not return a value where as Function must always return a
value.

Department of BCA. 135 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

Creating a Function
CREATE OR REPLACE FUNCTION function name <parameter name> RETURN data type
IS |AS
BEGIN
function body
END [function name];

Where,
function-name specifies the name of the function.

REPLACE keyword used to overwrite the function with the same name was already present.

Parameter list contains 3 parameters. They are IN, OUT and INOUT. IN parameter is used to
send valued to stored function. OUT parameter is used to get values from stored function.

the function must contain a return statement.

RETURN clause specifies that data type you are going to return from the function.

function-body contains the executable part.

The AS keyword is used instead of the IS keyword for creating a standalone function.

Example 1: Program to illustrate function without arguments.


CREATE OR REPLACE function fun1 return varchar2 AS
BEGIN
dbms_output.put_line('Hello World!');
END;
/
When above code is executed using SQLQuery.

Department of BCA. 136 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

SQL> select *from dual;

Example 2: Program to illustrate function with arguments.


CREATE OR REPLACE function fun2 (s varchar2) return varchar2 AS
BEGIN
Return s;
END;
/
When above code is executed using SQL Query.

SQL> select fun2(‘vvfgc’) from dual;

Example 3: Program to find sum of 2 numbers using functions.


create or replace function fun1(a number,b number) return number as
begin
return(a+b);
end;
/
function created.
Now we will define body of the function.
declare
result number;
begin
result:=fun1(10,20);
dbms_output.put_line('addition of 2 numbers is'||result);
end;
/

Department of BCA. 137 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

Example 4:
The following is one more example which demonstrates Declaring, Defining, and Invoking a
Simple PL/SQL Function that computes and returns the maximum of two values.

DECLARE
a number;
b number;
c number;
FUNCTION findMax(x IN number, y IN number) RETURN number IS
z number;
BEGIN
IF x > y THEN
z:= x;
ELSE
Z:= y;
END IF;
RETURN z;
END;
BEGIN
a:= 23;
b:= 45;
c := findMax(a, b);
dbms_output.put_line(' Maximum of (23,45): ' || c);
END;
/
When the above code is executed at SQL prompt, it produces the following result:
Maximum of (23, 45): 78
PL/SQL procedure successfully completed.

Department of BCA. 138 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

Packages

Definition:

Package is a collection of procedures and functions.

These are having 2 parts. They are specification and body of the package.

Syntax:

CREATE OR REPLACE PACKAGE package name IS |AS


Procedure procedure name;
Function function name;
BEGIN
Statements;
END package name;

EXAMPLE 1: Program to illustrate creates and execute package.

SQL> create or replace package pack1 as

procedure proc1;

function fun1 return varchar2;

end pack1;

Package created.

The next step we will write the code for body of the package.

create or replace package body pack1 as

Department of BCA. 139 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

procedure proc1 as

begin

dbms_output.put_line('this is procedure concept');

end proc1;

function fun1 return varchar2 is

begin

return('hello this is function concept');

end fun1;

end pack1;

Package body created.

Now execute procedure and function through package.

SQL> execute pack1.proc1;

this is procedure concept

PL/SQL procedure successfully completed.

SQL> select pack1.fun1 from dual;

FUN1

--------------------------------------------------------------------------------

hello this is function concept.

Cursors

Department of BCA. 140 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

Cursor represents a structure in memory and is different from cursor variable. When we declare a
cursor it gets pointer variable which does not point anything.
When the cursor is open memory is allocated and the cursor will be created.

There are two types of cursors:


• Implicit cursors.
• Explicit cursors

The main use of cursor will be allowing the programmer to retrieve data from a table and perform
some actions on the data.

Implicit Cursors:
• Implicit cursors are automatically created by Oracle whenever an SQL statement is
executed.
• Programmers cannot control the implicit cursors and the information in it.

In PL/SQL, implicit cursor always has 4 attributes like %FOUND, %ISOPEN, %NOTFOUND,
and %ROWCOUNT.

The following table provides the description of the most used attributes:

Attribute Description

%FOUND Returns TRUE if an INSERT,


UPDATE, or DELETE statement
affected

The logical opposite of %FOUND. It returns TRUE if

Department of BCA. 141 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

%NOTFOUND an INSERT, UPDATE, or DELETE statement


affected no rows, or a SELECT INTO statement
returned no rows. Otherwise, it returns FALSE.

%ISOPEN Always returns FALSE for implicit cursors, because


Oracle closes the SQL cursor automatically after
executing its associated SQL statement.

%ROWCOUNT Returns the number of rows affected by an INSERT,


UPDATE, or DELETE statement, or returned by a
SELECT INTO statement

Example:
We will be using the CUSTOMERS table we had created and used in the previous chapters.
Select * from customers;
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |

The following program would update the table and increase salary of each customer by 500 and
use the SQL%ROWCOUNT attribute to determine the number of rows affected:

DECLARE

Department of BCA. 142 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

Total_ rows number(2);


BEGIN
UPDATE customers
SET salary = salary + 500;
IF sql %notfound THEN
dbms_output.put_line('no customers selected');
ELSIF sql%found THEN
Total_ rows := sql%rowcount;
dbms_output.put_line( total _rows || ' customers selected ');
END IF;
END;
/
When the above code is executed at SQL prompt, it produces the following result:

6 customers selected
PL/SQL procedure successfully completed.

If you check the records in customers table, you will find that the rows have been updated:

Select * from customers;


| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2500.00 |
| 2 | Khilan | 25 | Delhi | 2000.00 |
| 3 | kaushik | 23 | Kota | 2500.00 |
| 4 | Chaitali | 25 | Mumbai | 7000.00 |
| 5 | Hardik | 27 | Bhopal | 9000.00 |
| 6 | Komal | 22 | MP | 5000.00 |

Department of BCA. 143 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

Explicit Cursors
Explicit cursors are used to write a query for more than one row.
An explicit cursor should be defined in the declaration section of the PL/SQL Block.
It is created on a SELECT Statement which returns more than one row.

The syntax for creating an explicit cursor is


CURSOR cursor_name IS select_statement;

Working with an explicit cursor involves four steps:

Declaring the cursor for initializing in the memory

Opening the cursor for allocating memory

Fetching the cursor.

Closing the cursor.

Declaring the Cursor


Declaring the cursor defines the cursor with a name and the associated SELECT statement.

Opening the Cursor


Opening the cursor allocates memory for the cursor and makes it ready for fetching the rows
returned by the SQL statement into it.

Fetching the Cursor


Fetching the cursor involves accessing one row at a time.

Closing the Cursor


Closing the cursor means releasing the allocated memory.

Department of BCA. 144 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

Example:
Following is a complete example to illustrate the concepts of explicit cursors:
DECLARE
c_id customers.id%type;
c_name customers.name%type;
c_addr customers.address%type;
CURSOR c_customers is
SELECT id, name, address FROM customers;
BEGIN
OPEN c_customers;
LOOP
FETCH c_customers into c_id, c_name, c_addr;
dbms_output.put_line(c_id || ' ' || c_name || ' ' || c_addr);
EXIT WHEN c_customers%notfound;
END LOOP;
CLOSE c_customers;
END;
/
Triggers
Definition:
“Triggers are stored programs, which are automatically executed or fired when some
events occur.”

Triggers are, in fact, written to be executed in response to any of the following events:

A database manipulation (DML) statement (DELETE, INSERT, or UPDATE).

Department of BCA. 145 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

A database definition (DDL) statement (CREATE, ALTER, or DROP).

A database operation (SERVERERROR, LOGON, LOGOFF, STARTUP, or SHUTDOWN).

Triggers could be defined on the table, view, schema, or database with which the event is
associated.

Benefits of Triggers

Triggers can be written for the following purposes:


Generating some derived column values automatically

Enforcing referential integrity

Event logging and storing information on table access

Auditing

Synchronous replication of tables

Security Access Control.

Preventing invalid transactions


Creating Triggers
The syntax for creating a trigger is:

CREATE [OR REPLACE ] TRIGGER trigger_name


{BEFORE | AFTER }
{INSERT [OR] | UPDATE [OR] | DELETE} ON table_name
[FOR EACH ROW] WHEN (condition)
BEGIN

Department of BCA. 146 | P a g e


Database Management Systems. Seshadripuram College, Tumkur.

Trigger body

END;

Where,
• CREATE [OR REPLACE] TRIGGER trigger_name: Creates or replaces an existing
trigger with the trigger_name.

• {BEFORE | AFTER}: This specifies when the trigger would be executed.

• {INSERT [OR] | UPDATE [OR] | DELETE}: This specifies the DML operation.

• [ON table_name]: This specifies the name of the table associated with the trigger.

• [FOR EACH ROW]: This specifies a row level trigger, i.e., the trigger would be
executed for each row being affected.

• WHEN (condition): This provides a condition for rows for which the trigger would fire.

Trigger executes if the condition is true.

Department of BCA. 147 | P a g e

You might also like