Dbms Notes
Dbms Notes
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.
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:
File Information
Dname,
Doctor
Experience,
specialization,etc.
Pname, Disease,
Patient
Dname, ward
number etc.
In the above example 3 components they are doctor, patient and ward files. They will maintain
their own files to store their information.
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.
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:
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:
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. 10 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
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
Department of BCA. 11 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
Terminology:
Domain:
Ex: in the above relation SID column is the domain rest of the columns have duplicate values.
Cardinality of Relation:
Degree of Relation:
ADVANTAGES:
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.
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:
DISADVANTAGES:
Department of BCA. 13 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
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:
Department of BCA. 14 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
Schema:
Schema is a overall structure or description of a database, which does not have any
content in it.
Instance:
The actual content of database at a particular point at a time is referred as an Instance.
DBMS Architecture
There are 2 different ways to look at the architecture of DBMS they are
1. Logical Architecture
2. Physical Architecture.
Department of BCA. 15 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
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.
▪ 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.
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. “
It is the ability to change the conceptual schema without affecting the external
schemas or application programs.
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
Department of BCA. 18 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
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.
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:
Department of BCA. 20 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
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.
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.
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.
Ex: student
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.
Entity:
• 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:
Entity type:
Relationship:
Department of BCA. 23 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
Attribute:
It is represented by symbol.
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.
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
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.
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
Can WOMAN
MAN
marry
Department of BCA. 26 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
Can STUDENTS
TEACHER
teaches
Department of BCA. 27 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
Can STUDENTS
TEACHERS
teaches
Degrees of relationship
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.
Works DEPARTMENT
EMPLOYEE
in
sal
desg
Department of BCA. 28 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
BRANCH
location
Data Abstraction
The process of abstraction hides the data or information from the users.
• Generalization
• Specialization
• Aggregation
Department of BCA. 29 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
Example of Generalization.
Department of BCA. 30 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
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.
Department of BCA. 33 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
Department of BCA. 34 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
Department of BCA. 35 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
Storage Device:
The hardware that writes data to or reads data from a storage medium is called a storage
device. (or)
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.
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.
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:
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 block is physical data records separated on the medium from other blocks by inter blocks.
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.
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.
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.
“The time taken to rotate and position the beginning of the desired block under the
read/write head.”
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
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”.
Department of BCA. 41 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
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.
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 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.
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.
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
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.
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.
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:
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:
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.
Append: this operation is used to add extra information to the existing file.
Combined Operations:
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.
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.
A1 A2 A3 A4
File A
Department of BCA. 48 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
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 Levels
Department of BCA. 49 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
Advantages
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.
Department of BCA. 50 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
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.
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.
This uses byte 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 bytes. p1, p2, p3 are parities.
Advantages:
Disadvantages:
RAID level 4:[ Independent data disks with shared parity disk ]
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:
Disadvantages:
Department of BCA. 54 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
• 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:
• Just like RAID 5, this does block level striping. However, it uses dual parity.
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.
• 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.”
Each and every file organization technique has 3 types of operations. They are
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:
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:
Deleting:
Department of BCA. 58 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
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.
Department of BCA. 59 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
• Internal hashing
• External hashing
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.
Static hashing:
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.
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.
Department of BCA. 62 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
Ex:
Execution of transaction
Ex:
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
A.balance = New_Balance
Close_Account(A)
B’s Account
Open_Account(B)
Old_Balance = B.balance
B.balance = New_Balance
Close_Account(B)
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
Active –
In this state, the transaction is being executed. This is the initial state of every transaction.
Partially Committed –
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 −
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:
A DBMS is single-user means one user can use the system at a time.
Services services
Presentation services display forms, handles flow of information to/ from screen.
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
Services services
DBMS
Services services
Department of BCA. 68 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
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);
Y:=Y+N;
Write_item(X);
Write_item(Y);
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.
Ex:
T1 T3
Sum:=0;
Read_item(A);
Sum:=sum+A;
Read_item (X);
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.
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.
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.
• 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.
❖ 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.
Department of BCA. 73 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
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.
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.
A tuple is a collection of attribute values. (or) A single row in the table is called as tuple.
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:
Cell:
Relation:
Department of BCA. 75 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
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
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.
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.
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.
❖ Create
❖ Alter
❖ Drop
❖ Truncate
• Create Command:
It is used to create the definition of a new relation.
);
• Drop Command:
This command is used to delete unwanted tables from the database.
• Alter Command:
The alter command can be used on a table to modify the table.
Department of BCA. 78 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
o Add option:
Using this we can add column or primary key to the existing table.
o Drop option:
Using this we can drop a particular column from the table.
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.
o Rename option:
Using this we can rename the table name as well as column name in a table.
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.
Department of BCA. 79 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
❖ 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);
Syntax 2:
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:
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:
• 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:
Where <Condition>;
• Update Command:
This command is used update or modifies the contents of a table or relation.
Syntax:
where condition ;
• Commit:
This command is used to save the changes made by the transactions on
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;
• 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).
• 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:
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.
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:
Example:
CREATE TABLE Persons
(
PId number(10) unique,
LastName varchar2(25) unique,
FirstName varchar2(25),
Address varchar2(25),
City varchar2(25) unique
);
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.
• 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.
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:
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.
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) );
• 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:
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.
• 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.
Syntax:
Department of BCA. 87 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
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.
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.
• 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
LOWER ( ) FUNCTION:
UPPER ( ) FUNCTION:
Department of BCA. 89 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
INITCAP ( ) FUNCTION:
It converts character values to upper case for the first letter of each word rest of the letters
are in lowercase.
SUBSTR ( ) FUNCTION:
LENGTH ( ) FUNCTION:
INSTR ( ) FUNCTION:
LPAD ( ) FUNCTION:
TRIM ( ) FUNCTION:
Department of BCA. 90 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
REPLACE ( ) FUNCTION:
This function is used to replace a new word in the place of existing word in a given string.
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.
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.
MOD ( ) FUNCTION:
CEIL ( ) FUNCTION:
It is used to display positive value to be incremented by 1 and negative value display only
integer part.
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.
SQRT ( ) FUNCTION:
Date functions
SYSDATE () function:
Ex: we can display the current date by selecting Sysdate function from dual table is
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.
Department of BCA. 92 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
• MAX( ) :
• MIN( ) :
• AVG() :
• SUM() :
• COUNT() :
This function is used to Returns the numbers of rows present in the table.
• FIRST() :
• LAST() :
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.
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.
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.
SELECT column name1, column name2 …. FROM table name Where condition
ORDER BY column name1, column name2… ASC|DESC;
We can divide the table of information into smaller groups using the GROUP BY
clause.
SELECT column name, aggregate function (column name) FROM table name
WHERE condition
Department of BCA. 95 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
The HAVING clause was added to SQL because the WHERE keyword could not be used
with aggregate functions.
SELECT column name, aggregate function (column name) FROM table name
WHERE condition
GROUP BY column name
HAVING aggregate function(column name) operator value;
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.
SET operators
Set operators combine the results of 2 queries into a single result. Queries contains set operators
are called compound queries.
❖ 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.
Department of BCA. 97 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
1000 aaa 10
2000 bbb 10
5000 eee 30
3000 ccc 20
4000 ddd 20
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.
Example
Here employee table has 5 rows and depart table has 4 rows.thr result is 5 X 4=20 rows.
Department of BCA. 99 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
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.
We include <, >, <=, >=, like, in and between operators along with it.
Example:
Natural Join
The Natural join is same as our Equi join but only the difference is it will restrict to display
redundant values.
Example
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.
The left outer join displays all the rows from the left table even if there are no matches in the right
table.
Example
The right outer join displays all the rows from the right table even if there are no matches in the
left table.
Example:
Full Outer Join displays all the matching and non matching rows from both the tables.
Union
Example
union
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:
Example:
SQL INDEX
Definition:
Indexes are optional structures associated with tables to speed query execution and
provide uniqueness.
Use of indexes:
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.
Syntax:
Syntax:
We can drop (or) remove the index from the data base.
Syntax:
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.
Example:
Create view empview(empid,empname) as select (empno,ename) from employee;
Once we insert data into employee table automatically updates the view also.
Advantages of views:
Sub Queries
It is also referred as inner query or nested query. Sub queries are alternate way of returning data
from multiple tables.
First evaluate the sub query and then run the main 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.
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:
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.
Better performance:
PL/SQL engine processes multiple SQL statements simultaneously as a single block. it
improves performance.
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
• Named blocks
• Anonymous (or) Unnamed blocks
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
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;
PL/SQL Architecture
Comments in PL/SQL
• Second way is by starting the comment with /* and ending it with */.
Example: /* this comment can be of multiple lines */
PL/SQL provides a variety of predefined data types, which can be divided into three categories:
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 ( := )
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.
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.
PL/SQL Execution
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.
Flow chart:
IF-THEN-ELSE statement
Department of BCA. 117 | P a g e
Database Management Systems. Seshadripuram College, Tumkur.
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;
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.
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:
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;
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.
CASE selector
WHEN 'value1' THEN Statement1;
WHEN 'value2' THEN Statement2;
WHEN 'value3' THEN Statement3;
...
ELSE Statement n; -- default case
END CASE;
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;
LOOP
Sequence of statements;
END LOOP;
Example:
DECLARE
N number:=1;
Begin
Loop
Dbms_output.put_line(n);
N:=n+1;
Exit when n>5;
End loop;
End;
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;
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;
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
END LOOP;
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.
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;
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;
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;
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;
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;
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
Where,
procedure-name specifies the name of the procedure.
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.
The AS keyword is used instead of the IS keyword for creating a standalone procedure.
declare
c number;
begin
add1(10,20,c);
dbms_output.put_line ('sum is' ||c);
end;
This program finds the minimum of two values, here procedure takes two numbers using IN
mode and returns their minimum using OUT parameters.
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.
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.
RETURN clause specifies that data type you are going to return from the function.
The AS keyword is used instead of the IS keyword for creating a standalone function.
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.
Packages
Definition:
These are having 2 parts. They are specification and body of the package.
Syntax:
procedure proc1;
end pack1;
Package created.
The next step we will write the code for body of the package.
procedure proc1 as
begin
end proc1;
begin
end fun1;
end pack1;
FUN1
--------------------------------------------------------------------------------
Cursors
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.
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
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
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:
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.
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:
Triggers could be defined on the table, view, schema, or database with which the event is
associated.
Benefits of Triggers
Auditing
Trigger body
END;
Where,
• CREATE [OR REPLACE] TRIGGER trigger_name: Creates or replaces an existing
trigger with the trigger_name.
• {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.