0% found this document useful (0 votes)
7 views37 pages

of Chapter 2.3 - Control Structures

The document outlines a course on Database Management Systems (DBMS) taught by Dr. Gitanjali at the University Institute of Engineering. It covers key concepts such as data structures, SQL commands, normalization, transaction processing, and PL/SQL programming, including conditional and iterative control structures. The course aims to equip students with practical skills in database design, query construction, and performance evaluation.

Uploaded by

anshumanvats270
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views37 pages

of Chapter 2.3 - Control Structures

The document outlines a course on Database Management Systems (DBMS) taught by Dr. Gitanjali at the University Institute of Engineering. It covers key concepts such as data structures, SQL commands, normalization, transaction processing, and PL/SQL programming, including conditional and iterative control structures. The course aims to equip students with practical skills in database design, query construction, and performance evaluation.

Uploaded by

anshumanvats270
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

UNIVERSITY INSTITUTE OF

ENGINEERING

Department of Computer Science & Engineering

Databases Management System


(23CSH-205/23ITH-205)
DR. GITANJALI
E16525
ASSISTANT PROFESSOR DISCOVER . LEARN . EMPOWER
CSE
ABOUT COURSE
• The main DBMS course, we learn about the structural formation of data, maintain data
integrity, multitasking with concurrent access and recovery without occurring crashes,
data structures, data models etc. and their working in which every organization is
based.
• At the end of the course, the students may understand the concepts of data structures,
data models and design, construction of queries by using SQL, uses and applications of
database design etc

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


COURSE OUTCOMES

CO No. Title BT Level Will be covered in


this lecture
Students will be able to understand the database system
CO1
concepts and design databases for different applications.
Memorize

Students will be able to identify different types of DDL, DML,


CO2 DCL and TCL commands and their usage. Understanding

Students will be able to classifying Normalization,


CO3 Dependencies and Denormalization along with their Apply
requirements.
Students will be able to analyse and Compare Transaction
CO4
Processing techniques and Recovery techniques.
Analyze
Students will be able to evaluate database performance after
CO5 implementing Triggers, procedures, packages, cursors and Evaluate
views.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


COURSE OBJECTIVES

This course will enable students to,

1. To have good understanding of database system concepts and design databases for
different applications.
2. To learn how to use a DBMS and RDBMS.
3. To implement and understand different types of DDL, DML and DCL statements.
4. To understand transaction concepts related to databases and recovery/backup
techniques required for the proper storage of data.

Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


12/20/2024
Topics covered

Control Structures :

Introduction to conditional control, Iterative


control and sequential control statements,
Cursors, Views.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


PL/SQL Conditional Statements
PL/SQL (Procedural Language/Structured Query Language) is an extension of
SQL used in Oracle databases to write procedural code. It includes various
conditional statements that allow developers to execute different blocks of code
based on specific conditions. Decision-making statements in programming
languages decide the direction of the flow of program execution. Conditional
Statements available in PL/SQL are defined below:
• IF THEN
• IF THEN ELSE
• NESTED-IF-THEN
• IF THEN ELSIF-THEN-ELSE Ladder

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


PL/SQL Conditional Statements
1. IF THEN
If then the statement is the simplest decision-making statement. It is used
to decide whether a certain statement or block of statements will be
executed or not i.e if a certain condition is true then a block of statement is
executed otherwise not.
Syntax:
if condition then
-- do something
end if;

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


PL/SQL Conditional Statements
As the condition present in the if
statement is false. So, the block
below the if statement is not
executed.
Output:
I am Not in if

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


PL/SQL Conditional Statements
2. IF THEN ELSE
The if statement alone tells us that if a condition is true, it will execute a block of
statements and if the condition is false, it won’t. But what if we want to do something else if
the condition is false. Here comes the else statement. We can use the else statement with
if statement to execute a block of code when the condition is false.
Syntax:-
if (condition) then
-- Executes this block if
-- condition is true
else
-- Executes this block if
-- condition is false
if

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


PL/SQL Conditional Statements
Output:-
i'm in if Block
i'm not in if and not in else Block
The block of code following the else
statement is executed as the condition
present in the if statement is false after
calling the statement which is not in
block(without spaces).

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


PL/SQL Conditional Statements
3. NESTED-IF-THEN
A nested if-then is an if statement that is the target of another if statement. Nested if-then
statements mean an if statement inside another if statement. Yes, PL/SQL allows us to nest if
statements within if-then statements. i.e, we can place an if then statement inside another if
then statement. Syntax:-
if (condition1) then
-- Executes when condition1 is true
if (condition2) then
-- Executes when condition2 is true
end if;
end if;

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


PL/SQL Conditional Statements
Output:-
num1 small num2
num1 small num3 also
after end if

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


PL/SQL Conditional Statements
4. IF THEN ELSIF-THEN-ELSE Ladder
Here, a user can decide among multiple options. The if then statements are executed from the top
down. As soon as one of the conditions controlling the if is true, the statement associated with that if
is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final
else statement will be executed. Syntax:-
if (condition) then
--statement
elsif (condition) then
--statement
.
.
else
--statement
endif

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


PL/SQL Conditional Statements
Output:-
num1 small
after end if

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


PL/SQL Iterative Control
Iterative control indicates the ability to repeat or skip sections of a code block.
A loop marks a sequence of statements that has to be repeated. The keyword loop has to be placed
before the first statement in the sequence of statements to be repeated, while the keyword end loop
is placed immediately after the last statement in the sequence.
Once a loop begins to execute, it will go on forever. Hence a conditional statement that controls the
number of times a loop is executed always accompanies loops.
PL/SQL supports the following structures for iterative control:
• Loop
• WHILE loop
• FOR loop

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


PL/SQL Iterative Control
Simple loop:
In simple loop, the key word loop should be placed before the first statement in the sequence
and the keyword end loop should be written at the end of the sequence to end the loop.
Syntax:
Loop
< Sequence of statements >
End loop;

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


PL/SQL Iterative Control
Code: Output
i =: 0
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9
12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University
i=
PL/SQL Iterative Control
WHILE loop
The while loop executes commands in its body as long as the condition remains true

Syntax :

WHILE < condition >


LOOP
< Action >
END LOOP

Output
: of given number is:
reverse
321

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


PL/SQL Iterative Control
The FOR Loop
The FOR loop can be used when the number of iterations to be executed are known.
Syntax :
FOR variable IN [REVERSE] start..end
LOOP
< Action >
END LOOP;
The variable in the For Loop need not be declared. Also, the increment value cannot be
specified. The For Loop variable is always incremented by 1.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


PL/SQL Iterative Control
Output
Code: i =: 1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9
i = 10
12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University
PL/SQL Sequential Control
The GOTO Statement
The GOTO statement changes the flow of control within a PL/SQL block.
This statement allows execution of a section of code, which is not in the
normal flow of control. The entry point into such a block of code is marked
using the tags «userdefined name». The GOTO statement can then make
use of this user-defined name to jump into that block of code for execution.

Syntax :
GOTO jump;
....
<<jump>>

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


PL/SQL Sequential Control:
Code: Output
:
code starts
before gotostatements
flow of execution jumped here.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Cursors
Whenever DML statements are executed, a temporary work area is created in the system
memory and it is called a cursor. A cursor can have more than one row, but processing wise
only 1 row is taken into account. Cursors are very helpful in all kinds of databases like
Oracle, SQL Server, MySQL, etc. They can be used well with DML statements like Update,
Insert and Delete. Especially Implicit cursors are there with these operations. From time to
time, it changes the values and hence the implicit cursor attribute values need to be
assigned in a local variable for further use. In PL/SQL, two different types of cursors are
available.

1. Implicit cursors
2. Explicit cursors

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Cursors
Implicit Cursors
An Implicit Cursor is a cursor that is automatically created by the database management
system to handle single-row queries or DML operations (INSERT, UPDATE, DELETE). The
database automatically manages the lifecycle of an implicit cursor, so the programmer does
not need to explicitly declare, open, fetch, or close it.

%ROWCOUNT
It displays the number of rows produced by a SELECT INTO command or affected by DML
operations like INSERT, DELETE, and UPDATE.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Cursors
%FOUND
Its return result is TRUE if a SELECT INTO command produced one or more rows, or
if DML actions like INSERT, DELETE, and UPDATE influence at least one row or more
rows. Otherwise, FALSE is returned.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Cursors
%NOTFOUND
Its return result is TRUE if a SELECT INTO command produced one or more rows, or if
DML actions like INSERT, DELETE, and UPDATE influence at least one row or more
rows. Otherwise, FALSE is returned.
%NOTFOUND
Its return result is TRUE if no rows are affected by DML operations like INSERT,
DELETE, and UPDATE, or if a SELECT INTO command returns no rows. Otherwise,
FALSE is returned. It is directly opposed to %FOUND.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Cursors
Example of an Implicit Cursor:

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Cursors
Advantages of Implicit Cursors:
Simplifies coding for basic queries and DML operations.
Automatically managed by the DBMS, reducing programmer
effort.
Suitable for operations involving single rows or simple data
manipulation.

Disadvantages of Implicit Cursors:


Limited to single-row queries or operations.
Lack of control for handling multi-row query results (use explicit
cursors for this).

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Cursors
An Explicit Cursor is a database object that allows programmers to handle multi-row query
results in a controlled manner, processing each row one at a time. Unlike implicit cursors (which
are automatically created by SQL for single-row operations), explicit cursors must be explicitly
declared, opened, fetched, and closed by the user.

Steps for Using an Explicit Cursor:


• Declare the Cursor: Define the cursor and associate it with a SELECT query

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Cursors
• Open the Cursor: Execute the query and make the result set available for processing.

• Fetch Data: Retrieve one row at a time from the result set.

• Close the Cursor: Release the resources associated with the cursor.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Cursors
Example of an Explicit
Cursor:

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Cursors

Advantages of Explicit Cursors:


• Provides control over row-by-row processing.
• Enables complex operations on individual rows of a
query result set.

Disadvantages:
• Slower performance compared to set-based
operations.
• Increases code complexity.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Views
Views in SQL are a type of virtual table that simplifies how users interact with data across
one or more tables. Unlike traditional tables, a view in SQL does not store data on disk;
instead, it dynamically retrieves data based on a pre-defined query each time it’s accessed.
SQL views are particularly useful for managing complex queries, enhancing security, and
presenting data in a simplified format

How Views Work in Cursors


View Definition: A view is created as a virtual table that combines data from one or more
tables. You can create a cursor to process rows from a view instead of directly querying the
underlying tables.
Cursor Usage: The cursor can reference the view in its SELECT query to retrieve and
process data row by row.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Views
Steps to Use a Cursor with a View
• Create the View: Define the view using a CREATE VIEW statement.

• Declare the Cursor: Use the view in the SELECT query for the cursor declaration.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Views
• Open the Cursor: Execute the query to make the result set available for processing.

• Fetch Rows from the Cursor: Retrieve rows one at a time from the cursor .

• Process Each Row: Apply desired logic (e.g., calculations, updates) to the fetched data.
• Close the Cursor: Release resources after completing processing.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Views
Advantages of Using Views in Cursors

1. Simplicity:
⚬ Views can encapsulate complex queries,
making the cursor's logic simpler.
2. Reusability:
⚬ A view can be reused across different queries or
cursors.
3. Data Abstraction:
⚬ Views abstract the underlying table structure,
providing a layer of security and modularity.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


THANK YOU

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University

You might also like