Professional Documents
Culture Documents
Lecture 1.5 Cursors Usage in Packages and Advantages of Packages
Lecture 1.5 Cursors Usage in Packages and Advantages of Packages
Database
Management
System
UNIT-I [10h]
Overview of Databases: Database concepts, DBMS, Data Base System Architecture (Three
Level ANSI-SPARC Architecture), Advantages and Disadvantages of DBMS, Data Independence,
DBA and Responsibilities of DBA, Relational Data Structure, Keys, Relations, Attributes, Schema and
Instances, Referential integrity, Entity integrity.
Data Models: Relational Model, Network Model, Hierarchical Model, ER Model: Design,
issues, Mapping constraints, ER diagram, Comparison of Models.
UNIT-II [10h]
Functional dependencies and Normalization: Functional dependencies, Decomposition, Full
Functional Dependency (FFD), Transitive Dependency (TD), Join Dependency (JD), Multi-valued
Dependency (MVD), Normal Forms (1NF, 2NF, 3NF, BCNF), De-normalization.
Database Security: Introduction, Threats, Counter Measures.
Control Structures: Introduction to conditional control, Iterative control and sequential control
statements, Cursors, Views.
3
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
UNIT-III [10h]
Package, Procedures and Triggers: Parts of procedures, Parameter modes, Advantages of
procedures, Syntax for creating triggers, Types of triggers, package specification and package body,
developing a package, Bodiless package, Advantages of packages.
Transaction Management and Concurrency Control: Introduction to Transaction Processing,
Properties of Transactions, Serializability and Recoverability, Need for Concurrency Control, Locking
Techniques, Time Stamping Methods, Optimistic Techniques and Granularity of Data items.
Database Recovery of database: Introduction, Need for Recovery, Types of errors, Recovery
Techniques.
4
University Institute of Engineering (UIE)
Department of Computer and Communication Engineering (CCE)
Learning Objective
• Procedures with example
• Parameters and type of parameters
– Actual and Formal Parameters
Learning Outcome
• Understanding the concept of procedures with example.
• Discuss the various parameters and its types.
• Understanding the use of functions in Data Base Management
System.
• Discuss the concepts of Trigger and its various types.
• Discuss the concepts of packages and its advantages
Advantages of Packages
• Modularity : Packages let you encapsulate logically related types, items, and
subprograms in a named PL/SQL module. Each package is easy to understand,
and the interfaces between packages are simple, clear, and well defined. This
aids application development.
• Easier Application Design : When designing an application, all you need
initially is the interface information in the package specs. You can code and
compile a spec without its body. Then, stored subprograms that reference the
package can be compiled as well. You need not define the package bodies fully
until you are ready to complete the application.
• Information Hiding : With packages, you can specify which types, items, and
subprograms are public (visible and accessible) or private (hidden and
inaccessible). For example, if a package contains four subprograms, three might
be public and one private. The package hides the implementation of the private
subprogram so that only the package (not your application) is affected if the
implementation changes. This simplifies maintenance and enhancement. Also,
by hiding implementation details from users, you protect the integrity of the
package.
Advantages of Packages
• Added Functionality : Packaged public variables and cursors
persist for the duration of a session. So, they can be shared by all
subprograms that execute in the environment. Also, they allow
you to maintain data across transactions without having to store
it in the database.
• Better Performance : When you call a packaged subprogram
for the first time, the whole package is loaded into memory. So,
later calls to related subprograms in the package require no disk
I/O. Also, packages stop cascading dependencies and thereby
avoid unnecessary recompiling. For example, if you change the
implementation of a packaged function, Oracle need not
recompile the calling subprograms because they do not depend
on the package body.
Cursors
• A cursor is a temporary work area created in the system
memory when a SQL statement is executed.
• A cursor contains information on a select statement and the
rows of data accessed by it.
• This temporary work area is used to store the data retrieved
from the database, and manipulate this data.
• A cursor can hold more than one row, but can process only
one row at a time.
• The set of rows the cursor holds is called the active set.
Cursor Types
• There are two types of cursors in PL/SQL.
– Implicit cursors.
– Explicit cursors.
• Both implicit and explicit cursors have the same
functionality, but they differ in the way they are accessed.
Cursor Attributes
Name Description
%FOUND Returns TRUE if record was fetched
successfully, FALSE otherwise.
%NOTFOUND Returns TRUE if record was not fetched
successfully, FALSE otherwise.
%ROWCOUNT Returns number of records fetched from
cursor at that point in time.
%ISOPEN Returns TRUE if cursor is open, FALSE
otherwise.
Implicit cursors
Example
• 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.
Example
• 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; /
Explicit Cursors
Explicit Cursor
• Declaring the cursor defines the cursor with a name and the
associated SELECT statement.
• Syntax: CURSOR c_customers IS SELECT id, name, address
FROM customers;
• Opening the cursor allocates the memory for the cursor and
makes it ready for fetching the rows returned by the SQL
statement into it.
• Syntax: OPEN c_customers;
• Fetching the cursor involves accessing one row at a time.
• Syntax: FETCH c_customers INTO c_id, c_name, c_addr;
• Closing the cursor means releasing the allocated memory.
• CLOSE c_customers;
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
Example
• DECLARE
• c_id customers.id%type;
• c_name customer S.No.ame%type;
• c_addr customers.address%type;
• CURSOR c_customers is
• SELECT id, name, address FROM customers;
• BEGIN OPEN c_customers;
• LOOP FETCH c_customers into c_id, c_name, c_addr; EXIT
WHEN c_customers%notfound; dbms_output.put_line(c_id ||
' ' || c_name || ' ' || c_addr); END LOOP; CLOSE c_customers;
• END; /
Views
• A view is the result set of a stored query on the data, which
the database users can query just as they would in a
persistent database collection object.
• This pre-established query command is kept in the database
dictionary.
• Unlike ordinary base tables in a relational database, a view
does not form part of the physical schema: as a result set, it
is a virtual table computed or collated dynamically from data
in the database when access to that view is requested.
• Changes applied to the data in a relevant underlying
table are reflected in the data shown in subsequent
invocations of the view.
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
Example
• A view is equivalent to its source query. When queries are run
against views, the query is modified. For example, if there exists a
view named accounts_view with the content as follows:
• accounts_view:
• SELECT name,
• money_received,
• money_sent,
• (money_received - money_sent)
• AS balance, address, ...
• FROM table_customers c
• JOIN accounts_table a
• ON a.customer_id = c.customer_id
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
Example(2)
• The application could run a simple query such as:
• Simple query
• SELECT name, balance
• FROM accounts_view
• The RDBMS then takes the simple query, replaces the
equivalent view, then sends the following to the query
optimizer
Example(3)
• Preprocessed query:
• SELECT name, balance
• FROM (SELECT name, money_received, money_sent,
(money_received - money_sent)
• AS balance, address, ...
• FROM table_customers c
• JOIN accounts_table a
• ON a.customer_id = c.customer_id )
FAQ
• What is procedures ? Explain with the help of examples.
• What is formal and actual parameter?
• Explain Row trigger with the help of example.
• How we can enable, disable and drop trigger?
• What do you understand by packages and write its advantages.
References
• https://docs.oracle.com/database/121/LNPLS/packages.htm
• https://docs.oracle.com/cd/B19306_01/appdev.102/b14261/p
ackages.htm
• https://www.guru99.com/packages-pl-sql.html
• https://docs.oracle.com/cd/B19306_01/server.102/b14200/st
atements_6009.htm
• http://searchoracle.techtarget.com/definition/stored-procedur
e
• https://www.w3resource.com/sql/sql-procedure.php
• https://docs.oracle.com/cd/B19306_01/server.102/b14220/tri
ggers.htm
• https://docs.oracle.com/cd/A57673_01/DOC/server/doc/SCN7
3/ch15.htm
• https://docs.microsoft.com/en-us/sql/t-sql/statements/create-
trigger-transact-sql
University Institute of Engineering (UIE)