Professional Documents
Culture Documents
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
Department of Computer
of Computer Scienceand
Science andEngineering
Engineering (CSE)
(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 Science and Engineering (CSE)
Chapter 2.3
(Control Structures)
Control Structures: Introduction to conditional control, Iterative control and
sequential control statements, Cursors, Views.
Learning Objective
Learning Outcome
• Type of control structure in Database Management
System.
• Discuss the concepts of cursor and its types.
• Discuss views in Database Management System.
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
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.
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.
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.
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.
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
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.
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; /
Result:
6 customers selected
PL/SQL procedure successfully completed.
+‐‐‐‐+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐+
| 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.
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.
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.
STEPS
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;
RESULT:
1 Ramesh Ahmedabad
2 Khilan Delhi
3 kaushik Kota
4 Chaitali Mumbai
5 Hardik Bhopal
6 Komal MP
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
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;
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;
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.
Disadvantages
• Uses more resources
• Speed and performance issues.
• Increased network roundtrip.
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
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.