Database Management Systems-By-py Kumar
Database Management Systems-By-py Kumar
UNIT I
UNIT II
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
UNIT V
Page 1
Database Management Systems
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:
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.
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. 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).
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.
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.
1. Operational Database
2. Data Warehouse
Data ware house focus on storing data used to generate information required
to make tactical or strategic decisions.
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.
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.
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).
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:
2. A DBMS provides a framework for better enforcement of data privacy and security
policies.
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.
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
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
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.
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 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.
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 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 write specialized database application that does not fit into data-
processing frame work. Application involves knowledge base and expert system,
environment modeling system, etc.
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.
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:
2. Transaction manager.
3. File manager.
4. Buffer manager.
Transaction Management
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
File manager establishes and maintains the list of structure and indexes
– Create a file
– Delete 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.
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.
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.
Page 17
5. And the DML allows end users to extract the data from the database.
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.
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.
Eg: Employee salary must have values that are between 6,000 and 3,50,000
BUSINESS RULES
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).
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.
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
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
Page 20
6. The top layer (the root) is perceived as the parent of the segment directly
beneath it.
Final Assembly
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.
Page 21
SALESREP CUSTOMER
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.
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.
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)
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.
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.
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
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
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:
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
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.
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. 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.
ENTITIES:
EMPLOYEE
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.
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.
Example:
1. Consider the example, customer borrows loan.
2. Here loan is a weak entity.
ASSOCIATIVE ENTITY
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
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
36
Age Vehicle
Page 33
2. For example the attribute address is subdivided into street, city, state and zip
code.
Door_no
street City
Male/Female
Address
Pin No
ATM card
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
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
RELATIONSHIPS
M N
CITY Vehicle ROOTS
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
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,)
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.
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. 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
Page 37
EMPLOYEE Manages
One_to_many (1 – N):
Some organizations it may be possible for one employee to be managed by many other
employees.
Many_to_many (N – N)
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.
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.
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
Shipping mode
unitcost
Quaternary Relationships
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 is the relational database norm. consider the PAINTER paints
PAINTING example.
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.
PROFESS DEPARTMENT
Chairs
The M : N Relationship
CLASS
STUDENT has
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.
Week entity
Attribute
Page 41
Reducing ER Diagram to Tables
Mapping Algorithm
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
Page 42
are represented by rectangles with a single line.
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 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.
Page 43
Converting “Weak Entities” in ER Diagram to Tables
Page 44
and many-to-many.
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
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.
Generalization Generalization:
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
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.
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.
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
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.
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
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.
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
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
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.
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.
AB
In the above A determines B. A is called determination.
Functional dependence:
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.
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. ]
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.
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.
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.
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.
12. Non – Subversion Rule: This rule states that different levels of the language can not
subvert or bypass the integrity rules and constraints.
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.
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.
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).
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:
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.
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.
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.
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.
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).
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.
Notation: R __ S
Page 67
Equi Join
equality.
Given the two relations STAFF and DEPT, produce a list of staff and the
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 >, <, >=, <=.
σθ(R × S)
To illustrate theta join consider two relations FRIENDS and OTHERS with
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
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
Page 69
R still appear in the resulting relation.
Consider two relations PEOPLE and MENU determine the full outer, left
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.
Page 71
ADVANTAGES OF RELATIONAL ALGEBRA:
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.)
Page 73
10.An expression is of the form:
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:
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.
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
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)
Page 76
Syntax:
Column-name data-type (L);
Ex:
city char(18);
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
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:
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
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.
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:
Syntax:
Alter table table-name add (column name <data type>
(size));
Table altered.
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));
Table altered.
Page 79
SQL> alter table student modify(sname varchar2(15));
Table altered.
RENAMING TABLES:
Changing the table name from old to new name is called rename a table.
TRUNCATE:
Truncate command is used to delete to all the records permanently from the
database.
Page 80
INSERT (ADDING TABLE ROWS)
SQL requires the use of the INSERT command to enter data into a table.
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.
1. The attributes that have required values, by listing the attribute names inside
parentheses after the table name.
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.
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 *
Page 81
DELETE ( DELETING TABLE ROWS )
Syntax:
DELETE FROM TABLE_NAME
WHERE CONDITION;
Ex:
SQL> delete from student where sno=2;
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
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.
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>
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:
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.
Ex:
SQL> Select distinct sname from std;
Ex:
SQL>select *from std where sno=12;
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;
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
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
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;
PROJECTION OPERATION
1. The projection operation performs column wise filtering.
2. Specific columns are selected in projection operation.
Syntax:
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.
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.
/* 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.
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
e. AVG:
The avg function format is similar to that of min and max. it returns an average
value of given column name
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.
/* ADD_MONTH
---------
27-OCT-04 */
2. Months-between:
a. It returns a number date type as a result. Formate: - moths-between date1,
date2;
/* MONTHS_BETWEEN('27-AUG-85','06-OCT-83')
---------------------------------------
22.68 */
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:
Page 87
SQL> Select abs(-12.3467) from dual ;
/* ABS(-12.3467)
-------------
12.35 */
/* POWER(3,4)
----------
81 */
/* TRUNC(12.3456,2)
----------------
12.34 */
/* ROUND(12.3456,2)
----------------
12.35*/
/* FLOOR(68.78)
------------
68 */
/* CEIL(68.78)
-----------
69 */
CHARACTER FUNCTIONS: -
1. Characters functions accepts character input it returns either character value or
number some of these functions are
/* INI
---
Ram */
Page 88
Concat( ): - It combines two strings
/* CONCAT(
-------
goodday */
/* UPPE
----
GOOD */
/* LOW
---
Ram */
/* LPAD('GOOD',
------------
DAYDAYDAGOOD */
/* RPAD('GOOD',
------------
Gooddaydayda */
/* RTR
---
Sai */
/* C
Page 89
----
A */
MISLENIOUS FUNCTIONS:
/* GREATEST(100,200,300)
---------------------
300 */
/* LEAST(100,200)
--------------
100 */
/* USER
-----------
SCOTT */
/* 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,
3. Referential Constraint
a. Foreign key
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);
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);
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));
Syntax:
Alter table <table name> disable|enable constraint <constraint name>;
Syntax:
Page 92
Alter table <table name> drop constraint <constraint name>;
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:
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:
Ex:
Select ename,sal,losal,hisal,grade From emp,salgrade Where sal between losal
and hisal;
Ex:
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*/
Syntax:
Syntax:
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.
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 >,<=,<>,>=
Select ename from emp where sal > (select sal from emp where ename=’smith’);
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);
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);
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.
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)
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
Syntax:
Declare
Variable declaration
Begin
Process statements
Or execution statement
[Exception
Exception statement]
End;
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’);*/
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.
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;
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;
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;
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;
1. Procedures
2. Functions
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:
Hello World
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
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
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]
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:
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:
ON tablename
BEGIN
Trigger action;
END;
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
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.
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
– BEFORE ALTER and AFTER ALTER triggers fire when a schema object
– BEFORE DROP and AFTER DROP triggers fire when a schema object
Example 1
row Declare
Sdate varchar2(3);
Begin
Sdate:=to_char(sysdate,’dy’);
Raise_application_error(-2003,’This is weakend’);
End if;
End;
Example 2:
Declare
Tday varchar2(10);
Begin
Tday:=to_char(sysdate,’dy’);
Page 119
If tday=’mon’ then
End if;
End;
Example 3
Begin
If :old.sal>=10000 then
End if;
End;
Example 4:
Begin
End;
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.
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.
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.
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
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
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
Syntax
DECLARE
<exception name>EXCEPTION;
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