You are on page 1of 33

Department of Computer Science and Engineering (CSE)

Database
Management
System

Course Outcome Will be covered in


CO Title Level this lecture
Numbe
r
CO1 To perceive the significance and Remember
implementation of a commercial
relational database system (Oracle)
by writing SQL using the system.
CO2 To understand the relational database Understand
theory, and be able to write
relational algebra expressions for
queries

CO3 To identify the basic issues of Analysis and


transaction processing and application
concurrency control and find out its
solutions.
2
University Institute of Engineering (UIE)
Department
Department of Computer
of Computer Scienceand
Science andEngineering
Engineering (CSE)
(CSE)

Contents of the Syllabus

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.

Relational Algebra & Relational Calculus: Introduction, Syntax, Semantics, Additional


operators, Grouping and Ungrouping, Relational comparisons, Tuple Calculus, Domain Calculus,
Calculus Vs Algebra, Computational capabilities.

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
Department of Computer
of Computer Scienceand
Science andEngineering
Engineering (CSE)
(CSE)

Contents of the Syllabus

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 Science and Engineering (CSE)

Chapter 2.3

(Control Structures)
Control Structures: Introduction to conditional control, Iterative control and
sequential control statements, Cursors, Views.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Learning Objective

In this chapter, we will briefly cover the following topics:


• Conditional controls
– IF-THEN
– IF-THEN-ELSE, and
– IF-THEN-ELSIF
• Iterative controls
– Simple loops
– WHILE loops
– FOR loops
• Sequential control
• Cursor Manipulation.
• Using Cursor For Loops.
• Using Parameters with Cursors.
• Cursor variables
• Views
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)

Learning Outcome
• Type of control structure in Database Management
System.
• Discuss the concepts of cursor and its types.
• Discuss views in Database Management System.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Cursor
• 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.
There are two types of cursors:
Implicit cursors
Explicit cursors

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Cursor
• 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.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

NEED FOR CURSORS?

Cursor is a mechanism that provides a way to select multiple rows of data from
the database and then process each row individually inside a PL/SQL program.

The cursor first points at row1 and once it is processed it then


advances to row2 and so on.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

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 AttributesCursor 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.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

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.
• Implicit cursors are automatically created by Oracle whenever an SQL
statement is executed, when there is no explicit cursor for the statement.
• 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.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Implicit Cursors
• These are created by default when DML statements like,
INSERT, UPDATE, and DELETE statements are executed.
• The user is not aware of this happening & will not be able
to control or process the information.
• When an implicit cursor is working, DBMS performs the
open, fetches and close automatically

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

The following table provides the description of the most used


attributes −

Attribute Description
Name
Returns TRUE if cursor is open, FALSE if cursor is closed.
%ISOPEN SQL%ISOPEN always returns FALSE.

Returns TRUE if successful fetch has been executed, FALSE if no row was
%FOUND returned. SQL%FOUND is used to access it.

Return TRUE if no row was returned, FALSE if successful fetch has been
%NOTFOUND executed. SQL%NOTFOUND is used to access it.

Returns the number of rows affected by the query.


%ROWCOUNT SQL%ROWCOUNT is used to access it.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

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.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

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; /

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Result:
6 customers selected
PL/SQL procedure successfully completed.

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 |
+‐‐‐‐+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐+

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

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.

Working with an explicit cursor involves four steps:


– Declaring the cursor for initializing in the memory
– Opening the cursor for allocating memory
– Fetching the cursor for retrieving data
– Closing the cursor to release allocated memory

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

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)

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.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

The following table provides the description of the most


used attributes −

Attribute Description
Name
Returns TRUE if cursor is open, FALSE if cursor is closed.
%ISOPEN CursorName%ISOPEN is used to access it.

Returns TRUE if successful fetch has been executed, FALSE if no row was
%FOUND returned.
CursorName%FOUND is used to access it.

Return TRUE if no row was returned, FALSE if successful fetch has been
%NOTFOUND executed.
CursorName%NOTFOUND is used to access it.

Returns the number of rows affected by the query.


%ROWCOUNT CursorName%ROWCOUNT is used to access it.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Explicit cursor involves four steps:

1.Declaring the cursor for initializing in the memory


2.Opening the cursor for allocating memory
3.Fetching the cursor for retrieving data
4.Closing the cursor to release allocated memory

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

STEPS

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Declaring the Cursor


Syntax:
• CURSOR cursor_name IS select_statement;
For example:
• CURSOR c_customers IS
• SELECT id, name, address FROM customers;

Opening the Cursor


Syntax:
• OPEN cursor_name;
Example:
• OPEN c_customers;

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Fetching the Cursor


• Fetching the cursor involves accessing one row at a
time.
Syntax:
• FETCH cursor_name INTO record_name;
Example:
• FETCH c_customers INTO c_id, c_name, c_addr;

Closing the Cursor


• Closing the cursor means releasing the allocated
memory.
Syntax:
• CLOSE cursor_name;
Example:
• CLOSE c_customers;
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)

EXAMPLE
• DECLARE
• c_id customers.id%type;
• c_name customers.name%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;

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

RESULT:

1 Ramesh Ahmedabad
2 Khilan Delhi
3 kaushik Kota
4 Chaitali Mumbai
5 Hardik Bhopal
6 Komal MP

PL/SQL procedure successfully completed.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Cursor Attributes
• cursorname%ROWCOUNT Rows returned so far
• cursorname%FOUND One or more rows retrieved
• cursorname%NOTFOUND No rows found
• Cursorname%ISOPEN Is the cursor open

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Implicit cursor
DECLARE
l_rows number(5);
BEGIN
UPDATE employee SET salary = salary + 1000;
IF SQL%NOTFOUND THEN
dbms_output.put_line('None of the salaries where updated');
ELSIF SQL%FOUND THEN l_rows := SQL%ROWCOUNT;
dbms_output.put_line('Salaries for ' || l_rows || 'employees are
updated');
END IF;
END;

Oracle Tutorials: PL/SQL


University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)

Using explicit cursors


• Fetching results of a query into RECORD

DECLARE
l_employees employees%ROWTYPE;
CURSOR l_c (p_low NUMBER DEFAULT 0, p_high NUMBER DEFAULT 99) is
SELECT * FROM employees WHERE job_id > p_low AND job_id < p_high;
BEGIN
OPEN l_c(3,20);
LOOP
FETCH l_c INTO l_employees;
EXIT WHEN l_c%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(l_employees.last_name || l_employees.job_id );
END LOOP;
CLOSE l_c;
END;

Oracle Tutorials: PL/SQL


University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)

Advantages
• Using Cursor we can perform row by row processing .
• We can perform row wise validation or operations on
each
row.
• Allow application to access and move around in a set of
data rows, rather then merely retrieve a complete result
set.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Disadvantages
• Uses more resources
• Speed and performance issues.
• Increased network roundtrip.

University Institute of Engineering (UIE)


33 Department of Computer Science and Engineering (CSE)

HOME WORK

1 ____ are declared and manipulated in the PL/SQL block code for
handling a set of rows returned by a SELECT statement.
a. Explicit cursors
b. Dynamic cursors
c. static cursors
d. Implicit cursors
2. The ____ action used with an explicit cursor creates a named cursor
identified by a SELECT statement.
a. OPEN
b. DECLARE
c. FETCH
d. CLOSE

Answers:1.d, 2.b

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

References
Other References
• PL/SQL - Cursors - Tutorialspoint
• PL/SQL Cursor - javatpoint
• PL/SQL Cursor (plsqltutorial.com)

Suggested Book References
• C.J.Date, “An Introduction to DatabaseSystems”, Addison Wesley.
• Thomas M. Connolly, Carolyn & E.Begg,“Database Systems: A
Practical Approach to Design, Implementationand Management”,
5/E, University of Paisley, Addison-Wesley.
• Rob,”Database Principal Fundamental Design, Cengage Learning.

University Institute of Engineering (UIE)

You might also like