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

Database Management Systems-By-py Kumar

The document outlines the syllabus for a B.Sc. in Computer Science/Information Technology focusing on Database Management Systems (DBMS) under the CBCS framework. It covers various units including the overview of DBMS, Entity-Relationship models, relational models, SQL, and PL/SQL, detailing their structures, functionalities, and applications. Additionally, it discusses the evolution of DBMS, its objectives, classifications, and the problems associated with file-based data management systems.

Uploaded by

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

Database Management Systems-By-py Kumar

The document outlines the syllabus for a B.Sc. in Computer Science/Information Technology focusing on Database Management Systems (DBMS) under the CBCS framework. It covers various units including the overview of DBMS, Entity-Relationship models, relational models, SQL, and PL/SQL, detailing their structures, functionalities, and applications. Additionally, it discusses the evolution of DBMS, its objectives, classifications, and the problems associated with file-based data management systems.

Uploaded by

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

Andhra Pradesh State Council of Higher Education

B.Sc. Computer Science/Information Technology (IT) Syllabus Under CBCS


w.e.f.2015-2016 (Modified in April 2016)
Structure of Computer Science/Information Technology (IT) Syllabus
III YEAR V SEMESTER
Paper-V: Data Base Management System

UNIT I

Overview of Database Management System: Introduction, file-based system,


Drawbacks of file-Based System ,Data and information, Database, Database
management System, Objectives of DBMS, Evaluation of Database management System,
Classification of Database Management System, DBMS Approach, advantages of DBMS,
Anis/spark Data Model, data models, Components and Interfaces of Database
Management System. Database Architecture, Situations where DBMS is not Necessary,
DBMS Vendors and Their Products.

UNIT II

Entity-Relationship Model: Introduction, the building blocks of an entity relationship


diagram, classification of entity sets, attribute classification, relationship degree,
relationship classification, reducing ER diagram to tables, enhanced entity-relationship
model (EER model), generalization and specialization, IS A relationship and attribute
inheritance, multiple inheritance, constraints on specialization and generalization,
aggregation and composition, entity clusters, connection types, advantages of ER
modeling.

UNIT III

Relational Model: Introduction, CODD Rules, relational data model, concept of key,
relational integrity, relational algebra, relational algebra operations, advantages of
relational algebra, limitations of relational algebra, relational calculus, tuple relational
calculus, domain relational Calculus (DRC). QBE
UNIT IV

Structured Query Language: Introduction, History of SQL Standard, Commands in


SQL, Data Types in SQL, Data Definition Language, Selection Operation, Projection
Operation, Aggregate functions, Data Manipulation Language, Table Modification
Commands, Table Truncation, Imposition of Constraints, Join Operation, Set Operation,
View, Sub Query, Embedded SQL,

UNIT V

PL/SQL: Introduction, Shortcoming in SQL, Structure of PL/SQL, PL/SQL Language


Elements, Data Types, Operators Precedence, Control Structure, Steps to Create a
PL/SQL, Program, Iterative Control, Cursors, Steps to create a Cursors, Procedure,
Function, Packages, Exceptions Handling, Database Triggers, Types of Triggers.

Page 1
Database Management Systems

Design, Implementation and Management

P. Y. Kumar M.C.A, M.Tech, M.Phil..,

Head of the Department

Computer Science

This Book is dedicated to my Daughter. May God Bless you and be with you
little one!
UNIT – I
DATABASE SYSTEMS
Data:
Data are raw facts. The word raw indicates that the facts have not yet been
processed to reveal their meaning.
Or
The term data referred to known raw facts. Text, graphics, images and video
segments that have meaning in the user’s environment

Information:
Information is the result of processing raw data to reveal ors meaning. Data
processing can be as simple as organizing data to reveal patterns or as complex as
making forecasts or drawing inferences using statistical modeling.
Or
Data that have been processed in such a way as to increase the knowledge of the
person who use the data
Information is produced by processing data.
Information is used to reveal the meaning of data.
Accurate, relevant, and timely information os the key

Database: The database consists of logically related data stored in a single data
repository.

Metadata: The metadata provide a description of data characteristics and the set of
relationships that link the data found with in the database.

Ex:

Name Type Length Min Max Description

Course Alphanumeric 30characters - - Course ID and


name

Database management system (DBMS): DBMS is collection of programs that


manages the database structure and control access to the data stored in the database.

Field: A character or group of characters that has a specific meaning. A field is used to
define and store data.

Record: A logically connected a set of one or more fields that describes a person, place
or thing.

File: A collection of related records.

Page 3
 What is a Database Management System
1. A DBMS is a collection programs
2. That manages database structure
3. And control access to the data stored in the database.

1. A DBMS is a collection of programs


2. That enables to user to create, maintain the database.

A DBMS consists of:


1. A collection of interrelated and persistent data. This part of DBMS is referred to as
database (DB).
2. A set of application programs used to access, update, and manage data. It is
called data management system (DMS).
3. A DBMS is general-purpose software i.e., not application specific. The same DBMS
(e.g., Oracle, Sybase, MS Acess, INFORMATICA ,MYSQL etc.)
4. It can be used in railway reservation system, library management, university, etc.
5. A DBMS takes care of storing and accessing data, leaving only application
specific tasks to application programs.

Explain Structure of DBMS


1. The DBMS uses an application specific database description to define this
translation.
2. The database description is generated by a database designer from his or her
conceptual view of the database, which is called the Conceptual Schema.
3. The translation from the conceptual schema to the database description is
performed using a data definition language (DDL) or a graphical or textual design
interface.

 What are the Objectives of DBMS


The main objectives of database management system are
1. Data availability,
2. Data integrity,
3. Data security, and
4. Data independence

1. Data Availability

Page 4
Data availability refers to the fact That the data are creates available to the
wide variety of users can access the date easily in a meaningful format

2. Data Integrity
Data integrity refers to the correctness of the data in the database.
In other words, the data available in the database is a reliable data

1. Data Security
1. Data security refers to the fact that only authorized users can access the data.
Data security can be enforced by passwords.
If two separate users are accessing a particular data at the same time,
2. The DBMS must not allow them to make conflicting changes.

2. Data Independence
1. DBMS allows the user to store, update, and retrieve data in efficient manner.
DBMS provides an “abstract view” of how the data is stored in the database.
In order to store the information efficiently, complex data structures are used
to represent the data.
The system hides certain details of how the data are stored and maintained.
 Explain Evolution of Database Management Systems
In recent years, two approaches to DBMS are more popular, which are
a. Object-Oriented DBMS (OODBMS) and
b. Object Relational DBMS (ORDBMS).

The sequential order of the development of DBMS is as follows:


1. Flat files – 1960s–1980s
2. Hierarchical – 1970s–1990s
3. Network – 1970s–1990s
4. Relational – 1980s–present
5. Object-oriented – 1990s–present
6. Object-relational – 1990s–present
7. Data warehousing – 1980s–present
8. Web-enabled – 1990s–present

Early 1960s.
1. Charles Bachman at GE created the first general purpose DBMS Integrated
Data Store.
2. It created the basis for the network model which was standardized by CODASYL
(Conference on Data System Language).

Late 1960s:
1. IBM developed the Information Management System (IMS).
2. IMS used an alternate model, called the Hierarchical Data Model.

In 1970:
1. Edgar Codd, from IBM created the Relational Data Model.

In 1981:
1. Codd received the Turing Award for his contributions to database theory.
2. Codd Passed away in April 2003.

In 1976:
1. Peter Chen presented Entity-Relationship model, which is widely used in database
design.

In 1980:

Page 5
1. SQL developed by IBM, became the standard query language for databases. SQL
was standardized by ISO.

In 1980s and 1990s:


1. IBM, Oracle, Informix and others developed powerful DBMS.

 Classification of Database Management System OR TYPES OF


DBMS? The database management system can be broadly classified into
1. Passive Database Management System
2. Active Database Management System:

1. Passive Database Management System.


1. Passive Database Management Systems are program-driven.
2. The users query the current state of database
3. And retrieve the information currently available in the database.
4. Traditional DBMS are passive in the sense that they are explicitly and
synchronously invoked by user or application program initiated operations.
5. Applications send requests for operations to be performed by the DBMS and
wait for the DBMS to confirm and return any possible answers.
6. The operations can be definitions and updates of the schema, as well as
queries and updates of the data.

2. Active Database Management System.


1. Active Database Management Systems are data-driven or event-driven
systems.
2. In active database management system, the users specify to the DBMS the
information they need.
3. If the information of interest is currently available, the DBMS actively monitors
the arrival of the desired information and provides it to the relevant users.
4. The scope of a query in a passive DBMS is limited to the past and present data,
whereas the scope of a query in an active DBMS additionally includes future
data. An active DBMS reverses the control flow between applications and the
DBMS instead of only applications calling the DBMS, the DBMS may also call
applications in an active DBMS.
5. DBMS can support many different types of databases according to the no. of
users and the database locations.

1. Single User Database


Single user database supports only one user at a time. If user A is using the
database, users B and C must wait until user A is done. Single user database
runs on PC is called Desktop database.

2. Multi user Database


It supports multiple users at the same time. It is also called workgroup
database. It works few than 50 persons.
When the database is used by the entire organization and supports many users
(more than 50) across many departments, the database is known as Enterprise
Database.

Locations might also classify Centralized Database, Distributed Database.

1. Centralized Database
The database supports data located at a single site is called Centralized
Database.

Page 6
2. Distributed Database
Database that supports data distributed across several different sites is called
Distributed Database.

Now a days database classify as Operational Database, Data warehouse.

1. Operational Database

A database that is designed primarily to support company’s day to day operations


is classified as an Operational Database or Transactional or Production Database.

2. Data Warehouse
Data ware house focus on storing data used to generate information required
to make tactical or strategic decisions.

 PROBLEMS WITH FILE SYSTEM DATA MANAGEMENT

File: A file is a collection of records.

File system: A file system is a store organization computer files.

1. The file system method of organizing and managing data


2. It was a definite improvement over a manual system, and
3. The file system served a useful purpose in data management.

Problems:
1. The simplest data retrieval task requires extensive programming.
2. Any changes in an existing structure can be difficult in a file system environment.
3. In file system database is security features are difficult.
 It requires extensive programming.
 It can not perform ad-hoc (selected) queries.
 System administration can be complex and difficult.
 Security features are likely to be inadequate.

Those limitations in turn, lead to problems of structural and data dependency.

1. STRUCTURAL AND DATA DEPENDENCE:

1.A file system show structural dependence,


2.It means that access to a file is dependent on its structure.
3. For example, adding a customer date-of-birth field in CUSTOMER file,
4. The file system programs must be modified to conform to the new file
structure,
5. They exhibit structural dependence.
6. Even changes in the characteristics of data
7. Such as changing a field from integer to decimal,
8. It requires all changes in the programs that access the file.
9. That is changing the data type etc.
10.The file system is said to exhibit data dependence.

1. FIELD DEFINITIONS AND NAMING CONVERTIONS:

Page 7
1. Selecting proper field names is also important.
2. The field definitions and naming conventions are not unique to file systems.
3. The field names are reasonably descriptive.
4. With proper naming conventions, the file structure becomes self-documenting.

5. That is, by simply looking at field is likely to contain.


6. Some software packages place restrictions in the length of field names.
7. So it is wise to be as expressive as possible within those restrictions.
8. In addition, very long field names create it difficult to more than a few fields on
a page
9. They creating output values a problem.

2. DATA REDUNDANCY

1. The file system’s structure creates it difficult to combine data from multiple
sources
2. And its lack of security renders the file system.
3. The organizational structure promotes the storage of the same basic data in
different locations.
4. Because that data stored in difference location will always be updated
consistently.
5. Data redundancy exits when the same data stored unnecessarily at different
places.
a. Data inconsistency:
b. Data anomalies:
1. Update anomalies
2. Insertion anomalies
3. Deletion anomalies

 Data inconsistency:
1. Data inconsistency exists when different versions of the same data
appear in different places.
2. For example: we change an agent phone number or address in the
AGENT file.
3. If we forget to create corresponding changes in the CUSTOMER file
4. The files contain different data for the same agent.

 Data anomalies:
1. Data anomalies (difference) exist because any change in any field value
must be correctly made in many places to maintain data integrity.
2. The data anomalies are commonly defined as:

a. Update anomalies:
1. Changing any field data, we must change wherever be appear
that field.
2. In this case a large file system, such changes might occur in
hundreds or even thousands of records clearly,
3. The possible for data in consistencies is great.

b. Insertion anomalies:
1. To add each new customer in CUSTOMER file,
2. We must also add the corresponding agent data.
3. If we add several hundred new customers,
4. We must also enter several hundred agent names and telephone
numbers.

c. Deletion anomalies:

Page 8
1. We delete any field information we also loss another filed
information.
2. If delete the customers B., you will also delete His agent data.
3. Clearly this is not desirable (popularly).

 ROLE AND ADVANTAGES OF THE DBMS

1. The DBMS serves as the intermediary between the user and the database.
2. The DBMS receives all application requests
3. And translates them into the complex (multipart) operations required to fulfill those
requests.
4. Application program might be written by a programmer using a programming
languages
5. A DBMS interface between the end user’s applications and the database
6. First, the DBMS enables (allow) the data in the database to be shared among
multiple applications or users.
7. Second, the DBMS integrates (adds) the many different users’ views of the data into
a single all-encompassing data repository.
DBMS provides many advantages such as:

1. Improved data sharing


2. Improved data security
3. Better data integration
4. Minimized data inconsistency
5. Improved data access
6. Improved decision making
7. Increased end-users productivity
8. Mutli user data access
9. Backup and recovery management
10. Data integrity management

1. Improved data sharing:


1. The DBMS helps create an environment in which end users have better access
to more data and better-managed data.
2. Such access makes it possible for end users to respond quickly to changes in
their environment.

2. Improved data security:


1. The more users access the data, the greater the risks of data security breaks.

2. A DBMS provides a framework for better enforcement of data privacy and security
policies.

3. Better data integration:


1. It provides view of the organizations operations and a clearer view of the big
picture.
2. It becomes much easier to see how actions in one segment of the company
affect other segment (part).

4. Minimized data inconsistency:


1. Data inconsistency exists when different versions of the same data appear in different
places.
2. The probability of data inconsistency is greatly reduced in properly designed
database.

Page 9
5. Improve data access:
1. The DBMS makes it possible to produce quick answers to ad-hoc queries from a
database
2. A query is a specific request issued to the DBMS for data manipulation.
3. The DBMS sends back an answer to the application.

6. Improved decision making:


1. Better managed data and improved data access create
2. It possible to generate better quality information
3. That information is used to take better decisions.

7. Increased end-user productivity:


1. The availability of data, combined with the tools
2. That transforms data into usable information.
3. The end users to take quick decisions
4. That is used to success and failure in the global.

8. Multi User Access Control


1. DBMS provide many users can access data concurrently with out compromising
the integrity of the database
2. It can control to access multi user
3. It uses to multiple users can access the database.

9. Backup and Recovery Management


1. DBMS provides backup and data recovery
2. It generates data safety and integrity.
3. The special utility that allow the DBA to perform routine and special backup
4. And restore procedures when power failure or database failure.

10. Data Integrity Management


1. DBMS supports and implements integrity rules
2. Like not null, check, unique, default, primary key, and foreign key.

WHY DATABASE DESIGN IS IMPORTANT

Database design refers to the activities that focus on the design of the database
structure that will be used to store and manage end-user data. A database that meets all
user requirements does not just happen; its structure must be designed carefully. In fact,
database design is such a crucial aspect of working with database.
Proper database design requires the designer to identify precisely the database’s
expected use. Designing a transactional database emphasizes accurate and consistent
data and operational speed. Designing a database to be used in a centralized, single-
user environment requires a different approach from that used in the design of a
distributed, multiuser database.
A well-designed database facilitated data management and generates accurate
and valuable information.
A database contains redundant data when the same data about the same entity
are kept in different locations. Therefore, the existence of redundant data can produce
uncorrected data entries and you probably won’t know which value is the correct one.

Page 10
Database System versus File System
DBMS File Processing System

Minimal data redundancy problem in


Data Redundancy problem exits
DBMS

Data Inconsistency does not exist Data Inconsistency exist here

Accessing database is easier Accessing is comparatively difficult

The problem of data isolation is not found Data is scattered in various files and files may be
in database of different format, so data isolation problem
exists

Transactions like insert, delete, view,


In file system, transactions are not possible
updating, etc are possible in database

Concurrent access and recovery is


Concurrent access and recovery is not possible
possible in database

Security of data Security of data is not good

A database manager (administrator)


A file manager is used to store all relationships in
stores the relationship in form of
directories in file systems.
structural tables

DATABASE SYSTEMS (OR)


THE DATABASE SYSTEM ENVIRONMENT (OR)
COMPONENTS AND INTERFACES OF DATABASE MANAGEMENT SYSTEM (OR)
COMPONENTS OF DATABASESYSTEM

Page 11
1. The database consists of logically related data stored in a single data
repository (Storage area).
2. It is possible to remove most of the file system’s problems.
3. Like data inconsistency, data anomalies, data dependency and structural
dependency.
4. DBMS software not only the data structures, but it also the relationship
between those structures and the access structures path, in a central location.

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. The database system is composed of the five major ports
1. Hardware
2. Software
3. People
4. Procedures
5. Data

1. Hardware:
Hardware refers to all the system’s physical devices. The database system’s main
and most easily identified hardware component is the computer. Hardware also
includes all of the computer peripherals. Peripherals include keyboards, nice,
modems and printers. Peripherals also included any electronic devices that are used
to connect two or more computers.

Page 12
2. Software:
Software refers to the collection of programs used by the computer within the
database system. Three types of software to make the database system function fully
operating system software, DBMS software and application programs and utilities

 OS software manages all hardware components and makes its possible for all
other software to run the computers.
 DBMS software manages the database within the database system. Microsoft’s
Access and SQL server, oracle.
 Application programs and utility software’s are used to access and
manipulated the data in the DBMS. Application programs are most commonly
used to access the data found within the database to generate reports,
tabulations and other information to facilitate decision making. Utilities are the
software tools used to help manage the database system’s computer
components.

3. People:

.
Database Administrator

Database Administrator is a person having central control over data and programs
accessing that data. The database administrator is a manager whose responsibilities
are focused on management of technical aspects of the database system.

Database Designer

Database designer can be either logical database designer or physical database


designer. Logical database designer is concerned with identifying the data, the
relationships between the data, and the constraints on the data that is to be stored in
the database. The logical database designer must have thorough understanding of the
organizations data and its business rule.

Database Manager

Database manager is a program module which provides the interface between the low
level data stored in the database and the application programs and queries submitted
to the system:

– The database manager would translate DML statement into low level

file system commands for storing, retrieving, and updating data in the

database.

P.y.kumar M.C.A,M.Tech,M.Phil.., Page 13


– Integrity enforcement. Database manager enforces integrity by checking

consistency constraints like the bank balance of customer must be

maintained to a minimum of Rs. 300, etc.

– Security enforcement. Unauthorized users are prohibited to view the information

stored in the data base.

– Backup and recovery. Backup and recovery of database is necessary to ensure

that the database must remain consistent despite the fact of failures.

Database Users

Database users are the people who need information from the database to carry out
their business responsibility. The database users can be broadly classified into two
categories like application programmers and end users.

Sophisticated End Users

Sophisticated end users interact with the system without writing programs. They form
requests by writing queries in a database query language. These are submitted to query
processor. Analysts who submit queries to explore data in the database fall in this
category.

Specialized End Users

Specialized end users write specialized database application that does not fit into data-
processing frame work. Application involves knowledge base and expert system,
environment modeling system, etc.

Naive End Users

Na¨ıve end user interact with the system by using permanent application program
Example: Query made by the student, namely number of books borrowed in library
database.

System Analysts

System analysts determine the requirements of end user, and develop specification
for canned transaction that meets this requirement.

Canned Transaction

Ready made programs through which na¨ıve end users interact with the database is
called canned transaction.

4. Procedures
Procedures are the instructions and rules that govern the design and use of
the database system. Procedures play an important role in a company, they
enforced the standards procedures also are used to ensure that there is an
organized way to monitor and audit both the data that enter the database and the
information that is generated through the use of such data.

Page 14
5. Data:
The word ‘Data’ covers the collection of facts stored in the database data are
the raw material from which information is generated.
Integrated Data

A data can be considered to be a unification of several distinct data files and when any
redundancy among those files is eliminated, the data are said to be integrated data.

Shared Data

A database contains data that can be shared by different users for different application
simultaneously. It is important to note that in this way of sharing of data, the
redundancy of data are reduced, since repetitions are avoided, the possibility of
inconsistencies is reduced.

Persistent Data

Persistent data are one, which cannot be removed from the database as a side effect of
some other process. Persistent data have a life span that is not limited to single
execution of the programs that use them.

Functional Components of Database System Structure ?

The functional components of database system structure are:

1. Storage manager.

2. Query processor.

Storage Manager

Storage manager is responsible for storing, retrieving, and updating data in the
database. Storage manager components are:

1. Authorization and integrity manager.

2. Transaction manager.

3. File manager.

4. Buffer manager.

Transaction Management

– A transaction is a collection of operations that performs a single logical

function in a database application.

– Transaction-management component ensures that the database remains in a


consistent state despite system failures and transaction failure.

– Concurrency control manager controls the interaction among the concurrent


transactions, to ensure the consistency of the database.

Authorization and Integrity Manager

Checks the integrity constraints and authority of users to access data.

Page 15
Transaction Manager

It ensures that the database remains in a consistent state despite system failures. The
transaction manager manages the execution of database manipulation requests. The
transaction manager function is to ensure that concurrent access to data does not
result in conflict.

File Manager

File manager manages the allocation of space on disk storage. Files are

used to store collections of similar data. A file management system manages

independent files, helping to enter and retrieve information records.

File manager establishes and maintains the list of structure and indexes

defined in the internal schema. The file manager can:

– Create a file

– Delete a file

– Update the record in the file

– Retrieve a record from a file

Buffer

The area into which a block from the file is read is termed a buffer

Buffer Manager

Buffer manager is responsible for fetching data from disk storage into main memory.
Programs call on the buffer manager when they need a block from disk. The requesting
program is given the address of the block in main memory, if it is already present in the
buffer. If the block is not in the buffer, the buffer manager allocates space in the buffer
for the block, replacing some other block

 DBMS FUNCTIONS ?

1. A DBMS guarantee the integrity and consistency of the data in the database.
They include
a. Data dictionary management,
b. Data storage management,
c. Data transformation and presentation,
d. Security managements,
e. Multi user access control,
f. Backup and recovery management,
g. Data access using a languages
h. Application programming interfaces
i. And database communication interfaces.

2. Data dictionary management:


1. The DBMS requires that definitions of the data elements and their relationship
(metadata) be stored in a data dictionary.

Page 16
2. Any change made in a database structures are automatically recorded in the data
dictionary,
3. There supports to modify all the programs that access the changed structure.

3. Data storage management:


1. The DBMS creates the complex (parts) structures required for data storage
2. A modern DBMS System provides storage not only for the data.
3. But also for related data entry forms or screen definitions, report definitions, data
validations rules, procedural code, structures to handle video and picture formats and
so on.

4. Data transformation and presentation:


1. The DBMS transforms entered data to the data structures
2. That are required to store the data maintaining data independence
3. The DBMS translates logical requests into commands
4. That physically located and retrieve the requested data.
5. The DBMS formats the physically retrieved data to create it conform to the
user’s logical expectations.

5. Security management:
1. The DBMS creates a security system
2. That use user security and data privacy within the database.
3. Security rules determine (decide). Which users can access the database which
data item each user may access and which data operations (read, add, delete
or modify) the user may perform.
4. This is especially important in multi user database. Systems where many users
can be access the database simultaneously.

6. Multiuser access control:


1. The DBMS creates the (parts) complex structures that allowed multi user access to
the data.
2. In order to provided data integrity and data consistency.
3. The DBMS uses sophisticated (difficult) algorithms to ensure
4. Multi users can access the database concurrently without compromising the
integrity of the database.

7. Backup and recovery management:


1. The DBMS provides backup and data recovery procedures to support data
safety
2. Recovery management deals with the recovery of the database after a failure
3. Such as a bad sector in the disk of a power failure.
4. Such capability is critical to the preservation of the database’s integrity.

8. Data integrity management:


1. The DBMS promotes and enforces integrity rules to eliminate data integrity
problems
2. Thus minimizing data redundancy and maximizing data consistency.
3. The data relations stored in the data dictionary are used to enforce data
integrity.

9. Database access languages and application programming interface:


1. The DBMS provides data access thorough a query language.
2. A query language is a non-procedural.
3. The DBMS’s query language contains two components.
a. DDL (Data definition language)
b. DML (Data manipulation language)
4. The DDL defines the structures in the data are housed

Page 17
5. And the DML allows end users to extract the data from the database.

10. Database communication interfaces:


1. Current generation DBMS provide special communications
2. It allows the database to accept end user requests within a computer network
environment.
3. In fact, database communications capabilities are an essential feature of the modern
DBMS.

 DATABASE SYSTEM DISADVANTAGES:


Database systems main disadvantages are
1. Increased costs
2. Management complexity
3. Maintaining currency
4. Vendor dependence
5. Frequent upgrade/replacement cycles

1. Increased costs:
a. Database System required sophisticated hardware and software and highly skilled
personnel.
b. The cost of maintain the hardware, software and personnel required to operate
and manage a database system can be large.

2. Management Complexity:
1. Database Systems interface with many different technologies
2. And have a significant impact on a company’s resources and culture.
3. Database systems hold crucial company data
4. That are accessed from multiple sources, security issues must be assessed
constantly.

3. Maintaining currency:
1. To maximize the efficiency of the database system, you must key your system
current.
2. User must perform frequent updates and apply
3. The latest patches and security measures to all components.
4. Because database technologies advances rapidly, personnel training cost tend to
be significant.

4. Vendor dependence:
1. Given the heavy investment in technology and personnel training, companies
might be reluctant to change database vendors.

5. Frequent upgrade / replacement cycles:


1. DBMS vendors (seller) frequently upgrade their products by adding new
functionality.
2. Such new features often the software. Some of these versions require hardware
upgrades.

 DATA MODEL BASIC BUILDING BLOCKS

1. The basic building blocks of all data models are entities attributes, relationships and
constraints.
2. An entity is any thing (a person, a place, a thing or an event) about which data to
be collected and stored.
3. An attribute is a character of an entity. Attributes are the equivalent of fields in file
systems.

Page 18
4. A relationship describes an association among entities.
5. Data models are used three types of relationships.

One – to – Many (1: M)


Many – to – Many (M: M)
One – to – One (1: 1)

6. A constraint is a restrictions placed on the data constraints are important


7. Because they help to ensure data integrity.
8. Constraints are normally expressed in the form of rules.

Eg: Employee salary must have values that are between 6,000 and 3,50,000

 BUSINESS RULES

1. When database designers go about selecting or determining the entities,


attributes and relationships
2. That will be used to build data model
3. They might start by gaining a through understanding of what type of data are
in an organization, now the data are used and in what time frames they are
used.
4. A business rules is a short, exact, and clear description of a rule, route or
principal within a specific organization.

5. Business rules, derived from a detailed description of an organization’s


operation
6. It helps to create and implement actions within that organizations environment.
7. Business rules must be representation in writing and updated to reflect any
change in the organization’s operational environment.
8. Business rules must be easy to understand and widely disseminated shares a
common explanation of the rules.
9. Business rules describes in simple language.
10.The main sources of business rules are company managers, policy markers,
department managers and written documentation
11.Such as a company’s procedures, standards or operations manuals.

DATA MODELING AND DATA MODELS

Data modeling, the first step in designing a database, refers to the process of
creating a specific data model for a definite problem domain (area or server).

A data model is a relatively simple representation, usually graphical, of more


complex real word data structures.
A model is an abstraction (concept) of a more complex real-word object or event.
The database environment, a data model represents data structures and their
characteristics, relations, constraints, transformations and other constraints with the
purpose of supporting a specific problem domain.

THE IMPORTANCE OF DATA MODELS:

Data models can facilitate interaction among the designer the application
programmer, and the end user. A well developed data model can even foster improved
understanding of the organization for which the database design is developed.

 THE EVOLUTION OF DATA MODELS:

Page 19
Generation Time Model Examples Comments
Used mainly on IBM main
1960s Frame Systems.
First File System VMS/VSA,
1970s Managed records, not
Relationships
Hierarchical IMS
Early data base system
Second 1970s and Network ADABAS
Navigational All Accesses
Data Model IDS-II
DB2
Conceptual simplicity
Oracle
Mid 1970s Relational Entity Relationship modeling
Third MSSQL –
to present Data Model and support for Relational
Server
Data Modeling
MySQL
Versant
Object Support complex data
FastObjects.Net
Mid 1980s Oriented Extended relational products
Fourth Objectivity/DB
to Present Extended support object and data ware
DB/2 UDB
Relational housing.
Oracle 10g
Organization and
db XML Management of and
Tamino structured data.
Next Present
XML DB2/UDB
Generation Future
Oracle 10g Relational and Object models
MS SQL Server add supports for XML
documents.

 DATABASE MODELS

1. A database model is a collection of logical constructs


2. It used to represent the date relationships found within the database models
3. It can be grouped into two categories.
a. Conceptual models and
b. Implementation models

Conceptual model:
1. The conceptual model center on the logical nature of the data representation.
2. The conceptual model is disturbed with what is represented in the database rather
than with how it is represented.
3. Conceptual model included the entity relationship (E-R).

Implementation models:
1. An implementation model is used to how the data are represented in the database
2. Or how the data structures are implemented to represent what is modeled.
3. Implementation models include
1. The hierarchical database model
2. The network database model
3. The relational database model
4. The entity relationship data model and
5. The object oriented database model

1. The hierarchical database model


1. The hierarchical model was developed in the 1960s.
2. It manages large amounts of data for complex (parts) manufacturing projects.
3. Its basic logical structure is represented by an upside down tree.
4. The hierarchical structure contains levels, or segments.
5. A segment is the equaling of a file system’s record type within the hierarchy

Page 20
6. The top layer (the root) is perceived as the parent of the segment directly
beneath it.

Final Assembly

Component A Component B Component C

Assembly A Assembly B Assembly C

Part A Part B Part C Part D Part E


1. The hierarchical database model’s have many advantages over the file system
model.
2. Hierarchical database model’s features helped form the foundation for current database
models,
3. Database application advantages are artificial, even if in different form in current
database environments.

Advantages are.
1. It promotes data sharing.
2. Parent/Child relationship promotes conceptual simplicity.
3. Database security is provided and enforced by DBMS
4. Parent/Child relationship promotes data integrity.
5. It is efficient with 1: M relationships.

Disadvantages:
1. Different parts implementation requires knowledge of physical data storage
characteristics.
2. Navigational systems give up complex application development, management,
and use;
3. It requires knowledge of hierarchical path.
4. Changes in structure require changes in all application programs.
5. There are implementation limitations (no multi-parent or M:N relationship)
6. There is no data definition or data manipulation language in the DBMS
7. There is a lack of standards.

2. The network database model


1. The network model was created to represent complex data relationships
2. It more effectively than the hierarchical model to improve database
performance, and to impose a database standard.
3. The network model, the user understanding the network database as a collection
of records in 1:M relationships.
4. The network model allows network database terminology a relationship is called
a set.
5. Each set is composed of at least two record types an owner record and a member
record.
6. A set represents a 1:M relationship between the owner and the member

Page 21
SALESREP CUSTOMER

PRODUCT INVOICE PAYMENT

INV_LINE

Advantages:
1. Conceptual simplicity is at least equal to that of
2. It handles more relationship types, such as M:N and multi parent.
3. Data access is more flexible than in hierarchical and file system models.
4. Data Owner/Member relationship promotes data integrity.
5. There is conformance to standards.
6. It includes data definition language (DDL) and data manipulation language (DML) in
DBMS

Disadvantages:
1. System complexity limits efficiency still a hierarchical model, navigational system.
2. Navigational system yields (give up) complex implementation, application
development, and management.
3. Structural changes require changes in all application programs.

3. The relational database model


1. The relational database model is implemented through a very sophisticated
(difficult or complicated) relational database management system (RDMBS).
2. The RDBMS performs the same basic functions provided by the hierarchical and
network DBMS systems and add other functions
3. That makes the relational database model easier to understand and to
implement.
4. The RDBMS manages all of the complex physical details
5. The relational database is perceived (understand) by the user to be a collection
of tables in which data are stored.
6. Each ‘table’ is a matrix consisting of a series of row/column intersections.
7. Tables also called relations are related to each other by sharing a common entity
characteristic.

Advantages:
1. Structural independence is promoted by the use of independent tables.
2. Changes in a table structure do not affect data access or application programs.
3. Table view largely improves conceptual simplicity
4. They provide easier database design, implementation, management, and use.
5. Ad-hoc query capability is based on SQL.
6. Powerful RDBMS isolates (suppurate) the end user from physical level details
7. And improves implementation and management simplicity.

Disadvantages:
1. The RDBMS requires extensive hardware and system software overhead.
2. Conceptual simplicity gives relatively untrained people the tools to use a good
system poorly, and if unchecked, it may produce the same data anomalies found in
file systems.
3. It may promote islands of information problems as individuals and departments can
easily develop their own applications.

Page 22
4. The entity relationship data model
1. First introduced the ER model in 1976.
2. It was the graphical representation of entities and their relationships in a
database structure.
3. The relational data model and ERM combined to provide the foundation for
tightly (Good) structured database design.
4. ER models are normally represented in an entity relationship diagram (ERD)
5. It uses graphical representations to model database components.

The ER model components:


1. Entity
2. Entity instance
3. Attribute
4. Relationship

Entity:
1. A person place or thing about which data are to be collect and stored.
2. An entity represented by a rectangle in ER models.
3. The name of the entity is written in the center of the rectangle.
4. The entity name generally written in capital letters and is written in the singular.

Entity instance:
1. Each now in the relational table is known as an entity instance or entity
occurrence in the E-R model.

Attribute:
1. An attribute describes a particular characteristic of the entity.

Relationship:
1. Relationships describe associations between two entities.
One-to-many (1:M)
Many-to-many (M:N) and
One-to-many (1:1)
Relationships are represents by a diamond symbol connected with line to the
related entities.
Advantages
1. Visual modeling yields (give up) exceptional conceptual simplicity.
2. Visual representation makes it an effective communication tool
3. It is integrated (include) with main relational model.

Disadvantages
1. There is limited constraint representation
2. There is limited relationship representation.
3. There is no data manipulation language.
4. Loss of information content occurs when attributes are removed from entities to avoid
crowded (full) displays. (This limitation has been addressed in subsequent graphical
versions)

5. The object oriented database model


1. Object oriented data models are typically show using unified modeling
language (UML) class diagrams.
2. Unified modeling language is a language based on 00 concepts
3. That describes a set of diagrams and symbols that can be used a graphically model
system.
4. UML diagrams are used to represent data and their relationships within the
large UML object oriented systems modeling language.

Page 23
Advantage
1. Semantic content is added.
2. Visual representation includes semantic content.
3. Inheritance promotes data integrity.

Disadvantages
1. Slow development of standards caused vendors to supply their own
enhancements, thus eliminating a widely accepted standard.
2. It is a complex navigational system.
3. There is a steep learning curve.
4. High system overhead slows transactions.

 Ansi/Spark Data Model (American National Standard Institute/ Standards


Planning and Requirements Committee)
(Or)
 Explain Degree of Data abstraction.
(Or)
 Explain the levels of Data abstraction.
(Or)
 Explain three schema architecture

1. In the early 1970s the (American National Standards Institute) ANSI (standards
planning and Requirements committee) SPARC defined a framework for data
modeling based on degrees of data abstraction.
2. The ANSI/SPARC architecture defines three levels of data abstractions:
a. External,
b. conceptual and
c. Internal.
3. The ANSI/SPARC framework has been expanded with the addition of a physical
model to explicitly address physical level implementation address physical level
implementation details of the internal model.

Need for Abstraction

The main objective of DBMS is to store and retrieve information efficiently; all the users
should be able to access same data. The designers use complex data structure to
represent the data, so that data can be efficiently stored and retrieved, but it is not
necessary for the users to know physical database storage details. The developers hide
the complexity from users through several levels of abstraction.

Page 24
Data Independence

Data independence means the internal structure of database should be

unaffected by changes to physical aspects of storage. Because of data independence,


the Database administrator can change the database storage structures without
affecting the users view.

The different levels of data abstraction are:

1. Physical level or internal level

2. Logical level or conceptual level

3. View level or external level

1. External model:

1. The external model is the end user’s view of the data environment.
2. The term end user refers to people who use the application programs to
manipulate the data and generate information.
3. Each business unit is subject to specific constrains and requirements
4. And each one uses a data subset of the overall data in the organization.
5. Therefore, end users working those business units view
6. ER diagrams will be used to represent the external views.
7. A specific representation of an external view known as an external schema.
8. The use of external view representing subsets of the database.

Advantages

1. It creates easy to identify specific data


2. It required to supports each business unit’s operations.
3. It creates the easy designer’s
4. It provides feedback about the model’s adequacy (capability).
5. It helps to ensure security constraints in the database design.
6. Damaging an entire database is more difficult but data cannot be loss
because
7. It creates application program development much simpler.

2. Conceptual model:
1. A conceptual model is used, graphically represented by an ERD
2. To integrate (mix) all external views into single view.
3. The conceptual model represents a global view of the entire database and
organization.
4. That is the conceptual model integrates all external views (entities, relationships,
constraints and processes) into a single global view of the entire data in the
enterprise.
5. Also known as conceptual schema,
6. It is the basis for the identification and high-level description of the main data
objects.
7. The most widely used conceptual model is E-R model.
8. The E-R model is illustrated with the help of the ERD.
9. The ERD is used to graphically represent the conceptual schema.

Page 25
Advantages:

1. It provides a relatively easily understood macro level view of the data


environment.
2. The conceptual mode is independent of both software and hardware’s.
3. Software independence means that model.
4. Does not depend on the DBMS software
5. It used to implement the model
6. Hardware independence means that mode doesn’t depend on the hardware.
7. Used in the implementation of the model.
8. Therefore charge in either the hardware or the DBMS software will have no
effect on the database design at the conceptual level.

3. The Internal Model:


1. The internal model map (record) the conceptual model to the DBMS.
2. The conceptual models are mapped to tables in the relational model.
3. A relational database has been selected the internal schema is expressed using
SQL, the standard Query Language for relational databases.
4. The development of a detailed internal model is especially important to database
designers who work with hierarchical or network models.
5. The internal model depends on specify database software.
6. It is said to software dependent.
7. A change in DBMS software requires that the internal model be changed
8. User can change the internal model without affecting the conceptual model.
9. A change in storage devices or even a change in operating system will not effect the
internal model.

4. The Physical model:


1. The Physical model operates at the lowest level of abstraction (idea)
2. It describing the way data is saved storage in disks or tapes.
3. The physical model requires the definition of both the physical storage devices and
the (physical) access methods
4. It required storage devices, creating it both software and hardware dependent.
5. The storage structures used are dependent on the software
6. The physical model is dependent on the DBMS,
7. Methods of accessing files and types of hardware storage devices supported
by the operating system.
8. User can change the physical model without affecting the internal model.

 Explain Database Architecture


1. Database architecture essentially explains the location of all the pieces of
information that create the database application.
2. The database architecture can be classified into
a. Two-Tier,
b. Three-Tier, and
c. Multitier architecture.

Two-Tier Architecture
1. The two-tier architecture is a client–server architecture
2. The client contains the presentation code and the SQL statements for data access.
3. The database server processes the SQL statements and sends query results back to
the client.
4. The client, or first tier, is primarily responsible for the presentation of data to the user to
the server
5. The server, or the second tier, is primarily responsible for supplying data services to the
client.

Page 26
First tier tasks/services
1. User interface
2. Presentation services
3. Application services

Second tier tasks/services


1. Application service
2. Business service/objects
3. Data services

Presentation Services
1. “Presentation services” refers to the portion of the application which presents data to
the user.
2. It also provides for the mechanisms in which the user will interact with the data.

Business Services/objects
1. “Business services” are a category of application services.
2. Business services summarize an organizations business processes and
requirements.
3. These rules are derived from the steps necessary to carry out day-today business
in an organization.
4. These rules can be validation rules
5. It used to be sure that the incoming information is of a valid type and format

Application Services
1. “Application services” provide other functions necessary for the Application.

Data Services
1. “Data services” provide access to data independent of their location.
2. The data can come from legacy mainframe, SQL RDBMS, or proprietary data access
systems.

Advantages:
1. The two-tier architecture is a good approach for systems with stable requirements
and a moderate number of clients.
2. The two-tier architecture is the simplest to implement, due to the number of good
commercial development environments.

Disadvantages:
1. Software maintenance can be difficult because PC clients contain a mixture of
presentation, validation, and business logic code.
2. To make a significant change in the business logic, code must be modified on many PC
clients.
3. Moreover the performance of two-tier architecture can be poor when a large
number of clients submit requests because the database server may be
overwhelmed with managing messages.

Three-tier Architecture
1. A “Multitier,” often referred to as “three-tier” or “N-tier,” architecture provides
greater application scalability, lower maintenance, and increased reuse of
components.

Page 27
2. Three-tier architecture offers a technology neutral method of building client/server
applications with vendors who employ standard interfaces which provide services
for each logical “tier.”
3. The three-tier architecture it is clear that in order to improve the performance a
second-tier is included between the client and the server.
4. Through standard tiered interfaces, services are made available to the application.
5. A single application can employ many different services which may reside on
dissimilar platforms or are developed and maintained with different tools.
6. This approach allows a developer to leverage investments in existing systems
while creating new application which can utilize existing resources.
7. Although the three-tier architecture addresses performance degradations of the
two-tier architecture, it does not address division-of-processing concerns.
8. The PC clients and the database server still contain the same division of code
although the tasks of the database server are reduced.
9. Multiple-tier architectures provide more flexibility on division of processing.

Multitier Architecture
1. A multi-tier, three-tier, or N-tier implementation employs a three-tier logical
architecture superimposed on a distributed physical model.
2. Application Servers can access other application servers in order to supply
services to the client application as well as to other Application Servers.
3. The multiple-tier architecture is the most general client–server architecture.
4. It can be most difficult to implement because of its generality
5. A good design and implementation of multiple-tier architecture can provide the
most benefits in terms of scalability, interoperability, and flexibility.

 Which Situations where DBMS is not Necessary


1. It is also necessary to specify situations where it is not necessary to use a DBMS.
2. If traditional file processing system is working well
3. It takes more money and time to design a database,

Page 28
4. But it is better not to go for the DBMS.
5. If only one person maintains the data and that person is not skilled in designing a
database [as well as not comfortable in using the DBMS]
6. Then it is not advisable to go for DBMS.

1. DBMS is undesirable if the application is simple, well-defined, and not expected to


change
2. Runtime overheads are not feasible because of real-time requirements.
3. Multiple accesses to data are not required.

Compared with file systems, databases have some disadvantages:

1. Higher hardware costs


2. Higher programming costs
3. High conversion costs
4. Slower processing of some applications
5. Increased vulnerability
6. More difficult recovery

 DBMS vendors and their products or Potential costs of implementing the


database or DBMS disadvantages:
Database systems do impose (require) significant (major) costs.

1. Increased costs
2. Management complexity
3. Maintaining currency
4. Vendor dependency

Increased costs:
1. Database system requires sophisticated (difficult) hardware and software and
highly skilled personnel.
2. The cost of maintaining the hardware , software can be large
3. And personnel required to operate and manage a database system can be also
large.

Management complexity:
1. Database system interface with many different technologies
2. It has a significant (major) impact on a company resources and culture.

Maintaining currency:
1. To maximize the efficiency of the database system you must keep your system
current.
2. Therefore user must update all the components
3. Because database technologies are rapidly changes, personal training is very cost.

Page 29
Vendor dependency:
1. Give the heavy investment in the technology
2. And personnel training, companies may be unwilling to change database vendors.

Vendor products
IBM DB2/MVS
DB2/UDB
DB2/400
Informix Dynamic
Server(IDS)
Microsoft Access
SQL Server
DesktopEdition(MSDE)
Open Source MySQL
PostgreSQL
Oracle Oralce DBMS
RDB
Sybase Adaptive Server Enterprise (ASE)
Adaptive Server Anywhere (ASA)
Watcom

Page 30
Unit – II
Entity-Relationship Model: Introduction, the building blocks of an entity relationship
diagram, classification of entity sets, attribute classification, relationship degree,
relationship classification, reducing ER diagram to tables, enhanced entity-relationship
model (EER model), generalization and specialization, IS A relationship and attribute
inheritance, multiple inheritance, constraints on specialization and generalization,
aggregation and composition, entity clusters, connection types, advantages of ER
modeling.

ENTITY RELATIONSHIP(ER) MODELING


The E-R Models [ERM]:
1. An entity relationship model [E-R Model] is a defiled logical representation of
the data for an organization or for a business area.
2. The E–R Model is expressed in terms of entities.
3. A E-R Model is normally expressed as an entity relationship diagram
4. It is a graphical representation of an E-R model.
The components in E-R Model
1. Entities
2. Attributes
3. Relationships

 ENTITIES:
EMPLOYEE

1. An entity is an object of interest to the end user.


2. An entity is represented by a rectangle containing the entity’s name.
3. The entity name, a noun, is usually written in all capital letters.
4. An entity is anything a place, a person, a thing, an event about which data are collected
and store.
5. A collection of entities known as entity set or entity type.
6. Each row in the relational table is known as an entity instance or entity occurrence
(event).
The examples of entities are:

A particular person, for example Dr. A.P.J. Abdul Kalam is an entity.


A particular department, for example Electronics and Communication
Engineering Department.
A particular place, for example Coimbatore city can be an entity.

There are different types of entities


1. Strong entity.
2. Weak entity.
3. Associative entity (or) Composite entity (or) bridge entity

Strong Entities
1. A strong entity type is an entity that exists independently of other entity types.
2. Strong entity type always has a unique characteristic called an identifier.

Page 31
3. That is an attribute or combination of attribute
4. That uniquely identified each occurrence (incidence) of that entity type.
5. It can be denoted as single line rectangle.

Syntax ENTITY NAME Ex: STUDENT


:
Example:
1. Consider the example, student takes course.
2. Here student is a strong entity

Weak Entities

1. A weak entity is an entity type whose existence depends on some other entity
type.
2. The entity has a primary key
3. That is partially or totally derived from the parent entity in the relationship.
4. It is represented by double line rectangle.

Syntax: ENTITY NAME Ex: BANK LOAN

Example:
1. Consider the example, customer borrows loan.
2. Here loan is a weak entity.

ASSOCIATIVE ENTITY

1. An associative entity is an entity type that associates the instances of one or


more entity type.
2. It means a many to many relationship exits
3. It is also called composite entity or bridge entity
4. And it contains attributes that are peculiar to the relationship between those entity
instances.
5. It is represented by a diamond shape with in a rectangle.

 ATTRIBUTES: (Attribute Classification or Different types of attributes)

1. An attribute is a property or characteristics of an entity.


2. Attributes are represented by ovals and are connected to the entity rectangle
with a line.

Ename
Eno ESal

EMPLOYEE

Page 32
Entity type: EMPLOYEE

Attributes: Ename
Eno
Esal

Note: The naming attributes we use an initial capital letter followed by lower case

letter. There are different types of attributes

1. Domain
2. Composite and simple attributes
3. Single-valued attribute
4. Multivalued attribute
5. Derived attribute
6. Identifiers
7. Composite primary keys

Domains
1. Attribute have a domain.
2. A domain is the ser of possible values for a given attribute.
3. For example the domain for the gender attribute consists of only two possible: M
or F.

Male Female

Gender

Required and optional attributes


1. A required attribute is an attribute that must have a value.
2. It cannot be left empty.
3. An optional attribute is an attribute that does not require a value;
4. It can be left empty.

36

Age Vehicle

Required attribute optional attribute

Composite and simple attributes:


1. A composite attribute is an attribute that can be further subdivided to yield (give
up) additional attributes.

Page 33
2. For example the attribute address is subdivided into street, city, state and zip
code.
Door_no
street City
Male/Female
Address

Composite attribute simple attribute

3. A simple attribute is an attribute that cannot be subdivided.


4. For example age, sex and marital status would be classified as simple attributes.

Single- valued attributes


1. A single-valued attribute is an attribute that can have only a single value.
2. For example a person can have only one social security number
3. A single valued attribute is not necessarily a simple attribute.

Pin No

ATM card

Single valued attribute


Multivalued attribute

1. Multivalve attributes are attributes that may take on more than one value for a
given entity instance.
2. A multi value attribute with an ellipse with double lines.

Ename
skill
Eno

EMPLOYEE
Derived attributes

1. A derived attribute is an attribute whose value is calculated (derived) from other


attributes.
2. The derived attributes need not be physically stored within the database.
3. Derived attributes are sometimes referred to as computed attributes.
4. Indicate a derived attribute using an ellipse with a dashed line.

Ename
skill
Eno

EMPLOYEE Join_date
Experience

Page 34
Identifiers (Primary keys)
1. One or more attributes that uniquely identify each entity instance.
2. In the relational model, such identifiers are mapped to primary keys (PKs) in
table.
3. Identifiers are underlined I n the ERD.

Ex:
Jdc(Eno, ename, salary);
Null Value Attribute

In some cases, a particular entity may not have any applicable value for an attribute. For
such situation, a special value called null value is created. Null value situations Not
applicable Not known

Example

In application forms, there is one column called phone no. if a person do not have phone
then a null value is entered in that column

Composite identifiers

A composite identifier is a primary key composed of more than one attribute.

JDC( Eno, Deptno, Ename, Salary )

 RELATIONSHIPS

1. Relationship is an association between entities.


2. The entities that participate each relationship is identified by a name
that describes the relationship

3. The relationship represents diamond symbol.


4. Relationships between entities always operate in both directions.
5. For example: the relationship between the entities named EMPLOYEE and SKILL
6. An EMPLOYEE may learn many SKILLS. Each SKILL may learn by many EMPLOYEE
7.

M N
CITY Vehicle ROOTS

CONNECTOVITY AND CARDONALITY


1. The term connectivity is used to describe the relationship classification.
2. The entity relationships may be classified as

Page 35
a. one-to-one,

1 1
Principal Manages Department

b. one-to many

1 M
Subject has Lectures

c. many to many
M N
Lectures Teach Subjects

3. Cardinality expresses the minimum and maximum number of entity occurrence


(incidence) associated with one occurrence of the related entity.
4.
M N
Lectures Teach Subjects

EXISTENCE DEPENDENCE
1. An entity is said to be existence-dependent
2. If it can exist in the database only when it is associated with another related
entity occurrence.
3. In entity can exist apart from one or more related entities, it is said to be
existence-independent.
Author Book

1.For example, there are two entities named Author and Book.
2.A writer to take an advance to write a book for a publisher,
3.But the first book written by the writer for this publisher,
4.There will be no entry in the Book entity until the contracted book has been written and
published
5. In this case, the relationship between Author and Book is 1:0, indicating that the
existence of an instance of Book is optional
RELATIONSHIP STRENGTH
1. Relationship strength is based on how the primary key of a related entity is
demined.
2. To implement a relationship, the primary key of one entity connects as a foreign
key in the related entity.
Ex:
College (SNO, SNAME, SSECTION)
Hostel (SNO, SNAME, ROOMECODE,)

1. Hear Hostel student no is dependent on college sno


2. student don’t have sno in college that student sno not to store in hostel

Weak (Non-identifying) Relationships


1. A weak relationship, also known as a non-identifying relationship,
2. It exists if the PK of the related entity does not contain a PK component of the
parent entity.

College (SNO, SNAME, SSECTION)


Hostel (SNO, SNAME, ROOMECODE,)

Page 36
Strong (Identifying) Relationships
1. A strong relationship, also known as identifying relationship
2. It exists when the PK of the related entity contains a PK component of the parent
entity.

College (SNO, SNAME, SSECTION)


Hostel (SNO, SNAME, ROOMECODE,)

RELATIONSHIP PARTICIPATION
Participation in an entity relationship is either optional or mandatory.

Optional:
1. Optional participation means that one entity occurrence does not require a
corresponding entity occurrence in a particular relationship.
2. Ex: Some Lecturers may Teach any Students I Each Student must be taught by at
least one Lecturer

M N
Lectures Teaches Student

Mandatory:

1. Mandatory participation means that one entity occurrence requires


a corresponding entity occurrence in a particular relationship.
2. Some Lecturers may not Teach any Students I Each Student must be taught by at
least one Lecturer
M N
Lecturer Teaches Student

 REATIONSHIP DEGREE (DEGREES OF RELATIONSHIP)


The degree of relationship is number of entities or participates associated with
a relationship. The most common relationship degrees in E-R models are

1. Unary (Degree I)
2. Binary (Degree II)
3. Ternary (Degree III)
Higher relationship degrees are also possible but they are rear in use.

UNARY RELATIONSHIP:
1. A unary relationship is a relationship between the instances of a single entity
type.
2. These relationships are also called a recursive relationship.
One-to-one relationship (1-1):
One instant of entity type
associated one instant of the PERSON Is-married_to

same entity then it is called


one-to-one relationship

In this Eg is_married_to is shown as a 1-1 relationship between instances of the


person entity type.

Page 37
EMPLOYEE Manages

One_to_many (1 – N):

One instant of entity type


associated more than one instant of
the same entity then it is called one-
to-many relationship

In this Eg manages is shown as 1 to many relationship between the instances of the


employee entity type.

Some organizations it may be possible for one employee to be managed by many other
employees.

Many_to_many (N – N)

More than one instant of entity type


associated with more than one
Has-
instant of the same entity then it is
ITEM components
called many-to-many relationship

1. In this eg we represent many to many relationships.


2. The entity type item is used to represent all types of components.
3. Components for the name of the relationship that associated lower level item and
higher level items.

BINARY RELATIONSHIP:
A binary relationship is relationship between the instances of two entity types.

One_to_one(1-1): One instance of the one entity type can be associated with one
instances of other entity type.

EMPLOYEE Is_assigned PARKING PLACE

This eg indicates that an employee is assigned one parking place and each
parking place is assigned to only one employee.

One to many (1-N): One instance of the one entity type can be associated with
number of instances of other entity type.

Eg:
PRODUCT LINE Contains PRODUCT

In this eg has is the relation between product groups and products many no. of
products are related to only one product group.

Page 38
Many-to-many: The number of instances of one entity type can associated with
number of instances of other entity type.

Eg: STUDENT Registers-


_for COURSE

This eg indicated that an employee may complete more than one course and
then each course may have many employees.

TERNARY RELATIONSHIP
1. A ternary relationship is a simultaneous relationship among the instances of three
entity types.
PART

VENDOR Supplies WAREHOUSE

Shipping mode
unitcost

1. In this eg vendor can supply various parts to warehouse.


2. The relationship supply is used to record to specific parts that are supplied by a
given vendor to a particular warehouse.
3. Thus there is three-entity type VENDOR, PARTS, WAREHOUSE.
4. There are two attributes are relationship is shipping code and unit-cost.

Quaternary Relationships

Quaternary relationships involve four entities. The example of quaternary

relationship is “A professor teaches a course to students using slides.” Here the four
entities are PROFESSOR, SLIDES, COURSE, and STUDENT. The relationships between
the entities are “Teaches.”

Page 39
RELATIONSHIPS WITHIN THE RELATIONAL DATABASE
A relationship is an association among the instance of one or more entity types. The
relationship are classified as one-to-one (1: 1), one-to-many (1: M), and many to many
(M: N or M: M)

The 1:M Relationship:

The 1: M relationship is the relational database norm. consider the PAINTER paints
PAINTING example.

PAINTER paints PAINTING

The 1:M relationship between PAINTER and PAINTING

1. Each painting is painted by one and only one painter, but each painter could
have painted many paintings.
2. There is only one row in the PAINTER table for any given row in the painting
table, but there may be many rows in the PAINTER table for any given row in
the PAINTER table.
3. The 1:M relationship is found in any database environment.

The 1: 1 Relationship:

As the 1: 1 table implies, in this relationship, one entity can be related to only one
other entity, and vice-versa. For example, one department chair – a professor – can chair
only one department can have only one department chair. The entities PROFESSOR and
DEPARTMENT thus exhibit a 1: 1 relationship.

The preceding “PROFESSOR chairs DEPARTMENT” example illustrates a proper 1: 1


relationship. In fact, the use of a 1:1 relationship ensures that two entity sets are not
placed in the same table when they should not be.

PROFESS DEPARTMENT
Chairs

The 1:1 relationship between PROFESSOR and DEPARTMENT

The M : N Relationship

A many-to-many (M:N) relationship is not supported directly in the relational


environment. To explore the many-to-many (M:N) relationship, consider a rather typical
college environment in which each STUDENT can take many CLASSes, and each CLASS
can contain many STUDEMTs.

CLASS
STUDENT has

The ERM’s M:N relationship between STUDENT and CLASS

Page 40
INDEXS:
An index is an orderly arrangement used to logically access rows in a table.

Indexes in the relational database environment work like the indexes described in
the preceding paragraphs. From a conceptual point of view, an index is composed of an
index key and a set of pointers.

DBMS use index for many different purposes. An index can be used to retrieve
data more efficiently. But indexes can also be used by a DBMS to retrieve data ordered
by a specific attribute or attributes.

For example, creating an index on a customer’s last name will allow you to
retrieve the customer data alphabetically by the customer’s last name.

Index play an important role in DBMSs for the implementation of primary keys,
when you define a table’s primary key column(s). The DBMS automatically creates a
unique index on that attribute. A unique index as its name implies, is an index in which
the index key can have only one0020pointer value (row) associated with it.

 Symbols Used in ER Diagram


1. The elements in ER diagram are Entity, Attribute, and Relationship.
2. The different types of entities like strong, weak, and associative entity,
3. Different types of attributes like multivalued and derived attributes and identifying
relationship and their corresponding symbols

Strong entity Associate entity

Week entity
Attribute

Relation ship Multivalue attri.

Identify Rel.Ship Derive Attribute

Page 41
Reducing ER Diagram to Tables

Mapping Algorithm

The mapping algorithm gives the procedure to map ER diagram to tables.


The rules in mapping algorithm are given as:
– For each strong entity type say E, create a new table. The columns of the table are the
attribute of the entity type E.
– For each weak entity W that is associated with only one 1–1 identifying
owner relationship, identify the table T of the owner entity type. Include
as columns of T, all the simple attributes and simple components of the
composite attributes of W.
– For each weak entity W that is associated with a 1–N or M–N identifying
relationship, or participates in more than one relationship, create a new table
T and include as its columns, all the simple attributes and simple
components of the composite attributes of W. Also form its primary key by
including as a foreign key in R, the primary key of its owner entity
– For each binary 1–1 relationship type R, identify the tables S and T of
the participating entity types. Choose S, preferably the one with total
participation. Include as foreign key in S, the primary key of T. Include
as columns of S, all the simple attributes and simple components of the
composite attributes of R.
– For each binary 1–N relationship type R, identify the table S, which is at
N side and T of the participating entities. Include as a foreign key in S, the primary key
of T. Also include as columns of S, all the simple attributes and simple components of
composite attributes of R.
– For each M-N relationship type R, create a new table T and include as
columns of T, all the simple attributes and simple components of composite attributes of
R. Include as foreign keys, the primary keys of the participating entity types. Specify as
the primary key of T, the list of
foreign keys.
– For each multivalued attribute, create a new table T and include as
columns of T, the simple attribute or simple components of the attribute A.
Include as foreign key, the primary key of the entity or relationship

type that has A. Specify as the primary key of T, the foreign key and the

columns corresponding to A.

Regular Entity

Regular entities are entities that have an independent existence and generally

represent real-world objects such as persons and products. Regular entities

Page 42
are represented by rectangles with a single line.

Converting Composite Attribute in an ER Diagram to

Tables Example

In this example the composite attribute is the Customer address, which consists of
Street, City, State, and Zip.

The first relation contains all of the attributes of the entity type except

the multivalued attribute.

The second relation contains two attributes that form the primary key of

the second relation. The first of these attributes is the primary key from the first
relation, which becomes a foreign key in the second relation. The second is the
multivalued attribute.

Mapping Multivalued Attributes in ER Diagram to Tables

Page 43
Converting “Weak Entities” in ER Diagram to Tables

Converting Binary Relationship to Table

A relationship which involves two entities can be termed as binary relationship.

This binary relationship can be one-to-one, one-to-many, many-to-one,

Page 44
and many-to-many.

Mapping one-to-Many Relationship

Mapping Associative Entity to Tables

Many-to-many relationship can be modeled as an associative entity in the ER diagram.

Page 45
Converting Ternary Relationship to Tables

Page 46
Page 47
ADVANCED DATA MODELING

 What is an extended entity relationship model? Explain entity super types and
sub types? Or THE EXTENDED ENTITY RELATONSHIP MODEL (EER model)

EER Models:

As the complexity of the data structures being modeled has increased and as
application software requirements, there has been as increasing more information in the
data model. In this model represent some business rules also This model that has
resulted from extending the original ER model with new modeling constructs is called
EER model

ENTITY SUPERTYPES AND SUBTYPES

 SUPERTYPE:
1. An entity super-type is a generic entity type
2. That is related to one or more entity subtypes
3. Where the entity super-type contains the common characteristics,
4. And the entity subtypes contain the unique characteristics of each entity
subtype
.
 SUBTYPE:
1. A subtype is a subgroup of the entities in an entity type

Page 48
2. That is meaningful of the organization shares common attributes or
relationships distinct from other sub-grouping.

INHERITANCE
1. The property of inheritance enables (allow) an entity subtype to inherit the
attributes and relationships of the super type.
2. A super type contains those attributes that are common to all of its subtype.
3. In contrast, subtypes contain only the attributes that are unique to the
subtype.
4. One important inheritance characteristic is that all entity subtype inherit their
primary key attribute from their super type.

SUBTYPE DISCRIMINATOR
1. A subtype discriminator is the attribute in the super type entity that determines
to which subtype the super type occurrence is related.

Explain Specialization and

Generalization Generalization:

Generalization is the process of minimizing the differences between entities by


identifying common features. It can also be defined as the process of defining a
generalized entity type from a set of entity types.

Specialization is a process of identifying subsets of an entity set (the

superset) that share some distinguishing characteristics. In specialization

the super class is defined first and the subclasses are defined next. Specialization is the
process of viewing an object as a more refined, specialized object.

Page 49
Make Model

V-name
Cab Type
V-ID

Truck Capacity

Price Engine Displacement

Make Model

V-name
V-ID

Vehicle

Engine Displacement
Price

Cab Type
Car Truck

Capacity
No. of passengers

The process of defining a more general entity type from a set of more specialized entity
types. Thus generalization is a “Bottom up process”.

Ex:

Page 50
In the above diagrams car, truck and motor cycle contains a no. of attributes in
common, vehicle-ID, v-name, and price and engine displacement.

This fact suggests that each of the three entity types is really a version of a more
general entity types. This more general entity type (vehicle) together with the resulting
supertype / subtype relationships is shown in above fig b.

The entity car has the specific attribute, no. of passengers, while truck has 2
specific attributes capacity and cab type. Thus generalization has allowed us to group
entity types along with their common attributes and at the same time preserve specific
attribute that are peculiar to each subtype.

In the above examples, motorcycle is not included in the relationship because all
attributes of motor cycle are those that are common to all vehicles.

Specialization:

The process of defining one or more subtypes of the supertype and forming
supertype/subtype relationships.

Price
Supplier
Description
Supplier-ID
Part-no
Part
Routing-No

Quantity
on Hand Location

Page 51
Description Location

Quantity
Part-no Part on Hand S-ID

Routing-No Supplier
Unit
Price
Manufactur Purchased
Supplies
ed Part Part

In the above ex entity type part having attributes partno, description, unit price,
location, quality on hand, routing no and supplier.(a supplier attribute is Multivalued
attribute. since there may be more than one supplier with associated unit price, for a
part).there are 2 possible source for parts. Some are manufactured internally, while
others are purchased from outside suppliers. In this case, the choice depends on factors
such as manufacturing capacity, unit price of the parts. Thus routing_no applies only to
manufactured parts, while supplier_id and unit price apply only to purchase parts.

ISA Relationship and Attribute Inheritance

IS A relationship supports attribute inheritance and relationship participation.

In the EER diagram, the subclass relationship is represented by ISA

relationship. Attribute inheritance is the property by which subclass entities

inherit values for all attributes of the superclass.

Consider the example ofEMPLOYEEentity set in a bank. TheEMPLOYEE

in a bank can be CLERK, MANAGER, CASHIER, ACCOUNTANT, etc. It

is to be observed that the CLERK, MANAGER, CASHIER, ACCOUNTANT

inherit some of the attributes of the EMPLOYEE.

In this example the superclass is EMPLOYEE and the subclasses are

CLERK, MANAGER, and CASHIER. The subclasses inherit the attributes

Page 52
of the superclass. Since each member of the subclass is an ISA member of the
superclass, the circle below the EMPLOYEE entity set represents ISA relationship.

Multiple Inheritance

A subclass with more than one superclass is called a shared subclass. A subclass inherits
attributes not only of its direct superclass, but also of all its predecessor superclass, that
is it has multiple inheritance from its superclasses. In multiple inheritance a subclass can
be subclass of more than one superclass.

 Constraints on Specialization and Generalization


The constraints on specialization and generalization can be broadly classified
into disjointness and completeness. The dis jointness constraint allows us to
specify whether an instance of a super type may simultaneously be a member of
two or more subtypes. In dis jointness we have two categories (1) Overlap and
(2) Disjoint. In completeness we have two categories

(1) Total and (2) Partial

Overlap Constraint
Overlap refers to the fact that the same entity instance may be a member of more
than one subclass of the specialization
Example of Overlap Constraint
Consider the example of ANIMAL entity, which can be further subdivided into
LAND ANIMAL and WATER ANIMAL. Consider the example of Frog and Crocodile
which can live in both land and water hence the division of ANIMAL into LAND
and WATER animals is an example of overlap constraint.

Page 53
Disjoint Constraint

Disjoint refers to the fact that the same entity instance may be a member of only
one subclass of the specialization.
Example of Disjointness Constraint

Consider the example of CATALOGUE. The CATALOGUE is a superclass,

which can be further subdivided into BOOKS, JOURNALS, and PERIODICALS.This


falls under disjointness because a BOOK entity can be neither JOURNAL nor
PERIODICAL.

Total Specialization

 Total completeness refers to the fact that every entity instance in the
superclass must be a member of some subclass in the specialization. With total
specialization, an instance of the supertype must be a member of at least one
subtype.

Partial Specialization
Partial completeness refers to the fact that an entity instance in the superclass
need not be a member of any subclass in the specialization. With partial

Page 54
specialization, an instance of a supertype may or may not be a member of any
subtype.
Example of Partial Specialization
Consider the PERSON specialization into EMPLOYEE and STUDENT. This
is an example of partial specialization because there can be a person who
is unemployed and does not study.

12) Aggregation and Composition

Relationships among relationships are not supported by the ER model. Groups of


entities and relationships can be abstracted into higher level entities using
aggregation. Aggregation represents a “HAS-A” or “IS-PART-OF” relationship
between entity types. One entity type is the whole, the other is the part.
Aggregation allows us to indicate that a relationship set participates in another
relationship set. Consider the example of a driver driving a car. The car has
various components like tires, doors, engine, seat, etc., which varies from one car
to another. Relationship drives is insufficient to model the complexity of this
system. Part of relationships allow abstraction into higher level entities. In this
example
engine, tires, doors, and seats are aggregated into car.

Composition is a stronger form of aggregation where the part cannot exist without
its containing whole entity type and the part can only be part of one entity
type.Consider the example of DEPARTMENT has PROJECT. Each project is
associated with a particular DEPARTMENT. There cannot be a PROJECT without
DEPARTMENT. Hence DEPARTMENT has PROJECT is an example of composition.

Page 55
Entity Clusters
EER diagrams are difficult to read when there are many entities and relationships.
One possible solution is to group entities and relationships into entity clusters.
Entity cluster is a set of one or more entity types and associated relationships
grouped into a single abstract entity type.

Connection Traps

Connection trap is the misinterpretation of the meaning of certain relationships.


This connection traps can be broadly classified into fan and chasm trap. Any
conceptual model will contain potential connection traps. An error in the
interpretation of the meaning of the relationship may cause the database to be
incapable of storing certain information. Both the fan and chasm trap arise when
the relationships appear to exist between entity types, but the links between
occurrences may be ambiguous or not exist. Related groups of entities could
become clusters.

Fan Trap
Fan trap occurs when the model represents a relationship between entity types
but the pathway between certain entity occurrences is ambiguous. Fan trap
occurs when 1–M relationships fan out from a single entity. In order to understand
the concept of Fan trap, consider the following example Contractor works in a
team. . . . . . . . . Statement (1)
Team develops projects. . . . . . . . . . . . Statement (2)
Statement (1) represents M–1 relationship. Statement (2) represents 1–M
relationship. But the information about which contractors are involved in
developing which projects is not clear.

Consider another example of Fan trap.

Department is on Site. . . . . . . . . Statement (1)


Site employs Staff. . . . . . . . . . . . . . . Statement (2)

Page 56
Statement (1) represents M–1 relationship, because many departments may be in
a single site. Statement (2) represents 1–M relationships. However which staff
works in a particular department is ambiguous. The fan trap is resolved by
reconstructing the original ER model to represent the correct association
Chasm Trap
A chasm trap occurs when a model suggests the existence of a relationship
between entity types, but the pathway does not exist between certain entity
occurrences. It occurs where there is a relationship with partial participation,
which forms part of the pathway between entities that are related. Consider the
relationship shown later.

A single branch may be allocated to many staff who oversees the management of
properties for rent. It should be noted that not all staff oversee property and not
all property is managed by a member of staff. Hence there exist a partial
participation of Staff and Property in the relation “oversees,” which means that
some properties cannot be associated with a branch office through a member of
staff. Hence the model has to modified as shown later

15) Advantages of ER Modeling


An ER model is derived from business specifications. ER models separate

the information required by a business from the activities performed within a

business. Although business can change their activities, the type of information
tends to remain constant. Therefore, the data structures also tend to be
constant. The advantages of ER modeling are summarized later:
1. The ER modeling provides an easily understood pictorial map for the
database design.
2. It is possible to represent the real world problems in a better manner in
ER modeling.
3. The conversion of ER model to relational model is straightforward.
4. The enhanced ER model provides more flexibility in modeling real
world problems.
5. The symbols used to represent entity and relationships between entities
are simple and easy to follow.

Page 57
Differences of Specialization & generalization:

No specilaization generalization
1. It is a Top down approach It is a Bottom Up apporach
2. Specialization stems from single Generalization proceeds from the recognition
entity set; it emphasizes differences that a number of entity sets share some
among entities the set by creating common features(namely,they are described
distinct low level entity sets. by the same attributes and participate in the
same relationship sets)
3. The process of designating sub The process of designating groupings from
groupings with in an entity set is various entity sets is called Generalization
called Specialization.
4. Specialization is a result of taking a Generalization is a result of taking the lower
subset of higher level entity set to level entity sets to produce a higher level
form a lower level entity set. entity set.

UNIT III

Relational Model: Introduction, CODD Rules, relational data model, concept of key,
relational integrity, relational algebra, relational algebra operations, advantages of
relational algebra, limitations of relational algebra, relational calculus, tuple relational
calculus, domain relational Calculus (DRC). QBE

 Relational Data Model


1. The relational model uses a collection of tables to represent both data and
relationships.
2. Tables are logical structures maintained by the database manager.
3. The relational model is a combination of three components,
a. Structural
b. Integrity and
c. Manipulative parts.

a. Structural Part
1. The structural part defines the database as a collection of relations.

b. Integrity Part
1. The database integrity is maintained in the relational model using primary
and foreign keys.

c. Manipulative Part
1. The relational algebra and relational calculus are the tools
2. It used to manipulate data in the database.
3. Thus relational model has a strong mathematical background.

Table: (Characteristics of a relational table)


A table is perceived as a two-dimensional structure composed of rows and columns.
1. A table is also called relation.
2. Each row in the table is called tuple.
3. Each column in the table is called attribute.
4. Each column represents an attribute, and each column has a distinct name
5. Each rows and columns can be exactly the same

Page 58
6. The column will have data value.
7. All values in a column must conform to the same data format
8. The order of the rows and columns is unimportant to the DBMS.
9. In relational model attributes can be in any order.
10.In relational model rows can be in any order.
11.Relations must have a key.
12.Keys can be a set of attributes.
13.Each column has a specific range of values known as the attribute domain
14.Domain is the set of valid values for an attribute.
15.Degree of the relation is the number of attributes (columns) in the relation.
16.Cardinality of the relation is the number of tuples (rows) in the relation

Keys:
1. A key is a one or more attributes that determine (decide) other attributes.
2. For example sno identifies all of the student attributes
3. Such as sfname, slname, sintial, dob, class and phno.
4. The one attribute determines other attribute is called determination.
AB
In the above A determines B. A is called determination.

Functional dependence:

1) Functional dependency is a relationship that exists when one attribute


uniquely determines another attribute.
2) The attribute B is functionally dependent on the attribute A
3) If each value in column A determines one and only one value in column B.
4) Attribute A determines attribute B (that is, B is functionally dependent on A) if all
of the rows in the table that agree in value for attribute.
5) A also agree in value for attribute B

Functional Dependencies and Keys


A functional dependency is a constraint between two attributes or two sets of
attributes.
If the attribute (B) is functionally dependent on a composite key(A) but not on any
subset of that composite key, the attribute (B) is fully functionally dependent on (A).
For any relation R. attribute B is functionally dependent on attribute A if, for every
valued instance of A, that value of a uniquely determines that value of B.
The functional dependency of B on A is represented by an arrow as follows.
AB
Eg: Consider the relation EMP-COURSE

EMP-COURSE (Eno, Course_title, Date_completed);


Eno, Course_title  Date_completed.

Fully functional dependencies


1. All non candidate key attributes must depend on the primary key.
2. (Remember, a candidate key attribute is any key (for example, a primary or
foreign key)
3. It used to uniquely identify a database record.
4. If the attribute (B) is functionally dependent on a composite key(A)
5. But not on any subset of that composite key, the attribute (B) is fully functionally
dependent on (A).
EMP-COURSE (Eno, Course_title,);
Eno Course_title

Page 59
Determinants:
The attribute on the left-hand side of the arrow in a functional dependency is
called a determinant.
Eg: In the EMP-COURSE relation the combination of eno, course_tittle is a
determinant.

 Types of SQL Keys

There are different types keys relation data model. They are
1. Composite key
2. Candidate key
3. Super key
4. Alternate key
5. Secondary key
6. Primary key
7. Foreign key
1. Composite key:
a. It is a combination of two or more columns in a table
b. That can be used to uniquely identify each row in the table.
c. Such a multi attribute key is known as composite key.
Ex:
CREATE TABLE BABA
(Sno integer, deptno
integer, address varchar(50), PRIMARY KEY (sno, deptno));

Explanation:

2. Candidate key:
a. A Candidate Key can be any column or a combination of columns
b. That can qualify as unique key in database.
c. There can be multiple Candidate Keys in one table.
d. Each Candidate Key can qualify as Primary Key.
e. [A Primary Key is a column or a combination of columns that uniquely identify a
record
f. A minimal super key. A super key that does not contain a subset of attributes
that is itself a super key. ]

CREATE TABLE BABA


(Sno integer, deptno integer, email varchar(50), PRIMARY KEY (sno, deptno,email));

Explanation: EmpID , EmailID and SSN all will be always unique for any
employee in that case all these three columns called as candidate keys.

3. Super key:
a. Super key is a combination of columns
b. That uniquely identifies any row within a RDBMS table.
c. It is reduced to the minimum number of columns required to uniquely identify each
row.
d. An attribute or combination of attributes that uniquely identifies each row in a
table is called super key.
Exp :

4. Alternate key:
a. A Alternate key is a key that can be work as a primary key.
b. Basically it is a candidate key that currently is not primary key.
Or
a. Among of candidate keys if any single key or combination of keys made as

Page 60
primary key then rest candidate key called as alternate key.
Exp:
1. Suppose in employee table EmpID is primary key
2. Then Emailid and SSN are called as alternate key
3. It mean later on these key can be act as primary key without affecting existing data in
table.

Secondary key:
1. An attribute (or combination of attributes) used strictly for data retrieval purpose is
called secondary key.
Exp :
create table tnam(ano number, bno number, bname char(12), primary
key(ano,bno))
In this bno is secondary key.

Primary key:
1. An attribute that uniquely identifies each row in a relation is called primary key.

2. The combination of unique and not null


3. It cannot support duplicate value and also empty value in attribute

Ex: CREATE TABLE BABA


(Sno integer, deptno integer, address varchar(50), PRIMARY KEY (sno));
Or
CREATE TABLE BABA
(Sno integer primary key, sna varchar2(20));

Foreign key:
1. It is used to create relation between two tables using with primary key.
2. A FOREIGN KEY is a key used to link two tables together
3. A FOREIGN KEY is a field (or collection of fields) in one table that refers to the
PRIMARY KEY in another table.
4. The table containing the foreign key is called the child table, and the table
containing the candidate key is called the referenced or parent table.

CREATE TABLE BABA1


( Sno int PRIMARY KEY,
Sna varchar2(20) NOT NULL,
SID int FOREIGN KEY REFERENCES JDC(ID)
);

 Explain Dr. E.F.CODD Rules


[For any RDBMS to be accepted as full fledged RDBMS it has to follow the 12 CODD
rules . The rules are] There are 12 CODD rules.

1. The information Rule: it is used to be represented all information as a data values


in the rows and columns of tables. This is the basis of the relational model.

2. The guaranteed access Rule: Every data value in a relational database should be
logically accessible by specifying a combination of the table name, the primary key
value on column name.

3. Systematic Treatment of NULL values: DBMS must support NULL values to


represent missing or inapplicable information. They must be distinct from zero or
spaces.

Page 61
4. Active Online Catalog Based on the Relational model: The system list is a
collection of tables that the DBMS maintains for its own use. These tables hold the
description of the structure of the database.

5. The Comprehensive Data Sub-language Rule: This Rule states that the system
must support at least one language that performs data definition, View Definition,
data manipulation operations, security and integrity constraints and transaction
management operations.

6. The View Updation Rule: It used to All views that are theoretically updateable must
be updateable by the system.

7. High Level insert, update, and Delete: it used to rows should be treated as sets
in insert, delete and update operations.

8. Physical data independence: Application Program must remain un impaired


(undamaged) when any changes are made in storage representation or access
method.

9. Logical Data Independence :Users and user Programs should be independent of


the logical structure of the database.

10. Integrity independence: Integrity constraints must be storable in System catalog.

11. Distribution independence: Database must allow manipulation of Distributed data


located on other computer systems.

12. Non – Subversion Rule: This rule states that different levels of the language can not
subvert or bypass the integrity rules and constraints.

 Integrity rules or relational integrity:


1. Relational database integrity rules are very important to good database design.
2. Many (but by no means all) RDBMSs enforce integrity rules automatically.
3. The set of rules which are used to maintain the accuracy of the data and to integrate
(add) the data in database is called as integrity constraints.
1. Domain Integrity.
2. Entity Integrity.
3. Referential Integrity.

1. Domain Integrity:
1. A domain is a set of values which is assigned to an attribute.
2. The components of the domain are domain name, meaning, data type, size and
range of the values.

Ex: student relation the domains are regdno, name.

2. Entity Integrity :
1. All primary key entries are unique, and no part of a primary key may be null.
2. Each row will have a unique identity, and foreign key values can properly
reference primary key values.
3. A primary key is an attribute in a relation which is used to identify each row in a
relation.
4. No. of components of a primary key value may be NULL.
5. This is called as entity integrity.
6. A NULL is an undefined value. One NULL is not equal to another NULL.

Page 62
3. Referential Integrity :
1. A referential integrity is used to implement the foreign key.
2. A foreign key is a non key attribute in the relation R and it is a primary key in
relation R2.
3. This key is called as foreign key. The value of the foreign key is either NULL or primary
key.
4. Another integrity rules that can be enforced in the relational model are the
NOT NULL and UNIQUE constraints.

NOT NULL: its not supports empty value in attribute. The NOT NULL constraint
can be placed a value for that column.

UNIQUE: The UNIQUE constraint is used to not support duplicate values exists
column.

 What is a Relational Algebra


1. The relational algebra is a notional language with operations
2. That work on one or more relations to define another relation without changing the
original relation.
3. The both the operands and the results are relations;
4. The output from one operation can become the input to another operation.
5. This allows expressions to be nested in the relational algebra. This property is called
closure.
6. Relational algebra is an abstract language,
7. It means that the queries formulated in relational algebra are not intended to be
executed on a computer.
8. Relational algebra consists of group of relational operators that can be used to
manipulate relations to obtain a desired result.
9. Knowledge about relational algebra allows us to understand query execution and
optimization in relational database management system.

 Explain Relational Algebra Operations (Relational Set operators)


Operations in relational algebra can be classified into set operation and database
operations.
1. Unary and Binary Operations
1. Unary operation involves one operand,
a. selection
b. Projection
2. Binary operation involves two operands.
a. Union operation
b. Difference operation
c. Cartesian product operation
d. Intersection operation
e. Division operation
f. Join operations operation
2. Relational algebra operations
3. Set Operations Database operations

Three main database operations are SELECTION, PROJECTION, and JOIN.

1. a. Selection Operation
1. The selection operation works on a single relation R
2. And it defines a relation that contains only those tuples of R
3. That satisfies the specified condition (Predicate).

Page 63
4. Selection operation can be considered as row wise filtering.
5. This is pictorially represented also known as RESTRICT
6. It creates values for all rows found in a table that satisfy a given
condition.
7. Select can be used to list all of the row values, or it can create only those
row values
8. That matches a specified criterion.
9. Select create a horizontal subset of a table.
Syntax
σ Predicate (R).

ex: σ subject = "database"(Books)

P_CODE P_DESCRIPT PRICE


123456 Flash light 5.26
123457 Lamp 25.15
123458 Box Fan 10.99
123459 9v battery 1.92
123455 100W bulb 1.47
123454 Powerdrill 34.99

SELECT only PRICE less than Rs. 2.00 yields P_CODE P_DESCRIPT PRICE
123459 9v battery 1.92
123455 100W bulb 1.47
1. b. Projection Operation
2. The projection operation works on a single relation R
3. And defines a relation that contains a vertical subject of R,
4. And extracting the values of specified attributes and elimination duplicates.
5. The projection operation can be considered as column wise filtering.
It’s creates all values for selected attributes. In other words, project creates a
vertical subset of a table.

Syntax
a1,a2,......an (R).
[Where a1, a2, . . . . . . an are attributes and R stands for relation To illustrate
projection operation consider the relation STAFF, with the attributes Staff number, Name,
Gender, Date of birth, and Salary.]
SALES:

P_CODE P_DESCRIPT PRICE PRICE


123456 Flashlight 5.26 5.26
PROJECT PRICE yields
123457 Lamp 25.15 25.15
123458 Box Fan 10.99 10.99
123459 9v battery 1.92 1.92
Select price from sales;
123455 100w bulb 1.47 1.47
123454 Power drill 34.99 34.99

PRICE
P_DESCRIPT
Flashlight 5.26
PROJECT P_DESCRIPTand PRICE yields Lamp 25.15
Box Fan 10.99
9v battery 1.92
100w bulb 1.47
Select components, price, from sales; Power drill 34.99

2. a. Union Operation

Page 64
1. Union combines all rows from two tables, excluding (without) duplicate
rows.
2. The tables must have the same attribute characteristics to be used in the
UNION.
Or
3. The union of two relations R and S defines a relation
4. That contains all the tuples of R or S or both R and S,
5. Duplicate tuples being eliminated.

Relational Algebra Expression


The union of two relations R and S are denoted by R ∪ S. R ∪ S is pictorially represented

P_CODE P_DESCRIPT PRICE


123456 Flashlight 5.26 PRICE
123457 Lamp 25.15 P_CODE P_DESCRIPT
UNION
123458 Box Fan 10.99 345678 Microwave 160.00
123459 9v battery 1.92 345679 Dishwasher 500.00
123455 100w bulb 1.47
123454 Powerdrill 34.99

P_CODE P_CESCRIPT PRICE


123456 Flashlight 5.26
123457 Lamp 25.15
OUTPUT 123458 Box Fan 10.99
123459 9v battery 1.92
123455 100w bulb 1.47
123454 Powerdrill 34.99
345678 Microwave 160
345679 Dishwasher 500

b. Difference Operation
1. The set difference operation defines a relation consisting of the tuples
2. That is in relation R but not in S.
3. It create all rows in one table that are not found in the other table
4. That is, it subtracts one table from the other.
5. The effect of a DIFFERENCE is shown in follow in example.
6. That subtracting the first table from the second table is not the same as
subtracting the second table from the first table.

Relational Algebra Expression


The difference between two relations R and S is denoted by R–S.
F_NAME F_Name F_Name
Yields
Krishna Sree Krishna
sree DIFFERENCE Kumar Sridhar
Sridhar Hari Sheshu
Sheshu Kiran
Hari

c. Cartesian Product Operation


1. The Cartesian product operation defines a relation
2. That is the concatenation of every tuples of relation R with every tuples of relation
S.

Page 65
3. The result of Cartesian product contains all attributes from both relations R and S.
4. It’s creates all possible pairs of rows from two tables
5. It is also known as the Cartesian product.
6. For example one table has six rows and the other table has three rows,
7. The PRODUCT creates a list composed of 6 X 3 = 18 rows.

Relational Algebra Symbol for Cartesian product:


The Cartesian product between the two relations R and S is denoted by R ×
S.

Example
If there are 5 tuples in relation “R” and 2 tuples in relation “S” then the number of tuples in R × S is 5 ∗ 2 = 10.
R S
R S
A 1 A 1
A 2
A 3
B 2 Note:
B 1
No. of tuples in R X S = 2 x 3
3 B 2 =6
B 3

d. Intersection Operation
1. The set of all tuples that are in both R and S.
2. Intersect create only the rows that appear in both tables.
3. As we true in the case of UNION table must be union-compatible to produce
(create) valid results.
4. For example, you cannot use INTERSECT if one of the attributes is numeric
and one is character – based.

F_NAME F_Name F_Name


Yields
Kumar INTERSECT
Sree Sree
Sree Lal Hari
Kiran Hari
Sheshu Krishna
Hari

e. Division Operation
1. The division of the relation R by the relation S is denoted by R ÷ S,
where R ÷ S is given by: R ÷S = ΠR−−S(r) − ΠR−−S((ΠR−−S(r) × s)
− r)

CODE LOC
A 5
A 9 CODE
A yields
A 4 DIVIDE LOC
B
B 5
B 3
C 6
D 7
D 8
E 8

f. Join Operations
1. Join operation combines two relations to create a new relation.
2. The tables should be joined based on a common column.
3. The common column should be compatible in terms of domain.

Page 66
4. It allows information to be combined from two or more tables.
5. Join is the real power behind the relation database, allowing the use of
independent table linked by common attributes (Characteristics).

There are different types of join operations

 Types of Join Operation


There are different types of join operations

Natural Join

The natural join performs an equi join of the two relations R and S over all common
attributes. One occurrence of each common attribute is eliminated from the result. In
other words a natural join will remove duplicate attribute.

In most systems a natural join will require that the attributes have the same name to
identity the attributes to be used in the join. This may require a renaming mechanism.
Even if the attributes do not have same name, we can perform the natural join provided
that the attributes should be of same domain.

Input: Two relations (tables) R and S

Notation: R __ S

Purpose: Relate rows from second table and

– Enforce equality on all column attributes

– Eliminate one copy of common attribute

* Short hand for _ L(R × S):

– L is the union of all attributes from R and S with duplicate removed

– P equates all attributes common to R and S

Page 67
Equi Join

A special case of condition joins where the condition C contains only

equality.

Example of Equi Join

Given the two relations STAFF and DEPT, produce a list of staff and the

departments they work in.

Page 68
Theta Join

A conditional join in which we impose condition other than equality condition. If equality
condition is imposed then theta join become equi join. The symbol θ stands for the
comparison operator which could be >, <, >=, <=.

Expression of Theta Join

σθ(R × S)

Illustration of Theta Join

To illustrate theta join consider two relations FRIENDS and OTHERS with

the attributes Name and age.

Outer Join

In outer join, matched pairs are retained unmatched values in other tables are

left null.
Types of Outer Join

The pictorial representation of the left and the right outer join of two relations

R and S are :

1. Left Outer Join. Left outer joins is a join in which tuples from R that do

not have matching values in the common column of S are also included in

the result relation.

2. Right Outer Join. Right outer join is a join in which tuples from S that do not have
matching values in the common column of R are also included in the result relation.

3. Full Outer Join. Full outer join is a join in which tuples from R that do

not have matching values in the common columns of S still appear and

tuples in S that do not have matching values in the common columns of

Page 69
R still appear in the resulting relation.

Example of Full Outer Left Outer and Right Outer Join

Consider two relations PEOPLE and MENU determine the full outer, left

outer, and right outer join.

Page 70
Semi-Join

The semi-join of a relation R, defined over the set of attributes A, by relation S, defined
over the set of attributes B, is the subset of the tuples of R that participate in the join of
R with S. The advantage of semi-join is that it decreases the number of tuples that need
to be handled to form the join. In centralized database system, this is important because
it usually results in a decreased number of secondary storage accesses by making better
use of the memory. It is even more important in distributed databases, since it usually
reduces the amount of data that needs to be transmitted between sites in order to
evaluate a query.

Expression for Semi-Join R _F S =_ A(R _F S) where F is the predicate.

Page 71
 ADVANTAGES OF RELATIONAL ALGEBRA:

1. The relational algebra has solid mathematical background.


2. It is the basis of many interesting developments and theorems.
3. If we have two expressions for the same operation
4. And if the expressions are proved to be equivalent
5. Then a query optimizer can automatically substitute the more efficient form.
6. The relational algebra is a high level language
7. It talks in terms of properties of sets of tuples and not in terms of for-loops.
Limitations of Relational Algebra
1. The relational algebra cannot do arithmetic.
2. For example, if we want to know the price of 10 l of petrol, by assuming a 10%
increase in the price of the petrol,
3. It cannot be done using relational algebra.
4. The relational algebra cannot sort or print results in various formats.
5. For example we want to arrange the product name in the increasing order of their
price.
6. It cannot be done using relational algebra.
7. Relational algebra cannot perform aggregates.
8. For example we want to know how many staff is working in a particular
department.
9. This query cannot be performed using relational algebra.
10.The relational algebra cannot modify the database.
11.For example we want to increase the salary of all employees by 10%.
12.This cannot be done using relational algebra.
13.The relational algebra cannot compute “transitive closure.”
14.In order to understand the term transitive closure consider the relation
RELATIONSHIP, which describes the relationship between persons.
15.Consider the query, Find all direct and indirect relatives of Gopal?
16.It is not possible to express such kind of query in relational algebra.
17.Here transitive means, if the person A is related to the person B and if the person
B is related to the person C means indirectly the person A is related to the person
C.
18.But relational algebra cannot express the transitive closure

Page 72
Relationship
Person1 Person2 Relationship
Hari Krishna Lakshmi Mother
Sree harilal Nandalal Father
Hari Raj Brother
Sree Sravanthi Husband

 Relational Calculus
1. The purpose of relational calculus is to provide a formal basis for defining
declarative query languages appropriate for relational databases.
2. Relational Calculus comes in two flavors
a. Tuple Relational Calculus (TRC) and
b. Domain Relational Calculus (DRC).
3. The basic difference between relational algebra and relational calculus is that the
former gives the procedure of how to evaluate the query whereas the latter gives
only the query without giving the procedure of how to evaluate the query:
a. The variable in tuple relational calculus formulae range over tuples.
b. The variable in domain relational calculus formulae range over individual
values in the domains of the attributes of the relations.
c. Relational calculus is nonoperational, and users define queries in terms of
what they want, not in terms of how to compute it. (Declarativeness.)

Relational Calculus and Relational Algebra:


The major difference between relational calculus and relational algebra is
1. A relational calculus query specifies what information is retrieved
2. A relational algebra query specifies how information is retrieved
 Tuple Relational Calculus
1. Tuple relational calculus is a logical language with variables ranging over
tuples.
2. The general form of tuple relational calculus is given by:

{<tuple variable list> | <conditions>} {t | COND(t)}

3. Here t is the tuple variable, which stands for tuples of relation.


4. COND (t) is a formula that describes t.
5. The meaning of the earlier expression is to return all tuples T that satisfy the
condition COND:
a. T/R(T)} means return all tuples T such that T is a tuple in relation R.
b. For example, {T.name/FACULTY(T)} means return all the names of
faculty in the relation FACULTY.
c. {T.name/ FACULTY(T) AND T.deptid=‘EEE’} means return the value of
the name of the faculty who are working in EEE department.

 Domain relational calculus

1. Domain Relational Calculus is the second form of relational calculus.


2. It uses domain values or variables that take on values from an attribute
domain, rather than values for an entire tuple.
3. However, the DRC is closely related to TRC.
4. DRC acts as a theoretical basis for Query by Example.
5. Queries in the DRC have the form:

6. The X1, ..., Xn are a list of domain variables.


7. The condition is a logical expression of classical first-order logic.
8. The language QBE is based on DOMAIN RELATIONAL CALCULUS.
9. It differs from tuple calculus in the type of variables used.

Page 73
10.An expression is of the form:

{ X1,X2…………..Xn | COND(X1,X2…….Xn, Xn+1,……..Xm+n) }

Example:

Retrieve the birth dates and address of the employee whose name is ‘John
B.Smith’

{ uv | ( (q) (r) (s) (t) (w) (x) (y) (z) (EMPLOYEE(qrstuvwxyz) and q = ‘John’ and r =
‘B’ and s = ‘Smith’ ) }
or
{ uv | EMPLOYEE(‘John’, ‘Smith’, ‘B’, t,u,v,w,x,y,z ) }

 Explain QBE
1. QBE stands for Query By Example. QBE uses a terminal display with attribute
names as table headings for queries.
2. It is very easy to list the entire schema, simply by scrolling information on the
screen.
3. QBE was developed originally by IBM in the 1970s to help users in their retrieval of
data from a database.
4. QBE represents a visual approach for accessing data in a database through the
use of query templates.
5. QBE can be considered as GUI (Graphical User Interface) based on domain
calculus.
6. QBE allows users to key in their input requests by filling in empty tables on the
screen, and the system will also display its response in tabular form.
7. QBE is user-friendly because the users are not required to formulate sentences for
query requests with rigid query-language syntax.
8. In QBE the request is entered in the form of tables whose skeletons are initially
constructed by QBE.
9. Some of the QBE query template examples:

Example 1. Projection operation

1. In this template P. implies “Print.” The meaning is: Print the PLAYER ADDRESS who
belong to the country INDIA.
2. To make a projection only put P. in any column of the projection.
3. QBE will enforce uniqueness of projections automatically.

 Relational algebra Vs Relational Calculus

1. The relational algebra and the relational calculus have the same expressive
power;
2. i.e. all queries that can be formulated using relational algebra can also be
formulated using the relational calculus and vice versa.
3. E. F. Codd first proved this in 1972.
4. This proof is based on an algorithm by which an arbitrary expression of the
relational calculus can be reduced to a semantically equivalent expression of
relational algebra.
5. It is sometimes said that languages based on the relational calculus are "higher
level" or "more declarative"
6. Than languages based on relational algebra because the algebra (partially)
specifies the order of operations while the calculus leaves it to a compiler or
interpreter to determine the most efficient order of evaluation.

Page 74
UNIT IV

 Introduction to ORACLE

1. Oracle is a Relational Database Management System (RDBMS).


2. Oracle being RDBMS, stored data in tables called relations.
3. These relations are data can be representation in two-dimensional
4. The rows are called tuples it represents records
5. The columns called attributes it represents pieces of information contained in the
record.

 Benefits of Standardized Relational Language


The main advantages of standardized language are given below.
1. Reduced training cost
2. Enhanced productivity
3. Application portability
a. Application portability means applications can be moved from machine to
machine when each machine uses SQL.
4. Application longevity:
a. A standard language tends to remain so for a long time,
b. There will be little pressure to rewrite old applications.
5. Reduced dependence on a single vendor SQL language development is given in a
nutshell below:
a. In 1970 E.F. Codd of IBM released a paper “A relational model of data for
large shared data banks.”
b. IBM started the project System R to demonstrate (show) the feasibility
(possibility) of implementing the relational model in a database
management system.
c. The language used in system R project was SEQUEL.
d. SEQUEL was renamed SQL during the project, which took place from 1974 to
1979.
6. The first commercial RDBMS from IBM was SQL/DS. It was available in 1981.
7. Oracle from relational software (now Oracle Corporation) was on the market
before SQL/DS, i.e., 1979.
8. Other products included INGRES from relational Technology Sybase from Sybase,
Inc. (1986), DG/SQL from Data General Corporation (1984).

 DATA TYPES:
It specified which type data and size can be stored in a specific field or it specified the
field size and type in a file.
 Numeric:
1. The number data type is used to store zero, positive and negative values.
2. User can specify a fixed point number using the following form
3. There are different types of data types
a. Number(L,D)
b. Integer
c. Smallint
d. Decimal(l,d)

a. Number(L,D):
1. The declaration Number (7,2) indicates numbers
2. That will be stored with two decimal places and may be up to seven digits
long,

Page 75
3. It includes the sign and the decimal place. Examples: 12.32, -134.23
4. L means length, D means Decimal numbers

Syntax:
Column-name data-type (L);
or
Column-name data-type (L, D)
Ex:
Sno number (3);
or
sno number(7,2);

b. Integer:
1. It may be abbreviated as INT
2. Integers are (whole) counting numbers,
3. So they cannot be used to store numbers that require decimal places
Syntax:
Column-name data-type (L);
Ex:
Sno integer (3);

c. Smallint:
1. Like Integer, but limited to integer values up to six digits.
2. If your integer values are relatively small, use smallint instead of Int.

Syntax:
Column-name data-type (L);
Ex:
Sno smallint(3);

d. Decimal(l,d)
1. Like the number specification, but the storage length is a minimum
specification.
2. That is, greater lengths are acceptable, but smller ones are not.
3. Decmal (9,2), decimal(9), and decimal are all acceptable.

Syntax:
Column-name data-type (L);
or
Column-name data-type (L, D)
Ex:
Sno decimal (3);
or
sno decimal (7,2);

 Character
The character data type is used to store character ( alphanumeric) data. This can
be fixed length or variable length
1. Char(L)
2. Varchar(L) or Varchar2(L)

a. Char(L)

1. Fixed-length character data fro up to 255 characters.


2. If you store strings that are not as long as the char parameter value
3. The remaining spaces are left unused.
4. Therefore, if you specify char (25), each stored as 25 characters

Page 76
Syntax:
Column-name data-type (L);
Ex:
city char(18);

b. Varchar (L) or Varchar2(L)

1. Variable-length character data


2. The designation varchar2 (25) will let you store characters long.
3. However, varchar will not leave unused spaces.
4. Oracle automatically converts varchar to varchar2

Syntax:
Column-name data-type (L);
Ex:
Sna varchar(25);

Syntax:
Column-name data-type (L);
Ex:
Sna varchar2(25);

c. Date
1. The Date data type is used to store date and time information.
2. For each date value the information stored is, Century, Year, Month, Day,
Hour, Minute, Second
3. The default format of the date data type is ‘DD-MON-YY’.
4. The format can be changed with NLS_DATE_FORMAT command.

Syntax:
Column-name DATE

Ex: date_of_birth date

Long:
The variable length character data up to 4GB

BLOB:
BLOB means Binary Large Object. It store Binary data up to 4GB

CLOB:
Character Large Object. Single byte character up to 4GB

Long raw:
It uses raw binary data up to 2000 bytes

BFILE:
A BFILE can store up to 4GB of data. The BFILE data type stores unstructured
binary data in operating system files outside the database.

 Commands in SQL
SQL commands can be classified in to different types:

1. Data Definition Language commands (DDL)


2. Data Manipulation Language commands (DML)
3. Transaction Control Language (TCL)

Page 77
4. Data Control Language commands (DCL)

Command Category
CREATE
ALTER
DROP Data Definition Language (DDL)
RENAME
TRUNCATE
INSERT
UPDATE DataManipulation Language
DELETE (DML)
SELECT
COMMIT
Transaction Control Language
ROLLBACK
(TCL)
SAVEPOINT
GRANT
Data Control Language (DCL)
REVOKE

 DDL (DATA DEFINITION LANGUAGE) COMMANDS:


The set of commands are used to define a database including creating.
Altering, dropping and truncate the table command are
1. CREATE: To create a table
2. ALTER: To the table structure
3. DROP: To Delete a table
4. TRUNCATE: To delete all the records.

 CREATE TABLE STRUCTURE

1. The CREATE TABLE command defines each column (Attribute) of the table uniquely

2. Each column has a name, data type and size (column width).
3. Each table column definition is separated from the other by a COMMA.
4. Finally the SQL statement is terminated with a semi colon.

Syntax: CREATE TABLE tablename


(column1 datatype constraint ,
column2 datatype constraint ,
column3 datatype constraint );

SQL> create table student (sno int,sname varchar2(10),sadd


varchar2(10));

/* SQL> desc student;


Name Null? Type
----------------------------------------- -------- ----------------------------
SNO NUMBER(38)
SNAME VARCHAR2(10)
SADD VARCHAR2(10) */

ALTER TABLE STRUCTURE

1. To change (alter) table structure by changing attribute character and by adding


columns.
2. All changes in the table structure are made by using the ALTER TABLE command.
3. ADD, MODIFY and DROP keywords
4. That produces the specific change user want to create

Page 78
5. Use ADD to add a column, MODIFY to change column characteristics and DROP
to delete a column from a table
6. Most RDBMSs do not allow you to delete a column (unless the column does not
contain any value).
7. The alter table command can also be used to add table constraints.

Syntax:
Alter table table-name ADD constraint [ADD constraint];

 ADDING A COLUMN:

Alter an existing table by adding one or more columns

Syntax:
Alter table table-name add (column name <data type>
(size));

SQL> alter table student add(totalmarks number(3));

Table altered.

/* SQL> desc student;


Name Null? Type
----------------------------------------- -------- ----------------------------
SNO NUMBER(3,1)
SNAME VARCHAR2(15)
SADD VARCHAR2(10)
TOTALMARKS NUMBER(3) */

 CHANGING (MODIFY) A COLUMNS DATA TYPE:

Some RDBMSs such as oracle, do not let you change data type unless the column
to be changed is empty.
Syntax: Alter table table-name add (column name <data type> (size));

SQL> alter table student modify(sno decimal(3,1));

Table altered.

/* SQL> desc student;


Name Null? Type
----------------------------------------- -------- ----------------------------
SNO NUMBER (3, 1)
SNAME VARCHAR2 (10)
SADD VARCHAR2 (10) */

 CHANGING (MODIFY) A COLUMN’S DATA CHARACTERISTICS

1. The column to be changed already contains data, make changes in the


column’s characteristics if those changes do not alter the data type. Increase
the width of the column size.

Syntax: Alter table table-name modify (column-name data-type (new


size));

Page 79
SQL> alter table student modify(sname varchar2(15));

Table altered.

/* SQL> desc student;


Name Null? Type
----------------------------------------- -------- ----------------------------
SNO NUMBER(3,1) SNAME VARCHAR2(15) SADD
VARCHAR2(10) */

 DROPPING A COLUMN: (REMOVE A COLUMN AND REMOVE TABLE)

1. User wants to delete a table column by using drop


2. User also remove the table using drop command
3. Some RDBMSs impose restrictions on attribute deletion.
4. The attribute that are involved in foreign key relationships not delete an
attribute of table that contains only that one attribute.

Syntax: Alter table-table-name drop column column-name;

Ex: SQL> alter table student drop column sadd;

Syntax: drop table tablename;

Ex: SQL> drop table stu;

o RESTRICTION ON ALTER COMMAND:


The alter table command cannot perform the following
1. Change the name of the table
2. Change the name of the column
3. Decrease the size of a column it table data exists.

 RENAMING TABLES:
Changing the table name from old to new name is called rename a table.

Syn: Rename oldtablename to newtablenaem;

SQL> rename student to stu;

 TRUNCATE:
Truncate command is used to delete to all the records permanently from the
database.

Syn: Truncate table tablename;

Eg:SQL> truncate tale stu;


 DML (Data manipulation language): DATA MANIPULATION COMMANDS

The basic SQL data manipulation commands

1. INSERT: To insert records into a table


2. UPDATE: To update records in a table.
3. SELECT: To display the all rows and columns
4. DELETE: Delete some or all the records in a table

Page 80
 INSERT (ADDING TABLE ROWS)

SQL requires the use of the INSERT command to enter data into a table.

Syn: INSERT INTO tablename VALUES( value1, value2, value3……);

SQL>insert into student values(2,'ram','gnt',300); /*

The preceding data entry lines:

1. The row contents are entered between parentheses. Note that the first
character after value is a parenthesis and that the last character in the
command sequence is also a parenthesis.
2. Character (String) and date value must be entered between apostrophes (‘).
3. Numerical entries are not enclosed in apostrophes.
4. Attribute entries are separated by commas.
5. A value is required for each column in the table.

This version of INSERT commands adds one table row at a time. */

 INSERTING ROWS WITH OPTIONAL ATTRIBUTES

1. The attributes that have required values, by listing the attribute names inside
parentheses after the table name.

Syntax: Insert into


tablename(cumnname,columname…..)values(value1,value2….);

SQL> insert into student (sno,sname)values(9,'saran');

 UPPDATE (UPDATING TABLE ROWS)

1. The update command to modify data in a table.

Syntax:
UPDATE TABLENAME
SET COLUMNNAME=EXPRESSION, COLUMNAME=EXPRESSION …,
WHERE CONDITION;
Ex:
SQL> UPDATE student SET totalmarks=499 WHERE sname='sam';

1. UPDATE command has not include the WHERE condition, will apply the changes
to all rows in the specified table.

 SELECT ( LISTING TABLE ROWS)

1. The SELECT command is used to list the contents of a table.

Syntax:
SELECT COLUMNLIST FROM Table name; or Select * from Table name; The
COLUMNLIST represents one or more attributes, separated by commas.
Ex:
SELECT ENO,ENAME FROM EMP; or SELECT * FROM EMP; The *

specifies that all columns appears in the EMP table.

Page 81
 DELETE ( DELETING TABLE ROWS )

1. It is easy to delete a table row using DELETE statement.

Syntax:
DELETE FROM TABLE_NAME
WHERE CONDITION;
Ex:
SQL> delete from student where sno=2;

1. DELETE is a set-oriented command.


2. The WHERE condition is optional.
3. Do not specify WHERE condition, all rows from the specified table will be
deleted.

 Transaction Control Language (TCL)


1. Transaction changes can be made (create) permanent to a database only if they are
committed.
2. A transaction executable statements and end explicitly with either rollback or
commit statement and implicitly.
a. Commit  This is used for save the changes in database
b. Rollback  This is used for do the undo operation
c. Savepoint  Divide the lengthy transaction into small
transactions

 COMMIT (SAVING TABLE CHANGES)

1. Any changes made to the table contents are not saved on disk until user close
the database,
2. To change the database to use the COMMIT command

Syntax: COMMIT [WORK]; Ex: COMMIT;

1. The COMMIT command permanently saves all changes-


2. Such as rows added, attributes modified, and rows delete to any table in the
database.

 ROLLBACK ( RESTORING TABLE CONTENTS )

1. ROLLBACK command work like undo command.


2. The COMMIT command to store the changes permanently in the database.
3. Restore the database to its previous condition with the ROLLBACK command.
4. To restore the data to their pre-change condition.

Syntax: ROLLBACK; Ex: ROLLBACK


 SAVE POINT:
1. Save point are like markers to divide a lengthy transaction to smaller
transactions.
2. Save points are used in conjunction (Combination) with rollback,
3. To rollback portions of the current transaction.

Syntax: savepoint <savepointname>;

Ex: savepoint s1;


/*Ex: SQL> insert into emp values(12,’manju’);
SQL>insert into emp values(13,’sai’);

Page 82
SQL>savepoint s1;
SQL> insert into emp values(14,’vani’);
SQL>insert into emp values(15,’anu’);
SQL>savepoint s2;
SQL> insert into emp values(16,’rani’);
SQL>insert into emp values(17,’vasu’);
SQL>rollback to s2;*/
1. In the above example we have to define two savepoint markers.
2. When rollback to s2 is given, whatever transactions happened after savepoint s2
will be undone.

 DCL(Data control Languge)


1. DCL commands are used to enforce (implemented) database security in a multiple
user database environment.
2. Two types of DCL commands
a. GRANT
b. REVOKE.
3. Only Database Administrator's or owner's of the database object can
provide/remove privileges (rights) on a database object.

 GRANT:
1. SQL GRANT is a command used to provide access or privileges on the database objects to
the users.

Syntax:
grant privilegesname on <object name> to <username>

 Priviliges name is all/select /execute

 Object name is table /view name

 Username is name of the user

Example: KUMAR: grant all on customers to BABA;

To view the data

Select * from username.objectname ; (select * from KUMAR.customers)

 Revoke:
1. The REVOKE command removes user access rights or privileges to the database
objects.

Syntax:
revoke privileges on <object name>from
<username> Ex: revoke all on customers from BABA;
 SELECTION:
1. To perform (create, display etc) a query we can the select command.
2. The query is a request for the information.
3. It is the most common database operation.

Syntax:
select[distinct | * | column_name[aliase],………]
From <table_nmae>[where<condition>
[Group by column list ]
[Having<condition>]

Page 83
[Order by column list];

Syntax:

Select column-name1, column-name2 ……… from table-name;

Ex:
Select sno, sname from std;

Syntax:
Select * from table-name;

Ex:
Select * from ABC;

The above example shows all the columns from the student.

 Selecting distinct rows:

1. To prevent (Check) the selection of duplicate rows, we include distinct clause in


the select command.
2. The following examples would eliminate duplicate values present in sname of the
student table.

Ex:
SQL> Select distinct sname from std;

 Select command with ‘where’ clause:


1. To select specific rows from a table we include a ‘where’ clause in the select
command.
2. It can appear only after the ‘form’ clause.
3. We can retrieve only the rows which specify ‘where’ condition.

Ex:
SQL>select *from std where sno=12;

 Select with order by clause command:


1. This clause is used to arrange rows in ascending or descending order.

SQL> Select * from std order by sname;

1. The above query will display the ascending order, which is default option.
2. To display the rows in descending order SQL>
select * from std order by sname desc;
 Group by Clause
1. Group by clause can be used to divide the rows in a table into smaller groups.
2. Group function can be used to return summary information for each group.

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

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

We can also use the group by clause to provide results for within groups.

Ex:

Page 84
To display average monthly salary bill for each type within a department

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

Rows may be pre exclude with WHERE clause, before dividing them into groups.

Ex:
To show the average salary for each JOB excluding manager. Select

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

 Having Clause
1. It works like a where clause, except that its logic is only related to the results of
group functions as opposite (different) to columns or expressions for individual
rows, which can still be selected with where clause.

Ex:
Select deptno,sum(sal) from emp group by deptno having sum(sal)>1000;

Select deptno,avg(sal) from emp group by deptno having coun(*)>3;

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


Or
Select job,avg(sal) from emp having job!=’MANAGER’ group by job;
Or
Select job,avg(sal) from emp group by job having job!=”MANAGER’;

 PROJECTION OPERATION
1. The projection operation performs column wise filtering.
2. Specific columns are selected in projection operation.

Syntax:

SELECT column-name1, column-name2, Column-name N FROM table-name;

If all the columns of the table are selected, then it cannot be considered as PROJECTION.

1. The SQL command to perform PROJECTION operation on the relation PEAKS and
the corresponding results
2. it is clear that only three columns are selected in the result.

Ex: select sno,sname from std;

 GROUP FUNCTIONS: OR AGGREGATE FUNCTIONS:


1. These functions can be used for calculations on groups of selected records.
2. Group functions can appear in select lists, GROUP BY and/or HAVING clauses. They
are,

a. Count  The no. of rows containing non-null values


b. Min  The minimum attribute value encountered in a given column
c. Max  The mximum attribute value encountered in a given column
d. Sum  The sum of all valued for given column
e. Avg  The arithmetic mean (average) from specified column

a. COUNT:
1. The count function is used to tally (count) the number of non-null values of an
attribute.

Page 85
2. COUNT can be used in conjunction (combination) with the distinct clause.

SQL> SELECT COUNT (EMPNO) FROM EMP;

/* COUNT(EMPNO)
------------
14 */

SQL> SELECT COUNT (*) FROM EMP WHERE SAL < =1500;

/* COUNT(*)
--------
7 */
Count (*) returns the number of total rows returned by the query, including the
rows that contain nulls.

b. MAX:
1. The aggregate function Max (column) can be used only in the column list of a select
statement.
2. It returns a minimum value of a expression.

SQL> SELECT MAX (SAL) FROM EMP;

c. MIN:
1. The aggregate function Min (column) can be used only in the column list of a select
statement.
2. It returns min value of a given expression

SQL> SELECT MIN (SAL) FROM EMP;


/*
MIN(SAL)
--------
800 */
d. SUM:
1. The sum function computes the total sum fro any specified attribute, using whatever
condition.

SQL> SELECT SUM (SAL) FROM EMP;

e. AVG:
The avg function format is similar to that of min and max. it returns an average
value of given column name

SQL> SELECT AVG(SAL) FROM EMP

 SINGLE ROW FUNCTIONS:

1. The single row functions can appear in a select command and can also be
included in where class.
2. The single row functions classified as
a. Date ,
b. Numeric
c. Character
d. Mislenious

Page 86
3. The examples which are not based on a particular table use the system table
‘dual’.
4. It is a table which is automatically created by oracle along with the data
dictionary.
5. DUAL is a table owned by SYS.
6. SYS owns the data dictionary, and DUAL is part of the data dictionary.
7. Dual is a small oracle worktable, which consists of only one row and one
column
8. It contains the value X in that column.
9. Besides arithmetic calculations, is also supports date retrieval and it’s
formatting.

 DATE FUNCTIONS: -

They operate on date values producing O/P which also belongs to date,
Data type except for months between date function which returns a number.

1. Add – months: It returns a date data type as a result format add-months date,
number.

Eg: - Select add_months ('27-Aug-04',2) from dual;

/* ADD_MONTH
---------
27-OCT-04 */

2. Months-between:
a. It returns a number date type as a result. Formate: - moths-between date1,
date2;

Eg: - SQL> SELECT MONTHS_BETWEEN ('27 -AUG-85', '06-OCT-83') FROM


DUAL;

/* MONTHS_BETWEEN('27-AUG-85','06-OCT-83')
---------------------------------------
22.68 */

3. System date: It returns the system date;

Eg: -
SQL> SELECT SYSDATE FROM DUAL;

/* SYSDATE
---------
24-SEP-10*/

 NUMERIC FUNCTIONS: -
1. Numeric functions accept numeric inputs and returns numeric values as the result.
2. The values that numeric functions return or accurate upto 38 decimal digits.
3. Numeric functions are:

Abs( ): - It returns absolute value

Page 87
SQL> Select abs(-12.3467) from dual ;

/* ABS(-12.3467)
-------------
12.35 */

Power ( ): - It returns the power value to the base value.

SQL> Select power (3,4) from dual;

/* POWER(3,4)
----------
81 */

Trunc ( ): - It returns truncated values

SELECT TRUNC(12.3456,2) FROM DUAL;

/* TRUNC(12.3456,2)
----------------
12.34 */

Round ( ): - It returns a number which is rounded upto some decimal points.

SQL> SELECT ROUND (12.3456,2) FROM DUAL;

/* ROUND(12.3456,2)
----------------
12.35*/

Floor ( ): - It returns the previous value.

SQL> SELECT FLOOR (68.78) FROM DUAL;

/* FLOOR(68.78)
------------
68 */

Ceil ( ): - It returns next value.

SQL> SELECT CEIL (68.78) FROM DUAL;

/* CEIL(68.78)
-----------
69 */
 CHARACTER FUNCTIONS: -
1. Characters functions accepts character input it returns either character value or
number some of these functions are

initcap ( ): - It returns first letter as a capital letter of the string.

Eg: - Select initcap ('ram') from dual

/* INI
---
Ram */

Page 88
Concat( ): - It combines two strings

SQL> Select concat('good', 'day') from dual;

/* CONCAT(
-------
goodday */

Upper ( ): - It convert all lower case letters into upper case.

SQL> Select upper ('good') from dual;

/* UPPE
----
GOOD */

Lower ( ): - It converts all the uppercase letters into lower case.

SQL> SELECT LOWER ('RAM') FROM DUAL;

/* LOW
---
Ram */

Lpad ( ): - It adds the string to the left side of the string1

Eg: - Select Lpad (‘good’,12,’day’) from dual;

/* LPAD('GOOD',
------------
DAYDAYDAGOOD */

Rpad ( ): - It add the string2 to the right side of the string1.


Eg: - Select Rpad (‘good’,12,’day’) from dual;

/* RPAD('GOOD',
------------
Gooddaydayda */

Ltrim ( ): - It removes left side spaces of the string

Eg: - select ltrim(‘ Raj’) from dual;

Rtrim ( ): - It removes right side spaces of the string

Eg: - select rtrim ( ‘sai ‘) from dual;

/* RTR
---
Sai */

Char ( ): - It returns a character according to the ASCII value

Eg: - select chr(65) from dual;

/* C

Page 89
----
A */

 MISLENIOUS FUNCTIONS:

Greatest ( ): - It returns the highest value

Eg: - select greatest (100,200,300) from dual;

/* GREATEST(100,200,300)
---------------------
300 */

Least ( ): - It returns least value;

Eg: - Select least(100, 200) from dual;

/* LEAST(100,200)
--------------
100 */

User: - It returns user name;

SQL> Select user from dual;

/* USER
-----------
SCOTT */

UID( ): - It return user ID which is stored in server.

SQL> Select UID from dual;

/* UID
----------
66 */

Page 90
 EXPLAIN DIFFERENT TYPES OF CONSTRAINTS.
1. Constraint is a rule that can be applied on a table or a column of a table.
2. Constraints can also be used to prevent (check) mistakes in data entry.
3. Constraints can be categorized into following,

1. Domain Integrity Constraints


a. Not null b. Check

2. Entity Integrity Constraints.


a. Primary key b. Unique

3. Referential Constraint
a. Foreign key

 Domain Integrity constraints


1. Not Null:
1. By default all columns in a table allow NULL values.
2. When a NOT NULL constraint is enforced on a column in a table,
3. It will not allow NULL values into that column.

Syntax:
Create table <table name> (<column name> <data type>
constraint <constraint name> NOT NULL);
Example:
Create table tab (no number(3) constraint C NOT NULL);

2. Check constraint
1. These are rules govern logical expressions or Boolean expressions.

Syntax:
Create table <table name> (<column name> <data type>
constraint <constraint name> check
(column name with condition));
Example:

Ex1: Create table stud (marks number (5) constraint c1 check (marks>35));

Ex2: alter table emp add constraint ck_emp check (sal + comm <10000);

 Entity Integrity Constraints


1. Unique Constraint:
1. This constraint allows only unique values to be stored in the column.
2. Oracle rejects duplication of records when the unique key constraint is used.
3. It can also allow NULL values. Since two NULL values are not allowed because it
is duplicate.

Syntax:
Create table <table name> (<column name> <data type>
constraint <constraint name> unique
(column name));
Ex:
Create table JDC (sno number(10) , sname varchar2(10), sphno number(15)
unique);

2. Primary key constraint:


1. It avoids duplication of rows and does not allow NULL values.
2. Primary key is the combination of NOT NULL and UNIQUE.

Page 91
3. It is also used to set the relations between the tables.
4.
Syntax:
Create table <table name> (<column name> <data type>
constraint <constraint name> primary key);
Ex:
Create table student(sno number(10) constraint P primary key, sname
varchar2(10));

3. Default constraint:
1. Its read automatically value when user not enter value

Syntax:
Create table <table name> (<Column name> <data
type> <Default> <value>)
Ex:
CREATE TABLE Persons (id number (3), name varchar2 (20) default
‘abhyudaya’);
 Referential Integrity Constraints
1. Foreign key Constraint
1. The referential integrity constraints enforce (impose) the relationship between the
tables
2. Tables are used primary key
3. It helps in creating a parent child relation ships between the tables.
4. A referential integrity constraint assigns a column as a foreign key.
5. Child table primary key is called foreign key
6. Parent table primary key is called referenced key is called parent table.

Syntax:
Create table <table name> (<column name> <data type>,
constraint <constraint name> references
<table name> (column name));
Ex:
Create table emp1 (eno number(3) constraint P1 primary key,
ename varchar2(20));

Create table dept (eno number(3), dname varchar2(20));

Now connecting the dept and emp using foreign key

Create table dept1(eno number(3) references emp1(eno),


dname varchar2(20));

 DELETING, ADDING A CONSTRAINT IN TABLES


1. The user can disable or remove or enable constraints attached to the column
with the help of alter command.

Syntax:
Alter table <table name> disable|enable constraint <constraint name>;

Alter table <table name> modify (<column name> <data type>


constraint <constraint name> <constrain type>);
Removing a Constraint:
1. To remove constraint attached to a column name alter with drop constraint
is used.

Syntax:

Page 92
Alter table <table name> drop constraint <constraint name>;

 EXPLAIN RELATIONAL SET OPERATORS.


1. Set operators combine the results of two queries into a single one.
2. Queries containing set operators are called compound queries.
3. The set operators are:
a. Union
b. Union all
c. Intersect
d. Minus
a. Union:
1. The union operator merges the output of two or more queries into a single set of
rows and columns.
2. The following statement combines the results with the union operator,
3. It eliminates duplicate selected rows.

Syntax:
Select query
Union
Select query;
Ex:
1. SQL>select empno,ename from emp union select no, name from std;
2. SQL> select *from emp union select *from std;

b. UNION ALL:

The UNION ALL operator written all rows selected by either query including
duplicates.

Syntax:
Select query
Union all
Select query;
Ex:

Select *from emp UNION ALL select *from std;


c. Intersect:
The intersect operator writtens only rows that are common to both the queries.

Syntax:
Select query
intersect
select query;

Ex:
SQL> select eno,ename from emp intersect select sno,sname from std;
Minus:
Minus operator writtens all distinct rows selected only by the first query and not by the
second.

Syntax:
Select query
Minus
Select query;

Page 93
Ex:
SQL> select eno,ename from emp minus select sno, sname from std;

 EXPLAIN JOINS.
1. The purpose of join is to combine the data spread across the tables.
2. There are basically three types.
a. Simple join,
b. Self join,
c. Outer join.

 Simple Join:
1. It retrieves rows from two tables having a common column and further
2. It classified into
a. Equi join and
b. Non equi join.

a. Equi Join:
A join which is based on equality is called an equi join.

Ex:

Select e. ename, e.job, d.deptno, d.dname, d.loc From emp e , dept d


Where e.deptno=d.deptno;

/* To display employee name, job, department no, department name and


location in which the employees work. */

b. Non Equi Join :


A non equi join specifies the relationship between column belonging to different
tables.

Ex:
Select ename,sal,losal,hisal,grade From emp,salgrade Where sal between losal
and hisal;

/* To display the grades of the employees based on salary. */

 Self Join (Recursive Join):


1. Joining of table to itself is known as self join.
2. An table alias is especially useful when a table must be joined to itself.

Ex:

Select e1.ename,e1.sal from emp e1,emp e2 Where e1.sal>e2.sal and


e2.ename=’CLARK’;

/* To display all employees getting salary > clark.*/

 Outer Join:
1. An outer join returns all the rows returned by simple join as well as those rows
from one table
2. That don’t match any row from the other table Using (+) operator.

Ex:

Page 94
Select ename, job, dname from emp e, dept d Where d.deptno(+)=e.deptno ;

/*To which department name , job and location from department that does
not corresponding employees*/

 EXPLAIN VIEWS OR VIRTUAL TABLES


1. A view is a virtual table based on select query.
2. The query contain column, computed column and aggregated functions from one
or more tables.
3. The tables on which the view is based are called base tables.

Syntax:

Create view <view name> as select query;


Characteristics
1. You can use the name of view any where a table name is expected in SQL
statements.
2. Views are dynamically updated. i.e. the view is recreated on demand each time is
invoked.
3. Views provide a level of security in the database because the view can restrict
users to only specified column and specified rows in a table.
4. View may also be used as the basis for reports.
5. It use little storage space.
6. It establishes physical data Independency.

1. Use processing time, each time the view is referenced.


2. It may or may not be directly updatable.

Drop View command


To delete a view from the database we use the drop view command.

Syntax:

Drop view <view name>;

Updatable Views
1. User can’t join tables in the update statement.
2. To solve that problem, users have to create update view.
3. An update view is a view that can be used to update attributes in the base table
that are used in the views.

Restrictions:

1. Group by expressions of aggregate functions can not be used in the update views.
2. User can’t use set operators.
3. Most restrictions are base on the use of joins or group operators in view.

Materialized Views
1. A materialized view is a physical copy of the base table
2. The results moved to another schema object.
3. Materialized views are also called snapshots
4. Because they are a kind of photograph of the base table.

Advantage of VIEW
1. The main advantages of view are
a. Improved security,

Page 95
b. less complexity,
c. better convenience, and
d. Customization.

1. Improved security:
a. We can restrict the user to access on the data that are appropriate for the
user.
b. Hence views provide improved security.

2. Less complexity:
a. A view can simplify queries,
b. By getting data from several tables into a single table
c. Transforming multi table queries into a single table queries.

3. Convenience:
a. A database may contain much information.
b. All the information will not be useful to the users.
c. The users are provided with only the part of the database
d. That is relevant to them rather than the entire database;
e. Hence views provide great convenience to the users.

4. Customization:
a. Views provide a method to customize the appearance of the database
b. So that the users need not see full complexity of database.
c. View creates the illusion of a simpler database customized to the needs of a
particular category of users.

Drawback of VIEW
1. If the base table is modified by adding one or more columns
2. Then the columns added will not be available in the view unless it is recreated.
3. When a view is created from the base table, it is to be noted that all the views are
not updatable.
4. Views created from multiple tables are in general not updatable when there is a
group function, a GROUP BY clause, or restriction operators.

 Explain Sub queries and Correlated sub queries

SUBQUERY: (BASIC CHARACTERSTICS OF A SUB QUERY)


1. A Sub query is a query inside a query.
2. A sub query is normally expressed inside parentheses.
3. The first query in the SQL statement is known as the outer query.
4. The query inside the SQL statement is known as the inner query .
5. The inner query is executed first.
6. The outer query of an inner query is used as the input for the outer query.
7. The entire SQL statements are sometimes referred to as a nested query.

Sub queries are 2 types.


1. single row sub queries,
2. multiple row sub queries

Single row sub queries


It returns only one row from the inner select statements.

Multiple row sub queries


It returns more then one row from the inner select statements.

Rules

Page 96
1. Enclose sub queries in parentheses.
2. Place sub queries on the right side of the comparison condition
3. The order by clause in the sub query is not needed unless you are performing top n
analysis.
4. Single row comparison operators are >,<=,<>,>=

Ex: /* Who gain more than smith salary?*/

Select ename from emp where sal > (select sal from emp where ename=’smith’);

Group functions in sub queries: (Where sub queries)

The most common type of sub query uses an inner SELECT subquery on the right
side of a WHERE comparison expression.

Ex:
SQL> select ename,sal from emp where sal>=(select avg(sal) from emp);

Having clause with sub queries:

A subquery with a HAVING clause , the HAVING clause is used to restrict the
output of a GROUP BY query by applying a conditional criteria to the grouped
rows.

Ex:
SQL> select deptno, sum(sal)
2 from emp
3 group by deptno
4 having sum(sal)>(select avg(sal) from emp);

Multiple row sub queries


It retrieves multiple rows result using the following operators
1. All
2. Any
3. In
4. not exit

1. MULTIROW SUBQUERY OPERATORS: IN, ANY AND ALL


All
1. The use of the ALL operator allows, to compares a single value with a list of
values
2. It returned by the sub-query using comparison operator other than equals.

ANY
1. ANY operator is multi row operator.
2. The ANY operator allows, to compares a single value of the list or less than
any value of the list.

SQL> Select * from staff


Where sal>=All
(Select sal from staff
Where deptna=’comp’);

IN
1. The equal to IN operator, which would be the equivalent of the ANY
operator.

Page 97
SQL> Select sal*40 from emp
where sal in (select sal from emp where sal>=50000);
/*
select ename ,sal*40 from emp 2
where sal*40>
all(select sal*40 from emp where sal in
(select sal from emp where sal>=50000));
Ex:
Find the employee who earn the same the salary as the min salary for each departments.
Select ename , sal, deptno from emp
Where sal in(select min(sal)from emp group by deptno);

To display employee who are clerk and whose salary is less than that of any clerk.
Select empno,ename,job,sal from emp
Where sal<any(select sal from emp where job=’clerk’); */
Example of NOT EXISTS Operator
1. In order to understand NOT EXISTS clause,
2. Ex: two relations EMPLOYEE and DEPENDENT.
3. Here DEPENDENT refers to those who are dependent on EMPLOYEE.
4. The attributes of EMPLOYEE relation are eid (employee ID), ename (employee
name). The attributes of the DEPENDENT relation are name (which refers to
dependent name)

5. and eid (employee ID).

Page 98
Ex:
Sql> select ename from emp
Where not exists (Select eid from dep
Where dept.eid=emp.eid);
Correlated sub query:
1. The subquery , the inner subquery executes first,
2. Its output is used by the outer query,
3. Which then executes until the last outer query executes.
4. A correlated subquery that executes once for each row in the outer query.
Embedded SQL
SQL can be used in conjunction with a general purpose programming language such as
PASCAL, C, C++, etc. The programming language is called the host language. Embedded
SQL statements are SQL statements written within application programming languages
such as C and Java
Dynamic SQL
Programs containing embedded dynamic SQL statements must be precompiled like those
containing static SQL, but unlike static SQL, the dynamic SQL statements are constructed
and prepared at run time. The SQL statement text is prepared and executed using either
the PREPARE and EXECUTE statements, or the EXECUTE IMMEDIATE statement. The
statement can also be executed with cursor operations if it is a SELECT statement

Page 99
UNIT V

PL/SQL: Introduction, Shortcoming in SQL, Structure of PL/SQL, PL/SQL Language


Elements, Data Types, Operators Precedence, Control Structure, Steps to Create a
PL/SQL, Program, Iterative Control, Cursors, Steps to create a Cursors, Procedure,
Function, Packages, Exceptions Handling, Database Triggers, Types of Triggers.

 PL/SQL (Procedural Language/Structured Query Language):


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

PL/SQL Block Structure:


1. A PL/SQL block contains 1 or more PL/SQL statements.
2. Each PL/SQL program consists of SQL and PL/SQL statements which from a PL/SQL
block.

Syntax:

Declare
Variable declaration
Begin
Process statements
Or execution statement
[Exception
Exception statement]
End;

A PL/SQL block can be divided into four sections. They are


1. Declaration section
2. Begin section
3. Exception section
4. End section

1. Declaration Section:
1. Code blocks start with a declaration section
2. In this block memory variable and other oracle objects can be declared
3. They can be used in SQL statements for data manipulation.

Example:
Declare
First_name varhcar2(10);
Num number(10);

2. Begin Section:
1. It consists of a set of SQL and PL/SQL statements
2. It describes process that has to be applied to table data.
3. Actual data manipulation, retrieval, looping and branching constructs are
specified in this section.

Page 100
3. Exception Section:
1. This section deals with handling of errors
2. That arise during execution of the data manipulation statements
3. The errors can arise due to syntax and logic.

4. End Section:
a. This makes the end of a PL/SQL block.

Example:

Declare
a number(4);
b number(4);
c number(4);
begin
a:=10;
b:=20;
c:=a+b;
dbms_output.put_line(c);
end;

/*
1. dbms_ouput: it is a package.
2. That includes a number of procedures and functions that accumulate information
in a buffer so that it can be retrieved later.
3. These functions can also be used to display message.
4. put_line: put a piece of information in the package buffer followed by an end-of-
line marker.
5. dbms_ouput.put_line(‘Hello’);*/

 PL/SQL Language Elements


There are different elements
1. Character Set
2. Lexical Units
a. Delimiters
b. Identifiers
c. Literals
d. Comments

1. Character Set
1. A PL/SQL program consists of text having specific set of characters.
2. Character set may include the following characters:
a. Alphabets, both in upper case [A–Z] and lower case [a–z]
b. Numeric digits [0–9]
c. Special characters ( ) + − * /< >= ! ∼ ˆ ; : . _ @ % , __ # $ & | { } ? [

]
d. Blank spaces, tabs, and carriage returns.

2. Lexical Units
1. A line of PL/SQL program contains groups of characters known as lexical units,
which can be classified as follows:
A. Delimiters
B. Identifiers
C. Literals
D. Comments

Page 101
A. Delimiters
a. A delimiter is a simple or compound symbol
b. That has a special meaning to PL/SQL.
c. Simple symbol consists of one character
d. Compound symbol consists of more than one character.

B. Identifiers
a. Identifiers are used in the PL/SQL programs
b. To name the PL/SQL program items
i. Like constants, variables, cursors, cursor variables, subprograms,
etc.
c. Identifiers can consists of alphabets, numerals, dollar signs, underscores,
and number signs only.
d. Any other characters like hyphens, slashes, blank spaces, etc.

C. Literals
a. A literal is an explicitly defined character, string, numeric, or Boolean
value,

D. Comments
1. Comments are used in the PL/SQL program
2. It used to improve the readability and understandability of a program.
3. A comment can appear anywhere in the program code.
4. The compiler ignores comments.
5. Generally, comments are used to describe the purpose and use of each
code segment.

Ex: /* Hello World! This is an example of multiline comments in PL/SQL */

 What are the data types available in PL/SQL?

Data type Description


Char Character value of fixed length
Varchar2 Variable length character value
Number Numeric values
Date Date values
Inherits the data type from a variable that you declared
% type
previously in database table.
It is used to declare variable to keep a single record, since a
% row record is nothing but collection of column. This is also known as
type composite data type.
Boolean data type can be used to store the values true, false or
Boolean
null.

Variable declaration
<variable name> data type [(size)] := &variable name;

 Control Structures:
The follow of control statements can be classified into the following categories.
1. Conditional control
2. Iterative control
3. Sequential control

Page 102
1. Conditional control
1. Conditional control, which run different statements for different data values.
2. It check the condition for single time only even it is true or false
3. The conditional control statements are If and case.
a. IF
i. If then statement
ii. If then else statement
iii. If then else if statement
iv. Nested if statement
b. Case

 If statement
If condition is true it can execute statement 1 to statement n otherwise it
cannot execute statement 1 to statement n.

Syntax:
if(condition) then
Statement 1;
……………
Statement n;
End if;

Example:
DECLARE
a number:=&a;
BEGIN
if(a<10)then
dbms_output.put_line(‘welcome to pl/sql’);
end if;
END;

 If then else statement


If condition is true it can execute statement 1. If the condition is false it
execute else statement or execute statement 2.

Syntax:
if(condition) then
Statement 1;
else
statement 2;
end if;

Example:
declare
a integer;
b integer;
begin
a:=&a; /* it take run time values*/
b:=&b; /* it take run time values*/
if(a>b) then
dbms_output.put_line(‘A is big’);
else
dbms_output.put_line(‘b is big’);

end if;
end;

Page 103
 If then else if statement
If condition is true it can execute statement 1. If the condition is false its
again chek for another condition if it is true it can execute statement 2. Other
execute else statement or execute statement 3.

Syntax:
if(condition) then
Statement 1;
elsif(condition) then
statement 2;
else
statement 3;
end if;

Example:
declare
a integer;
b integer;
c integer;
begin
a:=&a;
b:=&b;
c:=&c;
if(a>b and a>c) then
dbms_output.put_line(‘a is big’);
elsif(b>c) then
dbms_output.put_line(‘b is big’);
else
dbms_output.put_line(‘c is big’);
end if;
end;

 Nested if statement
Inner if is called nested if. if condition is false it execute stamen 3.
Other wise it is true its check for another condition if it is true it execute
statement 1 other wise execute statement 2.

Syntax:
if(condition) then
if(condition) then
statement 1;
else
statement 2;
end if;
else
statement 3;
end if;

Example:
declare
a integer;
b integer;
c integer;
begin
a:=&a;
b:=&b;

Page 104
c:=&c;
if(a>b) then
if(a>c) then
dbms_output.put_line(‘a is big’);
else
dbms_output.put_line(‘c is big’);
endif;
elsif(b>c)
dbms_output.put_line(‘b is big’);
else
dbms_output.put_line(‘c is big’);
end if;
end;

 Case:
Syntax:
case variablename
When value1 then stmt;
When value2 then stmt;
……………
Else stmt;
End case;
1. The case statement runs the first statements for which value equals variable
name remaining conditions are not evaluated.
2. If no value equals to variable name, the case statements runs else statements.

Ex:
declare
Grade char:=’&grade’;
begin
case grade
when ‘A’ then dbms_output.put_line(‘Excellent’);
when ‘B’ then dbms_output.put_line(‘Very good’);
when ‘C’ then dbms_output.put_line(‘Good’); when
‘D’ then dbms_output.put_line(‘Fair’); when ‘F’ then
dbms_output.put_line(‘poor’);
else dbms_output.put_line(‘no such grade’);
end case;
end;

2. Iterative control (or) Loop controls


Iterative statements are check the condition if condition is true it execute the
statements and again check the condition up to condition is false. There are different
types of iterative statement.

1. Simple loop
2. While loop
3. For loop

1. Simple loop:
The statements that exit a loop
are o Exit
o Exit when

Syntax:
Exit: Exit when:
Loop Loop

Page 105
Statement; Statement;
Exit; Exit when(condition);
end loop; end loop;

Example: Exit
Declare
n number:=0;
begin
loop
n:=n+1;
dbms_output.put_line(‘the value of n is’||n);
if(n>=10) then
exit;
end if;
end loop;
end;

2. while loop:
It is entry control loop. First check the condition if it is true it can be execute
and again check condition if it is true it can again execution other wise it close the
while loop.
Syntax:
While(condition)
loop
Statement 1;
…………
Statement n;
end loop;
Example:
declare
i number := 0;
begin
while i<= 10
loop
i:= i+ 1;
dbms_output.put_line('the value of i is ' || i);
end loop;
end;
3. For loop:

Syntax:
for variable in[reverse] start . . end
Loop
Statement 1;
…………
Statement n;
end loop;
Example:
begin
for i in 1..10
loop
dbms_output.put_line('the value of i is ' || i);
end loop;
end;

 SEQUENTIAL CONTROL:

Page 106
1. Sequential control statements, which are not crucial (central or Main) to PL/SQL
programming.
2. The sequential control statements are
a. GOTO
It goes to a specified statement, and NULL, which does nothing.

Goto statement:
1. The Oracle PL/SQL GOTO statement is a sequential control structure available in
Oracle.
2. The GOTO statement immediately transfers program control (called "branching")
using with label or block label.
3. The statement or label name must be unique in the block.

Syntax:
<<Lable>>
Statement 1;
…………..
Statement n;
if(condition) then
Goto label;
end if;
Ex:
declare
n integer:=&n;
i integer:=1;
begin
<<p>>
dbms_output.put_line(i);
i:=i+1;
if(i<=n) then
goto p;
end if;
end;

 Explain processing with cursors in PL/SQL.


1. PL/SQL provides to access more than one record from database
2. This method is a combination of select statement and memory management
routines from PL/SQL
3. To retrieve records from database using select statement and places them with in
memory known as cursor.

There are two types of cursors.

 Implicit Cursor –
1. It is automatically created in PL/SQL when the SQL statement returns only one
value.

 Explicit Cursor –
1. It is created to hold the output of an SQL statement that may return two or
more rows.

Syntax:
Cursor <cursor name> is select query;

Page 107
Cursor process commands open, fetch (get) and close any where between begin and
end keywords of PL/SQL block.

Command Explanation
1. Open the cursor, execute the SQL command and
populate the cursor with data.
Open 2. Before you can use a cursor, you need to open it.
Syntax:
open <cursor name>;
1. The fetch command to retrieve data from the cursor
and copy it to the PL/SQL variables for processing.
Fetch
Syntax:
fetch <cursor name> into <var list>;
1. The close command closes the cursor for processing.
Close Syntax:
close <cursor name>;

Syntax:
Declare
Cursor <cursorname> is <select statement>;
Variable declaration;
Begin
Open <cursorname>
Fetch <cursorname> into variablename1…..;
close <cursorname>;
End;

 Cursor Attributes

Attribute Description
Keeps the count of record for each fetch when the cursor is
%rowcount open. Other wise it returns 0.
Keep Boolean values TRUE if fetch is successful otherwise
%found
FALSE if fetch did not return any row.
Keep Boolean values TRUE if fetch is did not return any row
%notfound
otherwise FALSE.
Returns TRUE if cursor is open otherwise FALSE if the cursor
%isopen
is closed.

Example:
Declare
a emp%rowtype;
cursor c2 is select * from emp where deptno=&deptno;
begin
open c2;
loop
fetch c2 into a;
if c2%found then
dbms_output.put_line(a.empno||’ ‘||a.ename||’ ‘||a.sal);
else
exit;
end if;
end loop;
close c2;

Page 108
end;

 Explain PL/SQL Stored functions. or


 Stored procedures or
 Routines or
 sub programs:
Subprograms
1. Subprogram is a program unit/module
2. That performs a particular task. It also called block.
3. These subprograms are combined to form larger programs.
4. This is basically called the 'Modular design'. A
5. Subprogram can be invoked by another subprogram or program which is called
the calling program
6. Subprograms can be created at the schema level, inside the package, and inside
block
7. They are two types of sub programs

1. Procedures
2. Functions

Parameter Modes in PL/SQL Subprograms

User create a Procedure or function, you have to define this three parameters.

1. In
2. Out
3. In out

1. IN
a. IN parameter is referenced to the procedure or function.
b. Input parameter only, the value passed is input only.
c. And value of parameter in not overwritten

2. OUT
a. OUT parameter is referenced to the procedure or function.
b. Output only.
c. And value of parameter is overwritten allow.

3. IN OUT
a. IN OUT parameter is referenced to the procedure or function.
b. Both input and output.
c. And value of parameter is both overwritten allow or not allow.

1. Procedures
These subprograms do not return a value directly, mainly used to perform
an action.

Syntax:
CREATE [OR REPLACE] PROCEDURE procedure_name (procedure parameters)
{IS | AS}
BEGIN
< procedure_body >
END;

Example:
CREATE OR REPLACE PROCEDURE greetings

Page 109
AS
BEGIN
dbms_output.put_line('Hello World!');
END;
/

Procedure created.

Execute:

When above code is executed using SQL prompt, it will produce the following
result:

EXECUTE greetings; or CALL greetings();

Hello World

PL/SQL procedure successfully completed.

Or
You might call the procedure in a PL/SQL Block.
BEGIN
greetings;
END;
Hello World
PL/SQL procedure successfully completed.

2. Functions
1. These subprograms return a single value, mainly used to compute and return a
value.
2. A PL/SQL function is same as a procedure except that it returns a value.

Syntax:
CREATE OR REPLACE
FUNCTION function_name (function_params) RETURN return_type IS
BEGIN
function_body
RETURN something_of_return_type;
END;

Ex1:
CREATE OR REPLACE FUNCTION ADD_TWO (A INT,B INT) RETURN INT IS
BEGIN
RETURN (A + B);
END;
Calling function:
BEGIN
DBMS_OUTPUT.PUT_LINE(’RESULT IS: ’ || ADD_TWO(12,34));
END;
/
RESULT IS: 46

 Benefits of Procedures and Functions


Stored procedures and functions have many benefits and addition to part application
development.
1. It modifies one routine to affect multiple applications.

Page 110
2. It modifies one routine to eliminate duplicate testing.
3. It avoids PL/SQL parsing at runtime by parsing at compile time.
4. It reduces the number of calls to the database
5. Database network traffic by bundling (collecting) the commands
6. User-defined functions cannot be used in situations
7. That requires an unchanging definition.
8. Thus, you cannot use user-defined functions:
a. In a CHECK constraint clause of
a CREATE TABLE or ALTER TABLE statement
b. In a DEFAULT clause of a CREATE TABLE or ALTER TABLE statement

 Difference Between Function and Procedure

1. Functions must always return a value.


2. Functions cannot alter data or objects in a server.
3. You can embed functions within a SELECT statement.
4. Functions can accept input parameters but can return only a single
return value.
5. TEMP tables can’t be used in functions.
6. user-defined functions cannot be used in CHECK or DEFAULT constraints
7. and cannot manipulate database values
8. It is mandatory for a function to have at least one RETURN statement
9. It will not allow to do the DML on existing tables. But still we can do the DML
operation only on the table variable inside the user defined functions.
10.The transaction statement cannot be used in the function. Normally we won't
do any DML operations in the function.
11.The parameters for the stored procedures can be any data types which are
available on the sql server.

Procedures:
1. Stored procedures do not have this requirement.
2. Stored procedures can alter data and objects in database and server.
3. Stored procedures cannot be embedded within a SELECT statement.
4. Stored procedures can also accept OUTPUT parameters.
5. Both TEMP tables and Table variables can be used in stored procedures.
6. It is not mandatory for a procedures to have at least one RETURN statement
7. In case of procedures with RETURN statement, simply the control of execution
is transferred back to the portion of code that called the procedure.
8. The stored procedures can do all the DML operations like insert the new record,
update the records and delete the existing records.
9. The transaction statement can be used inside the stored procedures.
10.The function won't allow several data types of the sql server as a parameter.

 Explain Packages
1. A package can be defined as a collection of related program objects.
2. Such as procedures, functions, and associated cursors and variables together as a
unit in the database.
3. A package is a group of related procedures and functions
4. It stored together and sharing common variables, as well as local procedures and
functions.
5. A package contains two separate parts:
a. The package specification and
b. The package body.
6. The package specification and package body are compiled separately
7. And stored in the data dictionary as two separate objects.
8. The package body is optional and need not to be created

Page 111
9. The package specification does not contain any procedures or functions.
10.Applications or users can call packaged procedures and functions

Advantages of Packages
Packages offer a lot of advantages. They are as follows.
1. User can create package to store all related functions and procedures are
grouped together into single unit called packages.
2. Package is reused.
3. All function and procedure within a package can share variable among them
4. Packages are support overloading to overload functions and procedures.
5. Package are improve the performance to loading the multiple object into memory at
once, therefore, subsequent calls to related program doesn't required to calling
physically I/O.
6. Packages are reduced the traffic because all block execute all at once.
7. Packages enable the organization of commercial applications into efficient modules.
8. Each package is easily understood
9. Packages interfaces between packages are simple, clear and well defined.
10.Packages allow granting of privileges efficiently.
11.Packages promote code reuse through the use of libraries
12.That contain stored procedures and functions, thereby reducing redundant coding .
13.Each package is easy to understand, and the interfaces between packages are simple,
clear, and well defined.
14.Easier Application Design it stored programs and subprograms
15.Packages can specify which types, items, and subprograms are public (visible and
accessible) or private (hidden and inaccessible).
16.For example, if a package contains four subprograms, three might be public and one
private.
17.The package hides the implementation of the private subprogram so that only the
package (not your application) is affected if the implementation changes.
18.They can be shared by all subprograms that execute in the environment.
19.To maintain data across transactions without having to store it in the database.
20.User call a packaged subprogram for the first time, the whole package is loaded into
memory.
21.To related subprograms in the package require no disk I/O.
22.Packages stop cascading dependencies and thereby avoid unnecessary recompiling.
23.Change the implementation of a packaged function

Units of Packages

As described earlier, a package is used to store together, the logically related PL/SQL
units.
1. Procedures
2. Functions
3. Triggers
4. Cursors
5. Variables

Parts of Package
A Package has two parts. They are:
1. Package specification
2. Package body

1. Package Specification
1. The specification declares the types, variables, constants, exceptions, cursors,
and subprograms
2. These are public and thus available for use outside the package.

Page 112
3. In case in the package specification declaration there is only types, constants,
exception, or variables,
4. Then there is no need for the package body because package specification are
sufficient for them.
5. Package body is required when there is subprograms like cursors, functions,
etc.

2. Package Body
1. The package body fully defines subprograms such as cursors, functions, and
procedures.
2. All the private declarations of the package are included in the package body.
3. It implements the package specification.
4. A package specification and the package body are stored separately in the
database.
5. This allows calling objects to depend on the specification only, not on both.
6. Oracle invalidates the calling object if the package specification is changed.

Creating a Package
1. A package consists of package specification and package body.
2. Creation of a package involves creation of the package specification and then
creation of the package body.
3. The package specification is declared using the CREATE PACKAGE command.

Syntax:
CREATE [OR REPLACE] PACKAGE <package name>
[AS/IS]

PL/SQL package specification

1. All the procedures, sub programs, cursors declared in the CREATE PACKAGE
command
2. They are described and implemented fully in the package body along with
private members.

Syntax:

CREATE [OR REPLACE] PACKAGE BODY <package name>


[AS/IS]

PL/SQL package body

Referencing Package Subprograms

<PACKAGE NAME>.<VARIABLE NAME>

To reference procedures we have to use the syntax as follows:

EXECUTE <package name>.<procedure name(variables)>;


Removing a Package
A package can be dropped from the database just like any other table or database
object. The exact syntax of the command to be used for dropping a package is:

DROP PACKAGE <PACKAGE NAME>;

 Triggers:

Page 113
A trigger is an action which can be performed automatically whenever the data
manipulations are applied on the table
or
1. A database trigger is a stored procedure
2. That is fired when an insert, update or delete statement is issued against the associated
table.

Syntax:

CREATE [OR REPLACE] TRIGGER trigger-name

BEFORE (OR AFTER)

INSERT OR UPDATE OR DELETE [OF COLUMNS]

ON tablename

[FOR EACH ROW [WHEN (condition)]]

BEGIN

Trigger action;

END;

 Types of PL/SQL Triggers


There are two types of triggers based on the which level it is triggered.
1. Row level trigger –
a. An event is triggered for each row upated, inserted or deleted.

2. Statement level trigger –


a. An event is triggered for each sql statement executed.

In trigger two types of pseudo (artificial) (fake) column are available they are
On the Basis of Type of Trigger/Firing or Triggering Transaction

– BEFORE Triggers

– AFTER Triggers

– INSTEAD OF

BEFORE Triggers

BEFORE triggers execute the trigger action before the triggering statement is
executed. It is used to derive specific column values before completing a triggering
DML, DDL statement or to determine whether the triggering statement should be
allowed to complete.

Example

We can define a BEFORE trigger on the passengers detail table that gets fired before
deletion of any row. The trigger will check the system date and if the date is Sunday, it
will not allow any deletion on the table.

Page 114
The trigger can be created in Oracle The trigger action can be shown. As soon as we try
to delete a record from passenger detail table, the above

trigger will be fired and due to SUNDAY EXP fired, all the changes will be

rolled back or undone and the record will not be deleted.

AFTER Triggers

AFTER triggers execute the trigger action after the triggering statement is

executed. AFTER triggers are used when we want the triggering statement to
complete before executing the trigger action, or to execute some additional logic to
the before trigger action

Page 115
Example

We can define an AFTER trigger on the reserv det table that gets fired every time one
row is deleted from the table. This trigger will determine the passenger id of the deleted
row and subsequently delete the corresponding row from the passengers det table with
same passenger id.

Page 116
Triggers on LOGON and LOGOFF Events

LOGON and LOGOFF triggers can be associated with the database or with

a schema. Their attributes include the system event and username, and they can specify
simple conditions on USERID and USERNAME.

– LOGON triggers fire after a successful logon of a user.

– LOGOFF triggers fire at the start of a user logoff.

Example

Let us create a trigger on LOGON event called pub log, which will store

the number, date, and user of login done by different user in that particular database. The
trigger will store this information in a table called log detail. The table log detail must be
created before trigger creation by logging into Administrator login. The trigger can be
created After logging into another login, if we see the content of the relation

log detail it will show who are all logged into database. The value of the

attribute log times would go on increasing with every login into the database which is
indicated Note The log detail relation is visible only in Administrator login.

Page 117
Page 118
Triggers on DDL Statements

This trigger gets fired when DDL statement such as CREATE, ALTER, or

DROP command is issued. DDL triggers can be associated with the database or with a
schema. Moreover depending on the time of firing of trigger, this trigger can be classified
into BEFORE and AFTER. Hence the triggers on

DDL statements can be as follows:

– BEFORE CREATE and AFTER CREATE triggers fire when a schema

object is created in the database or schema.

– BEFORE ALTER and AFTER ALTER triggers fire when a schema object

is altered in the database or schema.

– BEFORE DROP and AFTER DROP triggers fire when a schema object

is dropped from the database or schema.

Example 1

Create or replace trigger abc before insert on dept for each

row Declare

Sdate varchar2(3);

Begin

Sdate:=to_char(sysdate,’dy’);

If sdate in (‘sat’,’sun’) then

Raise_application_error(-2003,’This is weakend’);

End if;

End;

Example 2:

Create or replace trigger etrig

Before insert or update or delete on emp

Declare

Tday varchar2(10);

Begin

Tday:=to_char(sysdate,’dy’);

Page 119
If tday=’mon’ then

Raise_application_error(-20001,’no transaction on maonday’);

End if;

End;

Example 3

Create or replace trigger strig

Before update on emp for each row

Begin

If :old.sal>=10000 then

Raise_application_error(-20001,’salary not > 10000 update’);

End if;

End;

Example 4:

Create or replace trigger tr4

Before delete on emp for each row

Begin

Insert into etemp(empno,ename) values(:old.empno,:old.ename);

Dbms_output.putline(‘backup copied to etemp table’);

End;

Triggers on DML Statements

This trigger gets fired when DML statement such as INSERT, UPDATE, or

DELETE command is issued. DML triggers can be associated with the database or with a
schema. Depending on the time of firing of trigger, this trigger can be classified into
BEFORE and AFTER.

Row Level Triggers

A row level trigger, as its name suggests, is fired for each row that will be

affected by the SQL statement, which fires the trigger. Suppose for example if an
UPDATE statement updates “N” rows of a table, a row level trigger defined for this

Page 120
UPDATE on that particular table will be fired once for each of those “N” affected rows. If
a triggering SQL statement affects no rows, a row trigger is not executed at all. To
specify a trigger of row type, FOR EACH ROW clause is used after the name of table.

Statement Level Triggers

Unlike row level trigger, a statement level trigger is fired only once on behalf of the
triggering SQL statement, regardless of the number of rows in the table that the
triggering statement affects. Even if the triggering statement affects no rows, the
statement level trigger will execute exactly once. For example, if a DELETE statement
deletes several rows from a table, a statement-level DELETE trigger is fired only once,
regardless of how many rows are deleted from the table.

INSTEAD-OF Triggers

INSTEAD-OF triggers are used to tell Oracle what to do instead of performing the actions
that executed the trigger. It is applicable to both object views and standard relational
database. This trigger can be used to redirect table inserts into a different table or to
update different tables that are the part of the view. This trigger is used to perform any
action instead of the action that executes the trigger.

Enabling and Disabling a Trigger

By default, a trigger is enabled when it is created. Only an enabled trigger gets fired
whenever the trigger restriction evaluates to TRUE. Disabled triggers do not get fired
even when the triggering statement is issued

Syntax

ALTER TRIGGER <Trigger name> ENABLE/DISABLE;

Dropping Triggers

Triggers can be dropped like tables using the drop trigger command. The drop trigger
command removes the trigger structure from the database

Syntax

DROP TRIGGER <trigger name>

 Explain Exceptions Handling


1. An exception is an error condition during a program execution.
2. PL/SQL supports programmers to catch such conditions using EXCEPTION block in
the program
3. And an appropriate action is taken against the error condition.
4. There are two types of exceptions
 System-defined exceptions
 User-defined exceptions

Advantages of Using Exceptions


1. When executing programs on encountering error conditions
2. The behavior of application becomes more reliable.

Page 121
3. Meaningful messages can be flagged so that the developer can become aware
(alert) of error and warning conditions and act upon them.
4. In traditional error checking system, if same error is to be checked at several
places
5. User required a code the same error check at all those places.
6. We will write the exception for that particular error only once in the entire code.
7. That type error occurs at any place in code, the exceptional handler will
automatically raise the defined exception.
8. Being a part of PL/SQL, exceptions can be coded at suitable places and can be
coded isolated (remote) like procedures and functions.
9. This improves the overall readability of a PL/SQL program.
10.Oracle’s internal exception mechanism combined with user-defined exceptions
11.It reduce the development efforts required for cumbersome (weightily) error
handling.
12.These two types of exceptions.
1. Pre-defined Exceptions
2. User Defined Exceptions

1. Pre-defined Exceptions (Internally)


1. It is also called internally
2. PL/SQL provides many pre-defined exceptions
3. Which are executed when any database rule is violated by a program
4. For example, the predefined exception NO_DATA_FOUND is raised when a
SELECT INTO statement returns no rows.

2. User Defined Exceptions


1. User-defined exceptions must be declared and raised explicitly by RAISE
statements.
2. Exceptions can be declared only in the declarative part of a PL/SQL block,
subprogram, or package.
3. An exception is declared by introducing its name, followed by the keyword
EXCEPTION

Syntax
DECLARE
<exception name>EXCEPTION;

Raising User-Defined and Internal Exceptions


1. Internally defined exceptions which are automatically raised by “OracleS” run time
engine
2. User-defined exceptions have to be raised explicitly by using RAISE statement.
RAISE <exception name>;

User-Defined Error Reporting – Use of Raise Application Error


1. RAISE APPLICATION ERROR lets display the messages we want whenever a
standard internal error occurs.
2. RAISE APPILCATION ERROR associates an Oracle Standard Error Number with
the message we define.

1. Any database error the database server automatically Exceptions are raised
2. But exceptions can be raised explicitly by the programmer by using the command
RAISE.

DECLARE
c_id customers.id%type := 8;

Page 122
c_name customerS.No.ame%type;
c_addr customers.address%type;
BEGIN
SELECT name, address INTO cname, c_addr
FROM customers
WHERE id = c_id;
DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);
DBMS_OUTPUT.PUT_LINE ('Address: ' || c_addr);
EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line('No such customer!');
WHEN others THEN
dbms_output.put_line('Error!');
END;
/

Page 123

You might also like