0% found this document useful (0 votes)
10 views97 pages

RDBMS

The document provides an overview of Database Management Systems (DBMS), detailing their purpose, applications, and the advantages of relational databases over file-oriented systems. It discusses various issues related to data management, such as redundancy, isolation, integrity, and security problems, while also introducing different database models including hierarchical, network, and relational models. Additionally, it outlines the role of a Database Administrator (DBA) and the architecture of database systems influenced by computer networking and distribution.

Uploaded by

rajannad99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views97 pages

RDBMS

The document provides an overview of Database Management Systems (DBMS), detailing their purpose, applications, and the advantages of relational databases over file-oriented systems. It discusses various issues related to data management, such as redundancy, isolation, integrity, and security problems, while also introducing different database models including hierarchical, network, and relational models. Additionally, it outlines the role of a Database Administrator (DBA) and the architecture of database systems influenced by computer networking and distribution.

Uploaded by

rajannad99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

[

Relational database management systems


rajanna ]

B Com- III yr / VI-Sem

2
[
Relational database management systems
rajanna ]

Unit-I
DBMS (Database Management Systems)
Introduction:

A database-management system (DBMS) is a collection of interrelated data and a set of


programs to access those data. The collection of data, usually referred to as the database, contains
information relevant to an enterprise. The primary goal of a DBMS is to provide a way to store and
retrieve database information that is both convenient and efficient.
Database systems are designed to manage large bodies of information. Management of data
involves both defining structures for storage of information and providing mechanisms for the
manipulation of information. In addition, the database system must ensure the safety of the information
stored, despite system crashes or attempts at unauthorized access. If data are to be shared among
several users, the system must avoid possible anomalous results. Because information is so important
in most organizations, computer scientists have developed a large body of concepts and techniques for
managing data.

Database-System Applications:

1. Enterprise Information

◦ Sales: For customer, product, and purchase information.


◦ Accounting: For payments, receipts, account balances, assets and other accounting
information.
◦ Human resources: For information about employees, salaries, payroll taxes, and benefits,
and for generation of pay checks.
◦ Manufacturing: For management of the supply chain and for tracking production of
items in factories, inventories of items in warehouses and stores, and
orders for items.
◦ Online retailers: For sales data noted above plus online order tracking, generation of
recommendation lists, and maintenance of online product evaluations.
2. Banking and Finance:
◦ Banking : For customer information, accounts, loans, and banking transactions.
◦ Credit card transactions: For purchases on credit cards and generation of
monthly statements.
◦ Finance: For storing information about holdings, sales, and purchases of
financial instruments such as stocks and bonds; also for storing real-time
market data to enable online trading by customers and automated trading
by the firm.
3. Universities: For student information, course registrations, and grades (in addition to standard
enterprise information such as human resources and accounting).
4. Airlines: For reservations and schedule information. Airlines were among the
first to use databases in a geographically distributed manner.
5. Telecommunication: For keeping records of calls made, generating monthly bills, maintaining
balances on prepaid calling cards, and storing information about the
communication networks.

3
[
Relational database management systems
rajanna ]

Purpose of Database Systems:

Data redundancy and inconsistency: Since different programmers create the files and application
programs over a long period, the various files are likely to have different structures and the programs
may be written in several programming languages. Moreover, the same information may be duplicated
in several places (files).

Data isolation: Because data are scattered in various files, and files may be in different formats,
writing new application programs to retrieve the appropriate data is difficult.
Integrity problems: The data values stored in the database must satisfy certain types of consistency
constraints. Suppose the university maintains an account for each department, and records the balance
amount in each account. Suppose also that the university requires that the account balance of a
department may never fall below zero. Developers enforce these constraints in the system by adding
appropriate code in the various application programs.

Concurrent-access anomalies: For the sake of overall performance of the system and faster response,
many systems allow multiple users to update the data simultaneously. Indeed, today, the largest Internet
retailers may have millions of accesses per day to their data by shoppers. In such an environment,
interaction of concurrent updates is possible and may result in inconsistent data.
Security problems: Not every user of the database system should be able to access all the data. For
example, in a university, payroll personnel need to see only that part of the database that has financial
information. They do not need access to information about academic records. But, since application
programs are added to the file-processing system in an ad hoc manner, enforcing such security
constraints is difficult.
FILE ORIENTED APPROACH:
The earliest business computer systems were used to process business records and produce
information. They were generally faster and more accurate than equivalent manual systems. These
systems stored groups of records in separate files, and so they were called file
Processing systems.
1. File system is a collection of data. Any management with the file system, user has to write
the procedures
[Link] system gives the details of the data representation and Storage of data.
[Link] File system storing and retrieving of data cannot be done efficiently.
4. Concurrent access to the data in the file system has many problems like a Reading the file
while other deleting some information, updating some information
[Link] system doesn’t provide crash recovery mechanism.

Eg. While we are entering some data into the file if System crashes then content of the file is
lost.

6. Protecting a file under file system is very difficult.


The typical file-oriented system is supported by a conventional operating system. Permanent
records are stored in various files and a number of different application programs are written to extract
records from and add records to the appropriate files.

4
[
Relational database management systems
rajanna ]

ADVANTAGES OF FILE-ORIENTED SYSTEM:


The following are the disadvantages of File-Oriented System:
Data Redundancy and Inconsistency:
Since files and application programs are created by different programmers over a long period of
time, the files are likely to be having different formats and the programs may be written in several
programming languages. Moreover, the same piece of information may be duplicated in several places.
This redundancy leads to higher storage and access cost. In addition, it may lead to data inconsistency.
Difficulty in Accessing Data:
The conventional file processing environments do not allow needed data to be retrieved in a
convenient and efficient manner. Better data retrieval system must be developed for general use.
Data Isolation:
Since data is scattered in various files, and files may be in different formats, it is difficult to
write new application programs to retrieve the appropriate data.
Concurrent Access Anomalies:
In order to improve the overall performance of the system and obtain a faster response time,
many systems allow multiple users to update the data simultaneously. In such an environment,
interaction of concurrent updates may result in inconsistent data.
Security Problems:
Not every user of the database system should be able to access all the data. For example, in
banking system, payroll personnel need only that part of the database that has information about
various bank employees. They do not need access to information about customer accounts. It is
difficult to enforce such security constraints.
Integrity Problems:
The data values stored in the database must satisfy certain types of consistency constraints. For
example, the balance of a bank account may never fall below a prescribed amount. These constraints
are enforced in the system by adding appropriate code in the various application programs. When new
constraints are added, it is difficult to change the programs to enforce them. The problem is
compounded when constraints involve several data items for different files.
Atomicity Problem:
A computer system like any other mechanical or electrical device is subject to failure. In many
applications, it is crucial to ensure that once a failure has occurred and has been detected, the data are
restored to the consistent state existed prior to the failure
Example:
Consider part of a savings-bank enterprise that keeps information about all customers and
savings accounts. One way to keep the information on a computer is to store it in operating system
files. To allow users to manipulate the information, the system has a number of application programs
that manipulate the files, including:
 A program to debit or credit an account
 A program to add a new account
 A program to find the balance of an account
 A program to generate monthly statements

5
[
Relational database management systems
rajanna ]

Programmers wrote these application programs to meet the needs of the bank. New application
programs are added to the system as the need arises. For example, suppose that the savings bank
decides to offer checking accounts.
As a result, the bank creates new permanent files that contain information about all the checking
accounts maintained in the bank, and it may have to write new application programs to deal with
situations that do not arise in savings accounts, such as overdrafts. Thus, as time goes by, the system
acquires more files and more application programs. The system stores permanent records in various
files, and it needs different
Application programs to extract records from, and add records to, the appropriate files. Before
database management systems (DBMS) came along, organizations usually stored information in such
systems. Organizational information in a file-processing system has a number of major disadvantages:
1. Data Redundancy and Inconsistency:
The address and telephone number of a particular customer may appear in a file that consists of
savings-account records and in a file that consists of checking-account records. This redundancy leads
to higher storage and access cost. In, it may lead to data inconsistency; that is, the various copies of the
same data may no longer agree. For example, a changed customer address may be reflected in savings-
account records but not elsewhere in the system.
2. Difficulty in Accessing Data:
Suppose that one of the bank officers needs to find out the names of all customers who live
within a particular postal-code area. The officer asks the data-processing department to generate such a
list. Because there is no application program to generate that. The bank officer has now two choices:
either obtain the list of all customers and extract the needed information manually or ask a system
programmer to write the necessary application program. Both alternatives are obviously unsatisfactory.
3. Data Isolation:
Because data are scattered in various files and files may be in different formats, writing new
application programs to retrieve the appropriate data is difficult.
4. Integrity Problems:
The balance of a bank account may never fall below a prescribed amount (say, $25).
Developers enforce these constraints in the system by adding appropriate code in the various
application programs. However, when new constraints are added, it is difficult to change the programs
to enforce them. The problem is compounded when constraints involve several data items from
different files.
5. Atomicity Problems:
A computer system, like any other mechanical or electrical device, is subject to failure. In many
applications, it is crucial that, if a failure occurs, the data be restored to the consistent state that existed
prior to the failure. Consider a program to transfer $50 from account A to account B. If a system failure
occurs during the execution of the program, it is possible that the $50 was removed from account A but
was not credited to account B, resulting in an inconsistent database state. Clearly, it is essential to
database consistency that either both the credit and debit occur, or that neither occur. That is, the funds
transfer must be atomic—it must happen in its entirety or not at all. It is difficult to ensure atomicity in
a conventional file-processing system.
6. Concurrent-Access Anomalies:
For the sake of overall performance of the system and faster response, many systems allow
multiple users to update the data simultaneously. In such an environment, interaction of concurrent
6
[
Relational database management systems
rajanna ]

updates may result in inconsistent data. Consider bank account A, containing $500. If two customers
withdraw funds (say $50 and $100 respectively) from account A at about the same time, the result of
the concurrent executions may leave the account in an incorrect (or inconsistent) state. Suppose that the
programs executing on behalf of each withdrawal read the old balance, reduce that value by the amount
being withdrawn, and write the result back. If the two programs run concurrently, they may both read
the value $500, and write back $450 and $400, respectively. Depending on which one writes the value
last, the account may contain $450 or $400, rather than the correct value of $350.
To guard against this possibility, the system must maintain some form of supervision. But
supervision is difficult to provide because data may be accessed by many different application
programs that have not been coordinated previously.
7. Security Problems:
Not every user of the database system should be able to access all the data. For example, in a
banking system, payroll personnel need to see only that part of the database that has information about
the various bank employees. They do not need access to information about customer accounts. But,
since application programs are added to the system in an ad hoc manner, enforcing such security
constraints is difficult. These difficulties, among others, prompted the development of database
systems.
The data models are as follows:

[Link] database model


[Link] Database model
[Link] Database model
File Management System:
The file management system also called as FMS in short is one in which all data is stored on a
single large file. The main disadvantage in this system is searching a record or data takes a long time.
This lead to the introduction of the concept, of indexing in this system. Then also the FMS system had
lot of drawbacks to name a few like updating or modifications to the data cannot be handled easily,
sorting the records took long time and so on. All these drawbacks led to the introduction of the
Hierarchical Database System.
Hierarchical Database model:
The previous system FMS drawback of accessing records and sorting records which took a
long time was removed in this by the introduction of parent-child relationship between records in
database. The origin of the data is called the root from which several branches have data at different
levels and the last level is called the
leaf. The main drawback in this was if there is any modification or addition made to the structure then
the whole structure needed alteration which made the task a tedious one. In order to avoid this next
system took its origin which is called as the Network Database System.

7
[
Relational database management systems
rajanna ]

Fig: Hierarchical Database model

Network Database model:

In this the main concept of many-many relationships got introduced. But this also followed the
same technology of pointers to define relationships with a difference in this made in the introduction if
grouping of data items as sets.

Relational Database model:

In order to overcome all the drawbacks of the previous systems, the Relational Database
System got introduced in which data get organized as tables and each record forms a row with many
fields or attributes in it. Relationships between tables are also formed in this system.

8
[
Relational database management systems
rajanna ]

Database Administrator:
One of the main reasons for using DBMSs is to have central control of both the data and the
programs that access those data. A person who has such central control over the system is called a
database administrator (DBA).
Database Administrator Functions/Roles:
• Schema definition. The DBA creates the original database schema by executing a set of data
definition statements in the DDL.
• Storage structure and access-method definition.
• Schema and physical-organization modification. TheDBAcarries out changes to the schema and
physical organization to reflect the changing needs of the organization, or to alter the physical
organization to improve performance.
• Granting of authorization for data access. By granting different types of authorization, the
database administrator can regulate which parts of the database various users can access. The
authorization information is kept in a
special system structure that the database system consults whenever someone attempts to access the
data in the system.
• Routine maintenance. Examples of the database administrator’s routine maintenance activities are:
◦ Periodically backing up the database, either onto tapes or onto remote servers, to prevent loss of data
in case of
disasters such as flooding.
◦ Ensuring that enough free disk space is available for normal operations, and upgrading disk space as
required.
◦ Monitoring jobs running on the database and ensuring that performan
Database Architecture:
The architecture of a database system is greatly influenced by the underlying computer system
on which it runs, in particular by such aspects of computer architecture as networking, parallelism, and
distribution:

•Networking of computers allows some tasks to be executed on a server system and some tasks to be
executed on client systems. This division of work has led to client –server database systems.

•Parallel processing within a computer system allows database-system activities to be speeded up,
allowing faster response to transactions, as well as more transactions per second. Queries can be
processed in a way that exploits the parallelism offered by the underlying computer system. The need
for parallel query processing has led to parallel database systems.

•Distributing data across sites in an organization allows those data to reside where they are generated
or most needed, but still to be accessible from other sites and from other departments. Keeping
multiple copies of the database across different sites also allows large organizations to continue their
database operations even when one site is affected by a natural disaster, such as flood, fire, or
earthquake. Distributed database systems handle geographically or administratively
9
[
Relational database management systems
rajanna ]

distributed data spread across multiple database systems.


Database applications are usually partitioned into two or three parts, as in below Figure.

In two-tier architecture, the application resides at the client machine, where it invokes database
system functionality at the server machine through query language statements. Application program
interface standards are used for interaction between the client and the server.
In contrast, in three-tier architecture, the client machine acts as merely a front end and does not
contain any direct database calls. Instead, the client end communicates with an application server,
usually through a forms interface. The application server in turn communicates with a database system
to access data. Three-tier applications are more appropriate for large applications, and for applications
that run on the World Wide Web.
Three Level Architecture:
For the system to be usable, it must retrieve data efficiently. The need for efficiency has led
designers to use complex data structures to represent data in the database. Since many database-system
users are not computer trained, developers hide the complexity from users through several levels of
abstraction, to simplify users interactions with the system:
• Physical level. The lowest level of abstraction describes how the data are actually stored.
The physical level describes complex low-level data structures in detail.
• Logical level. The next-higher level of abstraction describes what data are stored in the database, and
what relationships exist among those data. The logical level thus describes the entire database in terms
of a small number of relatively simple structures. Although implementation of the simple structures at
the logical level may involve complex physical-level structures, the user of the logical level does not
need to be aware of this complexity. This is referred to as physical data independence. Database
administrators, who must decide what information to keep in the database, use the logical level of
abstraction.
• View level. The highest level of abstraction describes only part of the entire database. Even though
the logical level uses simpler structures, complexity remains because of the variety of information
stored in a large database. Many users of the database system do not need all this information; instead,
they need to access only a part of the database. The view level of abstraction exists to simplify their
interaction with the system. The system may provide many views for the same database.

Figure: The three levels of data abstract

10
[
Relational database management systems
rajanna ]

Relational and ER Models:


The Concept of Keys:
A key is an attribute of a table which helps to identify a row. There can be many different types of keys
which are explained here.
Super Key or Candidate Key: It is such an attribute of a table that can uniquely identify a row in a
table. Generally they contain unique values and can never contain NULL values. There can be more
than one super key or candidate key in a table e.g. within a STUDENT table Roll and Mobile No. can
both serve to uniquely identify a student.
Primary Key: It is one of the candidate keys that are chosen to be the identifying key for the entire
table. E.g. although there are two candidate keys in the STUDENT table, the college would obviously
use Roll as the primary key of the table.
Alternate Key: This is the candidate key which is not chosen as the primary key of the table. They are
named so because although not the primary key, they can still identify a row.
Composite Key: Sometimes one key is not enough to uniquely identify a row. E.g. in a single class
Roll is enough to find a student, but in the entire school, merely searching by the Roll is not enough,
because there could be 10 classes in the school and each one of them may contain a certain roll no 5.
To uniquely identify the student we have to say something like “class VII, roll no 5”. So, a
combination of two or more attributes is combined to create a unique combination of values, such as
Class + Roll.
Foreign Key: Sometimes we may have to work with an attribute that does not have a primary key of
its own. To identify its rows, we have to use the primary attribute of a related table. Such a copy of
another related table’s primary key is called foreign key.
Constraints
Every relation has some conditions that must hold for it to be a valid relation. These conditions are
called Relational Integrity Constraints. There are three main integrity constraints −

 Key constraints
 Domain constraints
 Referential integrity constraints
Key Constraints:
There must be at least one minimal subset of attributes in the relation, which can identify a tuple
uniquely. This minimal subset of attributes is called key for that relation. If there are more than one
such minimal subsets, these are called candidate keys.
Key constraints force that −
 in a relation with a key attribute, no two tuples can have identical values for key attributes.

11
[
Relational database management systems
rajanna ]

 a key attribute can not have NULL values.


Key constraints are also referred to as Entity Constraints.
Domain Constraints:
Attributes have specific values in real-world scenario. For example, age can only be a positive integer.
The same constraints have been tried to employ on the attributes of a relation. Every attribute is bound
to have a specific range of values. For example, age cannot be less than zero and telephone numbers
cannot contain a digit outside 0-9.
Referential integrity Constraints
Referential integrity constraints work on the concept of Foreign Keys. A foreign key is a key attribute
of a relation that can be referred in other relation.
Referential integrity constraint states that if a relation refers to a key attribute of a different or same
relation, then that key element must exist.
Relational Set Operators:
Relational Set Operators uses relational algebra to manipulate contents in a database. All
together there are eight different types of operators. These operators are SQL commands.

UNION: It combines all of the rows in one table with all of the rows in another table except for the
duplicate tuples. The tables are required to have the same attribute characteristics for the Union
command to work. The tables must be union-compatible which means that two tables being used have
the same amount of columns and the columns have the same names, and also need to share the same
domain.
INTERSECT is the second SQL command that takes two tables and combines only the rows that
appear in both tables. The tables must be union-compatible to be able to use the Intersect command or
else it won't work.
DIFFERENCE in another SQL command that gets all rows in one table that are not found in the other
table. Basically it subracts one table from the other table to leave only the attributes that are not the
same in both tables. For this command to work both tables must be union-compatible.
PRODUCT command would show all possible pairs of rows from both tables being used. This
command can also be referred to as the Cartesian Product.
SELECT is the command to show all rows in a table. It can be used to select only specific data from
the table that meets certain criteria. This command is also referred to as the Restrict command.
PROJECT is the command that gives all values for certian attributes specified after the command. It
shows a vertical view of the given table.
JOIN takes two or more tables and combines them into one table. This can be used in combination
with other commands to get specific information. There are several types of the Join command. The
Natural Join, Equijion, Theta Join, Left Outer Join and Right Outer Join.
DIVIDE has specific requirements of the table. One of the tables can only have one column and the
other table must have two columns only.
The Entity-Relationship Model:
The entity-relationship (E-R) data model uses a collection of basic objects, called entities, and
relationships among these objects. An entity is a “thing” or “object” in the real world that is
distinguishable from other objects. For example, each person is an entity, and bank accounts can be
considered as entities.
Entities are described in a database by a set of attributes. For example, the attributes dept
name, building, and budget may describe one particular department in a university, and they form

12
[
Relational database management systems
rajanna ]

attributes of the department entity set. Similarly, attributes ID, name, and salary may describe an
instructor entity.
Arelationship is an association among several entities. For example, a member relationship
associates an instructor with her department. The set of all entities of the same type and the set of all
relationships of the same type are termed an entity set and relationship set, respectively.
The overall logical structure (schema) of a database can be expressed graphically by an entity-
relationship (E-R) diagram. There are several ways in which to draw these diagrams. One of the most
popular is to use the Unified Modeling Language (UML). In the notation we use, which is based on
UML, an E-R diagram is represented as follows:

1. Entity
An entity is an object or component of data. An entity is represented as rectangle in an ER diagram.
For example: In the following ER diagram we have two entities Student and College and these two
entities have many to one relationship as many students study in a single college. We will read more
about relationships later, for now focus on entities.
Strong Entity: which is an entity that cannot depend others so it is called strong [Link] is represented
by rectangle box.

Ex:
ENTITY

Weak Entity:
An entity that cannot be uniquely identified by its own attributes and relies on the relationship
with other entity is called weak entity. The weak entity is represented by a double rectangle.
For example – a bank account cannot be uniquely identified without knowing the bank to which the
account belongs, so bank account is a weak entity.

WEAKENTITY

Attributes:
Entities are represented by means of their properties, called attributes. All attributes have
values.
For example, a student entity may have name, class, and age as attributes. they are represented by
Ellipse of oval
There exists a domain or range of values that can be assigned to attributes. For example, a
student's name cannot be a numeric value. It has to be alphabetic. A student's age cannot be negative,
etc.
Types of Attributes
 Simple attribute − Simple attributes are atomic values, which cannot be divided further.
For example, a student's phone number is an atomic value of 10 digits.
9441170915
13
[
Relational database management systems
rajanna ]

 Composite attribute − Composite attributes are made of more than one simple attribute.
For example, a student's complete name may have first_name and last_name.

 Derived attribute − Derived attributes are the attributes that do not exist in the physical
database, but their values are derived from other attributes present in the database.
For example, average_salary in a department should not be saved directly in the
database, instead it can be derived. For another example, age can be derived from
data_of_birth.

 Multi-value attribute − Multi-value attributes may contain more than one values.
For example, a person can have more than one phone number, email_address, etc.

Relationship:
The association among entities is called a relationship. For example, an employee works_at a
department, a student enrolls in a course. Here, Works_at and Enrolls are called relationships.
Relationship Set:
A set of relationships of similar type is called a relationship set. Like entities, a relationship too
can have attributes. These attributes are called descriptive attributes.
Degree of Relationship:
Degree of Relationship:
The number of participating entities in a relationship defines the degree of the relationship.

 Binary = degree 2
 Ternary = degree 3
 n-ary = degree
Mapping Cardinalities
Cardinality defines the number of entities in one entity set, which can be associated with the number
of entities of other set via relationship set.
 One-to-one − One entity from entity set A can be associated with at most one entity of entity
set B and vice versa.

14
[
Relational database management systems
rajanna ]

 One-to-many − One entity from entity set A can be associated with more than one entities of
entity set B however an entity from entity set B, can be associated with at most one entity.

 Many-to-one − More than one entities from entity set A can be associated with at most one
entity of entity set B, however an entity from entity set B can be associated with more than one
entity from entity set A.

 Many-to-many − One entity from A can be associated with more than one entity from B and
vice versa.

15
[
Relational database management systems
rajanna ]

ER Diagram Representation

Let us now learn how the ER Model is represented by means of an ER diagram. Any object, for
example, entities, attributes of an entity, relationship sets, and attributes of relationship sets, can be
represented with the help of an ER diagram.

Entity:
Entities are represented by means of rectangles. Rectangles are named with the entity set they
represent.

Attributes
Attributes are the properties of entities. Attributes are represented by means of ellipses. Every ellipse
represents one attribute and is directly connected to its entity (rectangle).

If the attributes are composite, they are further divided in a tree like structure. Every node is then
connected to its attribute. That is, composite attributes are represented by ellipses that are connected
with an ellipse.

16
[
Relational database management systems
rajanna ]

Multivalued attributes are depicted by double ellipse.

17
[
Relational database management systems
rajanna ]

Derived attributes are depicted by dashed ellipse.

Relationship:
Relationships are represented by diamond-shaped box. Name of the relationship is written inside the
diamond-box. All the entities (rectangles) participating in a relationship, are connected to it by a line.
Binary Relationship and Cardinality:
A relationship where two entities are participating is called a binary relationship. Cardinality is the
number of instance of an entity from a relation that can be associated with the relation.
 One-to-one − When only one instance of an entity is associated with the relationship, it is
marked as '1:1'. The following image reflects that only one instance of each entity should be
associated with the relationship. It depicts one-to-one relationship.

18
[
Relational database management systems
rajanna ]

 One-to-many − When more than one instance of an entity is associated with a relationship, it
is marked as '1:N'. The following image reflects that only one instance of entity on the left and
more than one instance of an entity on the right can be associated with the relationship. It
depicts one-to-many relationship.

 Many-to-one − When more than one instance of entity is associated with the relationship, it is
marked as 'N:1'. The following image reflects that more than one instance of an entity on the
left and only one instance of an entity on the right can be associated with the relationship. It
depicts many-to-one relationship.

 Many-to-many − The following image reflects that more than one instance of an entity on the
left and more than one instance of an entity on the right can be associated with the
relationship. It depicts many-to-many relationship.

19
[
Relational database management systems
rajanna ]

Generalization Aggregation
The ER Model has the power of expressing database entities in a conceptual hierarchical
manner. As the hierarchy goes up, it generalizes the view of entities, and as we go deep in the
hierarchy, it gives us the detail of every entity included.
Going up in this structure is called generalization, where entities are clubbed together to represent a
more generalized view. For example, a particular student named Mira can be generalized along with
all the students. The entity shall be a student, and further, the student is a person. The reverse is
called specialization where a person is a student, and that student is Mira.
Generalization:
As mentioned above, the process of generalizing entities, where the generalized entities contain the
properties of all the generalized entities, is called generalization. In generalization, a number of
entities are brought together into one generalized entity based on their similar characteristics. For
example, pigeon, house sparrow, crow and dove can all be generalized as Birds.

Specialization: Specialization is the opposite of generalization. In specialization, a


group of entities is divided into sub-groups based on their characteristics. Take
a group ‘Person’ for example. A person has name, date of birth, gender, etc.
These properties are common in all persons, human beings. But in a company,
persons can be identified as employee, employer, customer, or vendor, based
on what role they play in the company.
Similarly, in a school database, persons can be specialized as teacher, student, or a staff, based on
what role they play in school as entities.

ER Model: when conceptualized into diagrams, gives a good overview of entity-relationship,


which is easier to understand. ER diagrams can be mapped to relational schema, that is, it is possible
to create relational schema using ER diagram. We cannot import all the ER constraints into relational
model, but an approximate schema can be generated.

20
[
Relational database management systems
rajanna ]

There are several processes and algorithms available to convert ER Diagrams into Relational Schema.
Some of them are automated and some of them are manual. We may focus here on the mapping
diagram contents to relational basics.
ER diagrams mainly comprise of −

 Entity and its attributes


 Relationship, which is association among entities.

Mapping Entity:
An entity is a real-world object with some attributes.

Mapping Process (Algorithm)

 Create table for each entity.


 Entity's attributes should become fields of tables with their respective data types.
 Declare primary key.
Mapping Relationship:
A relationship is an association among entities.

Unit-II
Relational Database Integrity:

21
[
Relational database management systems
rajanna ]

The keys:
Types of keys in DBMS
Primary Key – A primary is a column or set of columns in a table that uniquely identifies tuples
(rows) in that table.

Super Key – A super key is a set of one of more columns (attributes) to uniquely identify rows in a
table.

Candidate Key – A super key with no redundant attribute is known as candidate key

Alternate Key – Out of all candidate keys, only one gets selected as primary key, remaining keys are
known as alternate or secondary keys.

Composite Key – A key that consists of more than one attribute to uniquely identify rows (also known
as records & tuples) in a table is called composite key.

Foreign Key – Foreign keys are the columns of a table that points to the primary key of another table.
They act as a cross-reference between tables.

constraints/key constraints:-
Entity integrity constraints / key constraints specifies the condition and restricts the data that
can be stored, only in one table.
Definitions :
Key constraint: a key constraint is a statement that a certain minimal fields of a relation has a
unique identifier for all tuples. Actually key constraint is the general term, the term candidate key is
used for satisfying the constraints according to a key constraints.

Database Schema:
When we talk about a database, we must differentiate between the database schema, which is
the logical design of the database, and the database instance, which is a snapshot of the data in the
database at a given instant in time. The concept of a relation corresponds to the programming-language
notion of a variable, while the concept of a relation schema corresponds to the
programming language notion of type definition.
In general, a relation schema consists of a list of attributes and their corresponding domains.
We shall not be concerned about the precise definition of the domain of each attribute until we discuss
the QL language. The concept of a relation instance corresponds to the programming-language notion
of a value of a variable. The value of a given variable may change with time; similarly the contents of a
relation instance may change with time as the relation is updated.
Ex: 1. Student (sno,rollno,std_name,addr).
2. Department (dept_name, building, budget).
Schema Diagrams:
A database schema, along with primary key and foreign key dependencies, can be depicted by
schema diagrams. Figure 2.8 shows the schema diagram for our university organization. Each relation
appears as a box, with the relation name at the top in blue, and the attributes listed inside the box.
Primary key attributes are shown underlined. Foreign key dependencies appear as arrows from the
foreign key attributes of the referencing relation to the primary key of the referenced relation.
Relational Query Languages:
22
[
Relational database management systems
rajanna ]

A query language is a language in which a user requests information from the database. These
languages are usually on a level higher than that of a standard programming language. Query
languages can be categorized as either procedural or nonprocedural. In a procedural language, the user
instructs the system to perform a sequence of operations on the database to compute the desired result.
In a nonprocedural language, the user describes the desired information without giving a specific
procedure for obtaining that information.
Ex:
1. Classroom (building, room number, capacity)
2. Department (dept name, building, budget)
3. course (course id, title, dept name, credits)
4. instructor ( ID, name, dept name, salary)
5. section (course id, sec id, semester, year, building, room number, time slot id)
6. teaches ( ID, course id, sec id, semester, year)
7. student ( ID, name, dept _name, tot_ credit)
DBMS - Normalization
Functional Dependency:
Functional dependency (FD) is a set of constraints between two attributes in a relation.
Functional dependency says that if two tuples have same values for attributes A1, A2,..., An, then
those two tuples must have to have same values for attributes B1, B2, ..., Bn.
Functional dependency is represented by an arrow sign (→) that is, X→Y, where X functionally
determines Y. The left-hand side attributes determine the values of attributes on the right-hand side.
Armstrong's Axioms:
If F is a set of functional dependencies then the closure of F, denoted as F +, is the set of all functional
dependencies logically implied by F. Armstrong's Axioms are a set of rules, that when applied
repeatedly, generates a closure of functional dependencies.
 Reflexive rule − If alpha is a set of attributes and beta is_subset_of alpha, then alpha holds
beta.
 Augmentation rule − If a → b holds and y is attribute set, then ay → by also holds. That is
adding attributes in dependencies, does not change the basic dependencies.
 Transitivity rule − Same as transitive rule in algebra, if a → b holds and b → c holds, then a
→ c also holds. a → b is called as a functionally that determines b.
Trivial Functional Dependency:
 Trivial − If a functional dependency (FD) X → Y holds, where Y is a subset of X, then it is
called a trivial FD. Trivial FDs always hold.
 Non-trivial − If an FD X → Y holds, where Y is not a subset of X, then it is called a non-
trivial FD.
 Completely non-trivial − If an FD X → Y holds, where x intersect Y = Φ, it is said to be a
completely non-trivial FD.

Normalization:
If a database design is not perfect, it may contain anomalies, which are like a bad dream for any
database administrator. Managing a database with anomalies is next to impossible.

23
[
Relational database management systems
rajanna ]

 Update anomalies − If data items are scattered and are not linked to each other properly, then
it could lead to strange situations. For example, when we try to update one data item having its
copies scattered over several places, a few instances get updated properly while a few others
are left with old values. Such instances leave the database in an inconsistent state.
 Deletion anomalies − We tried to delete a record, but parts of it was left undeleted because of
unawareness, the data is also saved somewhere else.
 Insert anomalies − We tried to insert data in a record that does not exist at all.

Normalization is a method to remove all these anomalies and bring the database to a consistent state.
First Normal Form:
First Normal Form is defined in the definition of relations (tables) itself. This rule defines that all the
attributes in a relation must have atomic domains. The values in an atomic domain are indivisible
units.

We re-arrange the relation (table) as below, to convert it to First Normal Form.

Each attribute must contain only a single value from its pre-defined domain.
Second Normal Form:
Before we learn about the second normal form, we need to understand the following −
 Prime attribute − An attribute, which is a part of the candidate-key, is known as a prime
attribute.
 Non-prime attribute − An attribute, which is not a part of the prime-key, is said to be a non-
prime attribute.
If we follow second normal form, then every non-prime attribute should be fully functionally
dependent on prime key attribute. That is, if X → A holds, then there should not be any proper subset
Y of X, for which Y → A also holds true.

24
[
Relational database management systems
rajanna ]

We see here in Student_Project relation that the prime key attributes are Stu_ID and Proj_ID.
According to the rule, non-key attributes, i.e. Stu_Name and Proj_Name must be dependent upon both
and not on any of the prime key attribute individually. But we find that Stu_Name can be identified by
Stu_ID and Proj_Name can be identified by Proj_ID independently. This is called partial
dependency, which is not allowed in Second Normal Form.

We broke the relation in two as depicted in the above picture. So there exists no partial dependency.
Third Normal Form:
For a relation to be in Third Normal Form, it must be in Second Normal form and the following must
satisfy −

 No non-prime attribute is transitively dependent on prime key attribute.


 For any non-trivial functional dependency, X → A, then either −
o X is a superkey or,
o A is prime attribute.

We find that in the above Student_detail relation, Stu_ID is the key and only prime key attribute. We
find that City can be identified by Stu_ID as well as Zip itself. Neither Zip is a superkey nor is City a
prime attribute. Additionally, Stu_ID → Zip → City, so there exists transitive dependency.
To bring this relation into third normal form, we break the relation into two relations as follows −
25
[
Relational database management systems
rajanna ]

Boyce-Codd Normal Form


Boyce-Codd Normal Form (BCNF) is an extension of Third Normal Form on strict terms. BCNF
states that −

 For any non-trivial functional dependency, X → A, X must be a super-key.


In the above image, Stu_ID is the super-key in the relation Student_Detail and Zip is the super-key in
the relation ZipCodes. So,
Stu_ID → Stu_Name, Zip
and
Zip → City
Which confirms that both the relations are in BCNF.

DBMS - File Structure:


Relative data and information is stored collectively in file formats. A file is a sequence of records
stored in binary format. A disk drive is formatted into several blocks that can store records. File
records are mapped onto those disk blocks.
File Organization
File Organization defines how file records are mapped onto disk blocks. We have four types of File
Organization to organize file records −

26
[
Relational database management systems
rajanna ]

Heap File Organization:


When a file is created using Heap File Organization, the Operating System allocates memory area to
that file without any further accounting details. File records can be placed anywhere in that memory
area. It is the responsibility of the software to manage the records. Heap File does not support any
ordering, sequencing, or indexing on its own.
Sequential File Organization:
Every file record contains a data field (attribute) to uniquely identify that record. In sequential file
organization, records are placed in the file in some sequential order based on the unique key field or
search key. Practically, it is not possible to store all the records sequentially in physical form.
Hash File Organization:
Hash File Organization uses Hash function computation on some fields of the records. The
output of the hash function determines the location of disk block where the records are to be placed.
Hash Organization:
 Bucket − A hash file stores data in bucket format. Bucket is considered a unit of storage. A
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 the set of search-
keys K to the address where actual records are placed. It is a function from search keys to
bucket addresses.
Static Hashing
In static hashing, when a search-key value is provided, the hash function always computes the same
address. For example, if mod-4 hash function is used, then it shall generate only 5 values. The output
address shall always be same for that function. The number of buckets provided remains unchanged at
all times.

27
[
Relational database management systems
rajanna ]

Operation:
 Insertion − When a record is required to be entered using static hash, the hash
function h computes the bucket address for search key K, where the record will be stored.
Bucket address = h(K)
 Search − When a record needs to be retrieved, the same hash function can be used to retrieve
the address of the bucket where the data is stored.
 Delete − This is simply a search followed by a deletion operation.

*****

28
[
Relational database management systems
rajanna ]

UNIT III

SQL & TYPES OF SQL COMMANDS


SQL stands for Structured Query Language. SQL is used to communicate with a database.
According to ANSI (American National Standards Institute), it is the standard language for
relational database management systems. SQL statements are used to perform tasks such as
storing data, updating data, deleting data in a database, or retrieve data from a database. Some
common relational database management systems that use SQL are: Oracle, Sybase,
Microsoft SQL Server.
Features:
 SQL is relatively easy to learn.
 Basic command set has a vocabulary of less than 100 words.
 Nonprocedural language.
 ANSI prescribes a standard SQL.
Different types of SQL commands are explained below.
Data Definition Language (DDL) statements are used to define the database structure or
schema. Some examples:
o CREATE - to create objects in the database
o ALTER - alters the structure of the database
o DROP - delete objects from the database
o TRUNCATE - remove all records from a table, including all spaces
allocated for the records are removed
29
[
Relational database management systems
rajanna ]

o COMMENT - add comments to the data dictionary


o RENAME - rename an object
Data Manipulation Language (DML) statements are used for managing data within
schema objects. Some examples:
o SELECT - retrieve data from the a database
o INSERT - insert data into a table
o UPDATE - updates existing data within a table
o DELETE - deletes all records from a table, the space for the records remain
o LOCK TABLE - control concurrency
Data Control Language (DCL) statements. Some examples:
o GRANT - gives user's access privileges to database
o REVOKE - withdraw access privileges given with the GRANT command
Transaction Control (TCL) statements are used to manage the changes made by DML
statements. It allows statements to be grouped together into logical transactions.

30
[
Relational database management systems
rajanna ]

o COMMIT - save work done


o SAVEPOINT - identify a point in a transaction to which you can later roll back
o ROLLBACK - restore database to original since the last COMMIT OPERATORS
AVAILABLE IN SQL
An operator is a reserved word or a character used to perform an operation.
The operators available in SQL are:
 Arithmetic operators
 Comparison operators
 Logical and special operators
Set operators

SQL Arithmetic Operators:
Assume variable a holds 10 and variable b holds 20 then:

Operator Meaning Example

+ Addition a + b will give 30

- Subtraction a - b will give -10

* Multiplication a * b will give 200

/ Division b / a will give 2

% Modulus b % a will give 0

SQL Comparison Operators:


Assume variable a holds 10 and variable b holds 20 then:

Operator Meaning Example

= Equal to (a = b) is not true.

!= Not Equal to. (a != b) is true.

<> Not Equal to. (a <> b) is true.

> Greater than (a > b) is not true.

< Less than (a < b) is true.

>= Less than or equal to (a >= b) is not true.

31
[
Relational database management systems
rajanna ]

<= Greater than or equal to (a <= b) is true.

SQL Logical and special Operators:


Here is a list of all the logical operators available in SQL.

Operator Description

ALL The ALL operator is used to compare a value to all values in


another value set.

AND The AND operator allows the existence of multiple


conditions in an SQL statement's WHERE clause.

ANY The ANY operator is used to compare a value to any


applicable value in the list according to the condition.

BETWEEN The BETWEEN operator is used to search for values that are
within a set of values, given the minimum value and the
maximum value.

EXISTS The EXISTS operator is used to search for the presence of a row
in a specified table that meets certain criteria.

IN The IN operator is used to compare a value to a list of literal


values that have been specified.

LIKE The LIKE operator is used to compare a value to similar values


using wildcard operators.

NOT The NOT operator reverses the meaning of the logical operator
with which it is used. Eg. NOT EXISTS, NOT BETWEEN,
NOT IN etc. This is negate operator.

OR The OR operator is used to combine multiple conditions in an SQL


statement's WHERE clause.

IS NULL The NULL operator is used to compare a value with a NULL


value.

UNIQUE The UNIQUE operator searches every row of a specified table


for uniqueness (no duplicates).
Set operators:

32
[
Relational database management systems
rajanna ]

Operator Description

UNION Returns all distinct rows selected by


either query.

UNION ALL Returns all rows selected by either


query, including all duplicates.

INTERSECT and INTERSECT ALL Returns all distinct rows selected by


both queries.

MINUS Returns all distinct rows selected by


the first query but not the second.

THE ARITHMETIC OPERATORS WITH PRECEDENCE


When a complex expression has multiple operators, operator precedence determines the
sequence in which the operations are performed. The order of execution can significantly
affect the resulting value.
Operators have the precedence levels shown in the following table. An operator on higher
levels is evaluated before an operator on a lower level.
Level Operators

1 ~ (Bitwise NOT)

2 * (Multiply), / (Division), % (Modulo)

3 + (Positive), - (Negative), + (Add), (+ Concatenate), - (Subtract), & (Bitwise


AND), ^ (Bitwise Exclusive OR), | (Bitwise OR)

4 =, >, <, >=, <=, <>, !=, !>, !< (Comparison operators)

5 NOT

6 AND

7 ALL, ANY, BETWEEN, IN, LIKE, OR, SOME

8 = (Assignment)

33
[
Relational database management systems
rajanna ]

SCHEMA
In the SQL environment, a schema is a group of database objects such as tables and indexes,
which are related to each other. Usually the schema belongs to a single user or application. A
single database can hold multiple schemas belonging to different users or applications.
Schemas are used to define a portion of a database that a particular user owns. Schemas are
dependent on a catalog. Schemas enforce a first level of security by allowing each user to see
only the tables that belong to that user.
The syntax of the SQL command to create a database schema:

SQL>CREATE SCHEMA AUTHORIZATION { creator};

If the creator is RAMA, we use the command:

SQL>CREATE SCHEMA AUTHORIZATION RAMA;


SQL DATA TYPES
Data Type: Data Type tells what type of value an attribute or a variable can hold. Specifying
the data type for attributes in a table and variables in a program is mandatory. The common
data types used in SQL are listed below.
Oracle 8 Data Types:
Data type Description
VARCHAR2(size)
Variable length character string Maximum size is 4000,
and minimum is 1. We must specify size for a
VARCHAR2.

Number having p digits with decimal point with s digits


NUMBER(p,s)
from right.

LONG Character data of variable length up to 2 gigabytes, or 231 -1


bytes.
Valid date range from January 1, 4712 BC to
DATE
December 31, 4712 AD.
CHAR(size)
Fixed length character data of length size bytes. Maximum
size is 2000 bytes. Default and minimum size is 1 byte.

34
[
Relational database management systems
rajanna ]

CLOB A character large object containing single byte


characters. Varying-width character sets are not
supported. Maximum size is 4 gigabytes.

BLOB A binary large object. Maximum size is 4 gigabytes.


Contains a locator to a large binary file stored outside the
BFILE database. Maximum size is 4 gigabytes.
CREATING A TABLE STRUCTURE
The following are the guide lines to create a table structure.
 Identity the appropriate data types. Including length and precision for each attribute.
 Use create table command.
 Use one line per column (attribute) definition
 Use spaces to line up the attribute characteristics and constraints
 Table and attribute names are capitalized
 NOT NULL specification if needed
 UNIQUE specification if needed
 Primary key attributes contain both a NOT NULL and a UNIQUE specification
 RDBMS will automatically enforce referential integrity for foreign keys
 Command sequence ends with a semicolon
Syntax:

CREATE TABLE
table_name (
column_name1
data_type,
column_name2
data_type,
column_name3
data_type,
Example:
CREATE TABLE VENDOR (
V_CODE INTEGER NOT NULL UNIQUE,
V_NAME VARCHAR(35) NOT NULL,
V_CONTACT VARCHAR(15) NOT NULL,
V_AREACODE CHAR(3) NOT NULL,

35
[
Relational database management systems
rajanna ]

V_PHONE CHAR(8) NOT NULL,


V_STATE V_ORDERCHAR(2) CHAR(1) NOT NULL, NOT NULL,

PRIMARY KEY (V_CODE));

SQL CONSTRAINTS
Constraints are the rules that can not be violated by the user. Constraints are used to ensure
proper data entry in tables. They should be written when creating or altering the table
structures. Constraints used in SQL are listed below.
 Primary key constraint:
o Primary key attributes contain both a NOT NULL and a UNIQUE
specification
 Foreign key constraint:
o RDBMS will automatically enforce referential integrity for foreign keys. It
indicates that the proper foreign key values must match the primary key
values of other table other wise null
 NOT NULL constraint
o Ensures that a column does not accept nulls
 UNIQUE constraint
o Ensures that all values in a column are unique
 DEFAULT constraint
o Assigns a value to an attribute when a new row is added to a table
 CHECK constraint
o Validates data when an attribute value is entered
 ON UPDATE CASCADE
o Ensures that a change in pk will automatically be applied to all FK references
throughout the system
o Also have ON DELETE CASCADE and ON UPDATE CASCADE

Example:
SQL> create table emp99
(emp_num number(3),
emp_name varchar2(15) constraint emp99_emp_name_nn not null, emp_city
varchar2(10) default 'hyd',
emp_basic number(5) constraint emp99_emp_basic_ck

36
[
Relational database management systems
rajanna ]

check (emp_basic>10000),
deptno number(2),
constraint emp99_emp_num_pk primary key(emp_num),
constraint emp99_deptno_fk foreign key(deptno) references dept99(deptno));

DATA DEFINITION LANGUAGE (DDL) COMMANDS.

The DDL commands are used to define a database including creating, altering, and
dropping tables and establishing constraints. DDL deals with meta data.
The commands are explained below.
Create command:
The CREATE TABLE Statement is used to create tables to store data.

The Syntax for the CREATE TABLE Statement is:

CREATE TABLE
table_name
(column_name1 datatype,
column_name2 datatype,
... column_nameN datatype
);
 table_name - is the name of the table.
 column_name1, column_name2.... - is the name of the columns
 datatype - is the datatype for the column like char, date, number etc. For Example:
If you want to create the employee table, the statement would be like,
CREATE TABLE
employee ( id
number(5),
name
char(20),
dept
char(10), age
number(2),
salary
number(10),
location char(10)
);
ALTER COMMAND:
The SQL ALTER TABLE command is used to modify the definition
(structure) of a table by modifying the definition of its columns. The ALTER
37
[
Relational database management systems
rajanna ]

command is used to perform the following functions.


1) Add, drop, modify table columns
2) Add and drop constraints
3) Enable and Disable constraints
Syntax to add a column
ALTER TABLE table_name ADD column_name datatype;
For Example: To add a column "experience" to the employee table, the query would be
like
ALTER TABLE employee ADD experience number(3);
Syntax to drop a column
ALTER TABLE table_name DROP column_name;
For Example: To drop the column "location" from the employee table, the query would be
like
ALTER TABLE employee DROP location;
Syntax to modify a column
ALTER TABLE table_name MODIFY column_name datatype;
For Example: To modify the column salary in the employee table, the query would be like
ALTER TABLE employee MODIFY salary number(15,2);
DROP COMMAND:
It is used to delete the database objects like tables, indexes and views.
Syntax:
DROP object_type object_name;
Example : DROP table employee;

TRUNCATE command:
It is used to delete rows with auto commit
Syntax :
TRUNCATE table table_name;
Example:

TRUNCATE table employee;


RENAME COMMAND
The SQL RENAME command is used to change the name of the table or a database object.
Syntax to rename a table
RENAME old_table_name To new_table_name;
For Example: To change the name of the table employee to my_employee, the query
would be like RENAME employee TO my_emloyee;
38
[
Relational database management systems
rajanna ]

DATA MANIPULATIN COMMANDS (DML).


DML commands are used to maintain and access a database, including updating, inserting,
modifying and retrieving data. It deals with data. The commands are explained below.
SELECT Command:
The SQL SELECT statement is used to query or retrieve data from a table in the database. A
query may retrieve information from specified columns or from all of the columns in the table
Syntax of SQL SELECT Statement:
SELECT column_list FROM table-name
[WHERE Clause]
[GROUP BY
clause] [HAVING
clause] [ORDER
BY clause];
 table-name is the name of the table from which the information is retrieved.
 column_list includes one or more columns from which data is retrieved.
 The code within the brackets is optional.
Example:
SELECT first_name FROM student_details;
INSERT Command:
The INSERT Statement is used to add new rows of data to a table. We can
insert data to a table in two ways,
1) Inserting the data directly to a table.
Syntax for SQL INSERT is:
INSERT INTO TABLE_NAME
[ (col1, col2, col3,...colN)]
VALUES (value1, value2, value3,...valueN);

 col1, col2,...colN -- the names of the columns in the table into which you want to
insert data.
Example:
INSERT INTO employee (id, name, dept, age, salary location) VALUES (105,

'Srinath', 'Aeronautics', 27, 33000);

2) Inserting data to a table through a select statement. Syntax :


INSERT INTO table_name
[(column1, column2, ...
39
[
Relational database management systems
rajanna ]

columnN)]

40
[
Relational database management systems
rajanna ]

SELECT column1,
column2, ...columnN FROM
table_name [WHERE condition];

Example
INSERT INTO employee
SELECT * FROM temp_employee;
SQL UPDATE STATEMENT
The UPDATE Statement is used to modify the existing rows in a table.
Syntax
UPDATE table_name
SET column_name1 =
value1, column_name2 =
value2, ... [WHERE
condition]
 table_name - the table name which has to be updated.
 column_name1, column_name2.. - the columns that gets changed.
 value1, value2... - are the new values.
Example:
UPDATE employee
SET location
='Mysore' WHERE
id = 101;
Delete command:
The DELETE Statement is used to delete rows from a table.
Syntax:
DELETE FROM table_name [WHERE condition];
 table_name -- the table name which has to be updated.
Example: To delete an employee with id 100 from the employee table, the sql delete query
would be like,
DELETE FROM employee WHERE id = 100;
To delete all the rows from the employee table, the query would be like,
DELETE FROM employee;
DATA CONTROL LANGUAGE (DCL) COMMANDS.
DCL commands are used to control a database. It deals with accessibilitlity and transaction
changes. It includes a set of privileges that can be granted to or revoked from a user. The
commands are : GRANT, REVOKE, COMMIT, ROLLBACK and SAVEPOINT.
41
[
Relational database management systems
rajanna ]

GRANT COMMAND:
GRANT is a command used to provide access or privileges on the database objects to the
users.
Syntax:
GRANT
privilege_name ON
object_name
TO {user_name |PUBLIC |
role_name} [WITH GRANT
OPTION];
 privilege_name is the access right or privilege granted to the user. Some of the
access rights are ALL, EXECUTE, and SELECT.
 object_name is the name of an database object like TABLE, VIEW, STORED
PROC and SEQUENCE.
 user_name is the name of the user to whom an access right is being granted.
 PUBLIC is used to grant access rights to all users.
 ROLES are a set of privileges grouped together.
 WITH GRANT OPTION - allows a user to grant access rights to other users.
For Eample: GRANT SELECT ON employee TO user1;
This command grants a SELECT permission on employee table to user1.
REVOKE Command:
The REVOKE command removes user access rights or privileges to the database objects.
Syntax:
REVOKE
privilege_name ON
object_name
FROM {user_name |PUBLIC |role_name}
Example: REVOKE SELECT ON employee FROM user1;
This commmand will REVOKE a SELECT privilege on employee table from user1.
COMMIT Command
To make the changes done in a transaction permanent issue the COMMIT statement.
syntax
COMMIT ;
Example
insert into emp (empno,ename,sal) values (101,’Abid’,2300);
commit;
ROLLBACK
To rollback the changes done in a transaction give rollback statement. Rollback restore the
state of the database to the last commit point.
42
[
Relational database management systems
rajanna ]

Example :
delete from emp;
rollback; /* undo the changes */
SAVEPOINT
Specify a point in a transaction to which later you can roll back.

Example:
insert into emp (empno,ename,sal) values (109,’Sami’,3000);
savepoint a;
insert into dept values (10,’Sales’,’Hyd’);
Now if you give
rollback to a;
Then row dept will be roll backed. Now you can commit the row inserted into emp table or
rollback the transaction.

3. 11 SPECIAL OPERATORS IN SQL


The special operatores in SQL are "IN", "BETWEEN...AND", "IS NULL", "LIKE".
Operator Description
column value is similar to
LIKE
specified character(s).
column value is equal to any one
IN
of a specified set of values.

column value is between two


BETWEEN...AND values, including the end values
specified in the
range.
IS NULL column value does not exist.
SQL LIKE Operator
The LIKE operator is used to list all rows in a table whose column values match a specified
pattern.
For example: To select all the students whose name begins with 'S'
SELECT first_name,
last_name FROM
student_details WHERE
first_name LIKE 'S%';

43
[
Relational database management systems
rajanna ]

The output would be similar to:


first_name last_name
------------- -------------
Stephen Fleming
Shekar Gowda
SQL BETWEEN ... AND Operator
The operator BETWEEN and AND, are used to compare data for a range of values.
For Example: to find the names of the students between age 10 to 15 years, the query would
be like,
SELECT first_name, last_name,
age FROM student_details
WHERE age BETWEEN 10 AND 15;
The output would be similar to:
first_name last_name age

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


- - -

Rahul Sharma 10
Anajali Bhagwat 12
Shekar Gowda 15
SQL IN Operator:
The IN operator is used when you want to compare a column with more than one value. It is
similar to an OR condition.
For example: If you want to find the names of students who are studying either Maths or
Science, the query would be like,
SELECT first_name, last_name,
subject FROM student_details
WHERE subject IN ('Maths', 'Science');
The output would be similar to:

first_name last_name subject


------------- ------------- ----------
Anajali Bhagwat Maths
Shekar Gowda Maths
Rahul Sharma Science
Stephen Fleming Science

44
[
Relational database management systems
rajanna ]

SQL IS NULL Operator


A column value is NULL if it does not exist. The IS NULL operator is used to display
all the rows for columns that do not have a value.
For Example: If you want to find the names of students who do not participate in any
games, the query would be as given below:
SELECT first_name,
last_name FROM
student_details WHERE
games IS NULL
There would be no output as we have every student participate in a game in the table
student_details, else the names of the students who do not participate in any games would
be displayed.
EXISTS Operators:
EXISTS simply tests whether the inner query returns any row. If it does, then the outer
query proceeds. If not, the outer query does not execute, and the entire SQL statement
returns nothing.
The syntax for EXISTS is:
SELECT "column_name1"
FROM "table_name1"
WHERE EXISTS (SELECT *
FROM "table_name2"
WHERE [Condition]);
Example:
SELECT SUM(Sales) FROM Store_Information
WHERE EXISTS
(SELECT * FROM Geography
WHERE region_name = 'West');
Concatenation (||) operator:
This operator concatenates character strings.
Example : SELECT 'Name is ' || ename FROM emp;
Distinct Operator:
This operator lists the distinct values in a column.
Example : Select DISTINCT job from EMP;

WILD CARD CHARACTERS IN SQL


In SQL, there are two wildcard characters:
1. % (percent sign) represents zero, one, or more characters.
2. _ (underscore) represents exactly one character.
Wildcards are used with the LIKEkeyword in SQL. Below are

45
[
Relational database management systems
rajanna ]

some wildcard examples:

46
[
Relational database management systems
rajanna ]

• 'A_Z': All string that starts with 'A', another character, and end with 'Z'. For example,
'ABZ' and 'A2Z' would both satisfy the condition, while 'AKKZ' would not (because there
are two characters between A and Z instead of one).
• 'ABC%': All strings that start with 'ABC'. For example, 'ABCD' and 'ABCABC' would
both satisfy the condition.

SORTING THE RECORDS OF A TABLE


SQL ORDER BY CLAUSE:
The ORDER BY clause is used in a SELECT statement to sort results either in ascending
or descending order. Oracle sorts query results in ascending order by default.
Syntax:
SELECT column-list
FROM table_name [WHERE condition]
[ORDER BY column1 [, column2, .. columnN] [DESC]];
database table "employee";

id name salary location


100 Ramesh 25000 Bangalore
101 Hrithik 35000 Bangalore
102 Harsha 35000 Mysore
103 Soumya 20000 Bangalore
104 Priya 30000 Mangalore
For Example: If you want to sort the employee table by salary of the employee, the sql
query would be.
SELECT name, salary FROM employee ORDER BY salary;
The output would be like

name salary
---------- ----------
Soumya 20000
Ramesh 25000
Priya 30000
Hrithik 35000
Harsha 35000
If you want to sort the data in descending order, you must explicitly specify it as shown
below.
47
[
Relational database management systems
rajanna ]

SELECT name,
salary FROM
employee
ORDER BY name, salary DESC;
The above query sorts only the column 'salary' in descending order and the column
'name' by ascending order.
If you want to select both name and salary in descending order, the query would be as given
below.
SELECT name,
salary FROM
employee
ORDER BY name DESC, salary DESC;

SQL CLAUSES
The clauses in SQL are explained below:
SELECT CLAUSE
The most commonly used SQL command is SELECT statement. The SQL SELECT
statement is used to query or retrieve data from a table in the database.
WHERE CLAUSE
The WHERE Clause is used when you want to retrieve specific information from a table
excluding other irrelevant data.
ORDER BY CLAUSE
The ORDER BY clause is used in a SELECT statement to sort results either in ascending or
descending order. Oracle sorts query results in ascending order by default.

GROUP BY CLAUSE
The SQL GROUP BY Clause is used along with the group functions to retrieve data grouped
according to one or more columns.
HAVING CLAUSE
Having clause is used to filter data based on the group functions. This is similar to WHERE
condition but is used with group functions. Group functions cannot be used in WHERE
Clause but can be used in HAVING clause.
UNION Clause
Returns all distinct rows selected by either query.
UNION ALL Clause
Returns all rows selected by either query, including all duplicates.
INTERSECT Clause
Returns all distinct rows selected by both queries.
MINUS Clause Returns all distinct rows selected by the first query but not the second.
48
[
Relational database management systems
rajanna ]

AGGREGATE (GROUP) FUNCTIONS.


SQL GROUP Functions
Group functions are built-in SQL functions that operate on groups of rows and return
one value for the entire group. These functions are: COUNT, MAX, MIN, AVG,
SUM, DISTINCT

COUNT (): This function returns the number of rows in the table that satisfies the
condition specified in the WHERE condition. If the WHERE condition is not specified,
then the query returns the total number of rows in the table.

Example: SELECT COUNT (*) FROM


employee

WHERE dept =
'Electronics';

DISTINCT(): This function is used to select the distinct rows.


Example: If you want to select all distinct department names from employee table, the
query would be:

SELECT DISTINCT dept FROM employee;

MAX(): This function is used to get the maximum value from a column. To get
the maximum salary drawn by an employee, the query would be:

SELECT MAX (salary) FROM employee;

MIN(): This function is used to get the minimum value from a column. To get
the minimum salary drawn by an employee, he query would be:

SELECT MIN (salary) FROM employee;

AVG(): This function is used to get the average value of a numeric column. To get the
average salary, the query would be

SELECT AVG (salary) FROM employee;

SUM(): This function is used to get the sum of a numeric column To get
the total salary given out to the employees,

SELECT SUM (salary) FROM employee;

49
[
Relational database management systems
rajanna ]

TYPES OF AGGREGATIONS
Scalar aggregate: If a single value is returned from an SQL query that includes an aggregate
function then it is scalar aggregate. The following example returns single value. i.e. sum of
the salaries of all employees.
SELECT SUM(SAL) FROM EMP;
Vector aggregate: If multiple values are returned from an SQL query that includes an
aggregate function then it is vector aggregation. The following example returns multiple
values i.e. sum of the salaries of all employees in each department.
SELECT DEPTNO, SUM(SAL) FROM EMP GROUP BY DEPTNO;

GROUPING ON DATA.
SQL GROUP BY CLAUSE
The SQL GROUP BY Clause is used along with the group functions to retrieve data grouped
according to one or more columns.
For Example: If you want to know the total amount of salary spent on each department, the
query would be:
SELECT dept, SUM
(salary) FROM employee
GROUP BY dept;
The output would be like:
dept salary

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

Electrical 25000
Electronics 55000
NOTE: The group by clause should contain all the columns in the select list expect those
used along with the group functions.
SELECT location, dept, SUM
(salary) FROM employee
GROUP BY location, dept;
The output would be like:
location dept salary
----------
------------ -------------- -
- -
Bangalore Electrical 25000

50
[
Relational database management systems
rajanna ]

Bangalore Electronics 55000


SQL HAVING CLAUSE
Having clause is used to filter data based on the group functions. This is similar to WHERE
condition but is used with group functions. Group functions cannot be used in WHERE
Clause but can be used in HAVING clause.
For Example: If you want to select the department that has total salary paid for its employees
more than 25000, the sql query would be like;
SELECT dept, SUM
(salary) FROM employee
GROUP BY dept
HAVING SUM (salary) > 25000
The output would be like:

dept salary
------------- -------------
Electronics 55000
Aeronautics 35000
InfoTech 30000

When WHERE, GROUP BY and HAVING clauses are used together in a SELECT
statement, the WHERE clause is processed first, then the rows that are returned after the
WHERE clause is executed are grouped based on the GROUP BY clause. Finally, any
conditions on the group functions in the HAVING clause are applied to the grouped rows
before the final output is displayed.

3.18 VIEW
A VIEW is a virtual table, through which a selective portion of the data from one or more
tables can be seen. Views do not contain data of their own. They are used to restrict access
to the database or to hide data complexity. A view is stored as a SELECT statement in the
database. DML operations on a view like INSERT, UPDATE, DELETE affects the data in
the original table upon which the view is based.
The Syntax to create a sql view is
CREATE VIEW view_name

AS

51
[
Relational database management systems
rajanna ]

SELECT column_list

FROM table_name [WHERE condition];

 view_name is the name of the VIEW.


 The SELECT statement is used to define the columns and rows that you want to
display in the view.
For Example: to create a view on the product table the sql query would be like

CREATE VIEW view_product AS

SELECT product_id, product_name FROM product;

Advantages of views:
1. View the data without storing the data into the object.
2. Provides the most current data from table
3. Restrict the view of a table i.e. can hide some of columns in the tables.
4. Join two or more tables and show it as one object to user.
5. Restrict the access of a table so that nobody can insert the rows into the table.
Disadvantages:
1. Can not use DML operations on this.
2. When table is dropped view becomes inactive.. it depends on the table objects.
3. It is an object, so it occupies space.

3.19 UPDATABLE VIEWS


The views on which we perform INSERT, UPDATE and DELETE statements are treated as
updatable views. These are then performed on the base table upon which the view is defined.
However, certain views may not be updated. For example a view containing DISTINCT
values, where a single row in the view may represent several rows in the base table.
A view is not updatable if any of the following conditions are true:
 The keyword DISTINCT is used in the view definition
 The select list contains components other than column specifications, or contains
more than one specification of the same column
 The FROM clause specifies more than one table reference or refers to a non-
updatable view
 The GROUP BY clause is used in the view definition

52
[
Relational database management systems
rajanna ]

 The HAVING clause is used in the view definition

SQL JOINS:
SQL Joins are used to relate information in different tables. A Join condition is a part of the
sql query that retrieves rows from two or more tables. A SQL Join condition is used in the
SQL WHERE Clause of select, update, delete statements.
The Syntax for joining two tables is:
SELECT col1, col2, col3...
FROM table_name1, table_name2
WHERE table_name1.col2 = table_name2.col1;
Equijoins
An equijoin is a join with a join condition containing an equality operator ( = ). An equijoin
combines rows that have equivalent values for the specified columns.

Example :
select [Link], [Link], [Link], [Link], [Link], [Link] from emp
e, dept d where [Link]=[Link];
The above query can also be written like given below without using table qualifiers.
select empno,ename,sal,dname,city from emp,dept
where [Link]=[Link];
Non Equi Joins.
Non equi joins is used to return result from two or more tables where exact join is not
possible.
For example we have emp table and salgrade table. The salgrade table contains grade and
their low salary and high salary. Suppose you want to find the grade of employees based on
their salaries then you can use NON EQUI join.
select [Link], [Link], [Link], [Link] from emp e, salgrade s
where [Link] between [Link] and [Link]
Self Joins(recursive join)
A self join is a join of a table to itself. This table appears twice in the FROM clause and is
followed by table aliases that qualify column names in the join condition. To perform a self
join, Oracle combines and returns rows of the table that satisfy the join condition.
For example the following query returns employee names and their manager names for whom
they are working.
Select [Link], [Link], [Link] “Manager” from emp e,

53
[
Relational database management systems
rajanna ]

emp m where [Link]=[Link]


Inner Join:
An inner join (sometimes called a "simple join") is a join of two or more tables that
returns only those rows that satisfy the join condition.
Outer Joins:
An outer join extends the result of a simple join. An outer join returns all rows that satisfy
the join condition and also returns some or all of those rows from one table for which no rows
from the other satisfy the join condition.
select [Link],[Link],[Link],[Link],[Link],[Link] from emp e, dept d
where [Link](+)=[Link];
That is specify the (+) sign to the column which is lacking values.
Cartesian Products:
If two tables in a join query have no join condition, Oracle returns
their Cartesian product. Oracle combines each row of one table with each row of the other.
A Cartesian product always generates many rows and is rarely useful. For example, the
Cartesian product of two tables, each with 100 rows, has 10,000 rows. Always include a join
condition unless you specifically need a Cartesian product.

3.21 SQL JOIN OPERATORS.


The Relational Join Operators merge rows from two tables and return the rows with one of
the following conditions.
 Have common values in common columns (natural join)
 Meet a given join condition( equality or inequality)
 Having common values in common columns or have no matching values (outer
join)

54
[
Relational database management systems
rajanna ]

The different SQL join operators and their meanings and examples are listed below:

55
[
Relational database management systems
rajanna ]

3.22 SUB QUERY AND ITS PROPERTIES

Subquery or Inner query or Nested query is a query in a query. A subquery is usually added
in the WHERE Clause of the sql statement. Most of the time, a subquery is used when you
know how to search for a value using a SELECT statement, but do not know the exact value.
Subqueries are an alternate way of returning data from multiple tables.
Subqueries can be used with the following sql statements along with the comparision
operators like =, <, >, >=, <= etc.
 SELECT
 INSERT
 UPDATE
 DELETE
Properties of Sub-Query
 A sub query is select statement inside a query.
 A sub query must be enclosed in the parenthesis.
 A sub query must be put in the right hand of the comparison operator
56
[
Relational database management systems
rajanna ]

 The first query in the SQL statement is known as the outer query.
 The query inside the SQL statement is known as the inner query.
 The inner query is executed first.
 The output of the inner query is used as input for the outer query.
 A sub query cannot contain a ORDER-BY clause.
 A query can contain more than one sub-queries. Example:
the following are the examples of nested queries.
 To find the second max salary of the employees in emp table.
Select max(sal) from emp where sal< (select max(sal) from
emp);
 To print the name of the employee who draws maximum salary.
Select ename form emp where sal = (select max(sal) from emp);

2.23 MULTI-ROW SUB QUERY OPERATORS (OR)

ANY, IN AND ALL OPERATORS

Multiple row subquery returns one or more rows to the outer SQL statement.
The outer query may use the IN, ANY, or ALL operator to handle a sub query that
returns multiple rows
Using IN operator:

SELECT id, first_name FROM employee


WHERE id IN (SELECT id FROM employee WHERE first_name LIKE '%e%');

Using ANY operator:


SELECT id, last_name FROM employee
WHERE id < ANY (SELECT id FROM employee where city='Hyd');

Using ALL operator:


SELECT id, last_name FROM employee
WHERE salary > ALL (SELECT salary FROM employee where city='Hyd');

2.24 CORRELATED SUBQUERY


A query is called correlated subquery when both the inner query and the outer query are
interdependent. For every row processed by the inner query, the outer query is processed as
well. The inner query depends on the outer query before it can be processed.

57
[
Relational database management systems
rajanna ]

Example:
SELECT p.product_name FROM product p
WHERE p.product_id = (SELECT o.product_id FROM order_items
o WHERE o.product_id = p.product_id);
NOTE:
1) You can nest as many queries you want but it is recommended not to nest more than
16 subqueries in oracle.
2) If a subquery is not dependent on the outer query it is called a non- correlated
subquery.
More Examples on Correlated Sub queries:
The following query lists the names of the employees who joined on a same date.
Select ename from emp e where 1 < ( select count(*) from emp where
hiredate = [Link]

The following query lists the names of the employees who draw first 3 maximum salary

Select * from emp e where 3 > (select count(*) from emp where sal >
[Link]);
2.25 DUAL TABLE
This is a single row and single column dummy table provided by oracle. This is used to
perform mathematical calculations without using a table.

Select * from DUAL

Output:
DUMMY
-------
X

Select 777 * 888 from


Dual
Output:
777 * 888
---------
689976

2.26 SQL FUNCTIONS.


There are two types of functions in Oracle.
1) Single Row Functions: Single row or Scalar functions return a value for every row
that is processed in a query.
2) Group Functions:. The group functions are used to calculate aggregate values like
total or average, which return just one total or one average value after processing a
58
[
Relational database management systems
rajanna ]

group of rows.

59
[
Relational database management systems
rajanna ]

There are four types of single row functions. They are:


1) Numeric Functions: These are functions that accept numeric input and return
numeric values.
2) Character or Text Functions: These are functions that accept character
input and can return both character and number values.
3) Date Functions: These are functions that take values that are of datatype DATE
as input and return values of datatype DATE, except for the
MONTHS_BETWEEN function, which returns a number.
4) Conversion Functions: These are functions that help us to convert a value in
one form to another form. For Example: a null value into an actual value, or a value
from one datatype to another datatype like NVL, TO_CHAR, TO_NUMBER,
TO_DATE etc.
You can combine more than one function together in an expression. This is known as
nesting of functions.
1) Numeric Functions:
Numeric functions are used to perform operations on numbers. They accept numeric values
as input and return numeric values as output. Few of the Numeric functions are

Function Name Return Value

ABS (x) Absolute value of the number 'x'


CEIL (x) Integer value that is Greater than or equal to the number 'x'
FLOOR (x) Integer value that is Less than or equal to the number 'x'
TRUNC (x, y) Truncates value of number 'x' up to 'y' decimal places

ROUND (x, y) Rounded off value of the number 'x' up to the number 'y' decimal places

The following examples explains the usage of the above numeric functions .

nction Name Examples Return Value


ABS (1) 1
ABS (x)
ABS (-1) 1
CEIL (2.83) 3
CEIL (x) CEIL (2.49) 3

60
[
Relational database management systems
rajanna ]

FLOOR (2.83) 2
FLOOR (x)
FLOOR (2.49) 2

ROUND (125.456, 1) 125.4


TRUNC (x, y) ROUND (125.456, 0) 125
ROUND (124.456, -1) 120

TRUNC (140.234, 2) 140.23


TRUNC (-54, 1) 54
ROUND (x, y)
TRUNC (5.7) 5
TRUNC (142, -1) 140

2) Character or Text Functions:


Character or text functions are used to manipulate text strings. They accept strings or
characters as input and can return both character and number values as output.
Few of the character or text functions are as given below:
Function Name Return Value

All the letters in 'string_value' is converted to lowercase.


LOWER (string_value)

All the letters in 'string_value' is converted to uppercase.


UPPER (string_value)

All the letters in 'string_value' is converted to mixed case.


INITCAP (string_value)

LTRIM (string_value, All occurrences of 'trim_text' is removed from the left of


trim_text) 'string_value'.

RTRIM (string_value, All occurrences of 'trim_text' is removed from the right of


trim_text) 'string_value' .

All occurrences of 'trim_text' from the left and right of


TRIM (trim_text FROM
'string_value' , 'trim_text' can also be only one character long
string_value)
.

SUBSTR (string_value, m, Returns 'n' number of characters from 'string_value'


n) starting from the 'm' position.

LENGTH (string_value) Number of characters in 'string_value' in returned.

61
[
Relational database management systems
rajanna ]

Returns 'string_value' left-padded with 'pad_value' . The


LPAD (string_value, n,
length of the whole string will be of 'n' characters.
pad_value)

Returns 'string_value' right-padded with 'pad_value'


RPAD (string_value, n,
. The length of the whole string will be of 'n'
pad_value)
characters.

The following examples explains the usage of the above character or text functions

Function Name Examples Return Value

LOWER(string_value) LOWER('Good Morning') good morning

GOOD
UPPER(string_value) UPPER('Good Morning')
MORNING

INITCAP('GOOD MORNING')
INITCAP(string_value) Good Morning

LTRIM ('Good Morning', 'Good)


LTRIM(string_value, trim_text) Morning

RTRIM (string_value, trim_text) RTRIM ('Good Morning', '


Good
Morning')

TRIM (trim_text FROM TRIM ('o' FROM 'Good


Gd Mrning
string_value) Morning')

SUBSTR ('Good Morning', 6, 7)


SUBSTR (string_value, m, n) Morning

LENGTH (string_value) LENGTH ('Good Morning') 12

LPAD (string_value, n, pad_value)


LPAD ('Good', 6, '*') **Good

RPAD (string_value, n, pad_value)


RPAD ('Good', 6, '*') Good**

3) Date Functions:
These are functions that take values that are of datatype DATE as input and return values of
datatypes DATE, except for the MONTHS_BETWEEN function, which returns a number as
output.

62
[
Relational database management systems
rajanna ]

Few date functions are as given below.


Function Name Return Value

Returns a date value after adding 'n' months to the date


ADD_MONTHS (date, n)
'x'.

MONTHS_BETWEEN (x1, Returns the number of months between dates x1 and x2.
x2)

Returns the date 'x' rounded off to the nearest


ROUND (x, date_format)
century, year, month, date, hour, minute, or second as
specified by the 'date_format'.

Returns the date 'x' lesser than or equal to the nearest


century, year, month, date, hour, minute, or second as
TRUNC (x, date_format)
specified by the 'date_format'.

Returns the next date of the 'week_day' on or after


NEXT_DAY (x, week_day)
the date 'x' occurs.

It is used to determine the number of days remaining in


LAST_DAY (x)
a month from the date 'x' specified.

SYSDATE Returns the systems current date and time.

The below table provides the examples for the above functions

Function Name Examples Return Value

ADD_MONTHS ( ) ADD_MONTHS ('16-Sep-81', 3) 16-Dec-81

MONTHS_BETWEEN( MONTHS_BETWEEN ('16-Sep-81', '16-


3
) Dec-81')

NEXT_DAY ('01-Jun-08',
NEXT_DAY( ) 04-JUN-08
'Wednesday')

LAST_DAY( ) LAST_DAY ('01-Jun-08') 30-Jun-08

4) Conversion Functions:
These are functions that help us to convert a value in one form to another form. For Ex: a null
value into an actual value, or a value from one datatype to another datatype like NVL,
TO_CHAR, TO_NUMBER, TO_DATE.

63
[
Relational database management systems
rajanna ]

Few of the conversion functions available in oracle are:


Function Name Return Value

Converts Numeric and Date values to a character string value.


TO_CHAR (x [,y])
It cannot be used for calculations since it is a string value.

Converts a valid Numeric and Character values to a Date


TO_DATE (x [,
value. Date is formatted to the format specified by
date_format])
'date_format'.

If 'x' is NULL, replace it with 'y'. 'x' and 'y' must be of the
NVL (x, y)
same datatype.

DECODE (a, b, c, d, e, Checks the value of 'a', if a = b, then returns 'c'. If a = d, then
default_value) returns 'e'. Else, returns default_value.

The below table provides the examples for the above functions

Function Name Examples Return Value

TO_CHAR () TO_CHAR (SYSDATE, 'Day, Monday, June 2008


Month YYYY')

TO_DATE () TO_DATE ('01-Jun-08') 01-Jun-08

NVL () NVL (null, 1) 1

3.27 SEQUENCE IN ORACLE


In Oracle, you can create an autonumber field by using sequences. A sequence is an object
in Oracle that is used to generate a number sequence. This can be useful when you need to
create a unique number to act as a primary key.

Characteristics of Sequences:
 Oracle sequences are an independent object in the database. (Sequences are not a
data type)

64
[
Relational database management systems
rajanna ]

 Oracle sequences have a name and can be used anywhere a value is expected.
 Oracle sequences are not tied to a table or a column.
 Oracle sequences generate a numeric value that can be assigned to any column in
any table.
 The table attribute to which you assigned a value based on a sequence can be edited
and modified.
 An oracle sequence can be created and deleted anytime. The
syntax for a sequence is:

CREATE SEQUENCE sequence_name


MINVALUE value
MAXVALUE value
START WITH value
INCREMENT BY value
CACHE value;

For example:

CREATE SEQUENCE supplier_seq


MINVALUE 1
MAXVALUE 99999
START WITH 1
INCREMENT BY 1
CACHE 20;

This would create a sequence object called supplier_seq. The first sequence number that it
would use is 1 and each subsequent number would increment by 1 (ie: 2,3,4,...}. It will
cache up to 20 values for performance.

3.28 PL/SQL& STRUCTURE OF PL/SQL BLOCK.


PL/SQL stands for Procedural Language extension of SQL.
PL/SQL is a combination of SQL along with the procedural features of programming
languages. It was developed by Oracle Corporation in the early 90’s to enhance the
capabilities of SQL.

A Simple PL/SQL Block:

Each PL/SQL program consists of SQL and PL/SQL statements which form a PL/SQL
block.
A PL/SQL Block consists of three sections:
 The Declaration section (optional).
 The Execution section (mandatory).

65
[
Relational database management systems
rajanna ]

 The Exception (or Error) Handling section (optional).


Declaration Section:
The Declaration section of a PL/SQL Block starts with the reserved keyword DECLARE.
This section is optional and is used to declare variables, constants, records etc.
Execution Section:
The Execution section of a PL/SQL Block starts with the reserved keyword BEGIN
and ends with END. This is a mandatory section. It contains the program logic to
perform any task.
Exception Section:
The Exception section of a PL/SQL Block starts with the reserved keyword EXCEPTION.
This section is optional. Any errors in the program can be handled in this section, so that the
PL/SQL Blocks terminates gracefully. If the PL/SQL Block contains exceptions that cannot
be handled, the Block terminates abruptly with errors.
Every statement in the above three sections must end with a semicolon ; . Sample
PL/SQL Block appears as follows:

DECLARE
Variable declaration BEGIN
Program Execution EXCEPTION
Exception handling END;

Example
DECLARE
A Number := 50 ;
B Number := 0 ;
BEGIN
DBMS_OUTPUT.PUT_LINE('A+B= ' || (A+B));
END;

3.29 ADVANTAGES OF PL/SQL


Advantages of PL/SQL
 Block Structures: PL SQL consists of blocks of code, which can be nested within
each other. Each block forms a unit of a task or a logical module. PL/SQL Blocks can
be stored in the database and reused.

66
[
Relational database management systems
rajanna ]

 Procedural Language Capability: PL SQL consists of procedural language


constructs such as conditional statements (if else statements) and loops like (FOR
loops).
 Better Performance: PL SQL engine processes multiple SQL statements
simultaneously as a single block, thereby reducing network traffic.
 Error Handling: PL/SQL handles errors or exceptions effectively during the
execution of a PL/SQL program. Once an exception is caught, specific actions can be
taken depending upon the type of the exception or it can be displayed to the user with
a message.

3.30 PL-SQL ARCHITECTURE


Steps involved in PL-SQL Block execution are listed below:
 First, PL/SQL block is submitted to PL/SQL engine.
 The PL/SQL engine accepts it as input and separates SQL statements.
 PL/SQL engine executes procedural statements.
 It sends SQL statements to the SQL engine in the oracle database.
 The SQL engine executes the SQL Statements.
 The results are combined and presented
PL-SQL Architecture:

3. 31 COMMENTS IN PL/SQL
Comments are non-executable statements in a program. The comments are used to increase
the understandability of the program. Two different ways to comments in pl-sql are given
below:
We use two hyphens ( - - ) for single comments. We
use /* */ for multiline comment in programs

67
[
Relational database management systems
rajanna ]

PL-SQL DATA TYPES


Data type: It tells what type of value a variable or an attribute can hold. Specifying the data
type for attributes and variables in a program is mandatory. The common data types used in
SQL are listed below:
Simple data types:
These types includes binary_integer, decimal, int, long, number, float, real, char, string, date,
Boolean.
Composite data types:
Record, table.
Attribute type:
%type (simple) eg: x [Link]%type.
%rowtype(composite) eg. R emp%rowtype.
Large Object type:
CLOB, BLOB, BFILE

CONTROL STRUCTURES IN PL/SQL WITH EXAMPLE.


Control Structure:
The control structure controls the execution flow in the program. PL/SQL Supports
conditional control structure, loop control structures, jump control statements, and case
control statements.

68
[
Relational database management systems
rajanna ]

Conditional control statements:


Simple IF Statement
Syntax: Example:
IF <condition> THEN IF A > 40 THEN
<statements>; B :=A - 40;
END IF; END IF;

IF..ELSE Statement
Syntax Example:
IF <condition> THEN IF a > 40 THEN
<statements>; b := a - 40;
ELSE ELSE
<statements>; b := 0;
END IF; END IF;

If – elsif Statement
Syntax IF Score >= 90 THEN
IF <condition-1> THEN LetterGrade := 'A';
<statements>; ELSIF Score >= 80 THEN
ELSIF <condition-2> THEN LetterGrade := 'B';
<statements>; ELSIF Score >= 70 THEN
ELSIF <condition-3> THEN LetterGrade := 'C';
<statements>; ELSIF Score >= 60 THEN
.................... LetterGrade := 'D';
.................... ELSE
ELSE LetterGrade := 'E';
<statements>; END IF;
END IF;

69
[
Relational database management systems
rajanna ]

Loop control statements


PL/SQL support three looping control: SIMPLE Loop, WHILE Loop, FOR Loop.
Simple Loop
Syntax: Example:
LOOP LOOP
<Sequence of statements> dbms_output.put_line('Hello');
EXIT WHEN condition;} i:=i+1;
END LOOP; EXIT WHEN i>5;
END LOOP;

While Loop
Syntax: Example:
WHILE <condition> WHILE i<5
LOOP LOOP
<action> dbms_output.put_line('Hello');
END LOOP; i:=i+1;
END LOOP;
FOR Loop
Syntax: Example FOR
FOR variable IN [REVERSE] i IN 1..5 LOOP
start..end
LOOP
<action>
END LOOP;

Goto Statement: GOTO statement tranfers the control of program unconditionally. In


contrast to the IF statement GOTO can tranfer the control of program to any position in the
PL/SQL block.

70
[
Relational database management systems
rajanna ]

GOTO Syntax Example Code:


GOTO <<Code_Block_Name>>
----------- DECLARE
----------- BEGIN
<<Code_Block_Name>> FOR i IN 1..5
----------- LOOP
----------- dbms_output.put_line(i);
IF i=4 THEN
GOTO label1;
END IF;
END LOOP;
<<label1>>
dbms_output.put_line(Row Filled);
END;

SWITCH Statement
Syntax Example Code:
CASE
DECLARE
WHEN <condition>
a number := 5; BEGIN
THEN <statement> WHEN <condition>
CASE
THEN <statement> ELSE <statement>
WHEN a = 5 THEN
END CASE;
dbms_output.put_line('Execute Me'); WHEN a = 4 T
dbms_output.put_line('Not Execute Me');
ELSE
dbms_output.put_line('Else Statements');
END CASE; END;

EXCEPTION
In PL/SQL, a warning or error is called an exception. Handling run time errors is called
exception handling Exceptions can be internally defined (by the run- time system) or user
defined. Examples of internally defined exceptions include division by zero and out of
memory. Some common internal exceptions have predefined names, such as
ZERO_DIVIDE and STORAGE_ERROR.
71
[
Relational database management systems
rajanna ]

72
[
Relational database management systems
rajanna ]

When an error occurs, an exception is raised. That is, normal execution stops and control
transfers to the exception-handling part of your PL/SQL block or subprogram. Internal
exceptions are raised implicitly (automatically) by the run-time system. User-defined
exceptions must be raised explicitly by RAISE statements, which can also raise predefined
exceptions.
To handle raised exceptions, you write separate routines called exception handlers.
Buit-In Exception Syntax:
EXCEPTION
WHEN <Buit-in Exception> THEN
<User Defined Action to be taken>
Example Code:
DECLARE
V_NAME VARCHAR2(20);
BEGIN
SELECT EMP_NAME
INTO V_NAME FROM
EMP99
WHERE EMP_CITY = 'DELHI';
EXCEPTION
WHEN NO_DATA_FOUND
THEN
DBMS_OUTPUT.PUT_LINE('NO EMP EXIST WITH THE GIVEN
CITY');
END;

Buit-in Exception List


 DUP_VAL_ON_INDEX
 NO_DATA_FOUND
 TOO_MANY_ROWS
 VALUE_ERROR

STORED PROCEDURES WITH EXAMPLE.


A stored procedure or in simple a procedure is a named PL/SQL block which performs one
or more specific task. This is similar to a procedure in other programming languages. A
procedure has a header and a body. The header consists of the name of the procedure and the
parameters or variables passed to the procedure. The body consists or declaration section,
execution section and

73
[
Relational database management systems
rajanna ]

exception section similar to a general PL/SQL Block. A procedure is similar to an anonymous


PL/SQL Block but it is named for repeated usage.

We can pass parameters to procedures in three ways.


1) IN-parameters
2) OUT-parameters
3) IN OUT-parameters

A procedure may or may not return any value.

General Syntax to create a procedure is:

CREATE [OR REPLACE] PROCEDURE proc_name [list of


parameters] IS
Declaration
section BEGIN
Execution
section
EXCEPTION
Exception section
IS - marks the beginning of the body of the procedure and is similar to DECLARE in
anonymous PL/SQL Blocks. The code between IS and BEGIN forms the Declaration section.

There are two ways to execute a procedure.

From the SQL prompt.


EXECUTE [or EXEC] procedure_name;
Within another procedure – simply use the procedure name. procedure_name;

Example:
CREATE OR REPLACE PROCEDURE UPDATESAL1(N NUMBER, A NUMBER) IS
BEGIN

UPDATE EMP_NUM=N;
EMP99 SET EMP_BASIC=EMP_BASIC+A WHERE

END UPDATESAL1;

74
[
Relational database management systems
rajanna ]

FUNCTION IN PL/SQL
A function is a named PL/SQL Block which is similar to a procedure. The major difference
between a procedure and a function is, a function must always return a value, but a
procedure may or may not return a value.

The General Syntax to create a function is:

CREATE [OR REPLACE] FUNCTION function_name


[parameters] RETURN return_datatype;
IS
Declaration_secti
on BEGIN
Execution_section
Return
return_variable;
EXCEPTION
exception section
Return
1) Return Type: The header section defines the return type of the function. The return
datatype can be any of the oracle datatype like varchar, number etc.
2) The execution and exception section both should return a value which is of the datatype
defined in the header section.

Example:
CREATE OR REPLACE FUNCTION FACT (N NUMBER)
RETURN NUMBER
IS
I NUMBER(10);
F
NUMBER :=1;
BEGIN
FOR I IN 1.. N LOOP
F:= F*I;
END LOOP;
RETURN F;

75
[
Relational database management systems
rajanna ]

TRIGGER
A trigger is a pl/sql block structure which is fired when a DML statements like Insert,
Delete, Update is executed on a database table. A trigger is triggered automatically when
an associated DML statement is executed.
Syntax of Triggers
The Syntax for creating a trigger is:
CREATE [OR REPLACE ] TRIGGER trigger_name
{BEFORE | AFTER | INSTEAD OF }
{INSERT [OR] | UPDATE [OR] | DELETE}
[OF col_name]
ON table_name
[REFERENCING OLD AS o NEW AS n]
[FOR EACH ROW]
WHEN (condition)
BEGIN
--- sql statements
END;
 CREATE [OR REPLACE ] TRIGGER trigger_name - This clause creates a trigger
with the given name or overwrites an existing trigger with the same name.
 {BEFORE | AFTER | INSTEAD OF } - This clause indicates at what time should the
trigger get fired. i.e for example: before or after updating a table. INSTEAD OF is
used to create a trigger on a view. before and after cannot be used to create a trigger
on a view.
 {INSERT [OR] | UPDATE [OR] | DELETE} - This clause determines the triggering
event. More than one triggering events can be used together separated by OR
keyword. The trigger gets fired at all the specified triggering event.
 [OF col_name] - This clause is used with update triggers. This clause is used
when you want to trigger an event only when a specific column is updated.
 [ON table_name] - This clause identifies the name of the table or view to which
the trigger is associated.
 [REFERENCING OLD AS o NEW AS n] - This clause is used to reference the old
and new values of the data being changed. By default, you reference the values
as :old.column_name or :new.column_name. The reference names can also be
changed from old (or new) to any other user- defined name. You cannot reference
old values when inserting a record, or new values when deleting a record, because
they do not exist.

76
[
Relational database management systems
rajanna ]

 [FOR EACH ROW] - This clause is used to determine whether a trigger must fire
when each row gets affected ( i.e. a Row Level Trigger) or just once when the
entire sql statement is executed([Link] level Trigger).
 WHEN (condition) - This clause is valid only for row level triggers. The trigger is
fired only for rows that satisfy the condition specified.

PROCESS OF CREATING A TRIGGER WITH EXAMPLE


For Example: The price of a product changes constantly. It is important to maintain
the history of the prices of the products.

We can create a trigger to update the 'product_price_history' table when the price of the
product is updated in the 'product' table.

1)Create the 'product' table and 'product_price_history' table

CREATE TABLE product_price_history


(product_id number(5),
product_name varchar2(32),
supplier_name varchar2(32),
unit_price number(7,2) );

CREATE TABLE product


(product_id number(5),
product_name varchar2(32),
supplier_name varchar2(32),
unit_price number(7,2) );

Insert the rows into the table Product.


2)Create the price_history_trigger and execute it.

CREATE or REPLACE TRIGGER price_history_trigger


BEFORE UPDATE OF unit_price
ON product
FOR EACH ROW
BEGIN
INSERT INTO
product_price_history VALUES
(:old.product_id,

77
[
Relational database management systems
rajanna ]

:old.product_name,
:old.supplier_name,
:old.unit_price
);

3)Lets update the price of a product.

UPDATE PRODUCT SET unit_price = 800 WHERE product_id = 100


Once the above update query is executed, the trigger fires and updates the
'product_price_history' table.

4) If you ROLLBACK the transaction before committing to the database, the data inserted
to the table is also rolled back.

3. 39 TYPES OF TRIGGERS
Types of PL/SQL Triggers
There are two types of triggers based on the which level it is triggered.
1) Row level trigger - An event is triggered for each row upated, inserted or deleted.
2) Statement level trigger - An event is triggered for each sql statement executed

3.40 CHARACTERISTICS OF TRIGGERS


Characteristics of triggers:
 A trigger is invoked before or after a row is inserted, updated or deleted.
 Trigger may be row level or column level
 A trigger does not take parameters.
 A trigger is table dependent.
 Each table may have one or more triggers.

3.41 ADVANTAGES OF TRIGGERS


Advantages of triggers:
 It can be used to enforce complex constraints
 It can be used to ensure proper data entry. i.e. to maintain data integrity.
 It can be used to keep track of transactions on a table.
 It can be used to interrupt a transaction when it is not appropriate.
 It can replicate a table for back up process.
 It can be used to generate values for derived columns automatically.

78
[
Relational database management systems
rajanna ]

Unit-IV

Transaction Management and Concurrency Control


Transaction:
A transaction is any action that reads from and/or writes to a database.
 A transaction is a logical unit of work that must be entirely completed or entirely aborted; no
intermediate states are acceptable.
 A successful transaction changes the database from one consistent state to another.
Transaction Properties
Each transaction must have four very important properties. Those are:
1. Atomicity
2. Consistency
3. Isolation
4. Durability.
These properties are known as the ACID test. In addition, the DBMS must have the property
of serializability to control multiple transactions.
Atomicity: Atomicity requires that all operations of a transaction be completed. If not, the
transaction is aborted.
Consistency indicates the database’s consistent state. When a transaction is completed, the
database must be in a consistent state.
Isolation means that the data used during the execution of a transaction cannot be used by a
second transaction until the first one is completed. This property is particularly useful in
multiuser database environments.
Durability ensures that once transaction changes are done (committed), they cannot be
undone or
lost, even in the event of a system failure.
Serializability ensures that the schedule for the concurrent execution of the transactions
yields consistent results.
The Transaction Log
The transaction log is a critical part of the database. DBMS uses a transaction log to record
all transactions that update the database. It contains the before values and after values of each
transaction. The information stored in the transaction log is useful for database recovery
management.
Concurrency Control
The coordination of the simultaneous execution of transactions in a multiuser database
system is known as concurrency control.
The simultaneous execution of transactions can create several problems. The three main
problems are:
 Lost updates
 Uncommitted data
 Inconsistent retrievals
Lost Updates:
The lost update problem occurs when two concurrent transactions are updating the same data
element.
Uncommitted Data:
The uncommitted data problem occurs when two transactions are executed concurrently and
the first transaction (T1) is rolled back after the second transaction (T2) has already accessed
the uncommitted data.
Inconsistent Retrievals:

79
[
Relational database management systems
rajanna ]

Inconsistent retrievals occur when a transaction accesses data before and after another
transaction(s) finish working with such data.
*****
Concurrency Control with Locking Methods
A lock guarantees exclusive use of a data item to a current transaction.
Lock Granularity it indicates the level of lock use. It is of following levels:
 Database Level Locks
 Table Level Locks
 Page Level Locks
 Row Level Locks
 Field (attribute) Level Locks
Database Level
In a database-level lock, the entire database is locked. It prevents other transactions to use
any tables in the database. The following figure shows a database level lock:

Table Level
In a table-level lock, the entire table is locked. It prevents other transactions to use any row
in the tables. The following figure shows a database level lock:

Page Level
In a page-level lock, an entire diskpage is locked. It prevents other transactions to use any
page in the tables The following figure shows a page level lock:

80
[
Relational database management systems
rajanna ]

Row Level
A row-level lock allows the transactions to access different rows of the same table. The
following figure illustrates row-level lock:

Field Level
The field-level lock allows the transactions to access different fields (attributes) within a
row. Field-level locking requires high overhead. It can be rarely implemented.
***
Lock Types
The DBMS may use different lock types, such as:
 Binary Locks
 Shared/exclusive.
Binary Locks
 A binary lock has only two states: locked (1) or unlocked (0). It specifies that every transaction
requires a lock and unlock operation for each of its data items.
 If a transaction locks any database object then the other transactions are not allowed until it
unlocks that object.

Shared/Exclusive Locks
Exclusive Locks
 An exclusive lock reserves access only for the transaction that locked the object.  An
exclusive lock is issued when a transaction wants to update (write) a data item and only if
no other locks are held on the data item.
Shared Locks:
 A shared lock grants read access on the object. A shared lock is issued when a
transaction wants to read data from the database.
***

81
[
Relational database management systems
rajanna ]

Deadlock
A database deadlock is caused when two or more transactions wait for each other to
unlock data.

Two-Phase Locking Protocol


Two-phase locking protocol defines how transactions acquire and release locks. This
protocol has two phases in it. Those are:
1. A growing phase
2. A Shrinking Phase
Growing Phase:
· In this phase a transaction gets all required locks without unlocking any
data.
· Once all locks have been acquired, the transaction is in its locked point.
Shrinking phase:
· In this phase a transaction releases all locks and cannot obtain any
new lock.
The two-phase locking protocol is has the following rules:
· Two transactions cannot have conflicting locks.
· No unlock operation can precede a lock operation in the same
transaction.
· No data are affected until all locks are obtained.
The following figure shows the two-phase locking protocol:

Two-phase locking increases the transaction processing cost and might cause
deadlocks.
***
Deadlocks
A deadlock occurs when two transactions wait indefinitely for each other to unlock data. For
example, a deadlock occurs when two transactions, T1 and T2, exist in the following mode:
82
[
Relational database management systems
rajanna ]

T1 = access data items X and Y

83
[
Relational database management systems
rajanna ]

T2 = access data items Y and X


The three basic techniques to control deadlocks are:
Deadlock prevention: A transaction with a new lock request is aborted when there is the
possibility that a deadlock can occur.
Deadlock detection: The DBMS periodically tests the database for deadlocks. If a deadlock
is found, then that transaction is aborted and the other transaction continues. Deadlock
avoidance: The transaction must obtain all of the locks it needs before it can be executed.

Concurrency Control with Time-Stamping


Methods:
The time stamping approach assigns a unique time stamp to each transaction. It gives an
order for the transactions.
Time stamps must have two properties:
 Uniqueness and
 Monotonicity.
 Uniqueness ensures unique time stamp values
 Monotonicity ensures that time stamp values always increase.
There are two schemes to decide which transaction is rolled back and which should be
continued:
 The wait/die scheme
 The wound/wait scheme.
The Wait/Die scheme:
By using the Wait/Die Scheme:
If the transaction requesting the lock is the older then, it will wait until the other transaction
is completed and the locks are released. If the transaction requesting the lock is the younger,
it will die (roll back) and is rescheduled using the same time stamp. The wound/wait
scheme:
By using the Wound/Wait Scheme:
 If the transaction requesting the lock is the older then, it will pre-empt (wound)
the younger transaction. The younger transaction is rescheduled using the same
time stamp.
 If the transaction requesting the lock is the younger then, it will wait until the
other transaction is completed and the locks are released.
The disadvantage of the time stamping approach is that it increases memory needs and it
demands a lot of system resources.
****
Concurrency Control with Optimistic Methods
In the optimistic approach a transaction is executed without restrictions until it is
committed. In this approach each transaction moves into two or three phases:
1. Read Phase
2. Validation Phase
3. Write Phase
 During the read phase, the transaction reads the database and makes all its update operations in a
temporary update file.
 During the validation phase, the transaction is validated to ensure the integrity and
consistency of the database. If the validation test is positive, then the transaction goes to the
write phase. If the validation test is negative, the transaction is restarted and the changes are
rejected.
 During the write phase, the changes are permanently applied to the database.
84
[
Relational database management systems
rajanna ]

85
[
Relational database management systems
rajanna ]

Database recovery
Database recovery restores a database from an inconsistent) to a consistent state. Some
Critical events can cause damage to a database. Examples of critical events are:
Hardware/software failures: A hard disk failure, memory failure, etc are known as the Hardware
failures. The application program or operating system errors are known as software errors.
Human-caused incidents: This type of event can be of unintentional or intentional.
· An unintentional failure is caused by carelessness by end-users. Such errors include
deleting the wrong rows from a table, pressing the wrong key on the keyboard, or shutting
down the main database server by accident.
· Intentional events are of a more severe nature and are very serious. The security
threats caused by hackers and virus attacks comes under this category
 Natural disasters: This category includes fires, earthquakes, floods, and power
failures.

Transaction Recovery
Database transaction recovery is the process to recover a database from an inconsistent state
to a consistent state. There are four important concepts that affect the recovery process:
1) Write-ahead-log Protocol
2) Redundant transaction logs
3) Database Buffers
4) Database checkpoints
The write-ahead-log protocol ensures that transaction logs are always written before
any update.
Redundant transaction logs Maintains several copies of the transaction log to ensure
DBMS’s ability to recover data.
Database buffers are temporary storage areas in primary memory. These are useful to
speed up disk operations. It saves a lot of time.
For any transaction that had a ROLLBACK operation after the last checkpoint, nothing
needs to be done. Because the database was never updated. Database checkpoints are
operations in which the DBMS writes all of its updated buffers to disk.
Transaction recovery
Transaction recovery procedures will use two techniques:
1. Deferred-Write technique
2. Write-Through technique.
Transaction recovery procedures: Transaction recovery procedures will use two
techniques:
1. Deferred-Write technique
2. Write-Through technique.
Deferred-Write technique:

86
[
Relational database management systems
rajanna ]

In a deferred–write technique only the transaction log is updated. The recovery process in a
deferred-write technique has the following steps:
1. Identify the last checkpoint in the transaction log.
2. For a transaction that started and was committed before the last checkpoint, nothing needs
to be done.
3. For a transaction that performed a commit operation after the last checkpoint, the
DBMS uses the transaction log to update the database.
Write-through technique
In a write-through technique the database is immediately updated.
The recovery process in a write-through technique has the following steps:
1. Identify the last checkpoint in the transaction log.
2. For a transaction that started and was committed before the last checkpoint, nothing needs
to be done
3. For a transaction that was committed after the last checkpoint, the DBMS redoes the
transaction.
4. For any transaction that had a ROLLBACK operation after the last checkpoint, the DBMS
uses the transaction log to ROLLBACK the operations by using the “before” values in the
transaction log.
***

87
[
Relational database management systems
rajanna ]

Unit-V
DDBMS: In a distributed database, there are a number of databases that may be
geographically distributed all over the world. A distributed DBMS manages the distributed database
in a manner so that it appears as one single database to users. In the later part of the chapter, we go on
to study the factors that lead to distributed databases, its advantages and disadvantages.
A distributed database is a collection of multiple interconnected databases, which are spread
physically across various locations that communicate via a computer network.
Features
 Databases in the collection are logically interrelated with each other. Often they represent a
single logical database.
 Data is physically stored across multiple sites. Data in each site can be managed by a DBMS
independent of the other sites.
 The processors in the sites are connected via a network. They do not have any multiprocessor
configuration.
 A distributed database is not a loosely connected file system.
 A distributed database incorporates transaction processing, but it is not synonymous with a
transaction processing system.
Distributed Database Management System
A distributed database management system (DDBMS) is a centralized software system that manages a
distributed database in a manner as if it were all stored in a single location.
Features
 It is used to create, retrieve, update and delete distributed databases.
 It synchronizes the database periodically and provides access mechanisms by the virtue of
which the distribution becomes transparent to the users.
 It ensures that the data modified at any site is universally updated.
 It is used in application areas where large volumes of data are processed and accessed by
numerous users simultaneously.
 It is designed for heterogeneous database platforms.
 It maintains confidentiality and data integrity of the databases.
Factors Encouraging DDBMS:
The following factors encourage moving over to DDBMS −
 Distributed Nature of Organizational Units − Most organizations in the current times are
subdivided into multiple units that are physically distributed over the globe. Each unit requires
its own set of local data. Thus, the overall database of the organization becomes distributed.
 Need for Sharing of Data − The multiple organizational units often need to communicate with
each other and share their data and resources. This demands common databases or replicated
databases that should be used in a synchronized manner.
 Support for Both OLTP and OLAP − Online Transaction Processing (OLTP) and Online
Analytical Processing (OLAP) work upon diversified systems which may have common data.
Distributed database systems aid both these processing by providing synchronized data.

88
[
Relational database management systems
rajanna ]

 Database Recovery − One of the common techniques used in DDBMS is replication of data
across different sites. Replication of data automatically helps in data recovery if database in
any site is damaged. Users can access data from other sites while the damaged site is being
reconstructed. Thus, database failure may become almost inconspicuous to users.
 Support for Multiple Application Software − Most organizations use a variety of application
software each with its specific database support. DDBMS provides a uniform functionality for
using the same data among different platforms.
Advantages of Distributed Databases:
Following are the advantages of distributed databases over centralized databases.
Modular Development − If the system needs to be expanded to new locations or new units, in
centralized database systems, the action requires substantial efforts and disruption in the existing
functioning. However, in distributed databases, the work simply requires adding new computers and
local data to the new site and finally connecting them to the distributed system, with no interruption in
current functions.
More Reliable − In case of database failures, the total system of centralized databases comes to a
halt. However, in distributed systems, when a component fails, the functioning of the system
continues may be at a reduced performance. Hence DDBMS is more reliable.
Better Response − If data is distributed in an efficient manner, then user requests can be met from
local data itself, thus providing faster response. On the other hand, in centralized systems, all queries
have to pass through the central computer for processing, which increases the response time.
Lower Communication Cost − In distributed database systems, if data is located locally where it is
mostly used, then the communication costs for data manipulation can be minimized. This is not
feasible in centralized systems.
Adversities of Distributed Databases:
Following are some of the adversities associated with distributed databases.
 Need for complex and expensive software − DDBMS demands complex and often expensive
software to provide data transparency and co-ordination across the several sites.
 Processing overhead − Even simple operations may require a large number of
communications and additional calculations to provide uniformity in data across the sites.
 Data integrity − The need for updating data in multiple sites pose problems of data integrity.
 Overheads for improper data distribution − Responsiveness of queries is largely dependent
upon proper data distribution. Improper data distribution often leads to very slow response to
user requests.

Data Replication:
Data replication is the process of storing separate copies of the database at two or more sites. It is a
popular fault tolerance technique of distributed databases.

89
[
Relational database management systems
rajanna ]

Advantages of Data Replication:


 Reliability − In case of failure of any site, the database system continues to work since a copy
is available at another site(s).
 Reduction in Network Load − Since local copies of data are available, query processing can
be done with reduced network usage, particularly during prime hours. Data updating can be
done at non-prime hours.
 Quicker Response − Availability of local copies of data ensures quick query processing and
consequently quick response time.
 Simpler Transactions − Transactions require less number of joins of tables located at
different sites and minimal coordination across the network. Thus, they become simpler in
nature.
Disadvantages of Data Replication:
 Increased Storage Requirements − Maintaining multiple copies of data is associated with
increased storage costs. The storage space required is in multiples of the storage required for a
centralized system.
 Increased Cost and Complexity of Data Updating − Each time a data item is updated, the
update needs to be reflected in all the copies of the data at the different sites. This requires
complex synchronization techniques and protocols.
 Undesirable Application – Database coupling − If complex update mechanisms are not used,
removing data inconsistency requires complex co-ordination at application level. This results
in undesirable application – database coupling.
Some commonly used replication techniques are −

 Snapshot replication
 Near-real-time replication
 Pull replication
Fragmentation:
Fragmentation is the task of dividing a table into a set of smaller tables. The subsets of the table are
called fragments. Fragmentation can be of three types: horizontal, vertical, and hybrid (combination
of horizontal and vertical). Horizontal fragmentation can further be classified into two techniques:
primary horizontal fragmentation and derived horizontal fragmentation.
Fragmentation should be done in a way so that the original table can be reconstructed from the
fragments. This is needed so that the original table can be reconstructed from the fragments whenever
required. This requirement is called “reconstructiveness.”
Advantages of Fragmentation:
 Since data is stored close to the site of usage, efficiency of the database system is increased.
 Local query optimization techniques are sufficient for most queries since data is locally
available.
 Since irrelevant data is not available at the sites, security and privacy of the database system
can be maintained.
Disadvantages of Fragmentation:
 When data from different fragments are required, the access speeds may be very high.
 In case of recursive fragmentations, the job of reconstruction will need expensive techniques.
90
[
Relational database management systems
rajanna ]

 Lack of back-up copies of data in different sites may render the database ineffective in case of
failure of a site.
Vertical Fragmentation:
In vertical fragmentation, the fields or columns of a table are grouped into fragments. In order to
maintain reconstructiveness, each fragment should contain the primary key field(s) of the table.
Vertical fragmentation can be used to enforce privacy of data.
For example, let us consider that a University database keeps records of all registered students in a
Student table having the following schema.
STUDENT:

Regd_No Name Course Address Semester Fees Marks

Now, the fees details are maintained in the accounts section. In this case, the designer will fragment
the database as follows −
CREATE TABLE STD_FEES AS
SELECT Regd_No, Fees
FROM STUDENT;

Client - Server Architecture for DDBMS:


This is a two-level architecture where the functionality is divided into servers and clients. The
server functions primarily encompass data management, query processing, optimization and
transaction management. Client functions include mainly user interface. However, they have some
functions like consistency checking and transaction management.
The two different client - server architecture are −

 Single Server Multiple Client


 Multiple Server Multiple Client (shown in the following diagram)

91
[
Relational database management systems
rajanna ]

Peer- to-Peer Architecture for DDBMS


In these systems, each peer acts both as a client and a server for imparting database services. The
peers share their resource with other peers and co-ordinate their activities.

This architecture generally has four levels of schemas –

 Global Conceptual Schema − Depicts the global logical view of data.


 Local Conceptual Schema − Depicts logical data organization at each site.
 Local Internal Schema − Depicts physical data organization at each site.
 External Schema − Depicts user view of data.

92
[
Relational database management systems
rajanna ]

Multi - DBMS Architectures:


This is an integrated database system formed by a collection of two or more autonomous
database systems.
Multi-DBMS can be expressed through six levels of schemas −
 Multi-database View Level − Depicts multiple user views comprising of subsets of the
integrated distributed database.
 Multi-database Conceptual Level − Depicts integrated multi-database that comprises of
global logical multi-database structure definitions.
 Multi-database Internal Level − Depicts the data distribution across different sites and multi-
database to local data mapping.
 Local database View Level − Depicts public view of local data.
 Local database Conceptual Level − Depicts local data organization at each site.
 Local database Internal Level − Depicts physical data organization at each site.
There are two design alternatives for multi-DBMS −

 Model with multi-database conceptual level.


 Model without multi-database conceptual level.

93
[
Relational database management systems
rajanna ]

94
[
Relational database management systems
rajanna ]

Design Alternatives:
The distribution design alternatives for the tables in a DDBMS are as follows −

 Non-replicated and non-fragmented


 Fully replicated
 Partially replicated
 Fragmented
 Mixed
Non-replicated & Non-fragmented:
In this design alternative, different tables are placed at different sites. Data is placed so that it is at a
close proximity to the site where it is used most. It is most suitable for database systems where the
percentage of queries needed to join information in tables placed at different sites is low. If an
appropriate distribution strategy is adopted, then this design alternative helps to reduce the
communication cost during data processing.
Fully Replicated:
In this design alternative, at each site, one copy of all the database tables is stored. Since, each site has
its own copy of the entire database, queries are very fast requiring negligible communication cost. On
the contrary, the massive redundancy in data requires huge cost during update operations. Hence, this
is suitable for systems where a large number of queries is required to be handled whereas the number
of database updates is low.
Partially Replicated:
Copies of tables or portions of tables are stored at different sites. The distribution of the tables is done
in accordance to the frequency of access. This takes into consideration the fact that the frequency of
accessing the tables vary considerably from site to site. The number of copies of the tables (or
portions) depends on how frequently the access queries execute and the site which generate the access
queries.
Fragmented:
In this design, a table is divided into two or more pieces referred to as fragments or partitions, and
each fragment can be stored at different sites. This considers the fact that it seldom happens that all
data stored in a table is required at a given site. Moreover, fragmentation increases parallelism and
provides better disaster recovery. Here, there is only one copy of each fragment in the system, i.e. no
redundant data.
The three fragmentation techniques are −

 Vertical fragmentation
 Horizontal fragmentation
 Hybrid fragmentation

95
[
Relational database management systems
rajanna ]

Mixed Distribution:
This is a combination of fragmentation and partial replications. Here, the tables are initially
fragmented in any form (horizontal or vertical), and then these fragments are partially replicated
across the different sites according to the frequency of accessing the fragments.

Structure of a Client Server System:


In client server computing, the clients requests a resource and the server provides that resource.
A server may serve multiple clients at the same time while a client is in contact with only one server.
The different structures for two tier and three tier are given as follows −
Two - Tier Client/Server Structure
The two tier architecture primarily has two parts, a client tier and a server [Link] client tier sends a
request to the server tier and the server tier responds with the desired information.

An example of a two tier client/server structure is a web server. It returns the required web pages to the
clients that requested them.
An illustration of the two-tier client/server structure is as follows −

Advantages of Two - Tier Client/Server Structure


Some of the advantages of the two-tier client/server structure are −

 This structure is quite easy to maintain and modify.


 The communication between the client and server in the form of request response messages is
quite fast.
Disadvantages of Two - Tier Client/Server Structure
A major disadvantage of the two-tier client/server structure is −

96
[
Relational database management systems
rajanna ]

 If the client nodes are increased beyond capacity in the structure, then the server is not able to
handle the request overflow and performance of the system degrades.
Three - Tier Client/Server Structure
The three tier architecture has three layers namely client, application and data layer. The client layer is
the one that requests the information. In this case it could be the GUI, web interface etc. The
application layer acts as an interface between the client and data layer. It helps in communication and
also provides security. The data layer is the one that actually contains the required data.
An illustration of the three-tier client/server structure is as follows −

Advantages of Three - Tier Client/Server Structure


Some of the advantages of the three-tier client/server structure are −

 The three tier structure provides much better service and fast performance.
 The structure can be scaled according to requirements without any problem.
 Data security is much improved in the three tier structure.
Disadvantages of Three - Tier Client/Server Structure
A major disadvantage of the three-tier client/server structure is −

 Three - tier client/server structure is quite complex due to advanced features.

****

97

You might also like