Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 77

Database Management System 

Database Management System or DBMS in short refers to the technology of storing and


retrieving usersí data with utmost efficiency along with appropriate security measures. This
tutorial explains the basics of DBMS such as its architecture, data models, data schemas, data
independence, E-R model, relation model, relational database design, and storage and file
structure and much more.
Why to Learn DBMS?
Traditionally, data was organized in file formats. DBMS was a new concept then, and all the
research was done to make it overcome the deficiencies in traditional style of data
management. A modern DBMS has the following characteristics −
 Real-world entity − A modern DBMS is more realistic and uses real-world entities to
design its architecture. It uses the behavior and attributes too. For example, a school
database may use students as an entity and their age as an attribute.
 Relation-based tables − DBMS allows entities and relations among them to form
tables. A user can understand the architecture of a database just by looking at the table
names.
 Isolation of data and application − A database system is entirely different than its
data. A database is an active entity, whereas data is said to be passive, on which the
database works and organizes. DBMS also stores metadata, which is data about data,
to ease its own process.
 Less redundancy − DBMS follows the rules of normalization, which splits a relation
when any of its attributes is having redundancy in values. Normalization is a
mathematically rich and scientific process that reduces data redundancy.
 Consistency − Consistency is a state where every relation in a database remains
consistent. There exist methods and techniques, which can detect attempt of leaving
database in inconsistent state. A DBMS can provide greater consistency as compared
to earlier forms of data storing applications like file-processing systems.
 Query Language − DBMS is equipped with query language, which makes it more
efficient to retrieve and manipulate data. A user can apply as many and as different
filtering options as required to retrieve a set of data. Traditionally it was not possible
where file-processing system was used.
Applications of DBMS
Database is a collection of related data and data is a collection of facts and figures that can
be processed to produce information.
Mostly data represents recordable facts. Data aids in producing information, which is based
on facts. For example, if we have data about marks obtained by all students, we can then
conclude about toppers and average marks.
A database management system stores data in such a way that it becomes easier to retrieve,
manipulate, and produce information. Following are the important characteristics and
applications of DBMS.
 ACID Properties − DBMS follows the concepts of Atomicity, Consistency, Isolation,
and Durability (normally shortened as ACID). These concepts are applied on
transactions, which manipulate data in a database. ACID properties help the database
stay healthy in multi-transactional environments and in case of failure.
 Multiuser and Concurrent Access − DBMS supports multi-user environment and
allows them to access and manipulate data in parallel. Though there are restrictions on
transactions when users attempt to handle the same data item, but users are always
unaware of them.
 Multiple views − DBMS offers multiple views for different users. A user who is in
the Sales department will have a different view of database than a person working in
the Production department. This feature enables the users to have a concentrate view
of the database according to their requirements.
 Security − Features like multiple views offer security to some extent where users are
unable to access data of other users and departments. DBMS offers methods to impose
constraints while entering data into the database and retrieving the same at a later
stage. DBMS offers many different levels of security features, which enables multiple
users to have different views with different features. For example, a user in the Sales
department cannot see the data that belongs to the Purchase department. Additionally,
it can also be managed how much data of the Sales department should be displayed to
the user. Since a DBMS is not saved on the disk as traditional file systems, it is very
hard for miscreants to break the code.

Relational data model

Relational Model was proposed by E.F. Codd to model data in the form of relations or
tables. After designing the conceptual model of Database using ER diagram, we need to
convert the conceptual model in the relational model which can be implemented using any
RDBMS languages like Oracle SQL, MySQL etc. So we will see what Relational Model is.
What is Relational Model? 
Relational Model represents how data is stored in Relational Databases.  A relational
database stores data in the form of relations (tables).

Relational data model is the primary data model, which is used widely around the world for
data storage and processing. This model is simple and it has all the properties and capabilities
required to process data with storage efficiency.

Relational Model Concepts in DBMS


1. Attribute: Each column in a Table. Attributes are the properties which define a
relation. e.g., Student_Rollno, NAME,etc.
2. Tables – In the Relational model the, relations are saved in the table format. It is
stored along with its entities. A table has two properties rows and columns. Rows
represent records and columns represent attributes.
3. Tuple – It is nothing but a single row of a table, which contains a single record.
4. Relation Schema: A relation schema represents the name of the relation with its
attributes.
5. Degree: The total number of attributes which in the relation is called the degree of the
relation.
6. Cardinality: Total number of rows present in the Table.
7. Column: The column represents the set of values for a specific attribute.
8. Relation instance – Relation instance is a finite set of tuples in the RDBMS system.
Relation instances never have duplicate tuples.
9. Relation key – Every row has one, two or multiple attributes, which is called relation
key.
10. Attribute domain – Every attribute has some pre-defined value and scope which is
known as attribute domain

Advantages of using the relational model


The advantages and reasons due to which the relational model in DBMS is widely accepted
as a standard are:
 Simple and Easy To Use - Storing data in tables is much easier to understand and
implement as compared to other storage techniques.
 Manageability - Because of the independent nature of each relation in a relational
database, it is easy to manipulate and manage. This improves the performance of the
database.
 Query capability - With the introduction of relational algebra, relational databases
provide easy access to data via high-level query language like SQL.
 Data integrity - With the introduction and implementation of relational constraints,
the relational model can maintain data integrity in the database.
Highlights:
 Relational databases are simple to use, easy to manage, provide data integrity, and are
query capable.
 All the advantages of relational databases are because of the use of tables and
constraints.
Disadvantages of using the relational model
The main disadvantages of relational model in DBMS occur while dealing with a huge
amount of data as:
 The performance of the relational model depends upon the number of relations
present in the database.
 Hence, as the number of tables increases, the requirement of physical memory
increases.
 The structure becomes complex and there is a decrease in the response time for the
queries.
 Because of all these factors, the cost of implementing a relational database increase.
Highlights:
1. Relational databases work perfectly well for a limited number of relations.
2. Increasing the amount of data can lead to performance and storage issues with
relational databases.
Constraints in Relational Model
Relational models make use of some rules to ensure the accuracy and accessibility of the
data. These rules or constraints are known as Relational Integrity Constraints. These
constraints are checked before performing any operation like insertion, deletion, or updation
on the data present in a relational database. These constraints include:
 Domain Constraint : It specifies that every attribute is bound to have a value that lies
inside a specific range of values. It is implemented with the help of the Attribute
Domain concept.
 Key Constraint : It states that every relation must contain an attribute or a set of
attributes (Primary Key) that can uniquely identify a tuple in that relation. This key
can never be NULL or contain the same value for two different tuples.
 Referential Integrity Constraint : It is defined between two inter-related tables. It
states that if a given relation refers to a key attribute of a different or same table, then
that key must exist in the given relation.
Constraints
Every relation has some conditions that must hold for it to be a valid relation. These
conditions are called Relational Integrity Constraints. There are three main integrity
constraints −

 Key constraints
 Domain constraints
 Referential integrity constraints
Key Constraints
There must be at least one minimal subset of attributes in the relation, which can identify a
tuple uniquely. This minimal subset of attributes is called key for that relation. If there are
more than one such minimal subsets, these are called candidate keys.
Key constraints force that −
 in a relation with a key attribute, no two tuples can have identical values for key
attributes.
 a key attribute can not have NULL values.
Key constraints are also referred to as Entity Constraints.
Domain Constraints
Attributes have specific values in real-world scenario. For example, age can only be a
positive integer. The same constraints have been tried to employ on the attributes of a
relation. Every attribute is bound to have a specific range of values. For example, age cannot
be less than zero and telephone numbers cannot contain a digit outside 0-9.
Referential integrity Constraints
Referential integrity constraints work on the concept of Foreign Keys. A foreign key is a key
attribute of a relation that can be referred in other relation.
Referential integrity constraint states that if a relation refers to a key attribute of a different or
same relation, then that key element must exist.

Integrity Constraints in DBMS


In Database Management Systems, integrity constraints are pre-defined set of rules that are
applied on the table fields(columns) or relations to ensure that the overall validity, integrity,
and consistency of the data present in the database table is maintained. Evaluation of all the
conditions or rules mentioned in the integrity constraint is done every time a table insert,
update, delete, or alter operation is performed. The data can be inserted, updated, deleted, or
altered only if the result of the constraint comes out to be True. Thus, integrity constraints are
useful in preventing any accidental damage to the database by an authorized user.
Types of Integrity Constraints
There are four types of integrity constraints in DBMS:
1. Domain Constraint
2. Entity Constraint
3. Referential Integrity Constraint
4. Key Constraint
Domain Constraint
Domain integrity constraint contains a certain set of rules or conditions to restrict the kind of
attributes or values a column can hold in the database table. The data type of a domain can be
string, integer, character, DateTime, currency, etc.
Example:
Consider a Student's table having Roll No, Name, Age, Class of students.
Roll No Name Age Class
101 Adam 14 6
102 Steve 16 8
103 David 8 4
104 Bruce 18 12
105 Tim 6 A
In the above student's table, the value A in the last row last column violates the domain
integrity constraint because the Class attribute contains only integer values while A is a
character.
Entity Integrity Constraint
Entity Integrity Constraint is used to ensure that the primary key cannot be null. A primary
key is used to identify individual records in a table and if the primary key has a null value,
then we can't identify those records. There can be null values anywhere in the table except the
primary key column.
Example:
Consider Employees table having Id, Name, and salary of employees
ID Name Salary
1101 Jackson 40000
1102 Harry 60000
1103 Steve 80000
1104 Ash 1800000
James 36000
In the above employee's table, we can see that the ID column is the primary key and contains
a null value in the last row which violates the entity integrity constraint.
Referential Integrity Constraint
Referential Integrity Constraint ensures that there must always exist a valid relationship
between two relational database tables. This valid relationship between the two tables
confirms that a foreign key exists in a table. It should always reference a corresponding value
or attribute in the other table or be null.
Example:
Consider an Employee and a Department table where Dept_ID acts as a foreign key between
the two tables
Employees Table
ID Name Salary Dept_ID
1101 Jackson 40000 3
1102 Harry 60000 2
1103 Steve 80000 4
1104 Ash 1800000 3
1105 James 36000 1
Department Table
Dept_ID Dept_Name
1 Sales
2 HR
3 Technical
In the above example, Dept_ID acts as a foreign key in the Employees table and a primary
key in the Department table. Row having DeptID=4 violates the referential integrity
constraint since DeptID 4 is not defined as a primary key column in the Departments table.
Key constraint
Keys are the set of entities that are used to identify an entity within its entity set uniquely.
There could be multiple keys in a single entity set, but out of these multiple keys, only one
key will be the primary key. A primary key can only contain unique and not null values in the
relational database table.
Example:
Consider a student's table
Roll No Name Age Class
101 Adam 14 6
102 Steve 16 8
103 David 8 4
104 Bruce 18 12
102 Tim 6 2
The last row of the student's table violates the key integrity constraint since Roll No 102 is
repeated twice in the primary key column. A primary key must be unique and not null
therefore duplicate values are not allowed in the Roll No column of the above student's table.
Fazit
 Integrity Constraints in Database Management Systems are the set of pre-defined
rules responsible for maintaining the quality and consistency of data in the database.
 Evaluation against the rules mentioned in the integrity constraint is done every time
an insert, update, delete, or alter operation is performed on the table.
 Integrity Constraints in DBMS are of 4 types:
1. Domain Constraint
2. Entity Constraint
3. Referential Integrity Constraint
4. Key Constraint
All these anomalies can lead to unexpected behavior and inconvenience for the user. These
anomalies can be removed with the help of a process known as normalization.

Relational Query Languages


 
Relational algebra is used to break the user requests and instruct the DBMS to execute
them. Relational Query language is used by the user to communicate with the
database. They are generally on a higher level than any other programming language.
This is further divided into two types
 Procedural Query Language
 Non-Procedural Language
 
Procedural Query Language
The user instructs the system to perform a set of operations on the database to
determine the desired results.
Non-Procedural Language 
The user outlines the desired information without giving a specific procedure for
attaining the information.

Introduction
Relational Query Languages – So far we have seen what a database is, what is the features of
database, how to gather requirements and how to put them in ER diagrams, how to convert
them into tables and their columns, set their constraints etc. Once we have database ready
users will start using them. But how will they access the database? Most of the time they
access the data by using some applications. These applications will communicate to database
by SQL and DBMS is responsible for managing the application and SQL intact. SQL has its
own querying methods to interact with database. But how these queries work in the database?
These queries work similar to relational algebra that we have in mathematics. In database we
have tables participating in relational algebra.
Relational Query Languages
Relational query languages use relational algebra to break the user requests and instruct the
DBMS to execute the requests. It is the language by which user communicates with the
database. These relational query languages can be procedural or non-procedural.

Procedural Query Language


A procedural query language will have set of queries instructing the DBMS to perform
various transactions in the sequence to meet the user request. This procedural query language
tells the database what is required from the database and how to get them from the database.
Relational algebra is a procedural query language.
Non-Procedural Query Language
Non-procedural queries will have single query on one or more tables to get result from the
database.. Relational Calculus is a nonprocedural language which informs what to do with the
tables, but doesn’t inform how to accomplish this.

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

 Select
 Project
 Union
 Set different
 Cartesian product
 Rename
We will discuss all these operations in the following sections.
Select Operation (σ)
It selects tuples that satisfy the given predicate from a relation.
Notation − σp(r)
Where σ stands for selection predicate and r stands for relation. p is prepositional logic
formula which may use connectors like and, or, and not. These terms may use relational
operators like − =, ≠, ≥, < ,  >,  ≤.
For example −
σsubject = "database"(Books)
Output − Selects tuples from books where subject is 'database'.
σsubject = "database" and price = "450"(Books)
Output − Selects tuples from books where subject is 'database' and 'price' is 450.
σsubject = "database" and price = "450" or year > "2010"(Books)
Output − Selects tuples from books where subject is 'database' and 'price' is 450 or those
books published after 2010.
Project Operation (∏)
It projects column(s) that satisfy a given predicate.
Notation − ∏A1, A2, An (r)
Where A1, A2 , An are attribute names of relation r.
Duplicate rows are automatically eliminated, as relation is a set.
For example −
∏subject, author (Books)
Selects and projects columns named as subject and author from the relation Books.
Union Operation (∪)
It performs binary union between two given relations and is defined as −
r ∪ s = { t | t ∈ r or t ∈ s}
Notation − r U s
Where r and s are either database relations or relation result set (temporary relation).
For a union operation to be valid, the following conditions must hold −

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


 Attribute domains must be compatible.
 Duplicate tuples are automatically eliminated.
∏ author (Books) ∪ ∏ author (Articles)
Output − Projects the names of the authors who have either written a book or an article or
both.
Set Difference (−)
The result of set difference operation is tuples, which are present in one relation but are not in
the second relation.
Notation − r − s
Finds all the tuples that are present in r but not in s.
∏ author (Books) − ∏ author (Articles)
Output − Provides the name of authors who have written books but not articles.
Cartesian Product (Χ)
Combines information of two different relations into one.
Notation − r Χ s
Where r and s are relations and their output will be defined as −
r Χ s = { q t | q ∈ r and t ∈ s}
σauthor = 'tutorialspoint'(Books Χ Articles)
Output − Yields a relation, which shows all the books and articles written by tutorialspoint.
Rename Operation (ρ)
The results of relational algebra are also relations but without any name. The rename
operation allows us to rename the output relation. 'rename' operation is denoted with small
Greek letter rho ρ.
Notation − ρ x (E)
Where the result of expression E is saved with name of x.
Additional operations are −

 Set intersection
 Assignment
 Natural join
Relational Calculus
In contrast to Relational Algebra, Relational Calculus is a non-procedural query language,
that is, it tells what to do but never explains how to do it.
Relational calculus exists in two forms −
Tuple Relational Calculus (TRC)
Filtering variable ranges over tuples
Notation − {T | Condition}
Returns all tuples T that satisfies a condition.
For example −
{ T.name | Author(T) AND T.article = 'database' }
Output − Returns tuples with 'name' from Author who has written article on 'database'.
TRC can be quantified. We can use Existential (∃) and Universal Quantifiers (∀).
For example −
{ R| ∃T   ∈ Authors(T.article='database' AND R.name=T.name)}
Output − The above query will yield the same result as the previous one.
Domain Relational Calculus (DRC)
In DRC, the filtering variable uses the domain of attributes instead of entire tuple values (as
done in TRC, mentioned above).
Notation −
{ a1, a2, a3, ..., an | P (a1, a2, a3, ... ,an)}
Where a1, a2 are attributes and P stands for formulae built by inner attributes.
For example −
{< article, page, subject > | ∈ TutorialsPoint ∧ subject = 'database'}
Output − Yields Article, Page, and Subject from the relation TutorialsPoint, where subject is
database.
Just like TRC, DRC can also be written using existential and universal quantifiers. DRC also
involves relational operators.
The expression power of Tuple Relation Calculus and Domain Relation Calculus is
equivalent to Relational Algebra.

DATABASE DESIGN: DATA MODELING

Data models define how the logical structure of a database is modeled. Data Models are
fundamental entities to introduce abstraction in a DBMS. Data models define how data is
connected to each other and how they are processed and stored inside the system.
The very first data model could be flat data-models, where all the data used are to be kept in
the same plane. Earlier data models were not so scientific, hence they were prone to introduce
lots of duplication and update anomalies.
Entity-Relationship Model
Entity-Relationship (ER) Model is based on the notion of real-world entities and relationships
among them. While formulating real-world scenario into the database model, the ER Model
creates entity set, relationship set, general attributes and constraints.
ER Model is best used for the conceptual design of a database.
ER Model is based on −
 Entities and their attributes.
 Relationships among entities.
These concepts are explained below.

 Entity − An entity in an ER Model is a real-world entity having properties


called attributes. Every attribute is defined by its set of values called domain. For
example, in a school database, a student is considered as an entity. Student has various
attributes like name, age, class, etc.
 Relationship − The logical association among entities is called relationship.
Relationships are mapped with entities in various ways. Mapping cardinalities define
the number of association between two entities.
Mapping cardinalities −
o one to one
o one to many
o many to one
o many to many
Relational Model
The most popular data model in DBMS is the Relational Model. It is more scientific a model
than others. This model is based on first-order predicate logic and defines a table as an n-ary
relation.

The main highlights of this model are −

 Data is stored in tables called relations.


 Relations can be normalized.
 In normalized relations, values saved are atomic values.
 Each row in a relation contains a unique value.
 Each column in a relation contains values from a same domain.

Data modelling is the first step in the process of database design. This step is sometimes
considered to be a high-level and abstract design phase, also referred to as conceptual design.
The aim of this phase is to describe:
 The data contained in the database (e.g., entities: students, lecturers, courses, subjects)
 The relationships between data items (e.g., students are supervised by lecturers; lecturers
teach courses)
 The constraints on data (e.g., student number has exactly eight digits; a subject has four or
six units of credit only)

In the second step, the data items, the relationships and the constraints are all expressed using
the concepts provided by the high-level data model. Because these concepts do not include
the implementation details, the result of the data modelling process is a (semi) formal
representation of the database structure. This result is quite easy to understand so it is used as
reference to make sure that all the user’s requirements are met.

The third step is database design. During this step, we might have two sub-steps: one
called database logical design, which defines a database in a data model of a specific DBMS,
and another called database physical design, which defines the internal database storage
structure, file organization or indexing techniques. These two sub-steps are database
implementation and operations/user interfaces building steps.

In the database design phases, data are represented using a certain data model. The data
model is a collection of concepts or notations for describing data, data relationships, data
semantics and data constraints. Most data models also include a set of basic operations for
manipulating data in the database.

Normalization
Normalization is the process of organizing data in a database. This includes creating tables
and establishing relationships between those tables according to rules designed both to
protect the data and to make the database more flexible by eliminating redundancy and
inconsistent dependency.
Normalization
A large database defined as a single relation may result in data duplication. This repetition of
data may result in:
o Making relations very large.
o It isn't easy to maintain and update data as it would involve searching many records in
relation.
o Wastage and poor utilization of disk space and resources.
o The likelihood of errors and inconsistencies increases.
So to handle these problems, we should analyze and decompose the relations with redundant
data into smaller, simpler, and well-structured relations that are satisfy desirable properties.
Normalization is a process of decomposing the relations into relations with fewer attributes.
What is Normalization?
o Normalization is the process of organizing the data in the database.
o Normalization is used to minimize the redundancy from a relation or set of relations.
It is also used to eliminate undesirable characteristics like Insertion, Update, and
Deletion Anomalies.
o Normalization divides the larger table into smaller and links them using relationships.
o The normal form is used to reduce redundancy from the database table.
Why do we need Normalization?
The main reason for normalizing the relations is removing these anomalies. Failure to
eliminate anomalies leads to data redundancy and can cause data integrity and other problems
as the database grows. Normalization consists of a series of guidelines that helps to guide you
in creating a good database structure.
Normalization is the process of minimizing redundancy from a relation or set of relations.
Redundancy in relation may cause insertion, deletion, and update anomalies. So, it helps to
minimize the redundancy in relations. Normal forms are used to eliminate or reduce
redundancy in database tables.
Functional Dependency
Functional dependency (FD) is a set of constraints between two attributes in a relation.
Functional dependency says that if two tuples have same values for attributes A1, A2,..., An,
then those two tuples must have to have same values for attributes B1, B2, ..., Bn.
Functional dependency is represented by an arrow sign (→) that is, X→Y, where X
functionally determines Y. The left-hand side attributes determine the values of attributes on
the right-hand side.
Armstrong's Axioms
If F is a set of functional dependencies then the closure of F, denoted as F +, is the set of all
functional dependencies logically implied by F. Armstrong's Axioms are a set of rules, that
when applied repeatedly, generates a closure of functional dependencies.
 Reflexive rule − If alpha is a set of attributes and beta is_subset_of alpha, then alpha
holds beta.
 Augmentation rule − If a → b holds and y is attribute set, then ay → by also holds.
That is adding attributes in dependencies, does not change the basic dependencies.
 Transitivity rule − Same as transitive rule in algebra, if a → b holds and b → c holds,
then a → c also holds. a → b is called as a functionally that determines b.
Trivial Functional Dependency
 Trivial − If a functional dependency (FD) X → Y holds, where Y is a subset of X,
then it is called a trivial FD. Trivial FDs always hold.
 Non-trivial − If an FD X → Y holds, where Y is not a subset of X, then it is called a
non-trivial FD.
 Completely non-trivial − If an FD X → Y holds, where x intersect Y = Φ, it is said to
be a completely non-trivial FD.
Normalization
If a database design is not perfect, it may contain anomalies, which are like a bad dream for
any database administrator. Managing a database with anomalies is next to impossible.
 Update anomalies − If data items are scattered and are not linked to each other
properly, then it could lead to strange situations. For example, when we try to update
one data item having its copies scattered over several places, a few instances get
updated properly while a few others are left with old values. Such instances leave the
database in an inconsistent state.
 Deletion anomalies − We tried to delete a record, but parts of it was left undeleted
because of unawareness, the data is also saved somewhere else.
 Insert anomalies − We tried to insert data in a record that does not exist at all.
Normalization is a method to remove all these anomalies and bring the database to a
consistent state.
First Normal Form
First Normal Form is defined in the definition of relations (tables) itself. This rule defines that
all the attributes in a relation must have atomic domains. The values in an atomic domain are
indivisible units.

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

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

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

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

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


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

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

Boyce-Codd Normal Form


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

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


In the above image, Stu_ID is the super-key in the relation Student_Detail and Zip is the
super-key in the relation ZipCodes. So,
Stu_ID → Stu_Name, Zip
and
Zip → City

Key Points –
 BCNF is free from redundancy.
 If a relation is in BCNF, then 3NF is also satisfied.
  If all attributes of relation are prime attribute, then the relation is always in
3NF.
 A relation in a Relational Database is always and at least in 1NF form.
 Every Binary Relation ( a Relation with only 2 attributes ) is always in BCNF.
 If a Relation has only singleton candidate keys( i.e. every candidate key consists
of only 1 attribute), then the Relation is always in 2NF( because no Partial
functional dependency possible).
 Sometimes going for BCNF form may not preserve functional dependency. In
that case go for BCNF only if the lost FD(s) is not required, else normalize till
3NF only.
 There are many more Normal forms that exist after BCNF, like 4NF and more.
But in real world database systems it’s generally not required to go beyond
BCNF.
Oracle 9i database

Oracle9i offers the most secure internet platform available to protect company information
with multiple layers of security for data, users, and companies.

Oracle is fundamentally just like SQL Server and every other relational database system. Its
database architectural principles are the same and it operates with SQL (Structured Query
Language), plus Oracle's own PL/SQL extensions. It's relatively easy to learn — as long as
you have a good handle on Linux and SQL.

Relational Database Management System (RDBMS)


The relational model is the basis for a relational database management system (RDBMS).
An RDBMS moves data into a database, stores the data, and retrieves it so that applications
can manipulate it.
An RDBMS distinguishes between the following types of operations:
 Logical operations
In this case, an application specifies what content is required. For example, an
application requests an employee name or adds an employee record to a table.
 Physical operations
In this case, the RDBMS determines how things should be done and carries out the
operation. For example, after an application queries a table, the database may use an
index to find the requested rows, read the data into memory, and perform many other
steps before returning a result to the user. The RDBMS stores and retrieves data so
that physical operations are transparent to database applications.
Oracle Database is an RDBMS. An RDBMS that implements object-oriented features such as
user-defined types, inheritance, and polymorphism is called an object-relational database
management system (ORDBMS). Oracle Database has extended the relational model to an
object-relational model, making it possible to store complex business models in a relational
database.
PERSONAL DATABASE:
A personal database is used to store frequently used, unique, or customized information.
This provides faster access to targeted information for use across all studies and projects.

Personal database management systems (DBMSs) are tools specifically designed to store,


retrieve, and manage large amounts of data in both numeric and textual formats. DBMSs can
be used by end users to manipulate lists of data as well as by other applications that need to
store data for further processing. Virtually all DBMSs found in office productivity suites
implement the so-called “relational” model of DBMS (RDBMS) in which data are stored as
tables composed of rows and columns.
Personal RDBMSs, which are distinguished from the more robust “enterprise” RDBMSs, are
typically implemented as a file containing the database that must be read entirely before users
can access the tables. Enterprise RDBMSs are implemented as a set of specialized processes
that handle multiple user connections, optimizing the response time and overall performance.
A personal DBMS is designed to be used by a single user, typically on a low- to medium-
powered PC platform. Microsoft Access and Filemaker are examples of personal database
software.

Definition
Personal database system is the local database system which is only for one user to store and
manage the data and information on their own personal system. There are number of
applications are used in local computer to design and managed personal database system.

Functions of personal database


Support one application
Personal database management system requires only one application to store and manage data
in personal computer.

Having a few tables


Personal database management system is based on small database consisting of few tables in
local or personal computer. It is easily to handle and manage. There is no need to install other
devices to access and control the data and information.
Involve one computer
In this database management system only one computer which is involved to store and
manage database in personal computer.

Simple design
Design in database management system has much importance for storing and controlling the
data. In personal database management system, there is simple design to store data and
information.

Advantage of personal database system


Fast processing
Based on the local computer the data can be processed more fast and reliable in terms of
handling.

Higher security
Data is stored in personal computer does not need any special security arrangement for
authorization of data.

Disadvantage of personal database system:


Fewer amounts of data
Fewer amounts of data and information are stored in personal database management system.
There is no connectivity with other computer to get more data.

No connectivity for external database


Personal database management system has only personal database system. There is no
connectivity with other computer system or database system to access the data and
information.

client-server architecture, architecture of a computer network in which


many clients (remote processors) request and receive service from a centralized server (host
computer). Client computers provide an interface to allow a computer user to request services
of the server and to display the results the server returns. Servers wait for requests to arrive
from clients and then respond to them. Ideally, a server provides a standardized transparent
interface to clients so that clients need not be aware of the specifics of the system (i.e.,
the hardware and software) that is providing the service. Clients are often situated
at workstations or on personal computers, while servers are located elsewhere on the
network, usually on more powerful machines. This computing model is especially effective
when clients and the server each have distinct tasks that they routinely perform.
In hospital data processing, for example, a client computer can be running an application
program for entering patient information while the server computer is running another
program that manages the database in which the information is permanently stored. Many
clients can access the server’s information simultaneously, and, at the same time, a client
computer can perform other tasks, such as sending e-mail. Because both client and server
computers are considered independent devices, the client-server model is completely
different from the old mainframe model, in which a centralized mainframe computer
performed all the tasks for its associated “dumb” terminals, which merely communicated
with the central mainframe.

The advantages of client server database system are as follows:


(a)  Client server system has cost effective platforms to support the applications.
(b) Client offers graphical menu driven interface, which is superior to the traditional
command line.
(c)  Client server environment facilitates in more productive work by the users and efficient
use of existing data.
(d) Client server database system is more flexible.
(e)  A single database on server can be shared across several distinct client systems.

Disadvantages of client server database system


There are some disadvantages in client server database system. These are
(a)  In initial phases, the programming cost is high.
(b) There is a lack of management tools for performance monitoring and timing.

In client-server architecture many clients connected with one server. The server is
centerlines.it provides services to all clients. All clients request to the server for different
Service. The server displays the results according to the client’s request.
Client/server architecture is a computing model in which the server hosts (computer), send
and manages most of the resources and works to be required by the client. In this type of
architecture has one or more client computers attached to a central server over a network.
This system shares different resources.
Client/server architecture is also called as a networking computing model and client-server
network because all the requests and demands are sent over a network.

Structure of Client-server Database Architecture in DBMS


By using this architecture structure this software is divided into three different tiers:
1. Presentation tier
2. Logic tier
3. Data-tier
Each tier type builds and maintains independently.
1-Presentation tier
This is the first and topmost level of the application n. The basic work of this layer provides
user interface .the interface is a graphical user interface. The graphical user interface is an
interface that consists of menus, buttons, and icons, etc. The presentation tier presents
information related to such work as browsing, sales purchasing, and shopping cart contents. It
attaches with other tiers by computing results to the browser/client tier and all other tiers in
the network.
2-Logic tier 
The logical tier is also known as data access tier and middle tier. It lies between the
presentation tier and the data tier.it basically controls the application’s functions by
performing processing. The components that build this layer exist on the server, assist the
resources sharing these components also define the business rules like different government
legal rules, data rules, and different business algorithm .which are designed to keep data
structure consistent.
 3-Data tier
This is basically the DBMS (database management system) layer. This layer consist of
database.it can be used through the business services layer.in this layer, data is stored and
retrieved .the responsibility of this layer to keep data consistent and independent.
Providing data its own tier also improves scalability and performance. This layer consists of
data access components.

Advantages of Client-server Database Architecture in DBMS


1. All the data and resources are controlled by server .im this way all data and resources
are very consistent.
2. You can easily increase the number of client in this architecture at any time. This all
increases the scalability of the network.
3. This is very easy to maintain you can easily repair, replace or add clients in this
network. the independence of the changes also known as encapsulation.
4. This network is very easy to use and it is not complicated.

Disadvantages of Client-server Database Architecture in DBMS


1. Traffic is a big problem in this network.
2. When you add large numbers of the client with server this network will be more
complicated.
3. When the server goes down all the clients are not able to send their request. The
whole work will be stopped
4. The hardware and software are very expensive.
5. The client does not have resources for each resource they need to request the server.
Because of all resources exit on server

Structured Query Language (SQL)


SQL is a set-based declarative language that provides an interface to an RDBMS such as
Oracle Database. In contrast to procedural languages such as C, which describe how things
should be done, SQL is nonprocedural and describes what should be done.
SQL is the ANSI standard language for relational databases. All operations on the data in an
Oracle database are performed using SQL statements. For example, you use SQL to create
tables and query and modify data in tables.
A SQL statement can be thought of as a very simple, but powerful, computer program or
instruction. Users specify the result that they want (for example, the names of employees),
not how to derive it. A SQL statement is a string of SQL text such as the following:
SELECT first_name, last_name FROM employees;

SQL statements enable you to perform the following tasks:


 Query data
 Insert, update, and delete rows in a table
 Create, replace, alter, and drop objects
 Control access to the database and its objects
 Guarantee database consistency and integrity
SQL unifies the preceding tasks in one consistent language. Oracle SQL is an
implementation of the ANSI standard. Oracle SQL supports numerous features that extend
beyond standard SQL.
Various Syntax in SQL
All the examples given in this tutorial have been tested with a MySQL server.

SQL SELECT Statement


SELECT column1, column2....columnN FROM table_name;

SQL DISTINCT Clause


SELECT DISTINCT column1, column2....columnN FROM table_name;

SQL WHERE Clause


SELECT column1, column2....columnN FROM table_name WHERE CONDITION;

SQL AND/OR Clause


SELECT column1, column2....columnN FROM table_name WHERE CONDITION-1
{AND|OR} CONDITION-2;

SQL IN Clause

SELECT column1, column2....columnN FROM table_name


WHERE column_name IN (val-1, val-2,...val-N);
SQL BETWEEN Clause
SELECT column1, column2....columnN FROM table_name WHERE column_name
BETWEEN val-1 AND val-2;
SQL LIKE Clause
SELECT column1, column2....columnN FROM table_name WHERE column_name LIKE
{ PATTERN };

SQL ORDER BY Clause


SELECT column1, column2....columnN FROM table_name

WHERE CONDITION
ORDER BY column_name {ASC|DESC};

SQL GROUP BY Clause


SELECT SUM(column_name) FROM table_name

WHERE CONDITION
GROUP BY column_name;

SQL COUNT Clause


SELECT COUNT(column_name) /ROM table_name WHERE CONDITION;

SQL HAVING Clause


SELECT SUM(column_name) FROM table_name WHERE CONDITION

GROUP BY column_name
HAVING (arithematic function condition);

SQL CREATE TABLE Statement


CREATE TABLE table_name(column1 datatype,column2 datatype,column3 datatype,.....
columnN datatype, PRIMARY KEY( one or more columns ));

SQL DROP TABLE Statement


DROP TABLE table_name;

SQL CREATE INDEX Statement


CREATE UNIQUE INDEX index_name ON table_name ( column1, column2,...columnN);

SQL DROP INDEX Statement


ALTER TABLE table_name DROP INDEX index_name;

SQL DESC Statement


DESC table_name;

SQL TRUNCATE TABLE Statement


TRUNCATE TABLE table_name;
SQL ALTER TABLE Statement
ALTER TABLE table_name {ADD|DROP|MODIFY} column_name {data_ype};
SQL ALTER TABLE Statement (Rename)
ALTER TABLE table_name RENAME TO new_table_name;

SQL INSERT INTO Statement


INSERT INTO table_name( column1, column2....columnN) VALUES ( value1,
value2....valueN);

SQL UPDATE Statement


UPDATE table_name SET column1 = value1, column2 = value2....columnN=valueN
[ WHERE CONDITION ];

SQL DELETE Statement


DELETE FROM table_name WHERE {CONDITION};

SQL CREATE DATABASE Statement


CREATE DATABASE database_name;

SQL DROP DATABASE Statement


DROP DATABASE database_name;

SQL USE Statement


USE database_name;

SQL COMMIT Statement


COMMIT;

SQL ROLLBACK Statement


ROLLBACK;

SQL
SQL is Structured Query Language, which is a computer language for storing, manipulating
and retrieving data stored in a relational database.
SQL is the standard language for Relational Database System. All the Relational Database
Management Systems (RDMS) like MySQL, MS Access, Oracle, Sybase, Informix, Postgres
and SQL Server use SQL as their standard database language.
Also, they are using different dialects, such as −

 MS SQL Server using T-SQL,


 Oracle using PL/SQL,
 MS Access version of SQL is called JET SQL (native format) etc.
Why SQL?
SQL is widely popular because it offers the following advantages −
 Allows users to access data in the relational database management systems.
 Allows users to describe the data.
 Allows users to define the data in a database and manipulate that data.
 Allows to embed within other languages using SQL modules, libraries & pre-
compilers.
 Allows users to create and drop databases and tables.
 Allows users to create view, stored procedure, functions in a database.
 Allows users to set permissions on tables, procedures and views.

A Brief History of SQL


 1970 − Dr. Edgar F. "Ted" Codd of IBM is known as the father of relational databases.
He described a relational model for databases.
 1974 − Structured Query Language appeared.
 1978 − IBM worked to develop Codd's ideas and released a product named System/R.
 1986 − IBM developed the first prototype of relational database and standardized by
ANSI. The first relational database was released by Relational Software which later
came to be known as Oracle.
SQL Process
When you are executing an SQL command for any RDBMS, the system determines the best
way to carry out your request and SQL engine figures out how to interpret the task.
There are various components included in this process.
These components are −

 Query Dispatcher
 Optimization Engines
 Classic Query Engine
 SQL Query Engine, etc.
A classic query engine handles all the non-SQL queries, but a SQL query engine won't
handle logical files.
Following is a simple diagram showing the SQL Architecture −
SQL Commands
The standard SQL commands to interact with relational databases are CREATE, SELECT,
INSERT, UPDATE, DELETE and DROP. These commands can be classified into the
following groups based on their nature −
DDL - Data Definition Language
S.No. Command & Description

1 CREATE Creates a new table, a view of a table, or other object in the database.

2 ALTER Modifies an existing database object, such as a table.

3 DROP Deletes an entire table, a view of a table or other objects in the database.

DML - Data Manipulation Language


Sr.No. Command & Description

1 SELECT Retrieves certain records from one or more tables.

2 INSERT Creates a record.

3 UPDATE Modifies records.

4 DELETE Deletes records.


DCL - Data Control Language
Sr.No. Command & Description

1 GRANT Gives a privilege to user.

2 REVOKE Takes back privileges granted from user.

SQL Data Type is an attribute that specifies the type of data of any object. Each column,
variable and expression has a related data type in SQL. You can use these data types while
creating your tables. You can choose a data type for a table column based on your
requirement.
SQL Server offers six categories of data types for your use which are listed below −

Exact Numeric Data Types


DATA TYPE FROM TO

Bigint -9,223,372,036,854,775,808 9,223,372,036,854,775,807

Int -2,147,483,648 2,147,483,647

Smallint -32,768 32,767

Tinyint 0 255

Bit 0 1

Decimal -10^38 +1 10^38 -1

Numeric -10^38 +1 10^38 -1

Money -922,337,203,685,477.5808 +922,337,203,685,477.5807

Smallmoney -214,748.3648 +214,748.3647


Approximate Numeric Data Types
DATA TYPE FROM TO

float -1.79E + 308 1.79E + 308

real -3.40E + 38 3.40E + 38

Date and Time Data Types


DATA TYPE FROM TO

datetime Jan 1, 1753 Dec 31, 9999

smalldatetime Jan 1, 1900 Jun 6, 2079

Date Stores a date like June 30, 1991

Time Stores a time of day like 12:30 P.M.

Note − Here, datetime has 3.33 milliseconds accuracy where as smalldatetime has 1 minute
accuracy.
Character Strings Data Types
Sr.No DATA TYPE & Description
.

1 Char Maximum length of 8,000 characters.( Fixed length non-Unicode characters)

2 Varchar Maximum of 8,000 characters.(Variable-length non-Unicode data).

3 varchar(max) Maximum length of 2E + 31 characters, Variable-length non-Unicode


data (SQL Server 2005 only).

4 Text Variable-length non-Unicode data with a maximum length of 2,147,483,647


characters.

Unicode Character Strings Data Types


Sr.No DATA TYPE & Description
.

1 Nchar Maximum length of 4,000 characters.( Fixed length Unicode)

2 Nvarchar Maximum length of 4,000 characters.(Variable length Unicode)

3 nvarchar(max) Maximum length of 2E + 31 characters (SQL Server 2005 only).


( Variable length Unicode)

4 Ntext Maximum length of 1,073,741,823 characters. ( Variable length Unicode )

Binary Data Types


Sr.No DATA TYPE & Description
.

1 Binary Maximum length of 8,000 bytes(Fixed-length binary data )

2 Varbinary Maximum length of 8,000 bytes.(Variable length binary data)

3 varbinary(max) Maximum length of 2E + 31 bytes (SQL Server 2005 only). ( Variable


length Binary data)

4 Image Maximum length of 2,147,483,647 bytes. ( Variable length Binary Data)

Misc Data Types


Sr.No DATA TYPE & Description
.

1 sql_variant Stores values of various SQL Server-supported data types, except text,
ntext, and timestamp.

2 Timestamp Stores a database-wide unique number that gets updated every time a row
gets updated

3 Uniqueidentifier Stores a globally unique identifier (GUID)


4 Xml Stores XML data. You can store xml instances in a column or a variable (SQL
Server 2005 only).

5 Cursor Reference to a cursor object

6 Table Stores a result set for later processing

Constraints are the rules enforced on the data columns of a table. These are used to limit the
type of data that can go into a table. This ensures the accuracy and reliability of the data in
the database.
Constraints could be either on a column level or a table level. The column level constraints
are applied only to one column, whereas the table level constraints are applied to the whole
table.
Following are some of the most commonly used constraints available in SQL. These
constraints have already been discussed in SQL - RDBMS Concepts chapter, but it’s worth to
revise them at this point.
 NOT NULL Constraint − Ensures that a column cannot have NULL value.
 DEFAULT Constraint − Provides a default value for a column when none is specified.
 UNIQUE Constraint − Ensures that all values in a column are different.
 PRIMARY Key − Uniquely identifies each row/record in a database table.
 FOREIGN Key − Uniquely identifies a row/record in any of the given database table.
 CHECK Constraint − The CHECK constraint ensures that all the values in a column
satisfies certain conditions.
 INDEX − Used to create and retrieve data from the database very quickly.
Constraints can be specified when a table is created with the CREATE TABLE statement or
you can use the ALTER TABLE statement to create constraints even after the table is
created.
Dropping Constraints
Any constraint that you have defined can be dropped using the ALTER TABLE command
with the DROP CONSTRAINT option.
For example, to drop the primary key constraint in the EMPLOYEES table, you can use the
following command.
ALTER TABLE EMPLOYEES DROP CONSTRAINT EMPLOYEES_PK;
Some implementations may provide shortcuts for dropping certain constraints. For example,
to drop the primary key constraint for a table in Oracle, you can use the following command.
ALTER TABLE EMPLOYEES DROP PRIMARY KEY;
Some implementations allow you to disable constraints. Instead of permanently dropping a
constraint from the database, you may want to temporarily disable the constraint and then
enable it later.
Integrity Constraints
Integrity constraints are used to ensure accuracy and consistency of the data in a relational
database. Data integrity is handled in a relational database through the concept of referential
integrity.
There are many types of integrity constraints that play a role in Referential Integrity (RI).
These constraints include Primary Key, Foreign Key, Unique Constraints and other
constraints which are mentioned above.

The SQL GROUP BY Statement

The GROUP BY statement groups rows that have the same values into summary rows, like
"find the number of customers in each country".

The GROUP BY statement is often used with aggregate functions


(COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns.

GROUP BY Syntax
SELECT column_name(s) FROM table_name WHERE condition
GROUP BY column_name(s) ORDER BY column_name(s);

Group functions are mathematical functions to operate on sets of rows to give one result per
set. The types of group functions (also called aggregate functions) are:
 AVG, that calculates the average of the specified columns in a set of rows,
 COUNT, calculating the number of rows in a set.
 MAX, calculating the maximum,
 MIN, calculating the minimum,
 STDDEV, calculating the standard deviation,
 SUM, calculating the sum,
 VARIANCE, calculating the variance.
The general syntax for using Group functions is :
SELECT <column, ...>, group_function(column) FROM <table> WHERE <condition>
[GROUP BY <column>] [ORDER BY <column>]
Note that the column on which the group function is applied must exist in the SELECT
column list.
For example, the following selects the maximum salary from  the employee table.
SQL> SELECT dept, MAX(sal)    FROM employee;

Using Group Functions


You can use AVG and SUM for numeric data. MIN and MAX can be applied on any data
type including numbers, dates and strings. The order is defined accordingly.
Group functions ignore the NULL values in the column. To enforce the group functions ti
include the NULL value, use NVL function.
For example, the following statement forces to take the average for all salary values even
including 0 (although it is an odd case, let's assume this is possible.)
SQL> SELECT dept, AVG(NVL(sal,0))
    FROM employee;

Using Group-By Clause


Note that ALL columns in the SELECT list that are not in group functions must be in the
GROUP-By clause.
For example,
SQL> SELECT dept, job, MAX(sal) FROM employee GROUP BY dept, job;
The following statement is thus illegal, because column dept is not in an aggregate function
and is not in GROUP-BY clause.
SQL> SELECT dept, MAX(sal) FROM employee;
The correct statement which selects the maximum salary for each department, should be:
SQL> SELECT dept, MAX(sal) FROM employee GROUP BY dept;

Using Having-Clause
To exclude some group results, use Having-clause.
For example,
SQL> SELECT dept, MAX(sal) FROM employee GROUP BY dept HAVING MAX(sal) > 2000;
Note that you CANNOT use WHERE clause to restrict  groups. For example, the following is
illegal:
SQL> SELECT dept, MAX(sal) FROM employee WHERE MAX(sal) > 2000 GROUP BY dept;
This is because the group function cannot be used in WHERE clause.

Nesting Group Functions


You can nest group function calls. For example, the following example to show the
maximum of the average salary of all departments,
SQL> SELECT MAX(AVE(sal)) FROM emp GROUP BY dept_no;
Spooling is a process in which data is temporarily held to be used and executed by a device,
program or the system. Data is sent to and stored in memory or other volatile storage until the
program or computer requests it for execution. "Spool" is technically an acronym for
Simultaneous Peripheral Operations OnLine.
SPOOL is an acronym for simultaneous peripheral operations on-line.  It is a kind of
buffering mechanism or a process in which data is temporarily held to be used and executed
by a device, program or the system. Data is sent to and stored in memory or other volatile
storage until the program or computer requests it for execution.
In a computer system peripheral equipments, such as printers and punch card readers, etc
(batch processing), are very slow relative to the performance of the rest of the system.
Getting input and output from the system was quickly seen to be a bottleneck. Here comes
the need for spool.
Spooling works like a typical request queue where data, instructions and processes from
multiple sources are accumulated for execution later on. Generally, it is maintained on
computer’s physical memory, buffers or the I/O device-specific interrupts. The spool is
processed in FIFO manner i.e. whatever first instruction is there in the queue will be
popped and executed.
Applications/Implementations of Spool:
1) The most common can be found in I/O devices like keyboard printers and mouse. For
example, In printer, the documents/files that are sent to the printer are first stored in the
memory or the printer spooler. Once the printer is ready, it fetches the data from the spool
and prints it.
Even experienced a situation when suddenly for some seconds your mouse or keyboard
stops working? Meanwhile, we usually click again and again here and there on the screen to
check if its working or not. When it actually starts working, what and wherever we pressed
during its hang state gets executed very fast because all the instructions got stored in the
respective device’s spool.
2) A batch processing system uses spooling to maintain a queue of ready-to-run jobs which
can be started as soon as the system has the resources to process them.
3) Spooling is capable of overlapping I/O operation for one job with processor operations
for another job. i.e. multiple processes can write documents to a print queue without
waiting and resume with their work.
4) E-mail: an email is delivered by a MTA (Mail Transfer Agent) to a temporary storage
area where it waits to be picked up by the MA (Mail User Agent)
5) Can also be used for generating Banner pages (these are the pages used in computerized
printing in order to separate documents from each other and to identify e.g. the originator of
the print request by username, an account number or a bin for pickup. Such pages are used
in office environments where many people share the small number of available resources).
SQL join
A SQL Join is a special form of generating a meaningful data by combining multiple tables
relate to each other using a “Key”. Typically, relational tables must be designed with a
unique column and this column is used to create relationships with one or more other tables.
When you need a result-set that includes related rows from multiple tables, you’ll need to use
SQL join on this column
The various SQL join types are as follows
1. SQL inner join
a. Equi join
b. Non-equi join (Theta join)
2. SQL outer join
a. SQL left join or left outer join
b. SQL right join or right outer join
c. SQL full join or full outer join
3. SQL cross join
4. SQL self join
Equi join:
An equi join is the most common form of SQL inner join used in practice. If the join contains
an equality operator e.g. =, then it’s an equi-join.

Theta join (Non-equi join):


In general, this a Theta join used to specify operators or conditions (the ON clause in SQL).
In
practice, this is a rarely used SQL join types. In most cases, the join will use a non-equality
condition 
SQL self join
A SQL Self join is a mechanism of joining a table to itself. You would use a self join when
you wanted to create a result set joining records in the table with some other records from the
same table.

SQL cross join


A CROSS join returns all rows for all possible combinations of two tables. It generates all the
rows from the left table which is then combined with all the rows from the right table. This
type of join is also known as a Cartesian product(A*B).
QL outer join
On joining tables with a SQL inner join, the output returns only matching rows from both the
tables. When using a SQL outer join, not only it will list the matching rows, it will also list
the unmatched rows from the other tables.

A SQL left outer join will return all the records from the left table in the join clause,
regardless of matching records in the right table. The left SQL outer join includes rows where
the condition is met plus all the rows from the table on the left where the condition is not met.
Fields from the right table with no match will be displayed as null values.
A right outer join will return all the records in the right table in the join clause, regardless of
matching records in the left table. Using the right SQL outer join includes all the rows from
the table on the right. The right SQL outer join is considered a special case and many
databases don’t support right joins. Generally, a SQL right join can be rewritten as a SQL left
join by simply changing the order of the tables in the query. In this instance, fields from the
left table with no match will display null values

A SQL outer join, as you might expect by now, will return all the rows in both tables. When
rows don’t have a match in one of the tables, the field will display a null value. A full SQL
outer join combines the effects of the SQL left joins and SQL right joins. Many databases do
not support the implementation of full SQL outer joins

Set Operators

Operator Returns

UNION All distinct rows selected by either query

UNION ALL All rows selected by either query, including all duplicates

INTERSECT All distinct rows selected by both queries

MINUS All distinct rows selected by the first query but not the second
Set operators combine the results of two component queries into a single result. Queries
containing set operators are called compound queries.  They are fully described, including
examples and restrictions on these operators, in "The UNION [ALL], INTERSECT, MINUS
Operators".
Set operators are used to join the results of two (or more) SELECT statements.The SET
operators available in Oracle 11g are UNION,UNION ALL,INTERSECT,and MINUS.
The UNION set operator returns the combined results of the two SELECT
statements.Essentially,it removes duplicates from the results i.e. only one row will be listed
for each duplicated result.To counter this behavior,use the UNION ALL set operator which
retains the duplicates in the final result.INTERSECT lists only records that are common to
both the SELECT queries; the MINUS set operator removes the second query's results from
the output if they are also found in the first query's results. INTERSECT and MINUS set
operations produce unduplicated results.
All the SET operators share the same degree of precedence among them.Instead,during query
execution, Oracle starts evaluation from left to right or from top to bottom.If explicitly
parentheses are used, then the order may differ as parentheses would be given priority over
dangling operators.
Points to remember -
 Same number of columns must be selected by all participating SELECT
statements.Column names used in the display are taken from the first query.
 Data types of the column list must be compatible/implicitly convertible by oracle.
Oracle will not perform implicit type conversion if corresponding columns in the
component queries belong to different data type groups.For example, if a column in
the first component query is of data type DATE, and the corresponding column in the
second component query is of data type CHAR,Oracle will not perform implicit
conversion, but raise ORA-01790 error.
 Positional ordering must be used to sort the result set. Individual result set ordering is
not allowed with Set operators. ORDER BY can appear once at the end of the query.
For example,
 UNION and INTERSECT operators are commutative, i.e. the order of queries is not
important; it doesn't change the final result.
 Performance wise, UNION ALL shows better performance as compared to UNION
because resources are not wasted in filtering duplicates and sorting the result set.
 Set operators can be the part of sub queries.
 Set operators can't be used in SELECT statements containing TABLE collection
expressions.
 The LONG, BLOB, CLOB, BFILE, VARRAY,or nested table are not permitted for
use in Set operators.For update clause is not allowed with the set operators.

NESTED QUERY
A nested query is a query that has another query embedded within it. The embedded query is
called a subquery.
A subquery typically appears within the WHERE clause of a query. It can sometimes appear
in the FROM clause or HAVING clause.

select E.ename from employee E where E.eid IN (select S.eid from salary S where
S.regno=103);

There are mainly two types of nested queries:


 Independent Nested Queries: In independent nested queries, query execution starts
from innermost query to outermost queries. The execution of inner query is independent
of outer query, but the result of inner query is used in execution of outer query. Various
operators like IN, NOT IN, ANY, ALL etc are used in writing independent nested
queries.
Co-related Nested Queries: In co-related nested queries, the output of inner query depends
on the row which is being currently executed in outer query.

When a query is included inside another query, the Outer query is known as Main Query, and
Inner query is known as Subquery. In Nested Query, Inner query runs first, and only
once. Outer query is executed with result from Inner query.

A database object is any defined object in a database that is used to store or reference
data.Anything which we make from create command is known as Database Object.It can
be used to hold and manipulate the data.Some of the examples of database objects are :
view, sequence, indexes, etc.
 Table – Basic unit of storage; composed rows and columns
 View – Logically represents subsets of data from one or more tables
 Sequence – Generates primary key values
 Index – Improves the performance of some queries
 Synonym – Alternative name for an object
Different database Objects :
1. Table – This database object is used to create a table in database.
Syntax :
CREATE TABLE [schema.]table
(column datatype [DEFAULT expr][, ...]);
Example :
CREATE TABLE dept
(deptno NUMBER(2),
dname VARCHAR2(14),
loc VARCHAR2(13));
Output :
DESCRIBE dept;

2. View – This database object is used to create a view in database.A view is a logical
table based on a table or another view. A view contains no data of its own but is like a
window through which data from tables can be viewed or changed. The tables on which
a view is based are called base tables. The view is stored as a SELECT statement in the
data dictionary.
Syntax :
CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view
[(alias[, alias]...)]
AS subquery
[WITH CHECK OPTION [CONSTRAINT constraint]]
[WITH READ ONLY [CONSTRAINT constraint]];
Example :
CREATE VIEW salvu50
AS SELECT employee_id ID_NUMBER, last_name NAME,
salary*12 ANN_SALARY
FROM employees
WHERE department_id = 50;
Output :
SELECT *
FROM salvu50;

3. Sequence – This database object is used to create a sequence in database.A sequence is


a user created database object that can be shared by multiple users to generate unique
integers. A typical usage for sequences is to create a primary key value, which must be
unique for each row.The sequence is generated and incremented (or decremented) by an
internal Oracle routine.
Syntax :
CREATE SEQUENCE sequence
[INCREMENT BY n]
[START WITH n]
[{MAXVALUE n | NOMAXVALUE}]
[{MINVALUE n | NOMINVALUE}]
[{CYCLE | NOCYCLE}]
[{CACHE n | NOCACHE}];
Example :
CREATE SEQUENCE dept_deptid_seq
INCREMENT BY 10
START WITH 120
MAXVALUE 9999
NOCACHE
NOCYCLE;
Check if sequence is created by :
SELECT sequence_name, min_value, max_value,
increment_by, last_number
FROM user_sequences;
4. Index – This database object is used to create a indexes in database.An Oracle server
index is a schema object that can speed up the retrieval of rows by using a
pointer.Indexes can be created explicitly or automatically. If you do not have an index
on the column, then a full table scan occurs.
An index provides direct and fast access to rows in a table. Its purpose is to reduce the
necessity of disk I/O by using an indexed path to locate data quickly. The index is used
and maintained automatically by the Oracle server. Once an index is created, no direct
activity is required by the user.Indexes are logically and physically independent of the
table they index. This means that they can be created or dropped at any time and have
no effect on the base tables or other indexes.
Syntax :
CREATE INDEX index
ON table (column[, column]...);
Example :
CREATE INDEX emp_last_name_idx
ON employees(last_name);
5. Synonym – This database object is used to create a indexes in database.It simplify
access to objects by creating a synonym(another name for an object). With synonyms,
you can Ease referring to a table owned by another user and shorten lengthy object
names.To refer to a table owned by another user, you need to prefix the table name with
the name of the user who created it followed by a period. Creating a synonym
eliminates the need to qualify the object name with the schema and provides you with
an alternative name for a table, view, sequence,procedure, or other objects. This method
can be especially useful with lengthy object names, such as views.
In the syntax:
PUBLIC : creates a synonym accessible to all users
synonym : is the name of the synonym to be created
object : identifies the object for which the synonym is created
Syntax :
CREATE [PUBLIC] SYNONYM synonym FOR object;
Example :
CREATE SYNONYM d_sum FOR dept_sum_vu;

VIEWS

Views in SQL are kind of virtual tables. A view also has rows and columns as they are in a
real table in the database. We can create a view by selecting fields from one or more tables
present in the database. A View can either have all the rows of a table or specific rows
based on certain condition.

Syntax:
CREATE VIEW view_name AS
SELECT column1, column2.....
FROM table_name
WHERE condition;

view_name: Name for the View


table_name: Name of the table
condition: Condition

SEQUENCE

Sequence is a set of integers 1, 2, 3, … that are generated and supported by some database
systems to produce unique values on demand.
 A sequence is a user defined schema bound object that generates a sequence of numeric
values.
 Sequences are frequently used in many databases because many applications require
each row in a table to contain a unique value and sequences provides an easy way to
generate them.
 The sequence of numeric values is generated in an ascending or descending order at
defined intervals and can be configured to restart when exceeds max_value.
Syntax:
CREATE SEQUENCE sequence_name
START WITH initial_value
INCREMENT BY increment_value
MINVALUE minimum value
MAXVALUE maximum value
CYCLE|NOCYCLE ;
sequence_name: Name of the sequence.

initial_value: starting value from where the sequence starts.


Initial_value should be greater than or equal
to minimum value and less than equal to maximum value.

increment_value: Value by which sequence will increment itself.


Increment_value can be positive or negative.

minimum_value: Minimum value of the sequence.


maximum_value: Maximum value of the sequence.
cycle: When sequence reaches its set_limit
it starts from beginning.

nocycle: An exception will be thrown


if sequence exceeds its max_value.

INDEX

An index is a schema object. It is used by the server to speed up the retrieval of rows by
using a pointer. It can reduce disk I/O(input/output) by using a rapid path access method to
locate data quickly. An index helps to speed up select queries and where clauses, but it
slows down data input, with the update and the insert statements. Indexes can be created or
dropped with no effect on the data. In this article, we will see how to create, delete, and
uses the INDEX in the database. 
For example, if you want to reference all pages in a book that discusses a certain topic, you
first refer to the index, which lists all the topics alphabetically and is then referred to one or
more specific page numbers. 
Creating an Index:
Syntax:
CREATE INDEX index
ON TABLE column;
where the index is the name given to that index and TABLE is the name of the table on
which that index is created and column is the name of that column for which it is applied. 

For multiple columns:


 Syntax:
CREATE INDEX index
ON TABLE (column1, column2,.....);
Unique Indexes:
Unique indexes are used for the maintenance of the integrity of the data present in the table
as well as for the fast performance, it does not allow multiple values to enter into the table. 
 Syntax:
CREATE UNIQUE INDEX index
ON TABLE column;
When should indexes be created:
 
 A column contains a wide range of values.
 A column does not contain a large number of null values.
 One or more columns are frequently used together in a where clause or a join condition.
When should indexes be avoided:
 
 The table is small
 The columns are not often used as a condition in the query
 The column is updated frequently
Removing an Index:
To remove an index from the data dictionary by using the DROP INDEX command. 
Syntax:
DROP INDEX index;
To drop an index, you must be the owner of the index or have the DROP ANY
INDEX privilege. 
 
Altering an Index: 
To modify an existing table’s index by rebuilding, or reorganizing the index.
ALTER INDEX IndexName
ON TableName REBUILD;
Confirming Indexes :
You can check the different indexes present in a particular table given by the user or the
server itself and their uniqueness. 
Syntax:
select * from USER_INDEXES;
It will show you all the indexes present in the server, in which you can locate your own
tables too.
 
Renaming an index :
 You can use the system stored procedure sp_rename to rename any index in the database.
Syntax:
EXEC sp_rename
index_name,
new_index_name,
N'INDEX';

OBJECT PRIVILEGE
 System privileges—A system privilege gives a user the ability to perform a particular action,
or to perform an action on any schema objects of a particular type. ...
 Object privileges—An objectprivilege gives a user the ability to perform a particular action
on a specific schema object.

System privileges allow users to CREATE, ALTER, or DROP the database objects. An
object privilege is a privilege to perform a specific action on a particular table, function,
or package. For example, the right to delete rows from a table is an object privilege.

An object privilege is the right to perform a particular action on an object or to access


another user's object. Objects include tables, views, materialized views, indexes, synonyms,
sequences, cache groups, replication schemes and PL/SQL functions, procedures and
packages.
GRANT & REVOKE are the popular members of the SQL family. These are the types of
DCL commands that are used to assign permission to the users to perform a different
task. The GRANT command is used for permitting the users whereas the REVOKE
command is used for removing the authorization.

PL/SQL
The PL/SQL programming language was developed by Oracle Corporation in the late 1980s
as procedural extension language for SQL and the Oracle relational database. Following are
certain notable facts about PL/SQL −
 PL/SQL is a completely portable, high-performance transaction-processing language.
 PL/SQL provides a built-in, interpreted and OS independent programming
environment.
 PL/SQL can also directly be called from the command-line SQL*Plus interface.
 Direct call can also be made from external programming language calls to database.
 PL/SQL's general syntax is based on that of ADA and Pascal programming language.
 Apart from Oracle, PL/SQL is available in TimesTen in-memory database and IBM
DB2.
Features of PL/SQL
PL/SQL has the following features −

 PL/SQL is tightly integrated with SQL.


 It offers extensive error checking.
 It offers numerous data types.
 It offers a variety of programming structures.
 It supports structured programming through functions and procedures.
 It supports object-oriented programming.
 It supports the development of web applications and server pages.
Advantages of PL/SQL
PL/SQL has the following advantages −
 SQL is the standard database language and PL/SQL is strongly integrated with SQL.
PL/SQL supports both static and dynamic SQL. Static SQL supports DML operations
and transaction control from PL/SQL block. In Dynamic SQL, SQL allows embedding
DDL statements in PL/SQL blocks.
 PL/SQL allows sending an entire block of statements to the database at one time. This
reduces network traffic and provides high performance for the applications.
 PL/SQL gives high productivity to programmers as it can query, transform, and update
data in a database.
 PL/SQL saves time on design and debugging by strong features, such as exception
handling, encapsulation, data hiding, and object-oriented data types.
 Applications written in PL/SQL are fully portable.
 PL/SQL provides high security level.
 PL/SQL provides access to predefined SQL packages.
 PL/SQL provides support for Object-Oriented Programming.
 PL/SQL provides support for developing Web Applications and Server Pages.

Basic Syntax of PL/SQL which is a block-structured language; this means that the PL/SQL
programs are divided and written in logical blocks of code. Each block consists of three sub-
parts −

S.No Sections & Description

Declarations
1 This section starts with the keyword DECLARE. It is an optional section and defines all
variables, cursors, subprograms, and other elements to be used in the program.

Executable Commands
This section is enclosed between the keywords BEGIN and END and it is a mandatory
2 section. It consists of the executable PL/SQL statements of the program. It should have at
least one executable line of code, which may be just a NULL command to indicate that
nothing should be executed.

Exception Handling
3 This section starts with the keyword EXCEPTION. This optional section
contains exception(s) that handle errors in the program.

Every PL/SQL statement ends with a semicolon (;). PL/SQL blocks can be nested within
other PL/SQL blocks using BEGIN and END. Following is the basic structure of a PL/SQL
block −
DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
<exception handling>
END;
The 'Hello World' Example
DECLARE
message varchar2(20):= 'Hello, World!';
BEGIN
dbms_output.put_line(message);
END;
/
The end; line signals the end of the PL/SQL block. To run the code from the SQL command
line, you may need to type / at the beginning of the first blank line after the last line of the
code. When the above code is executed at the SQL prompt, it produces the following result −
Hello World

PL/SQL procedure successfully completed.


The PL/SQL Identifiers
PL/SQL identifiers are constants, variables, exceptions, procedures, cursors, and reserved
words. The identifiers consist of a letter optionally followed by more letters, numerals, dollar
signs, underscores, and number signs and should not exceed 30 characters.
By default, identifiers are not case-sensitive. So you can use integer or INTEGER to
represent a numeric value. You cannot use a reserved keyword as an identifier.
The PL/SQL Delimiters
A delimiter is a symbol with a special meaning. Following is the list of delimiters in PL/SQL

Delimiter Description

+, -, *, / Addition, subtraction/negation, multiplication, division

% Attribute indicator

' Character string delimiter

. Component selector

(,) Expression or list delimiter

: Host variable indicator

, Item separator

" Quoted identifier delimiter

= Relational operator

@ Remote access indicator

; Statement terminator

:= Assignment operator
=> Association operator

|| Concatenation operator

** Exponentiation operator

<<, >> Label delimiter (begin and end)

/*, */ Multi-line comment delimiter (begin and end)

-- Single-line comment indicator

.. Range operator

<, >, <=, >= Relational operators

<>, '=, ~=, ^= Different versions of NOT EQUAL


The PL/SQL Comments
Program comments are explanatory statements that can be included in the PL/SQL code that
you write and helps anyone reading its source code. All programming languages allow some
form of comments.
The PL/SQL supports single-line and multi-line comments. All characters available inside
any comment are ignored by the PL/SQL compiler. The PL/SQL single-line comments start
with the delimiter -- (double hyphen) and multi-line comments are enclosed by /* and */.
DECLARE
-- variable declaration
message varchar2(20):= 'Hello, World!';
BEGIN
/*
* PL/SQL executable statement(s)
*/
dbms_output.put_line(message);
END;
/
When the above code is executed at the SQL prompt, it produces the following result −
Hello World

PL/SQL procedure successfully completed.


PL/SQL Program Units
A PL/SQL unit is any one of the following −

 PL/SQL block
 Function
 Package
 Package body
 Procedure
 Trigger
 Type
 Type body
The PL/SQL variables, constants and parameters must have a valid data type, which specifies
a storage format, constraints, and a valid range of values. We will focus on the SCALAR and
the LOB data types in this chapter. The other two data types will be covered in other
chapters.

S.No Category & Description

Scalar
1
Single values with no internal components, such as a NUMBER, DATE, or BOOLEAN.

Large Object (LOB)


2 Pointers to large objects that are stored separately from other data items, such as text,
graphic images, video clips, and sound waveforms.

Composite
3 Data items that have internal components that can be accessed individually. For example,
collections and records.

Reference
4
Pointers to other data items.

PL/SQL Scalar Data Types and Subtypes


PL/SQL Scalar Data Types and Subtypes come under the following categories −

S.No Date Type & Description

Numeric
1
Numeric values on which arithmetic operations are performed.

Character
2
Alphanumeric values that represent single characters or strings of characters.

Boolean
3
Logical values on which logical operations are performed.
Datetime
4
Dates and times.

PL/SQL provides subtypes of data types. For example, the data type NUMBER has a subtype
called INTEGER. You can use the subtypes in your PL/SQL program to make the data types
compatible with data types in other programs while embedding the PL/SQL code in another
program, such as a Java program.
PL/SQL Numeric Data Types and Subtypes
Following table lists out the PL/SQL pre-defined numeric data types and their sub-types −

S.No Data Type & Description

PLS_INTEGER
1
Signed integer in range -2,147,483,648 through 2,147,483,647, represented in 32 bits

BINARY_INTEGER
2
Signed integer in range -2,147,483,648 through 2,147,483,647, represented in 32 bits

BINARY_FLOAT
3
Single-precision IEEE 754-format floating-point number

BINARY_DOUBLE
4
Double-precision IEEE 754-format floating-point number

NUMBER(prec, scale)
5 Fixed-point or floating-point number with absolute value in range 1E-130 to (but not
including) 1.0E126. A NUMBER variable can also represent 0

DEC(prec, scale)
6
ANSI specific fixed-point type with maximum precision of 38 decimal digits

DECIMAL(prec, scale)
7
IBM specific fixed-point type with maximum precision of 38 decimal digits

NUMERIC(pre, secale)
8
Floating type with maximum precision of 38 decimal digits
DOUBLE PRECISION
9 ANSI specific floating-point type with maximum precision of 126 binary digits
(approximately 38 decimal digits)

FLOAT
10 ANSI and IBM specific floating-point type with maximum precision of 126 binary digits
(approximately 38 decimal digits)

INT
11
ANSI specific integer type with maximum precision of 38 decimal digits

INTEGER
12
ANSI and IBM specific integer type with maximum precision of 38 decimal digits

SMALLINT
13
ANSI and IBM specific integer type with maximum precision of 38 decimal digits

REAL
14 Floating-point type with maximum precision of 63 binary digits (approximately 18
decimal digits)

Following is a valid declaration −


DECLARE
num1 INTEGER;
num2 REAL;
num3 DOUBLE PRECISION;
BEGIN
null;
END;
/
When the above code is compiled and executed, it produces the following result −
PL/SQL procedure successfully completed
PL/SQL Character Data Types and Subtypes

Following is the detail of PL/SQL pre-defined character data types and their sub-types −
S.No Data Type & Description

CHAR
1
Fixed-length character string with maximum size of 32,767 bytes

VARCHAR2
2
Variable-length character string with maximum size of 32,767 bytes

RAW
3 Variable-length binary or byte string with maximum size of 32,767 bytes, not interpreted
by PL/SQL

NCHAR
4
Fixed-length national character string with maximum size of 32,767 bytes

NVARCHAR2
5
Variable-length national character string with maximum size of 32,767 bytes

LONG
6
Variable-length character string with maximum size of 32,760 bytes

LONG RAW
7 Variable-length binary or byte string with maximum size of 32,760 bytes, not interpreted
by PL/SQL

ROWID
8
Physical row identifier, the address of a row in an ordinary table

UROWID
9
Universal row identifier (physical, logical, or foreign row identifier)

PL/SQL Boolean Data Types


The BOOLEAN data type stores logical values that are used in logical operations. The
logical values are the Boolean values TRUE and FALSE and the value NULL.
However, SQL has no data type equivalent to BOOLEAN. Therefore, Boolean values cannot
be used in −
 SQL statements
 Built-in SQL functions (such as TO_CHAR)
 PL/SQL functions invoked from SQL statements
PL/SQL Datetime and Interval Types
The DATE datatype is used to store fixed-length datetimes, which include the time of day in
seconds since midnight. Valid dates range from January 1, 4712 BC to December 31, 9999
AD.
The default date format is set by the Oracle initialization parameter NLS_DATE_FORMAT.
For example, the default might be 'DD-MON-YY', which includes a two-digit number for the
day of the month, an abbreviation of the month name, and the last two digits of the year. For
example, 01-OCT-12.
Each DATE includes the century, year, month, day, hour, minute, and second. The following
table shows the valid values for each field −

Field Name Valid Datetime Values Valid Interval Values

YEAR -4712 to 9999 (excluding year 0) Any nonzero integer

MONTH 01 to 12 0 to 11

01 to 31 (limited by the values of MONTH


DAY and YEAR, according to the rules of the Any nonzero integer
calendar for the locale)

HOUR 00 to 23 0 to 23

MINUTE 00 to 59 0 to 59

00 to 59.9(n), where 9(n) is the precision of 0 to 59.9(n), where 9(n)


time fractional seconds is the precision of
SECOND
interval fractional
seconds

-12 to 14 (range accommodates daylight


TIMEZONE_HOUR Not applicable
savings time changes)

TIMEZONE_MINUTE 00 to 59 Not applicable

Found in the dynamic performance view


TIMEZONE_REGION Not applicable
V$TIMEZONE_NAMES

Found in the dynamic performance view


TIMEZONE_ABBR Not applicable
V$TIMEZONE_NAMES
PL/SQL Large Object (LOB) Data Types
Large Object (LOB) data types refer to large data items such as text, graphic images, video
clips, and sound waveforms. LOB data types allow efficient, random, piecewise access to this
data. Following are the predefined PL/SQL LOB data types −
Data Type Description Size

Used to store large binary objects in System-dependent. Cannot


BFILE
operating system files outside the database. exceed 4 gigabytes (GB).

Used to store large binary objects in the 8 to 128 terabytes (TB)


BLOB
database.

Used to store large blocks of character data 8 to 128 TB


CLOB
in the database.

Used to store large blocks of NCHAR data 8 to 128 TB


NCLOB
in the database.
PL/SQL User-Defined Subtypes
A subtype is a subset of another data type, which is called its base type. A subtype has the
same valid operations as its base type, but only a subset of its valid values.
PL/SQL predefines several subtypes in package STANDARD. For example, PL/SQL
predefines the subtypes CHARACTER and INTEGER as follows −
SUBTYPE CHARACTER IS CHAR;
SUBTYPE INTEGER IS NUMBER(38,0);
You can define and use your own subtypes. The following program illustrates defining and
using a user-defined subtype −
DECLARE
SUBTYPE name IS char(20);
SUBTYPE message IS varchar2(100);
salutation name;
greetings message;
BEGIN
salutation := 'Reader ';
greetings := 'Welcome to the World of PL/SQL';
dbms_output.put_line('Hello ' || salutation || greetings);
END;
/
When the above code is executed at the SQL prompt, it produces the following result −
Hello Reader Welcome to the World of PL/SQL

PL/SQL procedure successfully completed.


NULLs in PL/SQL
PL/SQL NULL values represent missing or unknown data and they are not an integer, a
character, or any other specific data type. Note that NULL is not the same as an empty data
string or the null character value '\0'. A null can be assigned but it cannot be equated with
anything, including itself.
Variables in Pl/SQL. A variable is nothing but a name given to a storage area that our
programs can manipulate. Each variable in PL/SQL has a specific data type, which
determines the size and the layout of the variable's memory; the range of values that can be
stored within that memory and the set of operations that can be applied to the variable.
The name of a PL/SQL variable consists of a letter optionally followed by more letters,
numerals, dollar signs, underscores, and number signs and should not exceed 30 characters.
By default, variable names are not case-sensitive. You cannot use a reserved PL/SQL
keyword as a variable name.
PL/SQL programming language allows to define various types of variables, such as date time
data types, records, collections, etc. which we will cover in subsequent chapters. For this
chapter, let us study only basic variable types.
Variable Declaration in PL/SQL
PL/SQL variables must be declared in the declaration section or in a package as a global
variable. When you declare a variable, PL/SQL allocates memory for the variable's value and
the storage location is identified by the variable name.
The syntax for declaring a variable is −
variable_name [CONSTANT] datatype [NOT NULL] [:= | DEFAULT initial_value]
Where, variable_name is a valid identifier in PL/SQL, datatype must be a valid PL/SQL data
type or any user defined data type which we already have discussed in the last chapter. Some
valid variable declarations along with their definition are shown below −
sales number(10, 2);
pi CONSTANT double precision := 3.1415;
name varchar2(25);
address varchar2(100);
When you provide a size, scale or precision limit with the data type, it is called a constrained
declaration. Constrained declarations require less memory than unconstrained declarations.
For example −
sales number(10, 2);
name varchar2(25);
address varchar2(100);
Initializing Variables in PL/SQL
Whenever you declare a variable, PL/SQL assigns it a default value of NULL. If you want to
initialize a variable with a value other than the NULL value, you can do so during the
declaration, using either of the following −
 The DEFAULT keyword
 The assignment operator
For example −
counter binary_integer := 0;
greetings varchar2(20) DEFAULT 'Have a Good Day';
You can also specify that a variable should not have a NULL value using the NOT
NULL constraint. If you use the NOT NULL constraint, you must explicitly assign an initial
value for that variable.
It is a good programming practice to initialize variables properly otherwise, sometimes
programs would produce unexpected results. Try the following example which makes use of
various types of variables −
DECLARE
a integer := 10;
b integer := 20;
c integer;
f real;
BEGIN
c := a + b;
dbms_output.put_line('Value of c: ' || c);
f := 70.0/3.0;
dbms_output.put_line('Value of f: ' || f);
END;
/
When the above code is executed, it produces the following result −
Value of c: 30
Value of f: 23.333333333333333333

PL/SQL procedure successfully completed.


Variable Scope in PL/SQL
PL/SQL allows the nesting of blocks, i.e., each program block may contain another inner
block. If a variable is declared within an inner block, it is not accessible to the outer block.
However, if a variable is declared and accessible to an outer block, it is also accessible to all
nested inner blocks. There are two types of variable scope −
 Local variables − Variables declared in an inner block and not accessible to outer
blocks.
 Global variables − Variables declared in the outermost block or a package.
Following example shows the usage of Local and Global variables in its simple form −
DECLARE
-- Global variables
num1 number := 95;
num2 number := 85;
BEGIN
dbms_output.put_line('Outer Variable num1: ' || num1);
dbms_output.put_line('Outer Variable num2: ' || num2);
DECLARE
-- Local variables
num1 number := 195;
num2 number := 185;
BEGIN
dbms_output.put_line('Inner Variable num1: ' || num1);
dbms_output.put_line('Inner Variable num2: ' || num2);
END;
END;
/
When the above code is executed, it produces the following result −
Outer Variable num1: 95
Outer Variable num2: 85
Inner Variable num1: 195
Inner Variable num2: 185

PL/SQL procedure successfully completed.


Assigning SQL Query Results to PL/SQL Variables
You can use the SELECT INTO statement of SQL to assign values to PL/SQL variables.
For each item in the SELECT list, there must be a corresponding, type-compatible variable
in the INTO list. The following example illustrates the concept. Let us create a table named
CUSTOMERS −
(For SQL statements, please refer to the SQL tutorial)
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25),
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);

Table Created
Let us now insert some values in the table −
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)


VALUES (2, 'Khilan', 25, 'Delhi', 1500.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)


VALUES (3, 'kaushik', 23, 'Kota', 2000.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)


VALUES (4, 'Chaitali', 25, 'Mumbai', 6500.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)


VALUES (5, 'Hardik', 27, 'Bhopal', 8500.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)


VALUES (6, 'Komal', 22, 'MP', 4500.00 );
The following program assigns values from the above table to PL/SQL variables using
the SELECT INTO clause of SQL −
DECLARE
c_id customers.id%type := 1;
c_name customers.name%type;
c_addr customers.address%type;
c_sal customers.salary%type;
BEGIN
SELECT name, address, salary INTO c_name, c_addr, c_sal
FROM customers
WHERE id = c_id;
dbms_output.put_line
('Customer ' ||c_name || ' from ' || c_addr || ' earns ' || c_sal);
END;
/
When the above code is executed, it produces the following result −
Customer Ramesh from Ahmedabad earns 2000

PL/SQL procedure completed successfully

Programming languages provide various control structures that allow for more complicated
execution paths.
A loop statement allows us to execute a statement or group of statements multiple times and
following is the general form of a loop statement in most of the programming languages −

PL/SQL provides the following types of loop to handle the looping requirements. Click the
following links to check their detail.

S.No Loop Type & Description


PL/SQL Basic LOOP

1 In this loop structure, sequence of statements is enclosed between the LOOP and the
END LOOP statements. At each iteration, the sequence of statements is executed and
then control resumes at the top of the loop.

PL/SQL WHILE LOOP


2
Repeats a statement or group of statements while a given condition is true. It tests the
condition before executing the loop body.

PL/SQL FOR LOOP


3
Execute a sequence of statements multiple times and abbreviates the code that manages
the loop variable.

Nested loops in PL/SQL


4
You can use one or more loop inside any another basic loop, while, or for loop.

Labeling a PL/SQL Loop


PL/SQL loops can be labeled. The label should be enclosed by double angle brackets (<< and
>>) and appear at the beginning of the LOOP statement. The label name can also appear at
the end of the LOOP statement. You may use the label in the EXIT statement to exit from the
loop.
The following program illustrates the concept −
DECLARE
i number(1);
j number(1);
BEGIN
<< outer_loop >>
FOR i IN 1..3 LOOP
<< inner_loop >>
FOR j IN 1..3 LOOP
dbms_output.put_line('i is: '|| i || ' and j is: ' || j);
END loop inner_loop;
END loop outer_loop;
END;
/
When the above code is executed at the SQL prompt, it produces the following result −
i is: 1 and j is: 1
i is: 1 and j is: 2
i is: 1 and j is: 3
i is: 2 and j is: 1
i is: 2 and j is: 2
i is: 2 and j is: 3
i is: 3 and j is: 1
i is: 3 and j is: 2
i is: 3 and j is: 3

PL/SQL procedure successfully completed.


The Loop Control Statements
Loop control statements change execution from its normal sequence. When execution leaves
a scope, all automatic objects that were created in that scope are destroyed.
PL/SQL supports the following control statements. Labeling loops also help in taking the
control outside a loop. Click the following links to check their details.

S.No Control Statement & Description

EXIT statement
1
The Exit statement completes the loop and control passes to the statement immediately
after the END LOOP.

CONTINUE statement
2
Causes the loop to skip the remainder of its body and immediately retest its condition
prior to reiterating.

GOTO statement
3
Transfers control to the labeled statement. Though it is not advised to use the GOTO
statement in your program.

PL/SQL

By extending SQL, PL/SQL offers a unique combination of power and ease of use. You can
manipulate Oracle data flexibly and safely because PL/SQL fully supports all SQL data
manipulation statements (except EXPLAIN PLAN), transaction control statements, functions,
pseudo columns, and operators. PL/SQL also conforms to the current ANSI/ISO SQL
standard.

 Data Manipulation
To manipulate Oracle data you can include DML operations, such as INSERT, UPDATE,
and DELETE statements, directly in PL/SQL programs, without any special notation. You
can also include the SQL COMMIT statement directly in a PL/SQL program.

If you want to learn SQL in-depth, check out this SQL Tutorial!

 Transaction Control

Oracle is transaction-oriented; that is, Oracle uses transactions to ensure data integrity. A
transaction is a series of SQL data manipulation statements that does a logical unit of work
You use the COMMIT, ROLLBACK, SAVEPOINT, and SET TRANSACTION commands
to control transactions. COMMIT makes permanent any database changes made during the
current transaction. ROLLBACK ends the current transaction and undoes any changes made
since the transaction began. SAVEPOINT marks the current point in the processing of a
transaction. Used with ROLLBACK, SAVEPOINT undoes part of a transaction. SET
TRANSACTION sets transaction properties such as read-write access and isolation level.

SQL Functions

Example: Calling the SQL COUNT Function in PL/SQL


DECLARE
job_count NUMBER;
emp_count NUMBER;
BEGIN
SELECT COUNT(DISTINCT job_id) INTO job_count FROM employees;
SELECT COUNT(*) INTO emp_count FROM employees;
END;
/

SQL Pseudocolumns

PL/SQL recognizes the SQL pseudo columns CURRVAL, LEVEL, NEXTVAL, ROWID,
and ROWNUM. However, there are limitations on the use of pseudo columns, including the
restriction on the use of some pseudo columns in assignments or conditional tests.
 CURRVAL and NEXTVAL – CURRVAL returns the current value in a specified
sequence. A reference to NEXTVAL stores the current sequence number in CURRVAL.
NEXTVAL increments the sequence and returns the next value. To get the current or
next value in a sequence, use dot notation:
sequence_name.CURRVAL
sequence_name.NEXTVAL

 LEVEL – Use LEVEL with the SELECT CONNECT BY statement to organize rows


from a database table into a tree structure.
 ROWID – ROWID returns the rowid (binary address) of a row in a database table. You
can use variables of type UROWID to store rowids in a readable format.
 ROWNUM – ROWNUM returns a number indicating the order in which a row was
selected from a table.

SQL Operators

 Comparison Operators

 Set Operators

Set operators combine the results of two queries into one result. INTERSECT returns all
distinct rows selected by both queries. MINUS returns all distinct rows selected by the first
query but not by the second. UNION returns all distinct rows selected by either query.
UNION ALL returns all rows selected by either query, including all duplicates.
 Row Operators

Row operators return or reference particular rows. ALL retain duplicate rows in the result of
a query or in an aggregate expression. DISTINCT eliminates duplicate rows from the result
of a query or from an aggregate expression. PRIOR refers to the parent row of the current
row returned by a tree-structured query.

Transaction Processing in PL/SQL

Transaction processing with PL/SQL using SQL COMMIT, SAVEPOINT, and ROLLBACK
statements that ensure the consistency of a database.
 COMMIT – The COMMIT statement ends the current transaction, making any changes
made during that transaction permanent, and visible to other users.
 ROLLBACK – The ROLLBACK statement ends the current transaction and undoes
any changes made during that transaction. If you make a mistake, such as deleting the
wrong row from a table, a rollback restores the original data. If you cannot finish a
transaction because an exception is raised or a SQL statement fails, a rollback lets you
take corrective action and perhaps start over.
 SAVEPOINT – SAVEPOINT names and marks the current point in the processing of a
transaction. Savepoints let you roll back part of a transaction instead of the whole
transaction. The number of active savepoints for each session is unlimited.
PL/SQL EXCEPTION 
An exception is an error condition during a program execution. PL/SQL supports
programmers to catch such conditions using EXCEPTION block in the program and an
appropriate action is taken against the error condition. There are two types of exceptions −
 System-defined exceptions
 User-defined exceptions
Syntax for Exception Handling
The general syntax for exception handling is as follows. Here you can list down as many
exceptions as you can handle. The default exception will be handled using WHEN others
THEN −
DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
<exception handling goes here >
WHEN exception1 THEN
exception1-handling-statements
........
WHEN others THEN
exception3-handling-statements
END;
Raising Exceptions
Exceptions are raised by the database server automatically whenever there is any internal
database error, but exceptions can be raised explicitly by the programmer by using the
command RAISE. Following is the simple syntax for raising an exception −
DECLARE
exception_name EXCEPTION;
BEGIN
IF condition THEN
RAISE exception_name;
END IF;
EXCEPTION
WHEN exception_name THEN
statement;
END;

User-defined Exceptions
PL/SQL allows you to define your own exceptions according to the need of your program. A
user-defined exception must be declared and then raised explicitly, using either a RAISE
statement or the procedure DBMS_STANDARD.RAISE_APPLICATION_ERROR.
The syntax for declaring an exception is −
DECLARE
my-exception EXCEPTION;

Pre-defined Exceptions
PL/SQL provides many pre-defined exceptions, which are executed when any database rule
is violated by a program. For example, the predefined exception NO_DATA_FOUND is
raised when a SELECT INTO statement returns no rows. The following table lists few of the
important pre-defined exceptions −

Oracle SQLCOD
Exception Description
Error E

It is raised when a null object is


ACCESS_INTO_NULL 06530 -6530
automatically assigned a value.

It is raised when none of the choices in


CASE_NOT_FOUND 06592 -6592 the WHEN clause of a CASE statement is
selected, and there is no ELSE clause.

It is raised when a program attempts to


apply collection methods other than
EXISTS to an uninitialized nested table or
COLLECTION_IS_NULL 06531 -6531
varray, or the program attempts to assign
values to the elements of an uninitialized
nested table or varray.
It is raised when duplicate values are
DUP_VAL_ON_INDEX 00001 -1 attempted to be stored in a column with
unique index.

It is raised when attempts are made to


make a cursor operation that is not
INVALID_CURSOR 01001 -1001
allowed, such as closing an unopened
cursor.

It is raised when the conversion of a


character string into a number fails
INVALID_NUMBER 01722 -1722
because the string does not represent a
valid number.

It is raised when a program attempts to


LOGIN_DENIED 01017 -1017 log on to the database with an invalid
username or password.

It is raised when a SELECT INTO


NO_DATA_FOUND 01403 +100
statement returns no rows.

It is raised when a database call is issued


NOT_LOGGED_ON 01012 -1012
without being connected to the database.

It is raised when PL/SQL has an internal


PROGRAM_ERROR 06501 -6501
problem.

It is raised when a cursor fetches value in


ROWTYPE_MISMATCH 06504 -6504
a variable having incompatible data type.

It is raised when a member method is


SELF_IS_NULL 30625 -30625 invoked, but the instance of the object
type was not initialized.

It is raised when PL/SQL ran out of


STORAGE_ERROR 06500 -6500
memory or memory was corrupted.

It is raised when a SELECT INTO


TOO_MANY_ROWS 01422 -1422
statement returns more than one row.

VALUE_ERROR 06502 -6502 It is raised when an arithmetic,


conversion, truncation, or sizeconstraint
error occurs.

It is raised when an attempt is made to


ZERO_DIVIDE 01476 1476
divide a number by zero.

PL/SQL - Cursors

A cursor is a pointer to this context area. PL/SQL controls the context area through a cursor.
A cursor holds the rows (one or more) returned by a SQL statement. The set of rows the
cursor holds is referred to as the active set.
You can name a cursor so that it could be referred to in a program to fetch and process the
rows returned by the SQL statement, one at a time. There are two types of cursors −
Implicit cursors
Explicit cursors
Implicit Cursors
Implicit cursors are automatically created by Oracle whenever an SQL statement is executed,
when there is no explicit cursor for the statement. Programmers cannot control the implicit
cursors and the information in it.
Whenever a DML statement (INSERT, UPDATE and DELETE) is issued, an implicit cursor
is associated with this statement. For INSERT operations, the cursor holds the data that needs
to be inserted. For UPDATE and DELETE operations, the cursor identifies the rows that
would be affected.
In PL/SQL, you can refer to the most recent implicit cursor as the SQL cursor, which always
has attributes such as %FOUND, %ISOPEN, %NOTFOUND, and %ROWCOUNT. The
SQL cursor has additional
attributes, %BULK_ROWCOUNT and %BULK_EXCEPTIONS, designed for use with
the FORALL statement. The following table provides the description of the most used
attributes −

S.No Attribute & Description

%FOUND
1 Returns TRUE if an INSERT, UPDATE, or DELETE statement affected one or more
rows or a SELECT INTO statement returned one or more rows. Otherwise, it returns
FALSE.

2 %NOTFOUND
The logical opposite of %FOUND. It returns TRUE if an INSERT, UPDATE, or
DELETE statement affected no rows, or a SELECT INTO statement returned no rows.
Otherwise, it returns FALSE.

%ISOPEN
3
Always returns FALSE for implicit cursors, because Oracle closes the SQL cursor
automatically after executing its associated SQL statement.

%ROWCOUNT
4
Returns the number of rows affected by an INSERT, UPDATE, or DELETE statement,
or returned by a SELECT INTO statement.

Any SQL cursor attribute will be accessed as sql%attribute_name as shown below in the
example.
Example
We will be using the CUSTOMERS table we had created and used in the previous chapters.
Select * from customers;

+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
+----+----------+-----+-----------+----------+
The following program will update the table and increase the salary of each customer by 500
and use the SQL%ROWCOUNT attribute to determine the number of rows affected −
DECLARE
total_rows number(2);
BEGIN
UPDATE customers
SET salary = salary + 500;
IF sql%notfound THEN
dbms_output.put_line('no customers selected');
ELSIF sql%found THEN
total_rows := sql%rowcount;
dbms_output.put_line( total_rows || ' customers selected ');
END IF;
END;
/
When the above code is executed at the SQL prompt, it produces the following result −
6 customers selected

PL/SQL procedure successfully completed.


If you check the records in customers table, you will find that the rows have been updated −
Select * from customers;

+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2500.00 |
| 2 | Khilan | 25 | Delhi | 2000.00 |
| 3 | kaushik | 23 | Kota | 2500.00 |
| 4 | Chaitali | 25 | Mumbai | 7000.00 |
| 5 | Hardik | 27 | Bhopal | 9000.00 |
| 6 | Komal | 22 | MP | 5000.00 |
+----+----------+-----+-----------+----------+
Explicit Cursors
Explicit cursors are programmer-defined cursors for gaining more control over the context
area. An explicit cursor should be defined in the declaration section of the PL/SQL Block. It
is created on a SELECT Statement which returns more than one row.
The syntax for creating an explicit cursor is −
CURSOR cursor_name IS select_statement;
Working with an explicit cursor includes the following steps −
 Declaring the cursor for initializing the memory
 Opening the cursor for allocating the memory
 Fetching the cursor for retrieving the data
 Closing the cursor to release the allocated memory
Declaring the Cursor
Declaring the cursor defines the cursor with a name and the associated SELECT statement.
For example −
CURSOR c_customers IS
SELECT id, name, address FROM customers;
Opening the Cursor
Opening the cursor allocates the memory for the cursor and makes it ready for fetching the
rows returned by the SQL statement into it. For example, we will open the above defined
cursor as follows −
OPEN c_customers;
Fetching the Cursor
Fetching the cursor involves accessing one row at a time. For example, we will fetch rows
from the above-opened cursor as follows −
FETCH c_customers INTO c_id, c_name, c_addr;
Closing the Cursor
Closing the cursor means releasing the allocated memory. For example, we will close the
above-opened cursor as follows −
CLOSE c_customers;

Implicit_cursor_attribute :

These attributes are %ISOPEN, %FOUND, %NOTFOUND, and %ROWCOUNT.


%ISOPEN
This attribute is used to determine whether a cursor is in the open state. When a
cursor is passed as a parameter to a function or procedure, it is useful to know
(before attempting to open the cursor) whether the cursor is already open.
%FOUND
This attribute is used to determine whether a cursor contains rows after the
execution of a FETCH statement. If FETCH statement execution was successful, the
%FOUND attribute has a value of true. If FETCH statement execution was not
successful, the %FOUND attribute has a value of false. The result is unknown
when:
 The value of cursor-variable-name is null
 The underlying cursor of cursor-variable-name is not open
 The %FOUND attribute is evaluated before the first FETCH statement was
executed against the underlying cursor
 FETCH statement execution returns an error
The %FOUND attribute provides an efficient alternative to using a condition handler
that checks for the error that is returned when no more rows remain to be fetched.
%NOTFOUND
This attribute is the logical opposite of the %FOUND attribute.
%ROWCOUNT
This attribute is used to determine the number of rows that have been fetched since a
cursor was opened.

Explicit cursor 
An explicit cursor is a named pointer to a private SQL area that stores information for
processing a specific query or DML statement—typically, one that returns or affects multiple
rows. You can use an explicit cursor to retrieve the rows of a result set one at a time.
Before using an explicit cursor, you must declare and define it. You can either declare it first
(with cursor_declaration) and then define it later in the same block, subprogram, or package
(with cursor_definition) or declare and define it at the same time (with cursor_definition).
An explicit cursor declaration and definition are also called a cursor
specification and cursor body, respectively.

The Cursor FOR Loop


A cursor FOR loop is a loop that is associated with (and actually defined by) an explicit
cursor or a SELECT statement incorporated directly within the loop boundary. Use the cursor
FOR loop only if you need to fetch and process each and every record from a cursor, which is
often the case with cursors.
Here is the basic syntax of a cursor FOR loop:

FOR record IN { cursor_name | (explicit SELECT statement) }


LOOP
executable statement(s)
END LOOP;

where record is a record declared implicitly by PL/SQL with the %ROWTYPE attribute


against the cursor specified by cursor_name.

PROCEDURE
A subprogram is a program unit/module that performs a particular task. These subprograms
are combined to form larger programs. This is basically called the 'Modular design'. A
subprogram can be invoked by another subprogram or program which is called the calling
program.
A subprogram can be created −

 At the schema level


 Inside a package
 Inside a PL/SQL block
At the schema level, subprogram is a standalone subprogram. It is created with the
CREATE PROCEDURE or the CREATE FUNCTION statement. It is stored in the database
and can be deleted with the DROP PROCEDURE or DROP FUNCTION statement.
A subprogram created inside a package is a packaged subprogram. It is stored in the
database and can be deleted only when the package is deleted with the DROP PACKAGE
statement. We will discuss packages in the chapter 'PL/SQL - Packages'.
PL/SQL subprograms are named PL/SQL blocks that can be invoked with a set of
parameters. PL/SQL provides two kinds of subprograms −
 Functions − These subprograms return a single value; mainly used to compute and
return a value.
 Procedures − These subprograms do not return a value directly; mainly used to
perform an action.
This chapter is going to cover important aspects of a PL/SQL procedure. We will
discuss PL/SQL function in the next chapter.
Parts of a PL/SQL Subprogram
Each PL/SQL subprogram has a name, and may also have a parameter list. Like anonymous
PL/SQL blocks, the named blocks will also have the following three parts −

S.No Parts & Description

Declarative Part

1 It is an optional part. However, the declarative part for a subprogram does not start with
the DECLARE keyword. It contains declarations of types, cursors, constants, variables,
exceptions, and nested subprograms. These items are local to the subprogram and cease
to exist when the subprogram completes execution.

2 Executable Part
This is a mandatory part and contains statements that perform the designated action.

3 Exception-handling
This is again an optional part. It contains the code that handles run-time errors.

Creating a Procedure
A procedure is created with the CREATE OR REPLACE PROCEDURE statement. The
simplified syntax for the CREATE OR REPLACE PROCEDURE statement is as follows −
CREATE [OR REPLACE] PROCEDURE procedure_name
[(parameter_name [IN | OUT | IN OUT] type [, ...])]
{IS | AS}
BEGIN
< procedure_body >
END procedure_name;
Where,
 procedure-name specifies the name of the procedure.
 [OR REPLACE] option allows the modification of an existing procedure.
 The optional parameter list contains name, mode and types of the
parameters. IN represents the value that will be passed from outside and OUT
represents the parameter that will be used to return a value outside of the procedure.
 procedure-body contains the executable part.
 The AS keyword is used instead of the IS keyword for creating a standalone
procedure.
Example
The following example creates a simple procedure that displays the string 'Hello World!' on
the screen when executed.
CREATE OR REPLACE PROCEDURE greetings
AS
BEGIN
dbms_output.put_line('Hello World!');
END;
/
When the above code is executed using the SQL prompt, it will produce the following result

Procedure created.
Executing a Standalone Procedure
A standalone procedure can be called in two ways −
 Using the EXECUTE keyword
 Calling the name of the procedure from a PL/SQL block
The above procedure named 'greetings' can be called with the EXECUTE keyword as −
EXECUTE greetings;
The above call will display −
Hello World

PL/SQL procedure successfully completed.


The procedure can also be called from another PL/SQL block −
BEGIN
greetings;
END;
/
The above call will display −
Hello World

PL/SQL procedure successfully completed.


Deleting a Standalone Procedure
A standalone procedure is deleted with the DROP PROCEDURE statement. Syntax for
deleting a procedure is −
DROP PROCEDURE procedure-name;
You can drop the greetings procedure by using the following statement −
DROP PROCEDURE greetings;

FUNCTION
 A function is same as a procedure except that it returns a value. Therefore, all the discussions
of the previous chapter are true for functions too.
Creating a Function
A standalone function is created using the CREATE FUNCTION statement. The simplified
syntax for the CREATE OR REPLACE PROCEDURE statement is as follows −
CREATE [OR REPLACE] FUNCTION function_name
[(parameter_name [IN | OUT | IN OUT] type [, ...])]
RETURN return_datatype
{IS | AS}
BEGIN
< function_body >
END [function_name];
Where,
 function-name specifies the name of the function.
 [OR REPLACE] option allows the modification of an existing function.
 The optional parameter list contains name, mode and types of the parameters. IN
represents the value that will be passed from outside and OUT represents the
parameter that will be used to return a value outside of the procedure.
 The function must contain a return statement.
 The RETURN clause specifies the data type you are going to return from the function.
 function-body contains the executable part.
 The AS keyword is used instead of the IS keyword for creating a standalone function.
Calling a Function
While creating a function, you give a definition of what the function has to do. To use a
function, you will have to call that function to perform the defined task. When a program
calls a function, the program control is transferred to the called function.
A called function performs the defined task and when its return statement is executed or
when the last end statement is reached, it returns the program control back to the main
program.
To call a function, you simply need to pass the required parameters along with the function
name and if the function returns a value, then you can store the returned value. Following
program calls the function totalCustomers from an anonymous block −
DECLARE
c number(2);
BEGIN
c := totalCustomers();
dbms_output.put_line('Total no. of Customers: ' || c);
END;
/
When the above code is executed at the SQL prompt, it produces the following result −
Total no. of Customers: 6

PL/SQL procedure successfully completed.


PL/SQL Recursive Functions
We have seen that a program or subprogram may call another subprogram. When a
subprogram calls itself, it is referred to as a recursive call and the process is known
as recursion.
TRIGGERS
Triggers are stored programs, which are automatically executed or fired when some events
occur. Triggers are, in fact, written to be executed in response to any of the following events

 A database manipulation (DML) statement (DELETE, INSERT, or UPDATE)
 A database definition (DDL) statement (CREATE, ALTER, or DROP).
 A database operation (SERVERERROR, LOGON, LOGOFF, STARTUP, or
SHUTDOWN).

Triggers can be defined on the table, view, schema, or database with which the event is
associated.
Benefits of Triggers
Triggers can be written for the following purposes −

 Generating some derived column values automatically


 Enforcing referential integrity
 Event logging and storing information on table access
 Auditing
 Synchronous replication of tables
 Imposing security authorizations
 Preventing invalid transactions
Creating Triggers
The syntax for creating a trigger is −
CREATE [OR REPLACE ] TRIGGER trigger_name
{BEFORE | AFTER | INSTEAD OF }
{INSERT [OR] | UPDATE [OR] | DELETE}
[OF col_name]
ON table_name
[REFERENCING OLD AS o NEW AS n]
[FOR EACH ROW]
WHEN (condition)
DECLARE
Declaration-statements
BEGIN
Executable-statements
EXCEPTION
Exception-handling-statements
END;
Where,
 CREATE [OR REPLACE] TRIGGER trigger_name − Creates or replaces an existing
trigger with the trigger_name.
 {BEFORE | AFTER | INSTEAD OF} − This specifies when the trigger will be
executed. The INSTEAD OF clause is used for creating trigger on a view.
 {INSERT [OR] | UPDATE [OR] | DELETE} − This specifies the DML operation.
 [OF col_name] − This specifies the column name that will be updated.
 [ON table_name] − This specifies the name of the table associated with the trigger.
 [REFERENCING OLD AS o NEW AS n] − This allows you to refer new and old
values for various DML statements, such as INSERT, UPDATE, and DELETE.
 [FOR EACH ROW] − This specifies a row-level trigger, i.e., the trigger will be
executed for each row being affected. Otherwise the trigger will execute just once
when the SQL statement is executed, which is called a table level trigger.
 WHEN (condition) − This provides a condition for rows for which the trigger would
fire. This clause is valid only for row-level triggers.

PACKAGES
Packages are schema objects that groups logically related PL/SQL types, variables, and
subprograms.
A package will have two mandatory parts −

 Package specification
 Package body or definition
Package Specification
The specification is the interface to the package. It just DECLARES the types, variables,
constants, exceptions, cursors, and subprograms that can be referenced from outside the
package. In other words, it contains all information about the content of the package, but
excludes the code for the subprograms.
All objects placed in the specification are called public objects. Any subprogram not in the
package specification but coded in the package body is called a private object.
The following code snippet shows a package specification having a single procedure. You
can have many global variables defined and multiple procedures or functions inside a
package.
CREATE PACKAGE cust_sal AS
PROCEDURE find_sal(c_id customers.id%type);
END cust_sal;
/
When the above code is executed at the SQL prompt, it produces the following result −
Package created.
Package Body
The package body has the codes for various methods declared in the package specification
and other private declarations, which are hidden from the code outside the package.
The CREATE PACKAGE BODY Statement is used for creating the package body. The
following code snippet shows the package body declaration for the cust_sal package created
above. I assumed that we already have CUSTOMERS table created in our database as
mentioned in the PL/SQL - Variables chapter.
CREATE OR REPLACE PACKAGE BODY cust_sal AS

PROCEDURE find_sal(c_id customers.id%TYPE) IS


c_sal customers.salary%TYPE;
BEGIN
SELECT salary INTO c_sal
FROM customers
WHERE id = c_id;
dbms_output.put_line('Salary: '|| c_sal);
END find_sal;
END cust_sal;
/
When the above code is executed at the SQL prompt, it produces the following result −
Package body created.
Using the Package Elements
The package elements (variables, procedures or functions) are accessed with the following
syntax −
package_name.element_name;
Consider, we already have created the above package in our database schema, the following
program uses the find_sal method of the cust_sal package −
DECLARE
code customers.id%type := &cc_id;
BEGIN
cust_sal.find_sal(code);
END;
/
When the above code is executed at the SQL prompt, it prompts to enter the customer ID and
when you enter an ID, it displays the corresponding salary as follows −
Enter value for cc_id: 1
Salary: 3000

PL/SQL procedure successfully completed.

You might also like