Uploaded by Sharath

UNIT IV DBMS

advertisement
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 the undesirable characteristics like Insertion, Update and Deletion
Anomalies.
o
Normalization divides the larger table into the smaller table and links them using
relationship.
o
The normal form is used to reduce redundancy from the database table.
Types of Normal Forms
There are the four types of normal forms:
Normal
Description
Form
1NF
A relation is in 1NF if it contains an atomic value.
2NF
A relation will be in 2NF if it is in 1NF and all non-key attributes are fully
functional dependent on the primary key.
3NF
A relation will be in 3NF if it is in 2NF and no transition dependency exists.
4NF
A relation will be in 4NF if it is in Boyce Codd normal form and has no
multi-valued dependency.
5NF
A relation is in 5NF if it is in 4NF and not contains any join dependency and
joining should be lossless.
First Normal Form (1NF)
o
A relation will be 1NF if it contains an atomic value.
o
It states that an attribute of a table cannot hold multiple values. It must hold only singlevalued attribute.
o
First normal form disallows the multi-valued attribute, composite attribute, and their
combinations.
Example: Relation EMPLOYEE is not in 1NF because of multi-valued attribute EMP_PHONE.
EMPLOYEE table:
EMP_ID
EMP_NAME
EMP_PHONE
EMP_STATE
14
John
7272826385,
UP
9064738238
20
Harry
8574783832
Bihar
12
Sam
7390372389,
Punjab
8589830302
The decomposition of the EMPLOYEE table into 1NF has been shown below:
EMP_ID
EMP_NAME
EMP_PHONE
EMP_STATE
14
John
7272826385
UP
14
John
9064738238
UP
20
Harry
8574783832
Bihar
12
Sam
7390372389
Punjab
12
Sam
8589830302
Punjab
Second Normal Form (2NF)
o
In the 2NF, relational must be in 1NF.
o
In the second normal form, all non-key attributes are fully functional dependent on the
primary key
Example: Let's assume, a school can store the data of teachers and the subjects they teach. In a
school, a teacher can teach more than one subject.
TEACHER table
TEACHER_ID
SUBJECT
TEACHER_AGE
25
Chemistry
30
25
Biology
30
47
English
35
83
Math
38
83
Computer
38
In the given table, non-prime attribute TEACHER_AGE is dependent on TEACHER_ID which
is a proper subset of a candidate key. That's why it violates the rule for 2NF.
To convert the given table into 2NF, we decompose it into two tables:
TEACHER_DETAIL table:
TEACHER_ID
TEACHER_AGE
25
30
47
35
83
38
TEACHER_SUBJECT table:
TEACHER_ID
SUBJECT
25
Chemistry
25
Biology
47
English
83
Math
83
Computer
Third Normal Form (3NF)
o
A relation will be in 3NF if it is in 2NF and not contain any transitive partial dependency.
o
3NF is used to reduce the data duplication. It is also used to achieve the data integrity.
o
If there is no transitive dependency for non-prime attributes, then the relation must be in
third normal form.
A relation is in third normal form if it holds atleast one of the following conditions for every
non-trivial function dependency X → Y.
1. X is a super key.
2. Y is a prime attribute, i.e., each element of Y is part of some candidate key.
Example:
EMPLOYEE_DETAIL table:
EMP_ID
EMP_NAME
EMP_ZIP
EMP_STATE
EMP_CITY
222
Harry
201010
UP
Noida
333
Stephan
02228
US
Boston
444
Lan
60007
US
Chicago
555
Katharine
06389
UK
Norwich
666
John
462007
MP
Bhopal
Super key in the table above:
1. {EMP_ID}, {EMP_ID, EMP_NAME}, {EMP_ID, EMP_NAME, EMP_ZIP}....so on
Candidate key: {EMP_ID}
Non-prime attributes: In the given table, all attributes except EMP_ID are non-prime.
Here, EMP_STATE & EMP_CITY dependent on EMP_ZIP and EMP_ZIP dependent on
EMP_ID. The non-prime attributes (EMP_STATE, EMP_CITY) transitively dependent
on super key(EMP_ID). It violates the rule of third normal form.
That's why we need to move the EMP_CITY and EMP_STATE to the new
<EMPLOYEE_ZIP> table, with EMP_ZIP as a Primary key.
EMPLOYEE table:
EMP_ID
EMP_NAME
EMP_ZIP
222
Harry
201010
333
Stephan
02228
444
Lan
60007
555
Katharine
06389
666
John
462007
EMPLOYEE_ZIP table:
EMP_ZIP
EMP_STATE
EMP_CITY
201010
UP
Noida
02228
US
Boston
60007
US
Chicago
06389
UK
Norwich
462007
MP
Bhopal
Boyce Codd normal form (BCNF)
o
BCNF is the advance version of 3NF. It is stricter than 3NF.
o
A table is in BCNF if every functional dependency X → Y, X is the super key of the
table.
o
For BCNF, the table should be in 3NF, and for every FD, LHS is super key.
Example: Let's assume there is a company where employees work in more than one department.
EMPLOYEE table:
EMP_ID
EMP_COUNTRY
EMP_DEPT
DEPT_TYPE
EMP_DEPT_NO
264
India
Designing
D394
283
264
India
Testing
D394
300
364
UK
Stores
D283
232
364
UK
Developing
D283
549
In the above table Functional dependencies are as follows:
1. EMP_ID → EMP_COUNTRY
2. EMP_DEPT → {DEPT_TYPE, EMP_DEPT_NO}
Candidate key: {EMP-ID, EMP-DEPT}
The table is not in BCNF because neither EMP_DEPT nor EMP_ID alone are keys.
To convert the given table into BCNF, we decompose it into three tables:
EMP_COUNTRY table:
EMP_ID
EMP_COUNTRY
264
India
264
India
EMP_DEPT table:
EMP_DEPT
DEPT_TYPE
EMP_DEPT_NO
Designing
D394
283
Testing
D394
300
Stores
D283
232
Developing
D283
549
EMP_DEPT_MAPPING table:
EMP_ID
EMP_DEPT
D394
283
D394
300
D283
232
D283
549
Functional dependencies:
1. EMP_ID →
EMP_COUNTRY
2. EMP_DEPT → {DEPT_TYPE, EMP_DEPT_NO}
Candidate keys:
For
the
For
the
first
second
table: EMP_ID
table: EMP_DEPT
For the third table: {EMP_ID, EMP_DEPT}
Now, this is in BCNF because left side part of both the functional dependencies is a key.
Fourth normal form (4NF)
o
A relation will be in 4NF if it is in Boyce Codd normal form and has no multi-valued
dependency.
o
For a dependency A → B, if for a single value of A, multiple values of B exists, then the
relation will be a multi-valued dependency.
Example
STUDENT
STU_ID
COURSE
HOBBY
21
Computer
Dancing
21
Math
Singing
34
Chemistry
Dancing
74
Biology
Cricket
59
Physics
Hockey
The given STUDENT table is in 3NF, but the COURSE and HOBBY are two independent
entity. Hence, there is no relationship between COURSE and HOBBY.
In
the
STUDENT
relation,
a
student
with
STU_ID, 21 contains
two
courses, Computer and Math and two hobbies, Dancing and Singing. So there is a Multivalued dependency on STU_ID, which leads to unnecessary repetition of data.
So to make the above table into 4NF, we can decompose it into two tables:
STUDENT_COURSE
STU_ID
COURSE
21
Computer
21
Math
34
Chemistry
74
Biology
59
Physics
STUDENT_HOBBY
STU_ID
HOBBY
21
Dancing
21
Singing
34
Dancing
74
Cricket
59
Hockey
Dependency-Preserving Decomposition
The dependency preservation decomposition is another property of decomposed relational
database schema D in which each functional dependency X -> Y specified in F either appeared
directly in one of the relation schemas Ri in the decomposed D or could be inferred from the
dependencies
that
appear
in
some
Ri.
Decomposition D = { R1 , R2, R3,,.., ,Rm} of R is said to be dependency-preserving with respect
to F if the union of the projections of F on each Ri , in D is equivalent to F. In other words, R ⊂
join of R1, R1 over X. The dependencies are preserved because each dependency in F represents
a constraint on the database. If decomposition is not dependency-preserving, some dependency is
lost in the decomposition.
Example:
Let a relation R(A,B,C,D) and set a FDs F = { A -> B , A -> C , C -> D} are given.
A
relation
R
is
decomposed
into
-
R1 = (A, B, C) with FDs F1 = {A -> B, A -> C}, and
R2 = (C, D) with FDs F2 = {C -> D}.
F' = F1 ∪ F2 = {A -> B, A -> C, C -> D}
so, F' = F.
And so, F'+ = F+.
PL/SQL Introduction
PL/SQL is a block structured language that enables developers to combine the power of
SQL with procedural statements.All the statements of a block are passed to oracle engine all at
once which increases processing speed and decreases the traffic.
Disadvantages of SQL:
 SQL doesn’t provide the programmers with a technique of condition checking, looping and
branching.
 SQL statements are passed to Oracle engine one at a time which increases traffic and
decreases speed.
 SQL has no facility of error checking during manipulation of data.
Features of PL/SQL:
1. PL/SQL is basically a procedural language, which provides the functionality of decision
making, iteration and many more features of procedural programming languages.
2. PL/SQL can execute a number of queries in one block using single command.
3. One can create a PL/SQL unit such as procedures, functions, packages, triggers, and types,
which are stored in the database for reuse by applications.
4. PL/SQL provides a feature to handle the exception which occurs in PL/SQL block known as
exception handling block.
5. Applications written in PL/SQL are portable to computer hardware or operating system
where Oracle is operational.
6. PL/SQL Offers extensive error checking.
Differences between SQL and PL/SQL:
SQL
PL/SQL
SQL is a single query that is used to perform
DML and DDL operations.
PL/SQL is a block of codes that used to write
the entire program blocks/ procedure/ function,
etc.
It is declarative, that defines what needs to be
done, rather than how things need to be done.
PL/SQL is procedural that defines how the
things needs to be done.
Execute as a single statement.
Execute as a whole block.
Mainly used to manipulate data.
Mainly used to create an application.
Cannot contain PL/SQL code in it.
It is an extension of SQL, so it can contain SQL
inside it.
Structure of PL/SQL Block:
PL/SQL extends SQL by adding constructs found in procedural languages, resulting in a
structural language that is more powerful than SQL. The basic unit in PL/SQL is a block. All
PL/SQL programs are made up of blocks, which can be nested within each other.
Typically, each block performs a logical action in the program. A block has the following
structure:
DECLARE
declaration statements;
BEGIN
executable statements
EXCEPTIONS
exception handling statements
END;
 Declare section starts with DECLARE keyword in which variables, constants, records as
cursors can be declared which stores data temporarily. It basically consists definition of
PL/SQL identifiers. This part of the code is optional.
 Execution section starts with BEGIN and ends with END keyword.This is a mandatory
section and here the program logic is written to perform any task like loops and conditional
statements. It supports all DML commands, DDL commands and SQL*PLUS built-in
functions as well.
 Exception section starts with EXCEPTION keyword.This section is optional which contains
statements that are executed when a run-time error occurs. Any exceptions can be handled in
this section.
PL/SQL identifiers
There are several PL/SQL identifiers such as variables, constants, procedures, cursors, triggers
etc.
1. Variables:
Like several other programming languages, variables in PL/SQL must be declared prior to its
use. They should have a valid name and data type as well.
Syntax for declaration of variables:
variable_name datatype [NOT NULL := value ];
Example to show how to declare variables in PL/SQL :
File Organization
o
The File is a collection of records. Using the primary key, we can access the records. The
type and frequency of access can be determined by the type of file organization which
was used for a given set of records.
o
File organization is a logical relationship among various records. This method defines
how file records are mapped onto disk blocks.
o
File organization is used to describe the way in which the records are stored in terms of
blocks, and the blocks are placed on the storage medium.
o
The first approach to map the database to the file is to use the several files and store only
one fixed length record in any given file. An alternative approach is to structure our files
so that we can contain multiple lengths for records.
o
Files of fixed length records are easier to implement than the files of variable length
records.
Objective of file organization
o
It contains an optimal selection of records, i.e., records can be selected as fast as possible.
o
To perform insert, delete or update transaction on the records should be quick and easy.
o
The duplicate records cannot be induced as a result of insert, update or delete.
o
For the minimal cost of storage, records should be stored efficiently.
Types of file organization:
File organization contains various methods. These particular methods have pros and cons on the
basis of access or selection. In the file organization, the programmer decides the best-suited file
organization method according to his requirement.
Types of file organization are as follows:
o
Sequential file organization
o
Heap file organization
o
Hash file organization
o
B+ file organization
o
Indexed sequential access method (ISAM)
o
Cluster file organization
Indexing is a data structure technique which allows you to quickly retrieve records from a
database file. An Index is a small table having only two columns. The first column comprises a
copy of the primary or candidate key of a table. Its second column contains a set of pointers for
holding the address of the disk block where that specific key value stored.
An index –


Takes a search key as input
Efficiently returns a collection of matching records.
In this DBMS Indexing tutorial, you will learn:








Types of Indexing in DBMS
Primary Index in DBMS
Secondary Index in DBMS
Clustering Index in DBMS
What is Multilevel Index?
B-Tree Index
Advantages of Indexing
Disadvantages of Indexing
Types
of
Indexing
in
DBMS
Type of Indexes in Database
Indexing in Database is defined based on its indexing attributes. Two main types of indexing
methods are:


Primary Indexing
Secondary Indexing
Primary Index in DBMS
Primary Index is an ordered file which is fixed length size with two fields. The first field is the
same a primary key and second, filed is pointed to that specific data block. In the primary Index,
there is always one to one relationship between the entries in the index table.
The primary Indexing in DBMS is also further divided into two types.


Dense Index
Sparse Index
Dense Index
In a dense index, a record is created for every search key valued in the database. This helps you
to search faster but needs more space to store index records. In this Indexing, method records
contain search key value and points to the real record on the disk.
Sparse Index
It is an index record that appears for only some of the values in the file. Sparse Index helps you
to resolve the issues of dense Indexing in DBMS. In this method of indexing technique, a range
of index columns stores the same data block address, and when data needs to be retrieved, the
block address will be fetched.
However, sparse Index stores index records for only some search-key values. It needs less space,
less maintenance overhead for insertion, and deletions but It is slower compared to the dense
Index for locating records.
Below is an database index Example of Sparse Index
Secondary Index in DBMS
The secondary Index in DBMS can be generated by a field which has a unique value for each
record, and it should be a candidate key. It is also known as a non-clustering index.
This two-level database indexing technique is used to reduce the mapping size of the first level.
For the first level, a large range of numbers is selected because of this; the mapping size always
remains small.
Secondary Index Example
Let’s understand secondary indexing with a database index example:
In a bank account database, data is stored sequentially by acc_no; you may want to find all
accounts in of a specific branch of ABC bank.
Here, you can have a secondary index in DBMS for every search-key. Index record is a record
point to a bucket that contains pointers to all the records with their specific search-key value.
Clustering Index in DBMS
In a clustered index, records themselves are stored in the Index and not pointers. Sometimes the
Index is created on non-primary key columns which might not be unique for each record. In such
a situation, you can group two or more columns to get the unique values and create an index
which is called clustered Index. This also helps you to identify the record faster.
Example:
Let’s assume that a company recruited many employees in various departments. In this case,
clustering indexing in DBMS should be created for all employees who belong to the same dept.
It is considered in a single cluster, and index points point to the cluster as a whole. Here,
Department _no is a non-unique key.
What is Multilevel Index?
Multilevel Indexing in Database is created when a primary index does not fit in memory. In this
type of indexing method, you can reduce the number of disk accesses to short any record and
kept on a disk as a sequential file and create a sparse base on that file.
B-Tree Index
B-tree index is the widely used data structures for tree based indexing in DBMS. It is a
multilevel format of tree based indexing in DBMS technique which has balanced binary search
trees. All leaf nodes of the B tree signify actual data pointers.
Moreover, all leaf nodes are interlinked with a link list, which allows a B tree to support both
random and sequential access.




Lead nodes must have between 2 and 4 values.
Every path from the root to leaf are mostly on an equal length.
Non-leaf nodes apart from the root node have between 3 and 5 children nodes.
Every node which is not a root or a leaf has between n/2] and n children.
Advantages of Indexing
Important pros/ advantage of Indexing are:




It helps you to reduce the total number of I/O operations needed to retrieve that data, so
you don’t need to access a row in the database from an index structure.
Offers Faster search and retrieval of data to users.
Indexing also helps you to reduce tablespace as you don’t need to link to a row in a table,
as there is no need to store the ROWID in the Index. Thus you will able to reduce the
tablespace.
You can’t sort data in the lead nodes as the value of the primary key classifies it.
Disadvantages of Indexing
Important drawbacks/cons of Indexing are:




To perform the indexing database management system, you need a primary key on the
table with a unique value.
You can’t perform any other indexes in Database on the Indexed data.
You are not allowed to partition an index-organized table.
SQL Indexing Decrease performance in INSERT, DELETE, and UPDATE query.
Index Sequential Access Method (ISAM)
An early technology attempting fast retrieval of individual records and maintained sorted order.
Much knowledge of how the data is mapped to the physical data storage device.
ISAM files are generally an integrated storage structure



Clustered
Each index entry contains an entire row
A node contains F keys and F+1 pointers
This node structure is a generalization of a binary search tree node.
Separator entry = (ki , pi) where ki is a search key value and pi is a pointer to a lower level page
ki separates set of search key values in the two subtrees pointed at by pi-1 and pi
ki-1 <= (all entries in pi-1 ) < ki
F is determined by the index (or primary key) size of the data, S, plus 4 bytes for pointers
(maybe
8
for
large
db).
If B is the sector size then F = ⌊(B-4)/(S+4)⌋
Example B=4096 and S=30, then F = ⌊4092/34⌋ = 120. Note this is the log base! A node holds up
to 120 key value boundaries.
B+ Tree
Supports equality and range searches, multiple attribute keys and partial key searches
Either a secondary index (sometimes in a separate file) or the basis for an integrated storage
structure
Responds to dynamic changes in the table.


Growth inserts into non-leaf nodes and if the tree needs to be larger, it will create a new
root.
If a leaf node is full, it splits.
Leaf nodes
Leaf level nodes consist of a sorted linked list of index entries




all data tuples are represented by the index entry.
range searches are accommodated, not just specific lookup
sometimes the whole tuple is stored (the number of entries may then be smaller in a leaf
node)
the pointer may reference leaf nodes in another b-tree! (non-clustered, slower when
sequential access is used)
Sibling pointers support range searches in spite of allocation and deallocation of leaf pages (note
that leaf pages might not be physically contiguous)
Insertion and Deletion in B+ Tree
Structure of tree changes to handle row insertion and deletion - no overflow chains ever
Tree remains balanced: all paths from root to index entries have same length
Algorithm guarantees that the number of separator entries in an index page is between ⌊F / 2⌋ and
F (at least 50% full, exception is root which may have as few as one)



Hence the maximum search cost is ( log F/2Q) + 1)
where Q is the size of the dataset
So in our case where F was calculated as 120, then we have nodes containing 60 to 120
key value boundaries. If on average the node is midrange at 90 values, then a 4 level B+
tree has capacity of 904=65.6 million records.
What is a Database Transaction?
A Database Transaction is a logical unit of processing in a DBMS which entails one or more
database access operation. In a nutshell, database transactions represent real-world events of any
enterprise.
All types of database access operation which are held between the beginning and end transaction
statements are considered as a single logical transaction in DBMS. During the transaction the
database is inconsistent. Only once the database is committed the state is changed from one
consistent state to another.
States of Transactions
The various states of a transaction concept in DBMS are listed below:
State
Transaction types
Active State
A transaction enters into an active state when the execution process begins.
During this state read or write operations can be performed.
Partially
Committed
A transaction goes into the partially committed state after the end of a
transaction.
Committed
State
When the transaction is committed to state, it has already completed its
execution successfully. Moreover, all of its changes are recorded to the
database permanently.
Failed State
A transaction considers failed when any one of the checks fails or if the
transaction is aborted while it is in the active state.
Terminated
State
State of transaction reaches terminated state when certain transactions
which are leaving the system can’t be restarted.
State Transition Diagram for a Database Transaction
Let’s study a state transition diagram that highlights how a transaction moves between these
various states.
1. Once a transaction states execution, it becomes active. It can issue READ or WRITE
operation.
2. Once the READ and WRITE operations complete, the transactions becomes partially
committed state.
3. Next, some recovery protocols need to ensure that a system failure will not result in an
inability to record changes in the transaction permanently. If this check is a success, the
transaction commits and enters into the committed state.
4. If the check is a fail, the transaction goes to the Failed state.
5. If the transaction is aborted while it’s in the active state, it goes to the failed state. The
transaction should be rolled back to undo the effect of its write operations on the
database.
6. The terminated state refers to the transaction leaving the system.

Transaction States in DBMS
Difficulty Level : Basic

Last Updated : 22 Oct, 2021
States through which a transaction goes during its lifetime. These are the states which tell about
the current state of the Transaction and also tell how we will further do the processing in the
transactions. These states govern the rules which decide the fate of the transaction whether it will
commit or abort.
These are different types of Transaction States :
1. Active
State
–
When the instructions of the transaction are running then the transaction is in active state. If
all the ‘read and write’ operations are performed without any error then it goes to the
“partially committed state”; if any instruction fails, it goes to the “failed state”.
2. Partially
Committed
–
After completion of all the read and write operation the changes are made in main memory
or local buffer. If the the changes are made permanent on the Data Base then the state will
change to “committed state” and in case of failure it will go to the “failed state”.
3. Failed
State
–
When any instruction of the transaction fails, it goes to the “failed state” or if failure occurs
in
making
a
permanent
change
of
data
on
Data
Base.
4. Aborted
State
–
After having any type of failure the transaction goes from “failed state” to “aborted state”
and since in previous states, the changes are only made to local buffer or main memory and
hence
these
changes
are
deleted
or
rolled-back.
5. Committed
State
–
It is the state when the changes are made permanent on the Data Base and the transaction is
complete
and
therefore
terminated
in
the
“terminated
state”.
6. Terminated
State
–
If there isn’t any roll-back or the transaction comes from the “committed state”, then the
system is consistent and ready for new transaction and the old transaction is terminated.


ACID Properties in DBMS
Difficulty Level : Easy
Last Updated : 07 Apr, 2021
A transaction is a single logical unit of work which accesses and possibly modifies the contents
of a database. Transactions access data using read and write operations.
In order to maintain consistency in a database, before and after the transaction, certain properties
are followed. These are called ACID properties.
Atomicity
By this, we mean that either the entire transaction takes place at once or doesn’t happen at all.
There is no midway i.e. transactions do not occur partially. Each transaction is considered as one
unit and either runs to completion or is not executed at all. It involves the following two
operations.
—Abort: If a transaction aborts, changes made to database are not visible.
—Commit:
If
a
transaction
commits,
changes
made
are
visible.
Atomicity
is
also
known
as
the
‘All
or
nothing
rule’.
Consider the following transaction T consisting of T1 and T2: Transfer of 100 from account X to
account Y.
If the transaction fails after completion of T1 but before completion of T2.( say,
after write(X) but before write(Y)), then amount has been deducted from X but not added to Y.
This results in an inconsistent database state. Therefore, the transaction must be executed in
entirety in order to ensure correctness of database state.
Consistency
This means that integrity constraints must be maintained so that the database is consistent before
and after the transaction. It refers to the correctness of a database. Referring to the example
above,
The total amount before and after the transaction must be maintained.
Total before
T occurs
= 500
+
200
=
700.
Total after
T
occurs = 400
+
300
=
700.
Therefore, database is consistent. Inconsistency occurs in case T1 completes but T2 fails. As a
result T is incomplete.
Isolation
This property ensures that multiple transactions can occur concurrently without leading to the
inconsistency of database state. Transactions occur independently without interference. Changes
occurring in a particular transaction will not be visible to any other transaction until that
particular change in that transaction is written to memory or has been committed. This property
ensures that the execution of transactions concurrently will result in a state that is equivalent to a
state
achieved
these
were
executed
serially
in
some
order.
Let X=
Consider two transactions T and T”.
500, Y =
500.
Suppose T has been executed till Read (Y) and then T’’ starts. As a result , interleaving of
operations takes place due to which T’’ reads correct value of X but incorrect value of Y and
sum
computed
by
T’’:
(X+Y
=
50,
000+500=50,
500)
is
thus
not
consistent
with
the
sum
at
end
of
transaction:
T:
(X+Y
=
50,
000
+
450
=
50,
450).
This results in database inconsistency, due to a loss of 50 units. Hence, transactions must take
place in isolation and changes should be visible only after they have been made to the main
memory.
Durability:
This property ensures that once the transaction has completed execution, the updates and
modifications to the database are stored in and written to disk and they persist even if a system
failure occurs. These updates now become permanent and are stored in non-volatile memory.
The effects of the transaction, thus, are never lost.
The ACID properties, in totality, provide a mechanism to ensure correctness and consistency of
a database in a way such that each transaction is a group of operations that acts a single unit,
produces consistent results, acts in isolation from other operations and updates that it makes are
durably stored.
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
1
Attribute & Description
%FOUND
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.
%NOTFOUND
2
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.
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;
Procedures
In PLSQL, a Procedure is a subprogram unit made up of a collection of PLSQL statements that
can be invoked by name. Thus, each procedure in PLSQL has a distinct name that can be used to
refer to and invoke it. The Oracle database stores this subprogram unit as a database object.
Note: A subprogram is nothing more than a process that must be manually constructed according
to the requirements. They will be saved as database objects once they have been generated.
Procedure block
When you declare the procedure, we use CREATE OR REPLACE to ensure there are no existing
procedures. Also, you can have a parameter list when declaring the procedure. There are three
types of parameters that we can use in the procedures and functions.
Types of parameters :
1. IN parameters — read-only parameters
2. OUT parameters — Write only parameters
3. IN OUT parameters — Read and write parameters
FunctionsFunctions is a PLSQL subprogram that runs on its own. Functions, like PLSQL
procedures, have a unique name by which they can be identified. These are saved as database
bjects
Function block
in
PLSQL.
Some of the qualities of functions are listed below.

Functions are a type of standalone block that is mostly used to do calculations.

The value is returned using the RETURN keyword, and the datatype is defined at the time of
creation.

Return is required in functions since they must either return a value or raise an exception.

Functions that do not require DML statements can be called directly from SELECT queries,
whereas functions that require DML operations can only be invoked from other PL/SQL
blocks.

It can be defined and nested inside other blocks or packages, or it can have nested blocks.

The parameters can be used to pass values into the function or to retrieve values from the
process.

In addition to utilizing RETURN, a PLSQL function can return the value using OUT
parameters.

Because it always returns the value, it is always used in conjunction with the assignment
operator to populate the variables in the calling statement.
Example for function
Packages
Packages are used to group all the procedures, functions, variables, constants, and cursors. Also, it
follows the Object-oriented programming concepts and provides the encapsulation for the code.
Packages have two main parts.
1. Package Specification
2. Package body.
1. Package Specification
The specification defines the package’s interface. It simply DECLARES the types, variables,
constants, exceptions, cursors, and subprograms that can be used outside the package. In another
way, it contains all information about the package’s content but not the code for the subprograms.
All of the objects in the specification are referred to as public objects. A private object is a
subprogram that is not specified in the package specification but is coded in the package body.
2. Package Body
The code for various methods specified in the package specification and additional private
declarations concealed from code outside the package are stored in the package body. Refer to the
following structure for the package body.
Download