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

Mca DBMS

Uploaded by

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

Mca DBMS

Uploaded by

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

UNIT – I:: FUNDAMENTALS OF DATABASE CONCEPTS:

Introduction:
Data: Data is referred to known facts (Unorganized) that could be recorded on computer media. Data
consists of text, graphics, images, audio, video etc. that have meaning in the user’s environment.
Database: We can define a database as a well-organized collection of logically related data.

DBMS (Database Management System): A software application that is used to create, maintain, and
provide controlled access on databases to users.

Characteristics of Database Approach

 The database approach has proven far better than the traditional file management
system. Database Approach has many characteristics that make it more robust in nature.

1. Represent Some Aspects of real world applications


 A database represents some features of real world applications. Any change in the real world is
reflected in the database. If we have some changes in our real applications like railway
reservation system then it will be reflected in database too.
 For example, let us take railway reservation system; we have in our mind some certain
applications of maintaining records of attendance, waiting list, train arrival and departure time,
certain day etc. related to each train.
2. Manages Information
 A database always takes care of its information because information is always helpful for
whatever work we do. It manages all the information that is required to us. By managing
information using a database, we become more deliberated user of our data.
3. Easy Operation implementation
 All the operations like insert, delete, update, search etc. are carried out in a flexible and easy way.
Database makes it very simple to implement these operations. A user with little knowledge can
perform these operations. This characteristic of database makes it more powerful.
4. Multiple views of database
 Basically, a view is a subset of the database. A view is defined and devoted for a particular user
of the system. Different users of the system may have different views of the same system.
 Every view contains only the data of interest to a user or a group of users. It is the responsibility
of users to be aware of how and where the data of their interest is stored.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:1


5. Data for specific purpose
 A database is designed for data of specific purpose. For example, a database of student
management system is designed to maintain the record of student’s marks, fees and attendance
etc. This data has a specific purpose of maintaining student record.
6. It has Users of Specific interest
 A database always has some indented group of users and applications in which these user groups
are interested.
 For example, in a library system, there are three users, official administration of the college, the
librarian, and the students.
7. Self Describing nature
 A database is of self describing nature; it always describes and narrates itself. It contains the
description of the whole data structure, the constraints and the variables.
 It makes it different from traditional file management system in which definition was not the part
of application program. These definitions are used by the users and DBMS software when
needed.
8. Logical relationship between records and data
 A database gives a logical relationship between its records and data. So a user can access various
records depending upon the logical conditions by a single query from the database.
9. Shelter between program and data
 In traditional file management system, if any user makes changes in the structure of a file then all
the programs accessed by that file needed to be changed. The structure of data files is defined by
the application programs.
 But in database approach the data structure is carried in system catalog not in the program. So
user doesn’t need to make changes in all the programs.
Advantages of Database Approach
i) Improved Data Sharing:
The database approach provides end-users to have better access to more data and better-managed
data.
ii) Improved Data Security:
DBMS provides better facilities for data privacy and security policies.
iii) Better Data Integration:
As DBMS provides the advantage of data sharing, users can have wider access to well-managed
data that provides integrated view of the entire organization’s data operations.
iv ) Minimized Data Inconsistency (or) Improved Data Consistency:
Data Inconsistency exists when different copies of the same data appear in different places. As
we eliminate or reduce the redundancy of data, we improve the consistency of data.
v) Improved Data Access: DBMS makes it possible to produce quick answers to the queries
posted (requested) by end users.
vi) Improved Decision Making:
Better managed data and improved data access make it possible to generate better quality of
information, which improves Decision Making.
vii) Increased End-User Productivity:
End-users can make quick and informed decisions that can make the difference between success
and failure.
viii) Program-Data Independence:
User has the flexibility to change the metadata without changing the application programs that
process the data.
S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:2
ix) Minimal data redundancy:
Data are integrated into a single, logical unit. Each primary fact is recorded in only one place in
the database. So data redundancy can be minimized.
x) Improved Data Quality:
The database approach provides number of tools to improve data quality. For example Database
designers can specify integrity constraints.
xi) Reduced Program Maintenance:
The data that is stored in the database need not to be changed frequently. As in a database
approach, data is more independent of the application programs we can make the required
changes for application programs very easily.

A Brief History of Database Applications


1. Early Database Applications Using Hierarchical and Network Systems: Many early database
applications maintained records in large organizations, such as corporations, universities,
hospitals, and banks. In many of these applications, there were large numbers of records of
similar structure. One of the main problems with early database systems was the intermixing of
conceptual relationships with the physical storage and placement of records on disk. Another
shortcoming of early systems was that they provided only programming language interfaces. This
made it time-consuming and expensive to implement new queries and transactions, since new
programs had to be written, tested, and debugged.
2. Providing Application Flexibility with Relational Databases : Relational databases were
originally proposed to separate the physical storage of data from its conceptual representation
and to provide a mathematical foundation for databases. The relational data model also
introduced high-level query languages that provided an alternative to programming language
interfaces; hence, it was a lot quicker to write new queries. Eventually, relational databases
became the dominant type of database systems for traditional database applications. Relational
databases now exist on almost all types of computers, from small personal computers to large
servers.
3. Object-Oriented Applications and the Need for More Complex Databases : The emergence
of object-oriented programming languages in the 1980s and the need to store and share complex-
structured objects led to the development of object-oriented databases. Initially, they were
considered a competitor to relational databases, since they provided more general data structures.
They also incorporated many of the useful object oriented paradigms, such as abstract data types,
encapsulation of operations, inheritance, and object identity. However, the complexity of the
model and the lack of an early standard contributed to their limited use. They are now mainly
used in specialized applications, such as engineering design, multimedia publishing, and
manufacturing systems.
4. Interchanging Data on the Web for E-Commerce : The World Wide Web provided a large
network of interconnected computers. Users can create documents using a Web publishing
language, such as HTML (Hyper Text Markup Language), and store these documents on Web
servers where other users (clients) can access them. Documents can be linked together through
hvper links, which are pointers to other documents. A variety of techniques were developed to
allow the interchange of data on the Web. Currently, XML (eXtended Markup Language) is
considered to be the primary standard for interchanging data among various types of databases
and Web pages. XML combines concepts from the models used in document systems with
database modeling concepts.
5. Extending Database Capabilities for New Applications : The success of database systems in
traditional applications encouraged developers of other types of applications to attempt to use
them. Such applications traditionally used their own specialized file and data structures.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:3


Database Applications
 Database applications are software programs designed to collect, manage and disseminate
information efficiently. Many home and small business owners create simple databases such
as customer contact and mailing lists with easy to use software such as Microsoft "Access"
and "FileMaker Pro." "Oracle," "SQL Server," and "FoxPro" are examples of advanced
database applications with programming languages that can be used to build custom business
solutions in networked environments.
PURPOSE
Database applications are used to search, sort, calculate, report and share information. Databases can
also contain code to perform mathematical and statistical calculations on the data to support queries
submitted by users. Database applications provide security by restricting access to data based upon user
names and passwords. Most database applications are customized with a database programming
language to automate specific types of work.

ACCOUNTING APPLICATIONS
An accounting system is a custom database application used to manage financial data. Custom forms are
used to record assets, liabilities, inventory and the transactions between customers and suppliers. The
income statements, balance sheets, purchase orders and invoices generated are custom reports based
upon information that is entered into the database. Accounting applications can run on a single computer
suitable for a small business or in a networked shared environment to accommodate the needs of
multiple departments and locations in larger organizations. "Microsoft Money," "Quicken,"
"QuickBooks" and "Peachtree" are accounting systems built upon database applications.
CRM APPLICATIONS
A customer relationship management system (CRM) is another example of a database application that
has been customized to manage the marketing, sales, and support relationships between a business and
it's customers. The ultimate goal is to maximize sales, minimize costs and foster strategic customer
relationships. Simple contact management programs such as "ACT," or the task manager in Microsoft's
"Outlook" can be customized to suit the needs of individuals and small businesses. "SAP,"
"Salesforce.com," and Oracle's "Siebel" are robust CRM database applications suitable for larger
enterprises.
WEB APPLICATIONS
Many contemporary web sites are built using several database applications simultaneously as core
components. Most retail store Web sites including "Bestbuy.com," and "Amazon.com" use database
systems to store, update and present data about products for sale. These Web sites also combine an
accounting database system to record sales transactions and a CRM database application to incorporate
feedback and drive a positive customer experience. The popular Web-based "Facebook" application is
essentially a database built upon the "MySQL" database system and is an indication of the increasing
usage of database applications as foundations for Web-based applications.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:4


Chapter 2: Database System Concepts and Architecture:
Data Models
Data Modeling is the first step in database designing process. A data model is a collection of conceptual tools for
describing data, relationships, semantics and consistency constraints. There are the following data models to
represent the structure of a database.
Hierarchical Model: Hierarchical Model was developed to manage large amounts of data for complex projects.
In this model, the basic logical structure is represented by a top-down tree structure. This structure contains
several levels where each level contains several record types in file system.
Example:

In the above representation one level is specially designated as root. Every segment in the hierarchy will
have a parent and every parent can have multiple children. Thus the relationship between a parent and its children
is 1:M.
Network Model: In Network Model, the user views the database as a collection of records in 1:M relationships.
Network Model allows a record to have more than one parent.
Example:

Relational Model: Relational Model


uses a collection of tables to represent
both data and the relationships among
those data. Each table has multiple
columns and each column has a unique
name.

Entity Relational Model: Entity Relationship Data Model consists of a collection of objects called entities and
relationships among those entities.
An Entity is anything like a Person, Place, Object, Event or Concept in the user environment about which
data to be collected and stored.
Example:
Person: Employee, Student
Place : Store, State

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:5


Object : Building, Chair
An Attribute is a property or characteristic of a entity. Each entity has a set of attributes associated with it.
Example: Student : Stud_ID, Stud_Name, Address, Phone
A Relationship is an association among the instances of one or more entities.

Object-Oriented Model:
In Object-Oriented data model both data and their relationships are contained in a single structure called object.

DATABASE SCHEMA :
In the SQL environment, a schema is a group of database objects such as tables, indexes, views that are
related to each other. Usually the schema belongs to single user or application. A single database can hold
multiple schemas belonging to different users or applications. Schemas are useful as they group tables by owner
and enforce a first level security for them. Most of the RDBMSs automatically create a schema for each user
whenever any new user is created.
Syntax:
CREATE USER <User Name> IDENTIFIED BY <Password>;
GRANT <Privilege List> PRIVILEGES TO <User Name>;
Example:
CREATE USER Student IDENTIFIED BY Stu;
GRANT ALL PRIVILEGES TO Student;
Three schema architecture:

Design of a database is called the schema. Schema is of three types: Physical schema, logical schema
and view schema.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:6


 The design of a database at physical level is called physical schema or internal schema,
how the data stored in blocks of storage is described at this level.
 Design of database at logical level is called logical schema or conceptual schema,
programmers and database administrators work at this level, at this level data can be
described as certain types of data records gets stored in data structures, however the internal
details such as implementation of data structure is hidden at this level (available at physical
level).
 Design of database at view level is called view schema or external schema. This generally
describes end user interaction with database systems.
Definition of instance:
 The environment of database is said to be instance. A database instance or an ‘instance’ is made up of the
background processes needed by the database software. These processes usually include a process monitor,
session monitor, lock monitor, etc. They will vary from database vendor to database vendor.
 A database instance (Server) is a set of memory structure and background processes that access a set of
database files. The process can be shared by all users. The memory structure that are used to store most
queried data from database. This helps up to improve database performance by decreasing the amount of I/O
performed against data file.
 It includes RDBMS software, table structure, stored procedures and other functions. It is normally used when
administrators tend to describe multiple instances of the same database. It is also called as environment in
technical terms. You can better understand its workings with the help of an example. The example is of an
organization that has an employee database.
This database will have three instances, which are
- Production That is used for storing live data
- pre-production which is used to test new functionality prior to release for production
- development which is used by database developers in order to create new functionality.
Data Independence
A database system normally contains a lot of data in addition to users’ data. For example, it stores data about data,
known as metadata, to locate and retrieve data easily. It is rather difficult to modify or update a set of metadata once it is
stored in the database. But as a DBMS expands, it needs to change over time to satisfy the requirements of the users. If
the entire data is dependent, it would become a tedious and highly complex job.

Metadata itself follows a layered architecture, so that when we


change data at one layer, it does not affect the data at another
level. This data is independent but mapped to each other.

Logical Data Independence

Logical data is data about database, that is, it stores information


about how data is managed inside. For example, a table (relation)
stored in the database and all its constraints, applied on that
relation.

Logical data independence is a kind of mechanism, which


liberalizes itself from actual data stored on the disk. If we do some
changes on table format, it should not change the data residing on the disk.

Physical Data Independence

All the schemas are logical, and the actual data is stored in bit format on the disk. Physical data independence is the
power to change the physical data without impacting the schema or logical data.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:7


Database Languages and Interfaces
 Because a database supports a number of user groups, as mentioned previously, the DBMS must
have languages and interfaces that support each user group.

DBMS Languages

 DDL – the data definition language, used by the DBA and database designers to define the
conceptual and internal schemas.

 The DBMS has a DDL compiler to process DDL statements in order to identify the schema
constructs, and to store the description in the catalogue.

 In databases where there is a separation between the conceptual and internal schemas, DDL is
used to specify the conceptual schema, and SDL, storage definition language, is used to specify
the internal schema.

 For true three-schema architecture, VDL, view definition language, is used to specify the user
views and their mappings to the conceptual schema. But in most DBMSs, the DDL is used to
specify both the conceptual schema and the external schemas.
 Once the schemas are compiled, and the database is populated with data, users need to
manipulate the database. Manipulations include retrieval, insertion, deletion and modification.
 The DBMS provides operations using the DML, data manipulation language.
 In most DBMSs, the VDL, DML and the DML are not considered separate languages, but a
comprehensive integrated language for conceptual schema definition, view definition and data
manipulation. Storage definition is kept separate to fine-tune the performance, usually done by
the DBA staff.
 An example of a comprehensive language: SQL, which represents a VDL, DDL, DML as well as
statements for constraint specification, etc.

Data Manipulation Languages (DMLs)


Two main types:

High-level/Non procedural
 Can be used on its own to specify complex database operations.
 DMBSs allow DML statements to be entered interactively from a terminal, or to be embedded in
a programming language. If the commands are embedded in a general purpose programming
language, the statements must be identified so they can be extracted by a pre-compiler and
processed by the DBMS.

Low Level/Procedural
 Must be embedded in a general purpose programming language.
 Typically retrieves individual records or objects from the database and processes each separately.
 Therefore it needs to use programming language constructs such as loops.
 Low-level DMLs are also called record at a time DMLS because of this.
 High-level DMLs, such as SQL can specify and retrieve many records in a single DML
statement, and are called set at a time or set oriented DMLs.
 High-level languages are often called declarative, because the DML often specifies what to
retrieve, rather than how to retrieve it.

DML Commands
 When DML commands are embedded in a general purpose programming language, the
programming language is called the host language and the DML is called the data sub-
language.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:8


 High-level languages used in a standalone, interactive manner is called a query language.
 Casual end users use high-level query language to specify requests, where programmers usually
use embedded DML.
 Parametric end users usually interact with user-friendly interfaces, which can also be used by
casual users who don’t want to learn the high-level languages.
DBMS Interfaces
Types of interfaces provided by the DBMS include:
Menu-Based Interfaces for Web Clients or Browsing
 Present users with list of options (menus)
 Lead user through formulation of request
 Query is composed of selection options from menu displayed by system.

Forms-Based Interfaces
 Displays a form to each user.
 User can fill out form to insert new data or fill out only certain entries.
 Designed and programmed for naïve users as interfaces to canned transactions.

Graphical User Interfaces


 Displays a schema to the user in diagram form. The user can specify a query by manipulating the
diagram. GUIs use both forms and menus.

Natural Language Interfaces


 Accept requests in written English, or other languages and attempt to understand them.
 Interface has its own schema, and a dictionary of important words. Uses the schema and
dictionary to interpret a natural language request.

Interfaces for Parametric Users


 Parametric users have small set of operations they perform.
 Analysts and programmers design and implement a special interface for each class of naïve users.
 Often a small set of commands included to minimize the number of keystrokes required. (I.e.
function keys)

Interfaces for the DBA


 Systems contain privileged commands only for DBA staff.
 Include commands for creating accounts, setting parameters, authorizing accounts, changing the
schema, reorganizing the storage structures etc.

COMPONENTS OF THE DATABASE ENVIRONMENT:

Data System End


Administrators Developers users

CASE User Application

tools interface programs


Repository DBMS
Database

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:9


The major components of a typical database environment and their relations
1) Computer-aided software engineering (CASE) tools: Automated tools used to design databases
and application programs.
2) Repository: A repository is a centralized knowledge base of all data definitions, data relationships,
screen and report formats and other system components.
3) Database management system (DBMS): Commercial software system is used to define, create,
maintain and provide controlled access to the database and also to the repository.
4) Database: An organized collection of logically related data, is called a database.
5) Application programs: The computer programs that are used to create and maintain the database
and provide information to the users are called application programs.
6) User interface: The languages, menus and other facilities by which user interact with various
system components.
7) Data administrators: The persons who are responsible for the overall information resources of an
organization are called data administrators.
8) System developers: The persons such as systems analysts and programmers who design new
application programs are called system developers.
9) End users: The persons throughout the organization who add, delete and modify data in the database
and who request or receive information from it are called end users.

DBMS SYSTEM ENVIRONMENT

1. DBMS Component modules:

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:10


The term database system refers to an organization of components that define and regulate the collection,
storage, management, and use of data within a database environment.
These are:
• Hardware
• Software
• People
• Procedures
• Data
The figure is divided into two parts. The top half of the diagram refers to the various users of the
database environment and their interfaces. The lower half demonstrates the internals of the DBMS
responsible for storage of data and processing of transaction.

 The database and the DBMS catalogue are usually stored on disk. Access to the disk is
principally controlled by operating system (OS). This includes disk input/Output. A higher point
stored data manager module of DBMS controls access to DBMS information that is stored on the
disk.
The top part of the figure it shows interface to casual users, DBA staff, application programmers and
parametric users.
 The DDL compiler specified in the DDL, processes schema definitions as well as stores the
description of the schema in the DBMS Catalogue. The catalogue includes information such as
names and sizes of the sizes of the files and data types of data of data items.
 Storage particulars of every file mapping information among schemas as well as constraints.
 Casual users as well as persons with occasional need of information from database interact using
some of interface which is interactive query interface. The queries are parsed analyse for
correctness of the operations for the model. The names of the data elements as well as therefore
on by a query compiler that compiles them into internal form. The interior query is subjected to
query optimization. The query optimizer is worried with rearrangement and possible recording of
operations and eliminations of redundancies.
 Application programmer inscribes programs in host languages. The precompiled take out DML
commands from an application program.
2. Database System Utilities

 Loading: A loading utility is used to load existing data files-such as text files or sequential files-
into the database. Usually, the current (source) format of the data file and the desired (target)
database file structure are specified to the utility, which then automatically reformats the data and
stores it in the database. With the proliferation of DBMSs, transferring data from one DBMS to
another is becoming common in many organizations. Some vendors are offering products that
generate the appropriate loading programs, given the existing source and target database storage
descriptions (internal schemas). Such tools are also called conversion tools.
 Backup: A backup utility creates a backup copy of the database, usually by dumping the entire
database onto tape. The backup copy can be used to restore the database in case of catastrophic
failure. Incremental backups are also often used, where only changes since the previous backup
are recorded. Incremental backup is more complex but saves space.
 File Reorganization: This utility can be used to reorganize a database file into a different file
organization to improve performance.
 Performance Monitoring: Such a utility monitors database usage and provides statistics to the
DBA. The DBA uses the statistics in making decisions such as whether or not to reorganize files
to improve performance.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:11


3. Tools, Application Environment and Communication Facilities
1. Tools:
 Computer-aided software engineering (CASE) tools: Automated tools used to design
databases and application programs.
 Another tool is data Dictionary or repository is used for storing catalog information, constraints,
design decisions, application programs description and user information.
2. Application Environment:
 Application developments Environment such as PowerBuilder or JBuilder system provides an
environment for developing database applications and includes facilities that help in database
design, GUI development, querying and updating and application program development.
3. Communication:
 User interface: The languages, menus and other facilities by which user interact with various
system components.
 The communication software, whose function is to allow users at locations, remote from the
database system site to access the database through computer terminals, work stations or local
personal computers.

Centralized and Client/Server Architecture for DBMS


1. Centralized DBMS Architecture:
The centralized database system consists of a single processor together with its associated data storage
devices and other peripherals. It is physically confined to a single location. Data can be accessed from
the multiple sites with the use of a computer network while the database is maintained at the central site.

Disadvantages of Centralized Database System


• When the central site computer or database system goes down, then every one (users) is blocked from
using the system until the system comes back.
• Communication costs from the terminals to the central site can be expensive.
2. Basic Client/server Architecture
Database architecture is logically divided into two types.
1. Two-tier Client / Server architecture
1. Three-tier Client / Server architecture

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:12


Two-tier Client / Server Architecture

Two-tier Client / Server architecture is used for User Interface program and Application Programs that
runs on client side. An interface called ODBC (Open Database Connectivity) provides an API that allow
client side program to call the dbms. Most DBMS vendors provide ODBC drivers. A client program may
connect to several DBMS's. In this architecture some variation of client is also possible for example in
some DBMS's more functionality is transferred to the client including data dictionary, optimization etc.
Such clients are called Data server.
Three-tier Client / Server Architecture

Three-tier Client / Server database architecture is


commonly used architecture for web applications.
Intermediate layer called Application server or
Web Server stores the web connectivity software
and the business logic (constraints) part of
application used to access the right amount of data
from the database server. This layer acts like
medium for sending partially processed data
between the database server and the client.
• First Layer: User Interface which runs on end-
user's computer (the client) .
• Second Layer: Application Server It is a
business logic and data processing layer. This
middle tier runs on a server which is called as
Application Server.
• Third Layer: Database Server It is a DBMS, which stores the data required by the middle tier. This
tier may run on a separate server called the database server.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:13


Classification of Database Management Systems.
Databases can be classified into different types according to the Data model, number of users, location or
number of sites, cost and type of access path of the database.
1. Date Model:
 The main data model used in many current commercial DBMSs is relational model.
 The object data model was implemented in some commercial systems.
 Many older applications still run on Hierarchical and Network data models.
 The Relational data models continuously incorporating many concepts developed in object
databases. This leads to a new class of DBMS called object-relational DBMSs.
 The XML model is the standard for data interchange over internet.
2. Based on Number of Users
We can categorize databases into two types. They are
1. Single User Systems
Personal databases: Personal databases are designed to support one user.
2. Multi-user systems : support multiple users concurrently.
 Workgroup databases: A workgroup database is designed to support the joint efforts of Group
of users.
 Department database: Department databases are designed to support the various functions
(works) and activities of a department.
 Enterprise database: Enterprise databases are introduced to support organization wide
operations and decision-making.
 Internet, Intranet and Extranet databases.
3. Based on Location of database,
1. Centralized database
2. Distributed database
1) Centralized Database:
The database that supports data located at a single site is called as centralized databas2).
2) Distributed Databases:
The database that supports data that is distributed across several different sites is called as
“Distributed Database”.
4. Bases on Cost:
 A single-user low-end systems that work with micro computers cost between $100 to $300.
 A few elaborate packages cost more than $1,00,000
5. Based on type of use database
1. Operational Database:
The database that is designed basically to support day-to-day operations of an organization is called as
“Operational Databases”. They are also called as Transactional Databases or
Product ional Databases.
2. Dataware house:
Dataware houses basically focus on storing data used to generate information that is needed to make
strategic (planned) decisions. They are used in Decision Support Systems.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:14


Chapter 3: Data Modeling Using the Entity Relationship Model
:– An Example Database Application – Entity Types, Entity Sets, Attributes, and Keys – Relationship
Types, Relationship Sets, Roles, and Structural Constraints – Weak Entity Types – ER Diagrams,
naming Conventions, and Design Issues

Using High-Level Conceptual Data Models for Database Design

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:15


 The first step is requirements collection and analysis. At some stage to recognize and the
requirements are certified by the users that’s why the database designers are interrogate. When
we are identifying the data requirements in parallel it is necessary to identify the functional
requirements which we are acknowledged. These user-defined operations include both retrievals
and update.
 The data flow diagrams, sequence diagrams are frequently used in the software design. And some
of the functional requirements are identified. All of these requirements are examined together one
time. And then by using a high-level conceptual data model then generate a conceptual schema in
next step. The users’ database requirements are illustrated briefly in the conceptual schema the
complete descriptions of the entity types, relationships and constraints are also contained. By
using a commercial DBMS the database is implemented really in the next stage of the database
design.
 From a high level data model, the conceptual schema is changed into the implementation data
model when the implementation of the data model is used by the current commercial DBMS in
many ways. This is known as logical design or data model mapping. The physical design phase
is the very last step. Individually the internal storage structures, access paths, and file
organizations.
A Sample Database Application- COMPANY Database:
 Employees, departments, and projects
 Company is organized into departments
 Department controls a number of projects
 Employee: store each employee’s name, Social
 Security number, address, salary, sex (gender), and birth date, Keep track of the dependents of
each employee.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:16


Entity-Relationship Model
An Entity-Relationship Model (ERM) is a detailed, logical representation of the data for an organization.
The E-R model is expressed in terms of entities in the business environment, the relationships among
those entities and the attributes of both the entities and the relationships.
Entity:
An Entity is an object that has a definite meaning in the user’s environment about which the organization
wishes to maintain data. Example: EMPLOYEE, STUDENT, BUILDING
An entity may be a strong entity or a weak entity. The entity, which has a key/identifier is called strong
entity where as the entity, which does not have any key/identifier is called a weak entity.
Notation:

Example:

Attributes:
An Attribute is a property or characteristic of an Entity type. Based on the importance and behaviour,
attributes are divided into the following types.

Required Attributes:
A required attribute is an attribute that must have a value for each entity occurrence
of an entity set. So a required attribute of an entity set should not be left empty.
Optional Attribute:
An optional attribute is an attribute that may or may not have value for each
occurrence of an entity set. So an optional attribute can be left empty also.
Identifier:
An identifier is an attribute or set of attributes that uniquely identifies each instance of
an entity set. The attributes that are part of an identifier are underlined in ER Diagrams. When the
identifier contains only one key attribute, then it is called simple identifier otherwise it is called
composite identifier.
Composite Attribute:
The attribute that can be sub-divided to yield additional attributes is called as a
“Composite Attribute”.
Simple Attribute:
The attribute that cannot be sub-divided to yield additional attributes is called as a
“Simple Attribute”.
Single-Valued Attribute:
A single-valued attribute can have only one value for a given entity instance.
Multi-Valued Attribute:
A multi-valued attribute can have more than one value for a given entity instance.
Stored Attribute:
A stored attribute is an attribute about which the designer wants to store data in the
database.
Derived Attribute:
A derived attribute is an attribute whose value can be calculated from other stored
attributes.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:17


Notation:

Relationships:
A relationship is a meaningful association between entities. Each relationship is
identified by a name that describes the relationship. The entities that participate in a relationship are
called as participants.
The relationship between two entities might be a strong relationship or a weak
relationship. The relationship between two strong entities is called as strong relationship or identifying
relationship and the relationship between two weak entities or one strong and one weak entity is called
as weak relationship or non-identifying relationship.
Notation:

Relationship Participation:
 The participation in an entity relationship is either Optional or Mandatory.
 Optional participation of a relationship means that the occurrence of one entity does not require a
corresponding entity occurrence in that particular relationship.
 Mandatory participation of a relationship means that the occurrence of one entity requires a
corresponding entity occurrence in that particular relationship.

Example : If there are two entities PROFESSOR and CLASS, some Professors who conduct research
without teaching any class, but each class must be conducted by a professor. Hence the entity CLASS is
optional for the entity PROFESSOR in the relation “PROFESSOR teaches CLASS”, the entity
PROFESSOR is mandatory for the entity CLASS in the same relation.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:18


RELATIONSHIP DEGREE:
The degree of a relationship indicates that the number of entities that participate in
that relationship. In entity relationship modeling, we can see the following types of relationships based
on the number of entities that participate in that relationship.

Unary Relationship:
When there is an
association is maintained with only one
entity in a relationship, then it is called
“Unary Relationship.”

Binary Relationship:
When there is an
association is maintained between two
entities in a relationship, then it is called
“Binary Relationship.” Majority of the
relationships are Binary relationships.

Ternary Relationship:
When there is an
association is maintained among three entities
in a relationship, then it is called “Ternary
Relationship.”

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:19


UNIT – II :: FUNDAMENTALS OF RELATIONAL MODEL
Chapter 1: The Relational Data Model and Relational Database Constraints:
Relation Data Model
Relational data model is the primary data model, which is used widely around the world for data storage
and processing. This model is simple and it has all the properties and capabilities required to process
data with storage efficiency.
Concepts
Tables − In relational data model, relations are saved in the format of Tables. This format stores the
relation among entities. A table has rows and columns, where rows represents records and columns
represent the attributes.
Tuple − A single row of a table, which contains a single record for that relation is called a tuple.
Relation instance − A finite set of tuples in the relational database system represents relation instance.
Relation instances do not have duplicate tuples.
Relation schema − A relation schema describes the relation name (table name), attributes, and their
names.
Relation key − Each row has one or more attributes, known as relation key, which can identify the row
in the relation (table) uniquely.
Attribute domain − Every attribute has some pre-defined value scope, known as attribute domain.

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.
 A key attribute cannot 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.

In relational data model, the understanding and usage of keys is very important. Keys are used to identify
individual records of table uniquely. They are also used to represent relationships between tables and maintain
integrity of data.
Key:
A Key is an attribute or combination of attributes that identifies other attributes uniquely.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:20


Example: If the relation EMPLOYEE contains attributes such as Emp_ID, Emp_Name, Emp_Desi, Emp_Dept,
Emp_Salary, then the attribute Emp_ID can serve as a key. Hence the attribute Emp_ID can identify all the
remaining attributes of an individual Employee uniquely.
Simple Key:
The Key which consist only one attribute is called as “Simple Key”.
Composite Key:
The Key which consist multiple attributes is called as “Composite Key”.
Super Key :
A Super Key is any Key that identifies the rows of a table uniquely.
Example: Emp_Id
Emp_ID, Emp_Name
Emp_ID, Emp_Name, Emp_Dept
Note: Any super set of a Key can be called as a Super Key
Candidate Key:
A Candidate Key is a Key/Super Key which consist the attributes that are sufficient for unique
identification. A relation can have any number of candidate keys. It should not include any unnecessary attributes
to identify the rows of a table uniquely. A Candidate Key must satisfy the following properties.
1. Unique Identification : For every row the value of the key must uniquely identify that row.
2. Non-Redundancy : No attribute in the key can be deleted without destroying the property of unique
identification.
The Super Key {Emp_ID, Emp_Name} can be converted as Candidate Key by eliminating Emp_Name from the
set, because the attribute Emp_ID itself is sufficient to identify the individual rows of Employee table.
Primary Key:
A Primary Key is a Key that is selected from a list of Candidate Keys. It is the choice of database designer
to choose an appropriate candidate key as primary key. Primary Key is used to build relationship among several
entities in the relational data model.
Foreign Key:
A Foreign Key is an attribute or set of attributes whose value matches the Primary Key value in the
related table in the same database.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:21


What Is Relational Database Schema?
 A relational database schema is the tables, columns and relationships that make up a relational
database.

THE PURPOSE OF A SCHEMA
A relational database schema helps you to organize and understand the structure of a database. This is
particularly useful when designing a new database, modifying an existing database to support more
functionality, or building integration between databases.

CREATING THE SCHEMA


There are two steps to creating a relational database schema: creating the logical schema and creating the
physical schema. The logical schema depicts the structure of the database, showing the tables, columns
and relationships with other tables in the database and can be created with modeling tools or spreadsheet
and drawing software. The physical schema is created by actually generating the tables, columns and
relationships in the relational database management software (RDBMS). Most modeling tools can
automate the creation of the physical schema from the logical schema, but it can also be done by
manually.

MODIFYING THE SCHEMA


The structure of a relational database will inevitably change over time as data needs change. It's just as
important to document changes to your relational database schema as it was to document the original
design, otherwise it becomes increasingly difficult to update or integrate the database. Be sure to save
copies of previous configurations, so that changes can be removed if problems occur.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:22


Chapter 2:The Relational Algebra and Relational Calculus:
Relational Algebra
Relational database systems are expected to be equipped with a query language that can assist its
users to query the database instances. There are two kinds of query languages − relational algebra and
relational calculus.

Relational algebra is a procedural query language, which takes instances of relations as input and
yields instances of relations as output. It uses operators to perform queries. An operator can be
either unary or binary. They accept relations as their input and yield relations as their output.
Relational algebra is performed recursively on a relation and intermediate results are also considered
relations.

The fundamental operations of relational algebra are as follows −

 Select
 Project
 Union
 Set different
 Cartesian product
 Rename
Select Operation ( σ ): It selects tuples that satisfy the given predicate from a relation.
Notation − σp(r)

Where σ stands for selection predicate and r stands for relation. p is prepositional logic formula which
may use connectors like and, or, and not. These terms may use relational operators like − =, ≠, ≥, < , >,
≤.

For example −

σsubject = "database"(Books)
Output − Selects tuples from books where subject is 'database'.

σsubject = "database" and price = "450"(Books)


Output − Selects tuples from books where subject is 'database' and 'price' is 450.

σsubject = "database" and price = "450" or year > "2010"(Books)


Output − Selects tuples from books where subject is 'database' and 'price' is 450 or those books
published after 2010.

Project Operation ( ∏ ) : It projects column(s) that satisfy a given predicate.


Notation − ∏A1, A2, An (r)
Where A1, A2 , An are attribute names of relation r.
Duplicate rows are automatically eliminated, as relation is a set.
For example −

∏subject, author (Books)


Selects and projects columns named as subject and author from the relation Books.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:23


Union Operation (∪)
It performs binary union between two given relations and is defined as −
r ∪ s = { t | t ∈ r or t ∈ s}
Notation − r U s
Where r and s are either database relations or relation result set (temporary relation).

For a union operation to be valid, the following conditions must hold −

 r, and s must have the same number of attributes.


 Attribute domains must be compatible.

 Duplicate tuples are automatically eliminated.

∏ author (Books) ∪ ∏ author (Articles)


Output − Projects the names of the authors who have either written a book or an article or both.

Set Difference (−)


The result of set difference operation is tuples, which are present in one relation but are not in the
second relation.

Notation − r − s

Finds all the tuples that are present in r but not in s.

∏ author (Books) − ∏ author (Articles)


Output − Provides the name of authors who have written books but not articles.

Cartesian Product (Χ)


Combines information of two different relations into one.

Notation − r Χ s

Where r and s are relations and their output will be defined as −

r Χ s = { q t | q ∈ r and t ∈ s}

σauthor = 'tutorialspoint'(Books Χ Articles)


Output − Yields a relation, which shows all the books and articles written by tutorialspoint.

Rename Operation (ρ)


The results of relational algebra are also relations but without any name. The rename operation allows
us to rename the output relation. 'rename' operation is denoted with small Greek letter rho ρ.

Notation − ρ x (E)

Where the result of expression E is saved with name of x.


Additional operations are −
 Set intersection

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:24


 Assignment
 Natural join

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:25


Relational Calculus
 In contrast to Relational Algebra, Relational Calculus is a non-procedural query language, that
is, it tells what to do but never explains how to do it.

 Relational calculus exists in two forms −

Tuple Relational Calculus (TRC)


 Filtering variable ranges over tuples
 Notation − {T | Condition}
 Returns all tuples T that satisfies a condition.
For example −
 { T.name | Author(T) AND T.article = 'database' }
 Output − Returns tuples with 'name' from Author who has written article on 'database'.
 TRC can be quantified. We can use Existential (∃) and Universal Quantifiers (∀).
For example −
 { R| ∃T ∈ Authors(T.article='database' AND R.name=T.name)}
 Output − The above query will yield the same result as the previous one.

Domain Relational Calculus (DRC)


 In DRC, the filtering variable uses the domain of attributes instead of entire tuple values (as
done in TRC, mentioned above).
 Notation −{ a1, a2, a3, ..., an | P (a1, a2, a3, ... ,an)}
 Where a1, a2 are attributes and P stands for formulae built by inner attributes.
 For example −
 {< article, page, subject > | ∈ TutorialsPoint ∧ subject = 'database'}
 Output − Yields Article, Page, and Subject from the relation TutorialsPoint, where subject is
database.
 Just like TRC, DRC can also be written using existential and universal quantifiers. DRC also
involves relational operators.
 The expression power of Tuple Relation Calculus and Domain Relation Calculus is equivalent to
Relational Algebra.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:26


ER Model to Relational Model
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.

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.

Mapping Process

 Create table for a relationship.


 Add the primary keys of all participating Entities as fields of table with their respective data
types.

 If relationship has any attribute, add each attribute as field of table.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:27


 Declare a primary key composing all the primary keys of participating entities.

 Declare all foreign key constraints.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:28


Mapping Weak Entity Sets
A weak entity set is one which does not have any primary key associated with it.

Mapping Process
 Create table for weak entity set.

 Add all its attributes to table as field.


 Add the primary key of identifying entity set.
 Declare all foreign key constraints.

Mapping Hierarchical Entities


ER specialization or generalization comes in the form of hierarchical entity sets.

Mapping Process
 Create tables for all higher-level entities.

 Create tables for lower-level entities.


 Add primary keys of higher-level entities in the table of lower-level entities.
 In lower-level tables, add all other attributes of lower-level entities.
 Declare primary key of higher-level table and the primary key for lower-level table.
 Declare foreign key constraints.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:29


UNIT – III
Chapter:1:: RELATIONAL DATABASE DESIGN:
INFORMAL DESIGN GUIDELINES FOR RELATIONAL SCHEMA
 There are two traditional approaches to design the database.
1. Top-Down Design
2. Bottom-Up Design
 Top-Down design starts by identifying the data sets and then defines the data elements for each
of those sets. This process involves the identification of different entity types and the definition
of each entity’s attributes.
 Bottom-Up design identifies the data elements and then groups them together in data sets. That is
first identify the attributes and the group them to form entities.
The selection of a particular database design depends on the scope of the problem or personal priority.
The database design methods both top-down and bottom-up are not mutually exclusive.
The approach bottom-up is best suitable for small databases with few entities, attributes, relations and
transactions. The approach top-down is best suitable for relatively large databases which consists more
number and complexity of entities, relations and transactions.

A bottom-up approach design methodology considers the basic relationships among individual attributes
and uses those to construct relation schema.

1.Semantics of the Attributes


2.Reducing the Redundant Value in Tuples.
3.Reducing Null values in Tuples.
4.Dissallowing spurious Tuples.

1. Semantics of the Attributes:


 Whenever we are going to form relational schema there should be some meaning among the
attributes. This meaning is called semantics. This semantics relates one attribute to another with
some relation.
Eg: Sno Student name Sem
2. Reducing the Redundant Value in Tuples:
 Mixing attributes of multiple entities may cause problems. Information is stored redundantly
wasting storage problems with update anomalies
1. Insertion anomalies
2. Deletion anomalies
3. Modification anomalies

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:30


Consider the relation attributes
Studentname Sem DeptNo DeptName

 If we integrate these two and is used as a single table i.e Student Table

USN No Student name Sem Dept No Dept Name

 Here whenever if we insert the tuples there may be ‘N’ stunents in one department, so
Dept No,Dept Name values are repeated ‘N’ times which leads to data redundancy.
 Another problem is up data anomalies ie if we insert new dept that has no students.
If we delet the last student of a dept,then whole information about that department will be
deleted
 If we change the value of one of the attributes of particular table the we must update
the tuples of all the students belonging to thet dept else Database will become
Inconsistent.
Note: Design in such a way that no insertion ,deletion, modification anomalies will occur

3. Reducing Null values in Tuples.


 Relations should be designed such that their tuples will have as few NULL
values as possible.
 Attributes that are NULL frequently could be placed in separate relations (with the
primary key)

Reasons for Nulls:


1. attribute not applicable or invalid
2. attribute value unknown (may exist)
3. value known to exist, but unavailable

4. Disallowing spurious Tuples:


 Bad designs for a relational database may result in erroneous results for certain JOIN
operations
 The "lossless join" property is used to guarantee meaningful results for join operations

Note: The relations should be designed to satisfy the lossless join condition. No spurious tuples should
be generated by doing a natural-join of any relations.

Definition of functional dependency:


A functional dependency is denoted by x→y between two sets of attributes X and Y that are
subsets of R. the values the X component of tuple is depend on or determined by the values of the X
component of tupple uniquely determine the values of Y component. We say that these is a functional
dependency from X to Y or that Y is functionally dependent on X. cover’des the relational schema
EMPLOYEE from the sematies of attributes.

EMPLOYEE

ENO ENAME DOB ADDRESS DNUMBER


ENO→ENAME,DOB,ADDRESS,DNUMBER.The attribute on left hand side of A now is called as a
determination and also it is a candidate key.

Redundant information in tuple and update anomalies:

1. One goal of schema design is to minimize the storage space used by base relation

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:31


2. In EMPDEPT, the attribute value pestaining to a particular department (DNUMBER,DNAME,DMRENO)are
respected for every employee who works for that department . In contrast, each department’s
information appears only once in the DEPARTMENT relation. Only the department number (DNUMBER)
is repeated in the EMPLOYEE relation for each employee who works in that department.
EMP_DEPT
ENO ENAME DOB ADRESS DNUMBER DNAME DMGRENO
121 AAA - Gudur 4 production 12
122 BBB - Nellore
123 CCC - Gudur
124 DDD - Hyderabad
125 EEE - Nellore
126 FFF -

3. Another problem with using the relation as base relations is the problem of ‘UPDATE Anomalies’.
These can be classified into insertion, deletion, and modification anomalies.

1. It is difficult to insert a new department that has no employees.


2. If we delete from EMP-DEPT an employee tuple that happens to last department
information from the database.
3. In EMP-DEPT if we update the value of one of attributes of a particular department, the
manager of the department 5. We must update the tuples of all employee who work in that
department. Otherwise ,the database will become inconsistent.

Null values in tuples:

 Many of the attributes do not applay to all tuples in the relation we end up with many nulls in
those tuples. This can waste value space at storage level and may also lead to problems with
understanding the meaning of attributes and with specifying JOIN operations in logical level.

Inference rule for functional dependencies:

Definition: the set of all dependencies that include F as well as all dependencies that can be inferred
from F is called closure of f, it is denoted by F+ .

F={ENO→{ENAME,DOB,ADDRESS,DNUMBERS}, DNUMBERS→{DNAME,DMGRENO}

Some of the additional functional dependencies that we can infer from F are the following.

ENO→{DNAME,DMGENO} DNUMBER→DNAME

The closure F+ of F is the set of all functional dependencies that can be inferred from F. to
determine a systematic way to infer dependencies. We must discover a set of ‘inference rules’ that can
be used to infer new dependencies from a given set of dependencies.

We used the nation F1=X→Y to denote that the functional dependencies. The following six rules IR1
through IR6

IR1 (Reflexive rule) : if x≥Y thenX→Y

IR2 (argumentation rule) : (X→Y} |=X≥→Y

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:32


IR3 (transitive rule) : {X→Y,Y→Z}|=X→Z

IR4 (decomposition or projective rule) : {X→YZ}|=X→Y

IR5 (union or additive rule) : {X→Y,X→Z}|=X→YZ

IR6 (pseudo-transitive rule) : {X→Y,WY→Z}|=WX→Z

Equivalence of sets of functional dependencies


Definition: A set of functional dependencies F is said to coves another set of functional dependencies E.
if every F D in E is also in F+ : that is every dependency in E can be inferred from F. alternatively, we can
say that E is covered by F.

Definition: two set of the functional dependencies E and Equivalence if E+ =F+ . hence equivalence
means that every FD in E can be infers from F1 and every FD in F can be inferred from E. i.e, E is
equivalent of F if both the conditions E covers F and covers E hold.

Minimal sets of functional dependencies:

We can formally define a set of functional dependencies F to be minimal if it satisfies the following
conditions.

 Evry dependency in F has a single attribute for its right hand side .
 We can not replace any dependency X→A in F with a dependency Y→A, where Y is a proper
subset of X ,and still have a set of dependencies that is equivalent to F.
 We connect remove any dependency from F and still have a set of dependencies that is
equivalent to F.

Functional Dependency
A Functional Dependency is a constraint between two attributes or two sets of attributes. The
keys are identified based on the theory of Functional Dependency.
If there are two attributes A and B, then attribute B is said to be functionally dependent on
attribute A if each value in A determines one and only one value in B.
The functional dependency between the attributes A and B can be written as :

The above notations can be read as “A determines B”.


The attribute set on the left-hand side of the arrow is called “Determinant” and the attribute set on
right side of the arrow is called “Dependent”.
for example in the emp table empnoename,job,sal,comm.,deptno
The attributes ename,job,sal,comm,deptno are functionally dependent on empno.
NORMALIZATION

 Normalization is the process of evaluating, correcting a poor table structure to minimize data
redundancy and anomalies.
 Normalization works through a series of stages called Normal Forms.
First Normal Form(1 NF):

 A relation is said to be in 1NF, if it does not have repeating group of attributes. It means the
value of each row and column must be unique.
 In 1NF, the normalization process starts with 3 steps procedure.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:33


1. Eliminate the repeating groups or multivalued attributes
2. Identify the primary key
3. Identify all the dependencies such as partial functional dependency and transitive functional
dependency.
Consider the following employee relation

EMPLOYEE

Empno Name Age Address This relation is not in 1NF, because this has
multivalued attribute that is address.
101 Ravi 22 Gudur
1-2-44
Raja street
102 Siva 21 Nellore Now to convert this relation as into 1NF fill the
5-3-202 multivalued attribute with its sub attributes.
Ramlingapuram

Empno Name Age City StreetNo StreeName

101 Ravi 22 Gudur 1-2-44 Raja street

102 Siva 21 Nellore 5-3-202 Ramlingapuram

Second Normal Form(2NF)

 A relation is said to be in 2NF, if it is in 1NF and every non-key attribute is fully functionally
dependent on the primary key.
 A relation in 2NF applies following conditions.
1. It must not contain any partial functional dependencies.
2. Every non-key attribute functionally dependent on fully set of primary key attributes.
Consider the following relation and its functional dependencies.

STUDENT

SNO SNAME AGE GROUP COURSE FEES

121 AAA 22 MCA JAVA 3500


122 BBB 21 MBA MS-OFFICE 2500
123 CCC 22 MCA CPP 3200

The above relation is not in 2NF, here the primary key is (SNO,COURSE) is composite primary key.

The functional dependencies are,

1. SNO  SNAME,AGE,GROUP
2. SNO,COURSE  FEES
S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:34
In the second functional dependency SNO is a part of composite primary key. Hence, this dependency
is called partial functional dependency. This partial dependency creates redundancy in this relation,
which results anomalies.

1. Insert anomaly: if we want to insert student details who were not joined a course. We must
insert null values for related attributes, which results wastage of memory.
2. Deletion anomaly: if we delete the student details than the related course details also be
removed. So there is a loss of information.
3. Update anomaly: if we want to modify the more student details, it will take more time.
To overcome these problems, we must decompose the above relation into 2 relations

STUDENT and STUDENT_COURSE.

STUDENT STUDENT_COURSE

SNO SNAME AGE GROUP


SNO COURSE FEES
121 AAA 22 MCA
121 JAVA 3500
122 BBB 21 MBA
122 MS-OFFICE 2500
123 CCC 22 MCA
123 CPP 3200

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:35


Third Normal Form(3NF):

 A relation is said to be in 3N, it is in 2NF and it has no transitive functional dependency.


 A transitive dependency in a relation is a functional dependency between 2 or more non-key
attributes.
Consider the following relation CUSTOMER

Cust_NO Cust_Name Salesperson Place

Here the functional dependencies are

C101 AAA PPP SOUTH 1. Cust_No  Cust_Name, Salesperson, Place


2. Salesperson  Place
C102 BBB RRR NORTH Here the second functional dependency is a
transitive dependency. This dependency results in
C103 CCC QQQ WEST redundancy and anomalies in the relation. So that
C104 DDD RRR NORTH it can be removed by decomposing into 2
relations and make the non-key attribute as key
C105 EEE TTT EAST attribute.

C106 FFF PPP SOUTH

CUSTOMER SALESPERSON

Cust_NO Cust_Name Salesperson


Salesperson Place
…………...

C101 AAA PPP


PPP SOUTH
C102 BBB RRR
RRR NORTH
C103 CCC QQQ
QQQ WEST
C104 DDD RRR
TTT EAST
C105 EEE TTT

C106 FFF PPP

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:36


BOYCE CODD Normal Form (BCNF):

 When a relation has more than one candidate key, anomalies may result even though that
relation is in 3NF.
 Consider the following relation STUDENT in 3NF, but not in BCNF.
STUDENT

SNO SUBJECT LECTURER PERIODS

111 MATHS AAA 2


112 STAT BBB 2
113 ENGLISH CCC 1
114 COMPUTER DDD 2
115 MATHS AAA 2
116 STAT BBB 2

The functional dependencies are,

1. SNO,SUBJECT  LECTURER, PERIODS


2. LECTURER  SUBJECT
1. In the second functional dependency the determinant Lecutrer becomes part of the key and the
dependant attribute Subject, becomes a non key attribute. So the Dependency diagram is now
SNO, LECTURER  SUBJECT, PERIODS
2. There are problems with this structure as Subject is now dependant on only part of the key. This
violates the rules for 2NF, so the table needs to be divided with the partial dependency
becoming a new table. The dependencies would then be
SNO, LECTURER  SUBJECT, PERIODS
LECTURER  SUBJECT
3. The original table is divided into two new tables. Each is in 3NF and in BCNF
STUDENT LECTURER

SNO LECTURER, PERIDOS LECTURE SUBJECT

SNO LECTURER PERIODS


LECTURER SUBJECT
111 AAA 2
AAA MATHS
112 BBB 2
BBB STAT
113 CCC 1
114 DDD 2 CCC ENGLISH
115 AAA 2 DDD COMPUTER
116 BBB 2

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:37


Fourth Normal Form(4NF):

 A relation is in 4NF, it is in BCNF and contains no multi-valued dependency.


 A multi-valued dependency is a dependency in which the value of one column have several
values in other columns.
 For example, let us consider the following relation.

Subject Lecturer TextBook

Rahul Balaguruswamy

Cpp Vikram K.R Venugopal

Srinivas Eric S.Roberts

Java Pratap Herbert shildt

Roopsagar S.Hortsmann

KiranKumar ORelly

In this relation there is a multivalued dependency between the attributes subject, Lecturer and
TextBook.That is

1. Each subject has number of Lecturers


2. Each subject has number of TextBooks
3. The TextBook for a given course is independent of Lecture of that course.
Subject  Lecturer Subject  Textbook

Now, to eliminate the multivalued dependency for this relation by dividing the relation into 2
relations each of these relations contains 2 attributes that have a multivalued relationship in the
original relation.

Course_Lecturer Course_TextBook

Subject Textbook
Subject Lecturer
Cpp Balaguruswamy
Cpp Rahul
Cpp K.R Venugopal
Cpp Vikram
Cpp Eric S.Roberts
Cpp Srinivas
Java Herbert shildt
Java Pratap
Java S.Hortsmann
Java Roopsagar
Java ORelly
Java KiranKumar

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:38


Fifth Normal Form (5NF):

A database is said to be in 5NF, if and only if,

 It's in 4NF
 If we can decompose table further to eliminate redundancy and anomaly, and when we
re-join the decomposed tables by means of candidate keys, we should not be losing
the original data or any new record set should not arise. In simple words, joining two
or more decomposed table should not lose records nor create new records.

Consider an example of different Subjects


taught by different lecturers and the
lecturers taking classes for different
semesters.
Note: Please consider that Semester 1
has Mathematics, Physics and Chemistry
and Semester 2 has only Mathematics in
its academic year!!
In above table, Rose takes both Mathematics and Physics class for Semester 1, but she does
not take Physics class for Semester 2. In this case, combination of all these 3 fields is required
to identify a valid data. Imagine we want to add a new class - Semester3 but do not know
which Subject and who will be taking that subject. We would be simply inserting a new entry
with Class as Semester3 and leaving Lecturer and subject as NULL. As we discussed above,
it's not a good to have such entries. Moreover, all the three columns together act as a primary
key, we cannot leave other two columns blank.

Hence we have to decompose the table in such a way that it satisfies all the rules till
4NF and when join them by using keys, it should yield correct record. Here, we can represent
each lecturer's Subject area and their classes in a better way. We can divide above table into
three - (SUBJECT, LECTURER), (LECTURER, CLASS), (SUBJECT, CLASS)

Now, each of combinations


is in three different tables. If we
need to identify who is teaching
which subject to which semester,
we need join the keys of each table
and get the result.

For example, who teaches


Physics to Semester 1, we would
be selecting Physics and
Semester1 from table 3 above, join
with table1 using Subject to filter
out the lecturer names. Then join
with table2 using Lecturer to get
correct lecturer name. That is we joined key columns of each table to get the correct data.
Hence there is no lose or new data - satisfying 5NF condition.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:39


CHAPTER-2
Relational Database Design Algorithms and Further Dependencies

Properties of relational Decomposition

A set of relations that together form the relational database schema must posses certain
additional properties to ensure a good design. The properties such as dependency presentation and
lossless-join property.

I. Relation decomposition:
 A single universal relation schema R={A1,A2,. . . . . .An} that includes the attributes of the
database and every attribute name is unique.
 Using the functional dependencies, that algorithms decompose the universal relation
schema R into a set of relation schemas
D={R1 ,R2…Rm} that wise become the relational database schema, D is called a
decomposition’ of R.
 That each attribute in R will ppear in at least one relational schema Ri in the
decomposition so that no attribute are

This is called the attribute presentation condition of decomposition.

II. Dependency preservation property:


 The functional dependency X→Y specified in either appeared directly in one of the
relation schemas Ri in the decomposition. This is the dependency preservation condition.
 We want to preserve the dependencies because each dependency in F represents a
constraint on the database.
 If a decomposition is not dependency preserving, then some dependency is lost in the
decomposition.

Consider the following relation schema TEACH

STUDENT COURSE INSTRUCTOR

FD1 : STUDENT.COURSE→INSTRUCTOR

FD1 : INSTRUCTOR→COURSE

Decomposition this relation schema into two relation schemas in one of the following possible
pairs.

1. {STUDENT, INSTRUCTOR} AND {STUDENT , COURSE}

2. {COURSE, INSTRUCTOR} AND {COURSE, STIDENT}

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:40


3. {INSTUCTOR, COURSE} AND {INSTRUCTOR, STUDENT}

All three decomposition “lose” the functional dependency FD1.

III. Lossless (or) Nonadditive of a decomposition:


 A loss less join property ensures that no spurious tuples are generated when a national
join operation is applied to the relations in the decomposition.

Definition: a decomposition D={R1,R2, ….Rm }of R has the loseless join property with respect to the set
of dependencies F on Rif. For every every relation state r of R that statisfies F.

*(ΠR1 (r) ,……..πRm (r))=r

Where * is normal join of all relations in D.

The word loss refers to loss of information, not to be lose of tuples. If a decomposition does not
have the loseless join property. We may get additional spurious tuples affer the PROJECT (π) and
natural join(*)operation are applied.

The decomposition of

EMP-PROJ (ENO,PNUMBER,HOURS,ENAME,PNAME,PLOCATION)

into EMP-LOCS(ENAME,PLOCATION) and

EMP-PROJ(ENO,PNUMBER,HOURS,PNAME,PLOCATION)

does not have the lossless join property.

EMP-PROJ

ENO PNUMBER HOURS ENAME PNAME pLOCATION


Consider the two relation schemas EMP-LOCS and EMPPROJrelation.

EMP-LOCS EPM-PROJ1

ENAME PLOCATION
ENO PNUMBER HOURS PNAME PLOCATION
suppose we use EMP-LOCS and EMP-PROJ1 as the base relations instead of the EMP-PROJ, it produces
bad schema design, because we cannot recover the information that was originally in EMP-PROJ. if we
perform natural join operation on EMP-LOCS and EMP=PROJ1 result produces many more tuples that
the original set of tuples in EMP-PROJ. the additional tuples that are not in EMP-PROJ are
calledspurious tuples they represent wrong information that is not valid.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:41


Algorithm for relational database schema design
1. Dependency-presesning decomposition into 3NF schemas:
The following algorithm a dependency–preserving decomposition
D={R1,R2,…..RM} of a universal relation R based on a set of functional dependencies F. such that
each Ri in D in 3NF.

Algorithm: a universal relation into 3NF with dependency preservation

Input: a universal relation R and set of functional dependencies F on the attributes of R

 Find a minimal cover G for F


 For each left-hand side X of a functional dependency that appears in G. create a relation schema
in D with attributes {XU{A1}U{a2}….U{ak}},where X→A,X→A2….X→Ak are the only dependencies
in G with X as left hand side.
 Place any remaining attributes in a single relation schema to ensure the attribute preservation
property.

2. Lossless join decomposition into BCNF schemas:

The following algorithm decompose a universal relation schema R={a1,a2,..Rn} into a decomposition
D={R1,R2,….Rm} such that each Ri is in BCNF and decomposition Dhas the lossless join property with
respect to F.

Algorithm: relational decomposition into BCNF Non additive join property.

Input:

1. set D={R}
2. whole this is a relation schema Q in D that is not in BCNF do
{
a. choose a relation schema Q in D that is not in BCNF;
b. find a functional dependency X→Y in Q that violates BCNF;
c. replace Q in D by two relation schema (Q-Y)and(XUY);
};

Multivalued dependencies and Fourth normal Form:

For example consider the relation EMP,represent the fact that an Employee whose name
is Ename works on the project whose name is PNAME and has a dependent whise name is DNAME. An
employee may work on several projects and may have several departments, and the employees
projects and dependents are independent of one another.

EMP EMP-PROJECTS EMP-DEPARTMENT

ENAME PNAME DNAME ENAME PNAME ENAME DNAME


Kumar X Rama Kumar X Kumar Rama
Kumar Y Sita kumar Y kumar sita
Kumar X Rama
kumar Y sita
S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:42
Decomposing the EMP relation into two 4NF relations EMPPROJECTS and EMPDEPARTMENT

This is relation having two multi-valued dependencies


ENAME→→PNAME A relation is in 4NF,if it is in BCNF and contains no multi-valued dependency
ENAM→→DNAME
Definition: a multivalued dependency X→→Y Specified on relation schemas, where X and Y are both
subset of R, specify the following constraint on any relation state r of R:

1. if two tuples t1 to t2 exist in r such that t1 [X]=t2[X],


2. two tuplest3 and t4 should also exist in r with the following properties, where we also Z to
denote (R-(XUY))
 t3[X]=t4[X]=t1[X]=t2[X]
 t3[Y]t1[Y] and t4[Y]=t2[Y]
 t3[Z]=t2[Z] and t4[Z]=t1[Z]

when ever X→→Y holds, we say that X multidetermines Y. because of the symmetry in the definition,
when ever X→→Y holds in R so does X→→Z. hence, X→→Y implies X→→Z, and therefore it is some
times written as X→→Y/Z.

Join dependencies and 5NF

 The another dependency called join dependency, if it is present, carry out a malty way
decomposition into 5NF.
 A join dependency JD, denoted by JD(R1 R2….Rn)specified on relation schema R1 specifies a
constraint on the states r of R. The constraints states that every legal state r of R should have a
nonadditive join decomposition into R1,R2…..,Rm, that is for every r we have
*(πR1(r), πR2(r)… πRn()=r
A join dependency JD(R1,R2….Rn)specified on a relationship, trivial JD one of the relation
schemas Ri in JD(R1,R2,…Rn)is equal to R. such a dependency is called trivial because it has
non additive join property for any relation state r of R and hence does not specify any
constraint on R.
Definition: a relation schema R is in 5NF or project join normal form JNF with respect to a set F
of functional , multivalve , and join dependencies, if for every nontrivial join dependency
JD(R1,R2,……..Rn) in F+, every Ri is a super key of R.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:43


UNIT -IV

Chapter1 : Introduction to Transaction processing concepts and Theory

Introduction to Transaction Processing:

1. Single User Vs Multi-user systems:

 A database system is classified according to number of users who can use the system
concurrently.
 A DBMS is single-user if at most one user at a time can use the system, and it is multi-user if
many users can use the system.
 Multi-user DBMSs are at airline reservation system, banks, insurance agencies, stock exchanges,
super market where many users who submit transactions concurrently to the system.
 Multiple users can access databases with multiprogramming, which allows the computer to
execute multiple programs at same time. Multiprogramming OS execute some commands of one
process, then suspend that process and execute some commands from the next process and so on.
The concurrent execution of process is interleaved; it keeps the CPU busy when a process
requires an input/output operation. The CPU is switched to execute another process rather than
remain idle during I/O time.
2. Transactions, Read and Write operations and DBMS Buffer:

 A transaction is a logical unit of work, it includes one or more database access operations such as
insertion, deletion or retrieval operations.
 The database operations that form a transaction can either be embedded with in an application
program or specified interactivity with a high level query language such as SQL.
 One way of specifying the transaction boundaries is by specifying explicit ‘Begin transaction’
and ‘End Transaction’ statements in an application program, all the database access operations
between these two statements are considered as one transaction.
 A single application program may contain more than one transaction if it contains several
transaction boundaries.
 A database is basically represented as a collection of named data items. The size of data item is
called its ‘Granularity’ and it may be a field of some record or it may be a disk block.
 The basic database access operations that a transaction can include are,
read_item(x): Reads a data base item x into a program variable x.
write_item(x): Writes the value of variable x into the database item x.
 For read and write operations the item values in disk block are copied into a buffer in the main
memory.
3. Need of Concurrency Control:

 The co-ordination of simultaneous execution of transactions in a multi-user database system is


known as concurrency control.
 Several problems can occur when concurrent transactions execute in an uncontrolled manner.
Some of these problems are explained by a simplified airline reservation database.
 A transaction T1 transfers N reservations from one flight whose number of reserved seats stored
in database item X to another flight whose reserved seats stored in database item Y. A transaction
T2 reserves M seats on flight X.

T1 T2
T1 T2
read-item(X);
read-item(X) read-item(X) X:=X-N; read-item(X)
X:=X-N; X:=X+M; X:=X+M;
write-item(X); Write-item(X); write-item(X);
read-item(Y); read-item(Y);
Y=Y+N; S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:44
write-item(X);
write-item(Y); Y:=Y+N;
write-item(Y);
The lost update problem:
 This problem occurs when two transactions are access the same database item, and one of the
update is lost. The transactions T1 and T2 are submitted at same time and their operations are
interleaved, and then the final value of item X is incorrect, because T2 reads the value of X
before T1 changes in the database, and hence the update value resulting from T1 is lost.
 Let X=80, N=5 and M=4 the final result should be X=79 but it produces incorrect result X=84.

Transactions States and additional operations


 A transaction is an atomic unit of work
that is either completed entirely or not
done at all.
 For recovery purpose, the system
needs to keep track of when the
transaction starts, terminates and
commits or aborts.
 The recovery manager keeps track of
the following operations.
BEGIN_TRANSACTION:
READ OR WRITE:
END_TRANSACTION:
COMMIT_TRANSACTION:

A transaction in a database can be in one of the following states −


 Active − In this state, the transaction is being executed. This is the initial state of every transaction.
 Partially Committed − When a transaction executes its final operation, it is said to be in a partially
committed state.
 Failed − A transaction is said to be in a failed state if any of the checks made by the database recovery
system fails. A failed transaction can no longer proceed further.
 Aborted − If any of the checks fails and the transaction has reached a failed state, then the recovery
manager rolls back all its write operations on the database to bring the database back to its original state
where it was prior to the execution of the transaction. Transactions in this state are called aborted. The
database recovery module can select one of the two operations after a transaction aborts −
Re-start the transaction and Kill the transaction.

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

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:45


THE SYSTEM LOG:

 The system maintains a Log to keep track of all transaction operations that affect the values of database
items. This information may be needed to permit recovery from failures.
 The Log is kept on disk, so it not affected by any type of failures except disk crash.
 In the Log records, T refers to a unique Transaction_Id that is generated automatically by the system and
it is used to identify each transaction.
1. [start_transaction, T]: Indicates that transaction T has started execution.
2. [write_item, T , old_value, new_value, x]: Indicates that transaction T has changed the value of
database item x from old-value to new-value.
3. [read_item, T ,x]: indicates that transaction T has read the value of database item x.
4. [commit, T]: indicates that transaction T has completed successfully and recorded permanently
to the database.
5. [abort, T]: indicates that transacation T has been aborted.

Desirable Properties of Transactions:


A transaction is a very small unit of a program and it may contain several low level tasks. A transaction
in a database system must maintain Atomicity, Consistency, Isolation, and Durability − commonly
known as ACID properties − in order to ensure accuracy, completeness, and data integrity.
 Atomicity − This property states that a transaction must be treated as an atomic unit, that is,
either all of its operations are executed or none. There must be no state in a database where a
transaction is left partially completed. States should be defined either before the execution of the
transaction or after the execution/abortion/failure of the transaction.

 Consistency − The database must remain in a consistent state after any transaction. No
transaction should have any adverse effect on the data residing in the database. If the database
was in a consistent state before the execution of a transaction, it must remain consistent after the
execution of the transaction as well.

 Durability − The database should be durable enough to hold all its latest updates even if the
system fails or restarts. If a transaction updates a chunk of data in a database and commits, then
the database will hold the modified data. If a transaction commits but the system fails before the
data could be written on to the disk, then that data will be updated once the system springs back
into action.

 Isolation − In a database system where more than one transaction are being executed
simultaneously and in parallel, the property of isolation states that all the transactions will be
carried out and executed as if it is the only transaction in the system. No transaction will affect
the existence of any other transaction.

Characterizing schedules:

Schedules of Transaction:
 When transactions are executing concurrently in an interleaved manner, then the order of
execution of operations from various transactions is known as schedule.
 A schedule S of n transactions T1,T2,…Tn is an ordering of the operations of the transactions,
that transaction Ti that participates in S, the operation of Ti in S must appear in same order in
which they occur in Ti.
 For describing the schedule a notation r,w,e and a symbols are used for the operations read_item,
write_item, commit and abort respectively.
 Sa is the schedule of Lost Update problem transactions.
Sa=r1(x); r2(x); w1(x); w2(y); r1(y); c2; r2(x); c1(x).

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:46


Similarly Sb is the schedule of temporary update problem transactions,
Sb=r1(x); w1(x); r2(x); w2(x); r1(y);a1;
 Two operations in a schedule are said to be Conflict, it satisfies
(a). They belongs to different transactions.
(b). They access the same item x.
(c). atleast one of the operations is write_item(x).
So, in the schedule the operations r1(x) and w1(x), r2(x) and w2(x), w1(x) and w2(x) are
conflict. However r1(x) and r2(x) are do not conflict.
 A schedule S of n Transactions T1,T2,….Tn are said to be Complete schedule if the following
conditions satisfies.
(1) The operations in S are exactly those operations in T1, T2,…Tn including a commit or abort
operation.
(2) For any pair of operations from the same transaction Ti, their order in S is same as their order in
Ti.
(3) For any two conflicting operations, one of the two must occur before the other in the schedule.

Characterizing Schedules based on Recoverability:

 For some schedules it is easy to recover from transaction failures. It is important to characterize the
types of schedules for which recovery is possible as well as those for which recovery is relatively simple.
1. Once a transaction T is committed, it should never be necessary to rollback. The schedules meet this
criterion is called recoverable schedules and those do not are called Non_recoverable schedules. A
schedule S is recoverable if a transaction T reads from T1 which writes an item x and later read by T. T1
should not have been aborted before T reads item x, and these should no transactions that write x after
T1 writes it and before T reads it.
Consider the schedule S1a, which is same as Sa except that two commit operations have been added to
Sa.

S1a = r1(x); r2(x); w1(x); r1(y); w2(x); c2; w1(y); c1

2. A schedule is said to be cascade-less or to avoid cascading rollback, if every transaction in the schedule
reads only items that were written by committed transactions,

S1 = r1(x); w1(x); r2(x); r1(y); w2(x); w1(y); c1:c2; In this schedule r2(x) command must be postponed
until after T1 has committed, thus delaying T2 but ensuring no cascading rollback if T1 aborts.

3. A more restrictive type of schedule is a strict schedule, in which transactions can neither read nor write an
item x until the last transaction, that write x has committed or aborted. Strict schedules simplify the recovery
process.

Ex: S: w1(x,5); w2(x,8); a1;

Suppose the original value of x is 9, which is the before image stored in log. If T1 aborts, the recovery
procedure that restores the before image of an aborted write operation will restore the value of x to 9. Even
though it has already been changed to 8 by T2, thus loading to potentially incorrect results. A strict schedule
does not have this problem.

Characterizing schedule based on Serializability:

Serial schedule: A schedule S is serial if for every transaction T participating in the schedule, all the operations of
T is executed connectivity in the schedule. Otherwise, the schedule is called non serial schedule.

Consider the following schedules:

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:47


T1 T2 T1 T2

read-item(x) read-item(X);
X:=X-N; X=X+M;
write-item(X); write-item(X);
read-item(Y);
Y=Y+N; read-item(X);
write-item(Y);
read-item(X)
X:=X+M;
Write-item(X);

T1 T2 T1 T2

read-item(x); read-item(X)
X:=X-N; X:=X-N;
read-item(X) write-item(X); read-item(X)
write-item(x); X:=X+M; X:=X+M;
read-item(Y); write-item(X);
read-item(Y);
Y:=Y+N; write-item(X); Y=Y+N;
write-item(Y); write-item(Y);

schedule-C schedule-D
Schedules A and B is called serial because the operations of each transactions schedules C and D called non-
serial because each sequence inter levels operations from the transactions.

Serializable Schedule: A schedule S is serializable if it is equivalent to some serial schedule of the same n
transactions.
Result equivalent: Two schedules are called result equivalent if they producer the same final state of in the data
bas.
Conflict equivalent: Two schedules are said to be conflict equivalent if the order of any two conflicting
operations is the same in both schedules.
If the conflict operations are applied in different orders in two schedules, the effect can be different on the
database or no other transaction in the schedule, and hence the schedules are not conflict equivalent.
Ex: The operations r,(X),w2(X) in a schedule S1 and in the reverse order W1(X); r1(X) in schedule S2, the value
read by r1(X) can be different in the schedules.
Conflict serializable:
 A schedule & is said to be conflict serializable if it is conflict equivalent to some serial schedules.
 Being serializable is not some as being serial.
 Being serializable implies that the schedule is a concurrent schedule.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:48


Chapter 2: Concurrency Control Techniques
Concurrency Control
 In a multiprogramming environment where multiple transactions can be executed simultaneously, it is
highly important to control the concurrency of transactions.

 We have concurrency control protocols to ensure atomicity, isolation, and serializability of concurrent
transactions. Concurrency control protocols can be broadly divided into two categories − 1. Lock based
protocols and 2. Time stamp based protocols

1. Lock-based Protocols

 Database systems equipped with lock-based protocols use a mechanism by which any transaction
cannot read or write data until it acquires an appropriate lock on it.
 Locks are of two kinds −

Binary Locks − A lock on a data item can be in two states; it is either locked or unlocked.

Shared/exclusive − This type of locking mechanism differentiates the locks based on their uses. If a lock is
acquired on a data item to perform a write operation, it is an exclusive lock. Allowing more than one transaction
to write on the same data item would lead the database into an inconsistent state. Read locks are shared
because no data value is being changed.

There are four types of lock protocols available −

i) Simplistic Lock Protocol

Simplistic lock-based protocols allow transactions to obtain a lock on every object before a 'write' operation is
performed. Transactions may unlock the data item after completing the ‘write’ operation.

ii). Pre-claiming Lock Protocol:

Pre-claiming protocols evaluate their operations and create a


list of data items on which they need locks. Before initiating an
execution, the transaction requests the system for all the locks
it needs beforehand. If all the locks are granted, the transaction
executes and releases all the locks when all its operations are
over. If all the locks are not granted, the transaction rolls back
and waits until all the locks are granted.

iii). Two-Phase Locking 2PL:

This locking protocol divides the execution phase of a


transaction into three parts.
In the first part, when the transaction starts executing, it
seeks permission for the locks it requires.
The second part is where the transaction acquires all the
locks.

As soon as the transaction releases its first lock, the third


phase starts. In this phase, the transaction cannot demand
any new locks; it only releases the acquired locks.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:49


Two-phase locking has two phases, one is growing, where all the locks are being acquired by the transaction;
and the second phase is shrinking, where the locks held by the transaction are being released.
To claim an exclusive (write) lock, a transaction must first acquire a shared (read) lock and then upgrade it to an
exclusive lock.
According to the two-phase locking protocol, a transaction handles its locks in two distinct, consecutive
phases during the transaction's execution:

1. Expanding phase (Growing phase): locks are acquired and no locks are released (the number of locks can
only increase).
2. Shrinking phase: locks are released and no locks are acquired.

The two phase locking rule can be summarized as: never acquire a lock after a lock has been released.
The serializability property is guaranteed for a schedule with transactions that obey this rule.
Typically, without explicit knowledge in a transaction on end of phase-1, it is safely determined only
when a transaction has completed processing and requested commit. In this case all the locks can be
released at once (phase-2).
Lock compatibility for SCO
Lock read- write-
type lock lock
read-lock
write- X X
lock

iv). Strict Two-Phase Locking:


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

Strict-2PL does not have cascading abort as 2PL does.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:50


concurrency control with Time-Stamping methods
The time stamping approaches to schedule concurrent transactions assign a global, unique time
stamp for each transaction. The time stamp ordering protocol ensures that any conflicting READ and
WRITE operations are executed in time stamp order.
Time stamps must have two properties:
1. Uniqueness- Uniqueness ensures that no two transactions are assigned the same time stamp.
2. Monotonicity- Monotonicity ensures that the time stamp values always increase.

All databases READ and WRITE operations within the same transaction must have the same
time stamp. The DBMS executes conflicting operations in the time stamp order; it ensures serializability
of the transactions. If two transactions conflict, one is stopped, rolled back and rescheduled and assigned
the same time stamp value.

There are two schemes to control conflicting transactions by using time stamp methods:
1. Wait/Die Scheme
2. Wound/Wait Scheme

Wait/Die Scheme:
If there are two conflicting transactions each assigned a time stamp value, Wait/Die scheme follows the
following rules to control them.
1. If the transaction requesting the lock is the older of the two transactions, it will wait until the other
transaction is completed and the locks are released.
2. If the transaction requesting the lock is the younger of the two transactions, it will die (rollback) and
is rescheduled using the same time stamp value.
Wound/Wait Scheme:
If there are two conflicting transactions each assigned a time stamp value, Wound/Wait scheme follows
the following rules to control them.
1. If the transaction requesting the lock is the older of the two transactions, it will preempt (wound) the
younger transaction. The younger preempted transaction is rescheduled using the same time stamp
value.
2. If the transaction requesting the lock is the younger of the two transactions, it will wait until the other
transaction is completed and the locks are released.
In both the schemes, one of the transactions waits for other transaction to finish and release the locks .

Concurrency with optimistic methods


The optimistic approach is based on the assumption that the majority of the database operations
do not conflict. The optimistic approach does not require either locking or stamping techniques. In this
S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:51
approach a transaction is executed without any restrictions until it is committed. Each transaction moves
through two or more phases such as read, validation and write.
Read Phase:
During the read phase the transaction reads the database, executes the needed computations and makes
the updates to a private copy of the database values. All update operations of the transaction are recorded
in a temporary update file which is not accessed by the remaining transactions.
Validation Phase:
During the validation phase the transaction is validated to ensure that the changes made will not affect
the integrity and consistency of the database. If the validation test is positive, the transaction goes to the
write phase. If the validation test is negative, the transaction is restarted and the changes are discarded.
Write Phase:
During the write phase the changes are permanently applied to the database.
The optimistic approach is suitable and acceptable for transactions that acquire few update operations.

Timestamp-based Protocols
 The most commonly used concurrency protocol is the timestamp based protocol. This protocol uses
either system time or logical counter as a timestamp.
 Lock-based protocols manage the order between the conflicting pairs among transactions at the time of
execution, whereas timestamp-based protocols start working as soon as a transaction is created.
 Every transaction has a timestamp associated with it, and the ordering is determined by the age of the
transaction. A transaction created at 0002 clock time would be older than all other transactions that
come after it. For example, any transaction 'y' entering the system at 0004 is two seconds younger and
the priority would be given to the older one.
 In addition, every data item is given the latest read and write-timestamp. This lets the system know
when the last ‘read and write’ operation was performed on the data item.
Timestamp Ordering Protocol
The timestamp-ordering protocol ensures serializability among transactions in their conflicting read and writes
operations. This is the responsibility of the protocol system that the conflicting pair of tasks should be executed
according to the timestamp values of the transactions.
The timestamp of transaction Ti is denoted as TS(Ti).
Read time-stamp of data-item X is denoted by R-timestamp(X).
Write time-stamp of data-item X is denoted by W-timestamp(X).
Timestamp ordering protocol works as follows −
Ti issues a read(X) operation: Ti issues a write(X) operation −
 If TS(Ti) < W-timestamp(X)  If TS(Ti) < R-timestamp(X)
Operation rejected. Operation rejected.
 If TS(Ti) >= W-timestamp(X)
Operation executed.  If TS(Ti) < W-timestamp(X)
 All data-item timestamps Operation rejected and Ti rolled back.
updated.
Otherwise, operation executed.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:52


Chapter-3: Recovery Techniques
Recovery Management
Database recovery restores a database from a given inconsistent state to a previously consistent
state. Recovery techniques are based on the transaction atomicity property.
Recovery techniques apply to the database and to the system after some type of critical error has
occurred. Critical events can cause a database to become non-operational.

Hardware and Software Failures:

Hardware failures may be of several types like hard disk failure, a bad capacitor on motherboard, failing
memory etc. Software failures may occur in application programs, operating systems etc. These are the most
problems occur in database failures.

Human Intentional / Unintentional Actions:

Sometimes the end-users may do mistakes unintentionally or intentionally. Unintentional errors include
mistakes like deleting, updating wrong rows of a table, choosing inappropriate options etc. Intentional errors like
hacking data by accessing unauthorized data resources and attacking with viruses cause major problems.

Natural Disasters:

Natural disasters like fires, earthquakes, floods and power failures are critical to bring back a system into
an operational system.

Transaction Recovery:

Database transaction recovery uses data in the transaction log to recover a database from an
inconsistent state to a consistent state.

There are the following concepts that affect the recovery process:

1. The write-ahead-log protocol ensures that transaction logs are always written before any database data are
actually updated. In case of a failure, the database can recover to consistent state using the data in the
transaction log.
2. Database checkpoints are operations in which the DBMS writes all of its update buffers to disk.
The database recovery process involves brining the database to a consistent state after a failure.
Transaction recovery procedures generally make use of techniques like Deferred-Write and Write-
Through.
Database recovery-Techniques

 Deferred update: ( NO-UNDO, REDO )


The database in not physically updated until after a transaction reaches its commit point.
UNDO is not needed
REDO may be necessary
 Immediate update: ( UNDO,NO-REDO )
The database is physically updated before the transaction reaches to its
Commit point.
UNDO may be necessary when a transaction fails.
REDO is not needed.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:53


Recovery techniques based on Differed update

 A deferred update method obey the following rules:


 A transaction cannot change the database on disk until it reaches its commit point
 A transaction does not reaches its commit point until all its update operations are
recorded on disk until after the log and the log is force-written to disk.
 Because the database is never updated on disk until after the transaction commits, there is
never the needed to UNDO any operations (that is why a deferred update is known as
NO-UNDO/REDO recovery algorithm).
Deferred data modification
 Transaction starts by writing [starts-transaction, Ti] record to the log.
 A write operation result in a log record for record for data item X: [write-item, Ti, X, new-
value]
- Note: old value is not needed for this scheme
- The write is not performed on X at this time, but is deferred.
 When Ti partially commits, [commit, Ti] is written to the log
 Finally, the log records are read and used to actually execute the previously deferred writes

Differed update with the concurrent execution - In multi-user environment:

 In many cases, the concurrency control and recovery process are same.
 Concurrency control uses two-phasing locking, so the locks on items remain in effect remains in
effect until two transactions reach its commit point. After that the LOCKS ARE RELEASED
 The recovery using update is a multi-user environment, uses two lists of transactions, one is the
commit transaction T since the last check point, and another’s is the active transactions T’.
REDO all the write-item operations of committed transaction form the log, in order in which they
are written into the log. The active transactions and did not commit are effectively canceled and
must re submitted.
Consider the following schedule of executing transactions

When the check point was


taken at time T1, transaction t1
had committed, where as T3 and
T4 had not. Before the system
crash at time t2 t3 and t2 are
committed but not t4and T5.
According to recovery
procedure T2 andT3 must be
REDO and the transactions
T4 and T5 are ignored, no write
operations were recorded in the
database.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:54


Recovery techniques based on Immediate Update:

• In these techniques, the DB on disk can be updated immediately without any need to wait for the transaction
to reach its commit point.

• However, the update operation must still be recorded in the log (on disk) before it is applied to the database
*-using WAL protocol- so that we can recover in case of failure.

• Undo the effect of update operations that have been applied to the DB by a failed transaction. – Rollback the
transaction and UNDO the effect of its write operations

• If the recovery technique ensures that all updates of a transaction are recorded in the DB on disk before the
transaction commits, there is never a need to REDO any operations of committed transactions –

UNDO/NOREDO recovery algorithm-

• If the transaction is allowed to commit before all its changes are written to the DB, REDO all the operations of
committed transactions –UNDO/REDO recovery algorithm- UNDO/REDO Immediate Update in a Single-User
Environment – Procedure RIU_S – Recovery Immediate Update in Single-User environment –

• Use two lists of transactions maintained by the system: the committed transactions since the last checkpoint
and the active transactions –at most one because single-user-

• Undo all write-item operations of the active transaction from the log, using the UNDO procedure. – The
operations should be undone in the reverse of the order in which they were written into the log – After making
these changes, the recovery subsystem writes a log record [abort,T] to each uncommitted transaction into the
log.

• Redo the write-item operations of the committed transactions from the log, in the order in which they were
written in the log, using the REDO procedure. – UNDO(write-op)

• Examine the log entry [write-item, T,X, old-value ,new-value] and setting the value of item X in the DB to old-
value which is the before image (BFIM).

Log-Based Recovery

The most widely used structure for recording database modifications is the log. The log is a sequence
of log records and maintains a history of all update activities in the database. There are several types of
log records.
An update log record describes a single database write:

 Transactions identifier.
 Data-item identifier.

 Old value.

 New value.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:55


Other special log records exist to record significant events during transaction processing, such as the
start of a transaction and the commit or abort of a transaction. We denote the various types of log records
as:

 <Ti start>.Transaction Ti has started.


 <Ti, Xj, V1, V2> Transaction Ti has performed a write on data item Xj. Xj had value V1 before write, and
will have value V2 after the write.

 < Ti commit> Transaction Ti has committed.

 < Ti abort> Transaction Ti has aborted.

Whenever a transaction performs a write, it is essential that the log record for that write be created
before the database is modified. Once a log record exists, we can output the modification that has
already been output to the database. Also we have the ability to undo a modification that has already
been output to the database, by using the old-value field in the log records.
For log records to be useful for recovery from system and disk failures, the log must reside on stable
storage. However, since the log contains a complete record of all database activity, the volume of data
stored in the log may become unreasonable large.

Deferred Database Modification

The deferred-modification technique ensures transaction atomicity by recording all database


modifications in the log, but deferring all write operations of a transaction until the transaction partially
commits (i.e., once the final action of the transaction has been executed). Then the information in the
logs is used to execute the deferred writes. If the system crashes or if the transaction aborts, then the
information in the logs is ignored.
Let T0 be transaction that transfers $50 from account A to account B:
T0: read (A);
A: = A-50;
Write (A);
Read (B);
B: = B + 50;
Write (B).

Immediate Database Modification

The immediate-update technique allows database modifications to be output to the database while the
transaction is still in the active state. These modifications are called uncommitted modifications. In the
event of a crash or transaction failure, the system must use the old-value field of the log records to
restore the modified data items.
Transactions T0 and T1 executed one after the other in the order T0 followed by T1. The portion
of the log containing the relevant information concerning these two transactions appears in the
following,
Portion of the system log corresponding to T0 and T1
< T0 start >
< T0, A, 1000, 950 >
< T0, B, 2000, 2050 >
< T0 commit >
< T1 start >
< T1, C, 700, 600 >
< T0 commit >

Checkpoints

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:56


When a system failure occurs, we must consult the log to determine those transactions that need to be
redone and those that need to be undone. Rather than reprocessing the entire log, which is time-
consuming and much of it unnecessary, we can use checkpoints:
 Output onto stable storage all the log records currently residing in main memory.
 Output to the disk all modified buffer blocks.
 Output onto stable storage a log record, <checkpoint>.
Now recovery will be to only process log records since the last
Recovery techniques based on deferred update:

Recovery techniques based on deferred: an example of recovery in a multi-user environment.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:57


Shadow Paging:

 Shadow paging considers the database to make up of a member of fixed size disk blocks, for
recovery purpose.
 A directory with n entries is constricted, where ith entry point to the ith database page on disk.
The directory is kept in main memory if it is not too large, and all references (read/write) to the
database pages go through it.
 When a transaction begins executing current directory, whose entries is copied into a shadow
directory. The shadow directory is saved on disk while the current directory is is used by
transaction.
 During execution the shadow directory is never modified. When write item operation is
performed, a new copy of modified page is created, without over write copy. The new page is
created in unused disk block.
 Current directory entry is modified to point to new disk block, where as shadow directory is not
modified and continues to point to point to the old unmodified disk block.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:58


UNIT - II
Chapter : JDBC Objects
The Concept of JDBC:
 There are many commercial DBMSs available in the market, such as Oracle, DB2,
Sybase, and so on. Sun Microsystems has faced a challenge in late 1990 to develop a way
for java developers to write high-level code that access all popular DBMSs.
 In the year 1996 sun Microsystems developed the JDBC driver and JDBC API.
 The java programmers may use high-level data objects defined in the JDBC API to write
a routine that interact with the DBMS. Java data objects convert the routine into low-level
message that conform to the JDBC driver specification and send them to the JDBC driver.
The JDBC driver translates the routine into low-level messages that are understood and
processed by the DBMS.
 The JDBC drives created by DBMS manufacturers have to,
 Open a connection between the DBMS and the J2EE component.
 Translate the SQL statements sent by J2EE component into messages that can be
processed by the DBMS.
 Return data that conform to the JDBC specification to the JDBC driver.
 Return information such as error messages that conforms to the JDBC specification to
the JDBC driver.
 Provide transaction management routines that conform to the JDBC specification.
 Close the connection between the DBMS and the J2EE component.

JDBC Driver Types:


 The JDBC driver specification classifies JDBC drivers into 4 groups.
Type 1 JDBC –to – JDBC Driver:
 ODBC (Open Data Base Connectivity) is the basis from which Sun Microsystems created
JDBC.
 Both ODBC and JDBC have similar driver specifications and an API.
 This is also called JDBC/ODBC Bridge. The JDBC-to-ODBC driver receives messages
from a J2ee component that conforms to the JDBC specification. These messages are
translated by the JDBC-to-ODBC driver into the ODBC message format, which is then
translated into the message format understood by the DBMS.
 The disadvantage of using this driver is, it takes extra translation so it may impact
performance.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:59


Type 2 Java/ Native Code Driver:
 The Java/Native code driver uses java
classes to generate platform specific code,
only understood by a specific DBMS.
 The manufacturer of the DBMS provides
both the Java/Native Code driver and API
classes. So the J2EE component can
generate the platform specific code.
 The disadvantage of this driver is the loss
of some portability of code. This driver
probably won’t work with another
manufacturer’s DBMS.

Type 3 JDBC Driver:


 The Type 3 JDBC Driver, also
referred as the Java Protocol, is most
commonly used JDBC Driver.
 This driver converts SQL queries into
JDBC formatted statements. The
JDBC formatted statements are
translated into the format required by
the DBMS.

Type 4 JDBC Driver:


 Type 4 JDBC Driver is also known as the
Type 4 database protocol. This driver is
similar to the Type 3 Jdbc Driver except SQL
Queries are translated into the format required
by the DBMS.
 The SQL Queries do not need to be converted
to JDBC formatted systems. This is the fastest
way to communicate SQL queries to the
DBMS.

JDBC Packages:
 The JDBC API is contained in two packages. java.sql and javax.sql.
 The first package is called java.sql and contains core java does objects of the JDBC API.
It provides the basics for connecting to the DBMS and interacting with data stored in the
DBMS. It is a part of the J2SE.
 The other package that contains the JDBC API is javax.sql, which extends java.sql and is
in the J2EE. It includes java data objects that interact with Java Naming and Directory
Interface (JNDI) and java data objects that manage connection pooling, among other
advanced JDBC features.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:60


The JDBC Process:
This process is divided into five routines. These include
1. Loading the JDBC Driver
2. Connecting to the DBMS
3. Creating and executing a statement
4. Processing data returned by the DBMS
5. Terminating the connection with the DBMS
Loading the JDBC Driver:
 The JDBC driver must be loaded before the J2EE component can connect to the DBMS.
 The driver is loaded by calling Class.forName( ) method and passing it the name of the
driver.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connecting to the DBMS:
 Once driver is loaded, the J2EE component must connect to the DBMS using the
DriverManger.getConnection( ) method.
 The DriverManger.getConnection( ) method is passed the URL of the database, and the
user Id and password if required by the DBMS.
 The URL is a string object that contains the driver name and name of the database name
that is being accessed by the J2EE component.
 The following code is used to load the JDBC/ODBC bridge and connect.
Connection con= DriverManager.getConnection(URL,UserId,Password);
Example:
Connection con=
DriverManager.getConnection("jdbc:odbc:xyzcompany"scott","tiger");
Here xyzcompany is the Datasource name.
Create and Execute a SQL Statement:
 After driver loaded and connection is successfully made, then a SQL query is to be send to
the DBMS for processing.
 A SQL query consists of a series of SQL Commands that direct the DBMS to do something
such as to return rows of data to the J2EE component.
 The Connect.createStatement( ) method is used to create a Statement object.
 The Statement object is then used to execute a query and return a ResultSet object that
contains response from the DBMS, which contains one or more rows of information.

Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from student");
Process Data returned by the DBMS:
 The java.sql.ResultSet object is assigned the results received from the DBMS after the
query is processed. It contains methods used to interact with data that is returned by the
DBMS to the J2EE component.
 The result returned by the DBMS is assigned to the ResultSet object.
 The next( ) method positioned at the first row in the ResultSet and returns a Boolean value
that if false indicates that no rows are present in the ResultSet.
 The getInt( ) method is used to copy the value of specified column in the current row of the
ResultSet to a Integer object.
 The getString( ) method is used to copy the value of specified column in the current row of
the ResultSet to a String object.
For example the student table contains 3 columns namely sno, sname and class.
S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:61
while(rs.next())
{
System.out.println(rs.getInt("sno")+" "
+rs.getString("sname")+" "
+rs.getString("class"));
}
Terminating the connection with the DBMS:
 The connection to the DBMS is terminated by using the close( ) method of the connection
object once the J2EE component is finished accessing the DBMS.
 The close( ) method throws an exception if a problem is encountered when disengaging
object.
For example : con.close();
Database Connection:
 Before this connection is made, the JDBC driver must be loaded and registered with the
DriverManager.
 The JDBC driver is automatically registered with the DriverManager once the JDBC
driver is loaded and is therefore available to the JVM and can be used by J2EE
components.
 The Class.forName() method is used to load the JVM driver. In the example, the
JDBC/ODBC Bridge is he driver that is being loaded.
The Connection:
 The data source that the JDBC component will connect to is defined using the URL
format. The URL consists of the three parts. These are
1. jdbc which indicates that the JDBC protocol is to be used to read the URL.
2. <subprotocol> which is the JDBC driver name.
3. <subname> which is the name of the database.
 the connection to the database is established by using one of three getConnection()
methods of the DriverManager object.
 A connection object is returned by the getConnection( ) method if access is granted;
otherwise, the getConnection( ) method throws a SQLException.
String url=”jdbc:odbc:CustomerInfomration”;
Statement DataRequest;
Connection con;
try {
class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Connection con= DriverManager.getConnection
(“jdbc:odbc:svarts”,”scott”,”tiger”);
}
Catch(ClassNotFoundExcetion error)
{ System.err.println(“unable to load JDBC/ODBC bridge.” + error);
System.exit(1);
}
Catch(SQLException error)
{ System.err.println(“cannot connect to the database.” + error);
System.exit(2);
}

TimeOut:

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:62


 The public static void DriverManager.setLoginTimeout(int seconds) method can be used
by the j2EE component to establish the maximum time the DriverManager waits for a
response from a DBMS before timing out.
 The public static int DriveraManager.getLoginTimeout() method is used to retriever
the DriverManager the maximum time the DriverManager is set to wait until it time out.
 The DriverManager.getloginTimeout() method returns an int that represents seconds.

Associating the JDBC/ODBC Bridge with the Database:


 We can use the ODBC Data source Administrator to create the association between the
database and the JDBC/ODBC Bridge.
 We have follow these steps.
1. Select Start settings and then the Control Panel.
2. Select ODBC 32 to display the ODBC Data source Administrator.
3. Add a new user by selecting the Add button.
4. Select the driver then select finish.
a) Use the Microsoft Access Driver if you are using Microsoft Access. Enter the
name of the Data source name in the ODBC Microsoft Access setup dialog box.
Click the select button to browse the director of each hard drive connected to
define the path to the database in the Select Database dialog box, and click ok.
b) Use the Microsoft ODBC for Oracle Driver if you are using Oracle; otherwise,
select the driver for the DBMS that you are using. Enter the name of the Data
source name in the Microsoft ODBC for Oracle setup dialog box. This is the name
that will be used within your java database program to connect to the DBMS.
5. When ODBC setup dialog box appears, click Ok to close.
6. Select Ok to close the ODBC Data Source Administrator dialog box.
Connection Pool:
 A connection pool is a collection of database connections that are opened once and loaded
into memory. So, these connections can be reused without having to reconnect to the
DBMS.
 There are two types of connections made to the database. The first is the physical
connection, which is made by the application server using PooledConnection objects.
Pooled Connection objects ate cached and reused.
 The other type of connections is the logical connection. A logical connection is made by a
client calling the DataSource.getConnection( ) method, which connects to a Pooled
Connection object that already has a physical connection to the database.
 The close( ) method of the DataSource object is called once when the J2EE component is
finished accessing the database. The close( ) method closes the logical connection to the
database and not the physical database connection.
Context ctext =new InitialContext( );
DataSource pool=(DataSource);
ctext.lookup(“java:comp/env/jdbc/pool”);
Connection db=pool.getConnection( );
… …… … …… ……
db.close();

JDBC Statement Objects:


 Once a connection to the database is opened, the J2EE component creates and sends a
query to access the data contained in the database..
 There are 3 types of statement objects are used to execute the query.
1. Statement
S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:63
2. PreparedStatement
3. CallableStatement
1. The Statement Object:
 The Statement object is used whenever a J2EE component needs to immediately execute
a query.
 The Statement object contains the executeQuery() method, which is passed the query as
an argument. The query is then transmitted to the DBMS for processing.

Ex: Statement st=con.createStatement();


ResultSet rs=st.executeQuery("select * from student");
 The executeQuery( ) method returns one ResultSet object that contains rows, columns
and metadata that represent data requested by the query.
 The execute( ) method is used when there may be multiple results returned.
 The executeUpdate( ) method is used to execute queries that contain INSERT, UPDATE,
DELETE and DDL statements.

 Consider the following example to insert a row in the student table.


String query="insert into student values(555,’Rahul’,’mca’)";
Statement st=con.createStatement();
st.executeUpdate(query);
 Consider the following example to update a row in the student table.
String query="Update student set class=’mca’ where sno=111";
Statement st=con.createStatement();
st.executeUpdate(query);

 Consider the following example to delete a row in the student table.


String query="delete student where sno=222";
Statement st=con.createStatement();
st.executeUpdate(query);

2. PreparedStatement Object:
 The PreparedStatement interface extends the Statement interface, which gives you
added functionality with a couple of advantages over a generic Statement object.
 A SQL query can be precompiled and executed by using the PreparedStatement
object.
 The query is constructed with a question mark, for a placeholder for a value that is
inserted in the query after the query is compiled. This value change each time the
query is executed.
 The PrepareStatement( ) method of the connection object is called to return the
PreparedStatement object. This method is passed the query, which is then
precompiled.
 For example to insert a row, the statements are as follows,
String sql= “insert into student values(?,?,?,?)”;
PreparedStatement pstmt= conn.prepareStatement(sql);
 For example to update a row, the statements are as follows
String sql= “update student set age=? Where sno=?”;
PreparedStatement pstmt= conn.prepareStatement(sql);

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:64


 The setxxx( ) method of the PreparedStatement object is used to replace the question
mark with the value passed to the setxxx() method. This methods specifies the data
type of the value that is being passed to setxxx() method.
 The setxxx() method requires tow parameters. The first parameter is an integer that
identifies the position of the question mark placeholder. The second parameter is the
value that replaces the question mark placeholder.
 The execute( ) and executeQuery( ) methods does not require a parameter because the
query that is to be executed is already associated with the PreparedStatement object.
 For example the following code inserts a row dynamically.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con= DriverManager.getConnection
("jdbc:odbc:svmca","scott","tiger");
String s="insert into student values(?,?,?,?)";
PreparedStatement pst=con.prepareStatement(s);
DataInputStream in=new DataInputStream(System.in);
int num;
String s1,s2,s3;
System.out.println("Enter sno,sname,course and college");
num=Integer.parseInt(in.readLine());
s1=in.readLine();
s2=in.readLine();
s3=in.readLine();
pst.setInt(1,num);
pst.setString(2,s1);
pst.setString(3,s2);
pst.setString(4,s3);
pst.execute();
3. CallableStatement Object:
 The CallableStatement object will be used to execute a call to a database stored
procedure from within a J2EE object.
 The CallableStatement object uses three types of parameters when calling a stored
procedure. These parameters are IN, OUT, and INOUT.
Parameter Description

IN A parameter whose value is unknown when the SQL statement is created. We bind
values to IN parameters with the setxxx( ) methods.

OUT A parameter whose value is supplied by the SQL statement it returns. We retrieve
values from the OUT parameters with the getxxx( ) methods.

INOUT A parameter that provides both input and output values. We bind variables with
the setxxx () methods and retrieve values with the getxxx ( ) methods.

 The preparedCall( ) method of the Connection object is called and is passed the query.
 The registerOutParameter( ) method requires two parameters. The first parameter is an
integer that represents the number of parameter, and second parameter is the data type of
value returned by the stored procedure, which is Types.VARCHAR.
S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:65
 The execute( ) method of the CallableStatement object is called next to execute the query.
 The Syntax to create an object, is as follows
String query = "{call procedureName(?,?)}";
CallableStatement cstmt = conn.prepareCall(query);
cstmt.registerOutParameter(2,Types.VARCHAR);
In SQL, we can create a procedure as follows.
create or replace procedure getename(n in number,
name out varchar)is
begin
select ename into name from emp where empno=n;
end;
/
 Consider the following a J2EE component.
import java.sql.*;
import java.io.*;
class procedurecall
{
public static void main(String args[])throws
SQLException,IOException,Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=
DriverManager.getConnection("jdbc:odbc:svarts","scott","tiger");
DataInputStream in=new DataInputStream(System.in);
int num;
String ename;
System.out.println("Enter employee number");
num=Integer.parseInt(in.readLine());
String s="{call getename(?,?)}";
CallableStatement cstmt=con.prepareCall(s);
cstmt.setInt(1,num);
cstmt.registerOutParameter(2,Types.VARCHAR);
cstmt.execute();
ename=cstmt.getString(2);
System.out.println("Employee Name="+ename);
cstmt.close();
}
}

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:66


ResultSet:
 The executeQuery( ) method is used to send the query to the DBMS for processing
and returns a ResultSet object that contains data that was requested by the query.
 The ResultSet object contains methods that are used to copy data from the ResultSet
into a java collection object or variable for further processing. Data in a ResultSet
object is logically organized into a virtual table consisting of rows and columns.
 The ResultSet uses a virtual cursor to point to a tow of the virtual table. A J2EE
component must move the virtual cursor to each row and then use other methods of
the ResultSet object also contains metadata such as column names, column size, and
column data type
 The next() method returns a boolean true if the row contains data otherwise a boolean
false is returned indicating that no more rows exists in the ResultSet.
 Once the virtual cursor points to a row the getxxx ( ) method is used to copy data from
the row to a collection, object or variable.
 The getxxx( ) method requires one parameter, which is an integer that represents the
number of the column that contains the data. For example, getString(1) copies the data
from the first column of the ResultSet.
 Columns appear in the ResultSet in the order in which column names appeared in the
SELECT statement in the query.
Consider the following example:
while(rs.next())
{
System.out.println(rs.getInt("empno")+" "
+rs.getString("ename")+" "
+rs.getString("Job"));
}

Scrollable ResulstSet:
 The JDBC 2.1 API also enables a J2EE component to specify the number of rows to
returns from the DBMS.
 There are six methods first( ),last ( ), previous( ), absolute( ), relative( ), and getRow( ).
1. first( ): the first ( )method moves the virtual cursor to the first row in the ResultSet.
2. last( ): the last( ) method positions the virtual cursor at the last row in the ResultSet.
3. previous( ):the previous( ) method the virtual cursor to the previous row
4. absolute( ): the absolute( ) method positions the virtual cursor at the row number
specified by the integer passed as a parameter to the absolute ( ) method.
5. relative( ): the relative( ) method is again called to return the virtual back to its
original row by moving the virtual cursor two rows forward
6. getRow( ): the getRow() method returns an integer that represents the number of the
current row in the ResultSet.
 The Statement object that is created using the createStatement( )of the Connection object
must be set up to handle a scrollable ResultSet by passing the createStatement( ) method
one of three constants. These constants are TYPE_FORWARD_ONLY,
TYPE_SCROLL_SENSITIVE and TYPE_SCROLL_SENSITIVE.
 The TYPE_FORWARD_ONLY constant restricts the virtual cursor to downward
movement, which is the default setting. The TYPE_SCROLL_INSENSITIVE and
TYPE_SCROLL_SENSITIVE constants permit the virtual cursor to move in both
directions.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:67


 The TYPE_SCROLL_INSENSITIVE constant makes the ResultSet insensitive to
changes made by another J2EE component to data in the table whose rows are reflect in
the ResultSet. The TYPE_SCROLL_SENSITIVE constant makes the ResultSet sensitive
to those Changes.
Consider the following example:
try {
String query=”select empno,ename,job from emp”;
Statement st=con.createStatement(TYPE_SCROLL_INSENSITIVE);
ResultSet rs=st.executeQuery(query);
}catch(SQLException error)
{
System.out.println(error);
}
Note: That all JDBC Drivers are scrollable.

Updatable ResultSet:
 The rows contained in the ResultSet can be updatable by passing the createStatement( )
method of the Connection object the CONCUR_UPDATABLE.
 There are 3 ways in which a ResultSet can be changed.
1. Update a row in the ResultSet:
 The updatexxx( ) method is used to change the value of a column in the current row of
the ResultSet.
 The updatexxx( ) method requires two parameters. The first is either the number or
name of the column of the ResultSet that is being updated and the second parameter is
the values that will replace the value in the column of the ResultSet.
 The updateRow( ) method is called after all the updatexxx() method are called the
updateRow( ) method changes values in column row of the current row of the ResultSet
based on the values of the updatexxx() methods.
Consider the following example:
try {
String query=”select empno,ename,job from emp where ename=’SMITH’ ”;
Statement st=con.createStatement(ResultSet.CONCUR_UPDATABLE);
ResultSet rs=st.executeQuery(query);
rs.updateString(“job”,”MANAGER”);
rs.updateRow();
con.close();
}catch(SQLException error)
{
System.out.println(error);
}
2. Delete a row in the ResultSet:
 The deleteRow( ) method is used to remove a row fro a ResultSet.
 The deleteRow( ) method is passed an integer that contains the number of the row to be
deleted.
 However, the values of that row should be examined by the program to assure it is the
proper row before the deleteRow() method is called.
 The deleteRow( ) method is then passed a zero integer indicating that the current row
must be deleted, as show in the following statement.
Results.deleteRow(0);

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:68


3. Insert a row in the ResultSet:
 The updatexxx( ) method is used to specify the column and value that will be placed
into the column of the ResultSet.
 The updatexxx( ) method requires two parameters. The first parameter is either the
name of the column or the number of the column of the ResultSet. The second
parameter is the new value that will be placed in the column in the ResultSet.
 The insertRow( ) method is called after the updatexxx( ) methods, which causes a new
row to be inserted into the ResultSet having the values that reflect the parameter in the
updatexxx( ) methods.
 Consider the following example:
try {
String query=”select custname,place from customer”;
Statement st=con.createStatement(ResultSet.CONCUR_UPDATABLE);
ResultSet rs=st.executeQuery(query);
rs.updateString(1,”Rahul”);
rs.updateString(2,”Nellore”);
rs.insertRow();
con.close();
}catch(SQLException error)
{
System.out.println(error);
}

Transaction Processing
 A database transaction consists of a set of SQL statements, each of which must be
successfully completed for the transaction to be completed. If one fails, sql statements
that executed are rolled back.
 A database transaction is not completed until the J2EE component calls the commit( )
command method of the Connection object.
 The commit( ) method must be called regardless if the SQL statement is part of a
transaction or not.
 If a J2EE component is processing a transaction, the AutoCommit feature must be
deactivated by calling the setAutoCommit( ) method and passing it a false parameter.
Once the transaction is completed, the setAutoCommit( ) method is called again
automatically and pass the true parameter, when we call the commit( ) method.
Example:
con.setAutoCommit(false);

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:69


import java.sql.*;
import java.util.*;
class tcsdemo
{
public static void main(String args[])throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con= DriverManager.getConnection("jdbc:odbc:svarts","scott","tiger");
try
{
con.setAutoCommit(false);
Statement st=con.createStatement();
String query="insert into student values(666,'jeeva','mca')";
st.executeUpdate(query);
con.commit();
}catch(SQLException error) { }
Statement st1=con.createStatement();
ResultSet rs1=st1.executeQuery("select * from student");
while(rs1.next())
{
System.out.println(rs1.getInt("sno")+" "
+rs1.getString("sname")+" "
+rs1.getString("dob"));
}
con.close();
}
}

ResultSet Holdability:
 Whenever the commit( ) method is called, all ReslultSet objects that were created for the
transaction are closed. Sometimes a J2EE component needs to keep the ResultSet open
even after the commit( ) method is called.
 The HOLD_CURSOR_OVER_COMMIT constant keeps ReslutSet objects open
following a call to the commit( ) method and CLOSE_CURSOR_AT_COMMIT closes
ResultSet objects when the commit( ) method is called.
RowSets:
 The JDBC RowSets object is used to encapsulate a ResultSet for use with Enterprise
Java Beans (EJB).
 A RowSet object contains rows of data from a table that can be used in disconnected
operations.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:70


Metadata:
 Metadata is data about data, a J2EE component can access metadata by using the
DatabaseMetaData interface. The DatabaseMetaData interface s used to retrieve
information about databases, tables, columns and indexes among other information about
the DBMS.
 A J2EE component retrieves metadata about the database by calling the getMetaData( )
method of the Connection object. The getMetaData( ) method returns a
DatabaseMetaData object that contains information about the database and its component.
 Some of the more commonly used DatabaseMetaData object methods are:
1. getDatabaseProductName( ) : Returns the product name of the database.
2. getUserName( ) : Returns the username.
3. getSchemas( ): Returns the URL of the database.
4. getPrimaryKeys( ); Returns primary key.
5. getProcedures( ): Returns stored procedure names.
6. getTables( ) : Returns names of tables in the database.
ResultSet MetaData:
 Metadata that describes the ResultSet is retrieved by calling the getMetaData( ) method of
the ResultSet object.
ResultSetMetaData rsm=Result.getMetaData();
 Once the ResultSet metadata is retrieved, the J2EE component can call the methods of the
ResultSetMetaData object to retrieve specific kinds of metadata.
1. getColumnCount( ): Returns the number of columns contained in the ResultSet.
2. getColumnName(int number) : Returns the name of the column specified by the
column number.
3. getColumnTypeName(int number): Returns the data type of the column specified
by the column number.
import java.sql.*;
import java.io.*;
class metadata
{
public static void main(String args[])throws Exception
{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:svarts","scott","tiger");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from dept");
ResultSetMetaData rsm=rs.getMetaData();
for(int i=1;i<=rsm.getColumnCount();i++)
{ System.out.println(rsm.getColumnName(i)+" "
+rsm.getColumnTypeName(i)+" "
+rsm.getPrecision(i));
}
con.close();
}
}

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:71


Chapter: JDBC and Embedded SQL
Tables:
Creating a Table:
 We create a table by formatting a SQL query. The query contains the CREATE TABLE
SQL statement that contains the name of the table.
 Column definition, which are enclosed within parentheses. The data type of the columns
and the column size follows the columns name. A comma separates each column.
 Once a column is designated as primary key, the DBMS prevents duplicate and null
values.
String query= “CREATE TABLE CUSTOMER(“ + ”
CustNo Numeric(3) PRIMARY KEY, “ + ”
CustName Char(20), “ + ”
CustStreet char(10), “ + ”
CustCity char(20))”;
Statement st=con.createStatement( );
st.execute(query);
st.close( );
Requiring Data in a Column:
 There are business rules that require a value in specific columns of a row such as the
columns that contain a customer’s name.
 You can require the DBMS to reject rows that are missing a value in specific columns by
using the NOT NULL clause in the query when the table is created.
String query= “CREATE TABLE CUSTOMER(“ + ”
CustNo Numeric(3) NOT NULL, “ + ”
CustName Char(20) NOT NULL, “ + ”
CustStreet char(10), “ + ”
CustCity char(20))”;
Setting a Default Value for a Column:
 The DBMS can enter value into a column automatically if the column is left empty
whenever a new row is inserted into the table.
 A default value is assigned to a column when you create the table by using the
DEFAULT clause. The DEFAULT clause is followed in the query by the value the
DBMS will place in the column if the column is empty in the incoming row.
String query= “CREATE TABLE CUSTOMER(“ + ”
CustNo Numeric(3) NOT NULL, “ + ”
CustName Char(20) NOT NULL, “ + ”
CustStreet char(10), “ + ”
CustCity char(20) DEFAULT ‘GUDUR’ )”;
Dropping a Table:
 The decision to drop a table shouldn’t be made lightly because once a table is dropped
you are unable to recover the table.
 In addition to losing data elements stored in the table, dropping a table may also affect the
integrity of the database and table that relate to values in the dropped table. Using the
Drop table Statement in the query drops a table.
String query= “DROP TABLE CUSTOMER”;
Statement st=con.createStatement( );
st.execute(query);
st.close( );

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:72


consider the following program:
import java.sql.*;
import java.io.*;
import java.net.*;
class createtable
{
public static void main(String args[])throws Exception
{
try
{
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Connection con=DriverManager.getConnection(“jdbc:odbc:svarts”,”scott”,”tiger”);
String query= “CREATE TABLE CUSTOMER(“ +
“ CustNo Numeric(3)PRIMARY KEY, “ +
“ CustName Char(20)NOT NULL, “ +
“ CustStreet char(10), “ +
“ CustCity char(20)DEFAULT ‘GUDUR’)”;
Statement st=con.createStatement( );
st.execute(query);
st.close( );
con.close();
}catch(SQLException error){ }
}
}

Indexing:
Creating an Index:
 An index is created by using the CREATE INDEX statement in a query, contains the
name of the index and any modifier that describes to the DBMS the type of index that is
to be created.
try {
String query= “CREATE UNIQUE INDEX empnoindex on EMP(empno)”;
Statement st=con.createStatement( );
st.execute(query);
st.close( );
con.close();
}
Creating a Secondary Index:
 A secondary index is created by using the CREATE INDEX statement in a query without
the use of the UNIQUE modifier that a secondary index can have duplicate values.
try {
String query= “CREATE INDEX enameindex on EMP(ename)”;
Statement st=con.createStatement( );
st.execute(query);
st.close( );
con.close();
}
Creating a Clustered Index:
 A Clustered index is an index whose keys is created from two or more columns of a table.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:73


 This example creates an index called empindex and uses the empno and ename columns
of the emp table as the keys for the index.
String query= “CREATE INDEX empindex on EMP(empno,ename)”;
Dropping an Index:
An existing index can be removed from the database by using the DROP INDEX statement
the DROP INDEX statement is similar in construction to the DROP TABLE statement.
String query= “DROP INDEX empindex on EMP”;
Inserting Data into Tables:
 Once a database, tables and indexes are created a J2EE component can insert a new row
into a table.
 The executeUpdate( ) method is used to execute query that inserts a row.
Consider the table: Admission(sno,sname,class,jdadm)
Inserting a Row:
 The INSERT INTO statement is used to insert a new row into a table.
 Contains the name of the table into which the row is to be inserted and the name of the
column in which values are inserted.
 Each value is paced in the VALUES clause in the same order as the corresponding
columns name.
try {
String query= “INSERT INTO Admission values(101,’siva’,’M.C.A’,’12-AUG-2018’)”;
Statement st=con.createStatement();
st.executeUpdate(quey);
st.close( );
}
Inserting the System’s Date into a Column:
 Sometimes business rules require that the current date be placed into a column of a new
role.
 You can place the systems date into a column by calling the CURRENT_DATE function.
This is not implemented in all versions of DBMS. If you’re using Microsoft Access for
your DBMS, then you use NOW. Oracle uses SYSDATE. DB2 uses CURRENT DATE.
String query= “INSERT INTO Admission values(101,’siva’,’M.C.A’,CURRENT_DATE)”;
Inserting the System Time into a Column:
 Call the CURRENT_TIME ( ) function whenever a column requires the current time.
 The CURRENT_TIME( ) function is called by the DBMS when the query is proceed and
returns the current time of the server.
Inserting a Timestamp into a Column:
 A timestamp consists of both the current data and time and is used in applications where
both date and time are critical to the operation of the business.
 You can place the timestamp into a column by calling the TIMESTAMP( ) function. The
server’s system date and time is returned by the function and placed into the column.
String query= “INSERT INTO Admission “ + ”
Values (101,’siva’,’M.C.A’,CURRENT_TIMESTAMP)”;
Selecting Data From a Table:
Retrieving information from a database is the most frequently used routine of J2EE
components that interact with a database.
Selecting All Data from a Table:
 The SELECT statement is used to retrieve data from a table. The names of columns that
you want retrieved from the table are included in the SELECT statement.
 The executeQuery( ) method is used to execute the query and returns a ResultSet object.
S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:74
 The getString( ) method returns the values of the column, which is assigned to a String
object.
String query=”select * from student”;
ResultSet rs=st.executeQuery(query);
while(rs.next())
{ System.out.println(rs.getInt(“sno”)+” “
+rs.getString(“sname”)+” “
+rs.getString(“class”)+” “
+rs.getString(“college”));
}
Requesting one Column:
We can specify a column that we want to returned from the table by using the column
name in the SELECT statements.
String query=”select class from student”;
ResultSet rs=st.executeQuery(query);
while(rs.next())
{ System.out.println(rs.getString(1));
}
Requesting Multiple Columns:
Multiple columns can be retrieved by specifying the names of the columns in the
SELECT statements.
String query=”select sno,college from student”;
ResultSet rs=st.executeQuery(query);
while(rs.next())
{ System.out.println(rs.getInt(“sno”)+” “
+rs.getString(“college”));
}
Requesting Rows:
 Specific rows can be retrieved from a column by using the WHERE clause in conjunction
with the SELECT statement .
 The WHERE clause contains an expression that is used by the DBMS to identify rows
that should be returned in the ResultSet.
 Any logical expression can be used to include or exclude rows based on values in
columns of a row.
Requesting Rows and Columns:
 A query can select less than all the columns and all the rows of a table by using a
combination of technique.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:75


String query=”select empno,ename,sal from emp where job=’MANAGER’”;
ResultSet rs=st.executeQuery(query);
while(rs.next())
{ System.out.println(rs.getInt(1)+” “
+rs.getString(2)+” “
+rs.getString(3));
}

AND, OR and NOT Clauses:


 The WHERE clauses in a SELECT statement can evaluate values in more than one
column of a row by using the AND, OR and NOT clauses.
 For example, the AND clause require that both expressions in the compound expression
evaluate to true before the WHERE clause expression evaluates true and includes a
specified row in the ResultSet.
String query=”select * from emp where job=’MANAGER’ and sal>=3000”;
ResultSet rs=st.executeQuery(query);
while(rs.next())
{
System.out.println(rs.getInt(“empno”)+” “
+rs.getString(“ename”)+” “
+rs.getString(“job”)+” “
+rs.getInt(“sal”));
}

 The OR clause requires that a least one of the expression in the compound expression
evaluate to true before the WHERE clause expression evaluates true.
String query=”select * from emp where job=’MANAGER’ OR job=’CLERK’”;
 The NOT clause is used reverse the logic, changing an expression that evaluates true to a
false.
String query=”select * from emp where NOT job=’MANAGER’”;
ResultSet rs=st.executeQuery(query);

Equal and Not Equal Operators:


 The Equal(=) and Not Equal(!= or < >) operators are used to determine if the value in the
WHERE clause expression is or isn’t in the specify column.
 The not equal operator is used in a WHERE clause expression or sub expression to
identify rows that neither should nor be returned by the DBMS.
String query=”select * from emp where job=’MANAGER’ and sal<>3000;
Less Than and Greater than Operators:
 The less than and greater than operator direct the DBMS to access whether or not he
value in the specified column of the current row is less than or greater than the value in
the WHERE clause expression.
 Keep in mind that a value in a column that equals the value in the WHERE clause
expression is evaluated as a Boolean false.
 The value in the column be less than or greater than but not equal to the value in the
WHERE clause expression.
String query=”select * from emp where sal>=2000 and sal<=3000;
ResultSet rs=st.executeQuery(query);

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:76


BETWEEN:
 The BETWEEN operator s used to define a range of values that is to be used as the value
of the selection expression.
 The BETWEEN operator must follow the name of the column n the WHERE clause. The
AND operator is used to join the lower and upper values of the range.
 All values in the value, including the first and last values are considered when the DBMS
evaluates the value of the use of the BETWEEN operator.
String query=”select * from emp where sal BETWEEN 2000 and 3000;
ResultSet rs=st.executeQuery(query);
LIKE:
 The LIKE operator directs the DBMS to return a row in the ResultSet if a value in a
specified column partially matches the value of the WHERE clause expression.
 The WHERE clause expression must include a character that must be an exact match and
a wildcard character that is used to match any other character.
 Here are the wildcards that are used with the LIKE operator:
1. Underscore( _): A single character wildcard character.
String query=”select * from emp where ename Like ‘A%’;
ResultSet rs=st.executeQuery(query);
2. Percent(%): A multi-character wildcard character used to match any number of
character.
String query=”select * from emp where ename Like ‘_A_ _’;
ResultSet rs=st.executeQuery(query);
It will access the records whose name contains four character and second character is ‘A’.
IS NULL Operator:
 The IS NULL Operator is used to determine if a specified column does not contain any
value. We will find it useful to use the IS NULL operator whenever you need to identify
rows that are missing information in a column.
String query=”select * from emp where COMM IS NULL”;
ResultSet rs=st.executeQuery(query);
 It is important to understand that the number zero or a space is not NULL values.
String query=”select * from emp where COMM IS NOT NULL”;

DISTINCT Modifier:
 The SELECT statement return all rows in a table unless a WHERE clause is used to
exclude specific rows.
 However the ResultSet includes duplicate rows unless a primary key index is created for
the table or only unique rows are required in the table.
 When we want to exclude all but one copy of arrow from the ResultSet. We can do this
by using the DISTINCT modifier in the SELECT statement. The DISTINCT modifier
tells the DBMS not to include duplicate rows in the ResultSet.
String query=”select DISTINCT(JOB) from emp”;
ResultSet rs=st.executeQuery(query);

IN Modifier:
 The IN modifier is used to define a set values used by the DBMS to match values in a
specified column. The set can include any number of values and appear in any order.
String query=”select * from emp where EMPNO IN(101,103,105)”;
ResultSet rs=st.executeQuery(query);

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:77


NOT IN Modifier:
 The NOT IN modifier is similar to the IN modifier, except the NOT IN modifier reverse
the logic of the previous selection expression.
 The NOT IN modifier identifies a set of values that shouldn’t match rows returned the
program.
String query=”select * from emp where EMPNO NOT IN(101,103,105)”;

Updating Tables:
 Modifying data in a database is one of the most common functionalities included in every
J2EE component that provides database interactions. Generally any information that is
retrievable is also changeable depending on access rights and data integrity issues.
 The next several sections illustrate techniques that are used to update rows in a table of a
database. Code segments described the executeUpdate( ) method to process queries.
 The executeUpdate( ) method does not returns a ResultSet.
Updating a Row and Column:
 The UPDATE statement is used to change the value of one or more columns in one or
multiple rows of a table. The UPDATE statement must contain the name of the table that
is to be updated and a SET clause.
 The SET clause identifies the name of the column and the new values that will be placed
into the column, overriding the current value.
 The UPDATE statement may have a WHERE clause if a specific number of rows are to
be updated. If the WHERE clause is omitted all rows are updated based on the value of he
SET clause.
String query="Update student set class=’mca’ where sno=111";
Statement st=con.createStatement( );
st.executeUpdate(query);
Updating Multiple Rows:
 Multiple rows of a table can be updated by formatting the WHERE clause expressions to
include criteria that qualify multiple rows for the update.
 The IN test: the WHERE clause expression contains multiple values in the IN clause that
must match the value in the specified column for the update to occur in the row.
try {
String qry="Update student set class=’mca’ where sno in(111,222,333) ";
Statement st=con.createStatement( );
st.executeUpdate(query);
}
 The IS NULL: Rows that don’t have a value in the specified column are updated when
the IS NULL operator is used in the WHERE clause expression.
try {
String qry="Update student set class=’mca’ where college IS NULL";
Statement st=con.createStatement( );
st.executeUpdate(query);
}
 The Comparison test: Updating based on values in a column: The WHERE clause
expression contains a comparison operator that compares the value in the specified
column with a value in the WHERE clause expression.
try {
String qry="Update student set class=’mca’ where sno in(111,222,333)";

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:78


Statement st=con.createStatement( );
st.executeUpdate(query);
}

 All Rows: Updating Every row: A query can direct the DBMS to update the specified
column in all rows of a table by excluding the WHERE clause in the query.
try {
String qry="Update student set class=’mca’";
Statement st=con.createStatement( );
st.executeUpdate(query);
}
Updating Based on Values in a Column:
 An expression in the WHERE clause can be used to identify rows that are to be updated
by the UPDATE statement. The SELECT statement also applies to UPDATE statement.
Updating Every Row:
All rows in a table can be updated by excluding the WHERE clause in the UPDATE
statement.
Updating Multiple Columns:
Multiple columns are of rows can be updated simultaneously by specifying the column
names and appropriate values in the SET clause of the query.
Updating Using Calculations:
 The value that replaces the current value in a column does not to be explicit defined in the
SET clause if the value can be derived from a value in another column of the same row.
try {
String qry="Update employee set comm=basic*0.12;
Statement st=con.createStatement( );
st.executeUpdate(query);
}

Deleting Data from a Table:


 Deleting rows is necessary to purge erroneous information from the database and to
remove information that is no longer needed.
 However we must build safeguards in to assure that critical information isn’t
inadvertently deleted from the database. Before we delete a row from a table we must
certain that other tables are not negatively affected.
Deleting a Row from a Table:
 A row is deleted from a table by using the DELETE FROM statement. The DELETE
FROM statement includes the name of the table and a WHERE clause that contains an
expression that identifies the row or rows to remove from the table.
try{
String query= “DELETE TABLE CUSTOMER”;
Statement st=con.createStatement( );
st.execute(query);
}

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:79


Joining Tables:
 Tables are joined in a query using a two-step process. First both tables that are being
joined must be identified in the FROM clause where tables are listed one after other and
ate separated by a comma.
 Next an expression is created in the WHERE clause that identifies the column that are
used to create the join.
 Joining too many tables can cause performance degradation and bring response time to
crawl. Typically five is the maximum number of table that are joined.
Multiple Comparison Join:
 The WHERE clause expression used to join together tables can be a compound
expression. A compounded expression consists of two or more sub expressions each of
which is evaluated separately.
 A compound expression is used to specify more than one section criteria used to join two
tables.
Multi-table Join:
 More than two tables can be joined together by using the name of each table ion the join
in the FORM clause and by defining the join with the appropriate column names in the
WHERE clause expression.
 Notice that all three tables don’t need to have a common value used to join them. Each
pair of tables has a value common to both of them.
Creating a Column Name Qualifier:
 There is likely to be a conflict when two or more tables use the same name column name
to store the data element.
 Conflicts with column names can be resolved in a join by using a column name qualifier.
A column name qualifier identifies the table that contains the column name.
Creating a Table Alias:
 A query can be made readable by using able aliases. A table alias is an abbreviation for
the name of the table, and is used in place of the table name in the join and in the
SELECT statement.
 Each table name in the FROM clause is a letter, used as the table alias for the table. The
table alias is used in place of the table name in the column name qualifier.
Inner and Outer Joins:
 There are two kinds of joins, each of which either excludes or includes rows in both
tables of the join that don’t match. These are
Inner join:
 An inner join excludes rows of either table that don’t have a matching value.
String qry="SELECT empno,ename,job,dname FROM emp NATURAL JOIN DEPT";
ResultSet rs= st.executeQuery(qry);
while(rs.next())
{
System.out.println(rs.getInt(“empno”)+” “
+rs.getString(“ename”)+” “
+rs.getString(“job”)+” “
+rs.getString(“dname”));
}

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:80


Outer join:
 An outer join includes rows of either table that don’t have a matching value.
 There are 3 types of outer join operators.
1. Left Outer Join: This join includes the matched rows and unmatched rows from left table.
String qry="SELECT empno,ename,job,dname FROM Emp LEFT JOIN Dept ON
Emp.deptno=Dept.deptno";
2. Right Outer Join: This join includes the matched rows and unmatched rows from right table.
String qry="SELECT empno,ename,job,dname FROM Emp RIGHT JOIN Dept ON
Emp.deptno=Dept.deptno";
3. Full Outer Join: This join includes the matched and unmatched from both tables.
String qry="SELECT empno,ename,job,dname FROM Emp FULL JOIN Dept ON
Emp.deptno=Dept.deptno";
Calculating Data:
 The DBMS can calculate values in a table and return the result of the calculation in the
ResultSet by using one of the five built in calculation functions.
 Sum( ): It returns the sum of values in a column that is passed to the built-in functions.
Ex: ResultSet rs= st.executeQuery("SELECT sum(sal) FROM emp");
while(rs.next( ))
{ String res = rs.getString(1);
System.out.println(res);
}
 AVG( ): Averages values in a column that is passed to the built in functions.
Ex: ResultSet rs= st.executeQuery("SELECT AVG(sal) FROM emp");
while(rs.next( ))
{ String res = rs.getString(1);
System.out.println(res);
}
 MIN( ): Determines the minimum value in a column that is passed to the built in
functions.
Ex: ResultSet rs= st.executeQuery("SELECT MIN(sal) FROM emp");
while(rs.next( ))
{ String res = rs.getString(1);
System.out.println(res);
}
 MAX( ): Determines the maximum value in a column that is passed to the built in
functions.
Ex: ResultSet rs= st.executeQuery("SELECT MAX(sal) FROM emp");
while(rs.next( ))
{
String res = rs.getString(1);
System.out.println(res);
}

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:81


 COUNT( ):
Count(column) : Determines the number of rows in a column that is passed to the
built in function without counting null values.
Count(*) : Determines the number of rows in column including null values.
Ex:
ResultSet rs= st.executeQuery("SELECT COUNT(*), COUNT(COMM) FROM emp");
while (rs.next())
{
String res1 = rs.getString(1);
String res2=rs.getString(2);
System.out.println(“The number of rows=”+res1);
System.out.println(“The number of COMM values=”+res2);
}

Calculating a Subset of Rows:


 We can restrict the scope of a built in calculation function by using a WHERE clause
expression to specify the criteria for a row to be included in a calculation.
 Any valid WHERE clause expression can be used to filter rows to be excluded from the
calculation. Likewise the WHERE clause expression is used to include rows in the
calculation.
NULLs and Duplicates:
 Two common problems that occur when using built in functions are columns that don’t
contain a value and rows that contain duplicate values in the same column. Many times
we don’t want empty columns and duplicate rows included in the calculation.
 We can use the DISTNICT modifier to exclude duplicate rows from the calculation.
Likewise problems posed by NULL columns can be avoided by using the IS NULL
operator along with the NOT operator in a selection expression.

Grouping and Ordering Data:


GROUP BY clause:
 Columns are returned in the ResultSet in the order the column names appear in the
SELECT statement of the query. The order in which rows appear in the ResultSet can be
grouped into similar values or sorted in ascending or descending order by using the
GROUP BY clause.
Ex:
String q="SELECT deptno,sum(sal) FROM emp GROUP BY deptno";
ResultSet rs= st.executeQuery(q);
while(rs.next())
{
System.out.println(rs.getInt(1)+ " "+rs.getInt(2));
}

Grouping Multiple Columns:


 The DBMS can create a subgroup within a group in the ResutSet. A subgroup is a
grouping within another grouping.
 A subgroup is created by placing the name of the column used for the subgroup as the
second column name in the GROUP BY clauses of the query. Column names in the
GROUP BY clause must be separated with a comma.
String q="SELECT deptno,max(sal),min(sal) FROM emp GROUP BY deptno";
S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:82
ResultSet rs= st.executeQuery(q);
while(rs.next())
{
System.out.println(rs.getInt(1)+ " "+rs.getInt(2)+ " "+rs.getInt(3) );
}
Conditional Grouping:
 The DBMS uses the conditional expression to qualify whether or not the current row
should be included in any group of the ResultSet. Only rows that meet the condition are
returned. A row that doesn’t meet the condition is excluded. The conditional expression is
placed in the HAVING clause of the query. The HAVING clause sets the criteria for a
row to be included in a group.
Ex:
String q="SELECT deptno,sum(sal)from emp GROUP BY deptno having sum(sal)>6000";
ResultSet rs= st.executeQuery(q);
while (rs.next())
{
System.out.println(rs.getInt(1)+ " "+rs.getInt(2));
}

Sorting Data: ORDER BY Clause


 The ResultSet can be placed in alphabetical or numerical order by using the ORDER BY
clause in the query.

String q="SELECT empno,ename,job,sal from emp ORDER BY sal";


ResultSet rs= st.executeQuery(q);
while (rs.next())
{
System.out.println(rs.getInt(1)+" "+rs.getInt(2)+" "
+rs.getInt(3)+" "+rs.getInt(4));
}

Major and Minor Sort keys:


 We can create a sort within a sort by specifying more than one column to be sorted.
 The first column specified in the Order by clause is called the major sort key and is called
the initial value used to sort rows. The second and subsequent columns in the Order by
clause are called minor sort keys. A comma must separate each column name.

Ex: select empno,ename,job,sal from emp order by job,sal;


ResultSet rs= st.executeQuery(q);
while (rs.next())
{
System.out.println(rs.getInt(1)+" "+rs.getInt(2)+" "
+rs.getInt(3)+" "+rs.getInt(4));
}

Descending Sort:
 In addition to choosing the column to sort we can also select the direction sort by using
ASC or DSC modifier.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:83


 The DESC modifier I the ORDER BY clause to specify a descending sorts. By default the
sort is ascending, although we can use the ASC modifier to explicitly direct that the sort
be in ascending order.
 The ASC or DESC modifier must appear after the column name in the OEDER BY
clause.
String q="SELECT empno,ename,job,sal from emp ORDER BY sal DESC";
ResultSet rs= st.executeQuery(q);
while (rs.next())
{
System.out.println(rs.getInt(1)+" "+rs.getInt(2)+" "
+rs.getInt(3)+" "+rs.getInt(4));
}

SubQueries:
 We can direct the DBMS the result of a query by creating a subquery within the query.
A subquery joins together two queries to form one complex query, which efficiently
identifies data to be included in the ResultSet.
 A subquery is a query that is formatted very similar to a query. Each has a SELECT
statement and a FROM clause, and can also included a WHERE clause and a HAVING
clause to qualify rows to return.
 The WHERE clause and the HAVING clause are used to express a condition that must
be met for a row to be included in the ResultSet.
 There are two rules that we must follow when using a subquery in our program.
1. Return one column from the query: The purpose of a subquery is to derive a
list of information from which a query can choose appropriate rows. Only a
single column needs to be included in the list.
2. Don’t sort or group the result from a subquery: since the ResultSet of the
subquery isn’t going to be returned in the ResultSet of the query, there isn’t a
need to sort or group data in the ResultSet of a Subquery.

Creating a Subquery:
 A subquery is a query whose results are evaluated by an expression in the WHERE clause
of another query.
String qry="SELECT empno,ename,job,sal from emp where sal>=(select max(sal) from Emp)";
ResultSet rs= st.executeQuery(qry);
while (rs.next())
{
System.out.println(rs.getInt(1)+" "
+rs.getString(2)+" "
+rs.getString(3)+" "
+rs.getInt(4));
}

Conditional Testing:
 There are four types of conditional tests we can use with a subquery. these are
1. Comparison test: This is a test that uses comparison operators to compare values in
the temporary table with values in the table used by the query.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:84


2. Existence test: This test determines if a value in the current row of the query also
exists in the temporary table.
3. Set membership test: This test is similar to the existence test in that the DBMS is
directed to determine if a value in the current row of the table used by the query
also exists in the temporary table.
4. Qualified test: This test consists of either the any test or the all test and determines
if a value in the current row of the table used by the query is in one row of the
temporary table or all rows of the temporary table.
ANY Operator Test: We use the ANY operator in a WHERE clause to compare a value with
any of the values in a list. ANY evaluates to true if the result of an inner query contains at least
one row that satisfies the condition.
We must place an =, <>, <, >, <=, or >= operator before ANY.
Ex:
String query= “SELECT * FROM emp WHERE salary > ANY (2000, 3000, 4000)”;
ALL Operator Test: We use the ALL operator in a WHERE clause to compare a value with all
of the values in a list. ALL evaluates to true when each value of inner query satisfies the
condition. We must place an =, <>, <, >, <=, or >= operator before ALL.
Ex:
String query= “ELECT * FROM emp WHERE salary > ALL (2000, 3000, 4000)”;
VIEW:
 We can reduce the complexity of our J2EE component by crating one or more VIEW of
the database for each user ID that is passed to the J2EE component for data access.
 A VIEW is similar to creating a table that contains only the data the user ID is permitted
to access. A VIEW is like a filter that hides information from a user ID.
 Each VIEW is uniquely identified with a name and contains selection criteria for columns
and rows that appear in the VIEW are used by a J2EE component.
 Once a VIEW is created the J2EE component references a VIEW the same way that a
table is referenced in a query.
Rules of Using VIEW’s:
 Create as many VIEW as necessary to simplify access to a database.
 Restrict access to a table on a need to know basis.
 Work with the owner of he data to establish reasonable restrictions.
 Classify users into groups that have similar access requirements to information.
 Create a view for each classification of user, rather than for each user.
 More than one column can be used in a VIEW.
 More than one table can be used in a VIEW.
 A VIEW is treated as a table in a query regardless of the number of columns and tables
that are used to create the VIEW.
 Use a VIEW whenever our program accesses some column in many tables.
 The VIEW simplifies the number of tables that a J2EE component needs to directly
access.
 Beware of data security restrictions that affect the underlying columns and table s used to
create the VIEW. A VIEW inherits data security restrictions from tables used to create the
VIEW.
 Therefore, if the user ID doesn’t have rights to information in a table the VIEW also has
the same restrictions.

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:85


Creating a VIEW:
 A VIEW is created by using the CREATE VIEW statement. CREATE VIEW statement
contains the name of the VIEW.
try {
String query= "CREATE VIEW empview as select * from Emp”;
Statement st=con.createStatement( );
st.execute(query);
st.close( );
}
Selecting Columns to appear in the VIEW:
 We can include or exclude any column in a VIEW. Columns excluded from a VIEW
remain in underlying tables used to create the VIEW. However, those columns are hidden
from the J2EE component.
try{
String query= "CREATE VIEW empview as select empno,ename,job,sal from Emp”;
Statement st=con.createStatement( );
st.execute(query);
}
Ex: Horizontal view uses where condition and Vertical view uses column list.
try{
String qry= "CREATE VIEW empview as select empno,ename,job,sal “ +
“ from Emp where job= ‘CLERK’ ”;
Statement st=con.createStatement( );
st.execute(query);
}

Grouping and Sorting VIEW:


 Rows in a VIEW can be grouped and sorted when the VIEW is created, rather than
grouping or sorting rows in a query that uses the VIEW.
 A VIEW can be grouped or sorted by using the GROUP BY clause and/or the ORDER
BY clause.
Example:
try{
String qry= "CREATE VIEW empview as select * from Emp where ORDER BY job”;
Statement st=con.createStatement( );
st.execute(query);
}

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:86


Modifying a VIEW:
 A VIEW can be modified and those modifications affect the underlying tables that are
used to create the VIEW.
 There are three ways in which a VIEW can be modified. These are
1. Update: Values in one or more columns of a VIEW are changed to values supplied
by the query.
2. Insert: A new is added to the VIEW and indirectly to the underlying tables used to
create the VIEW.
3. Delete: A row is removed from the VIEW and from the underlying tables used to
create the VIEW.
Dropping a VIEW:
 A VIEW can be removed by using the DROP VIEW statement. The DROP VIEW
statement requires the name of the VIEW that is to be dropped.
 There are two modifiers that are used with the DROP VIEW statement. These are
1. CASCADE: Removes all VIEWs that depend on the VIEW specified in the DROP
VIEW statement and removes the specified VIEW.
2. RESTRCIT: Removes only the VIEW specified in the DROP VIEW statement. All
dependent VIEW remains intact.
try
{
String qry= "DROP VIEW empview CASCADE”;
Statement st=con.createStatement( );
st.execute(query);
}

S.V ARTS & SCIENCE COLLEGE: GUDUR MCA:DBMS PAGE:87

You might also like