You are on page 1of 31

Department of Computer Science and Engineering (CSE)

Database
Management
System

Course Outcome Will be covered in


CO Title Level this lecture
Number

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

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

3
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)

What is PL/SQL
• Procedural Language – SQL
• An extension to SQL with design features of programming languages
(procedural and object oriented)
• PL/SQL and Java are both supported as internal host languages within
Oracle products.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Why PL/SQL
• Acts as host language for stored procedures and triggers.
• Provides the ability to add middle tier business logic to client/server
applications.
• Provides Portability of code from one environment to another
• Improves performance of multi-query transactions.
• Provides error handling

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Structure of PL/SQL
• PL/SQL is Block Structured
A block is the basic unit from which all PL/SQL programs are built. A
block can be named (functions and procedures) or anonymous
• Sections of block
Declaration Section
Executable Section
Exception Section

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Structure of PL/SQL

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example of Structure of PL/SQL

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example of Structure of PL/SQL


DECLARE
a number;
text1 varchar2(20);
text2 varchar2(20) := “HI”;
BEGIN
---------- ---------- ----------
END;

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

PL/SQL Control Structure


PL/SQL has a number of control structures which includes:
• Conditional controls
• Iterative or loop controls.
• Sequential control

It is these controls, used singly or together, that allow the PL/SQL developer
to direct the flow of execution through the program.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

PL/SQL Control Structure


• Conditional Controls
IF....THEN....END IF;
IF....THEN...ELSE....END IF;
IF....THEN...ELSIF....THEN....ELSE....END IF;

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

PL/SQL Control Structure


• Conditional Controls
IF....THEN....END IF;
IF....THEN...ELSE....END IF;
IF....THEN...ELSIF....THEN....ELSE....END IF;

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

LOOP
• Syntax • Example

• This is used to repeatedly execute declare


a set of statements. This is the i number(2) := 1;
simplest form of looping begin
structures.
loop
LOOP
Statements; dbms_output.put_line(i);
EXIT [WHEN condition]; i := i + 1;
END LOOP; exit when i > 10;
end loop;
end;

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Nested Loops • Example


• Syntax
• It is possible to have a loop within another
loop. When a loop is placed within another declare
loop it is called as nested loop. The inner i number(2);
loop is executed for each iteration of outer j number(2);
loop. begin
• The following example will display table i := 1;
up to 10 for numbers from 1 to 5. loop
LOOP ... j:= 1;
loop
LOOP ... dbms_output.put_line(i || '*' || j || '='
EXIT outerloop || i * j);
WHEN ... j := j + 1;
– exit when j > 10;
end loop;
END LOOP; ... i := i + 1;
END LOOP; exit when i > 5;
end loop;
end;

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

WHILE
Syntax Example
• Executes a series of statements as declare
long as the given condition is true. i number(2) := 1;
begin
WHILE condition LOOP while i <= 10 loop
Statements;
END LOOP; dbms_output.put_line(i);
i := i + 1;
end loop;
end;

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

For
Syntax Example
This looping structure is best suited Declare
to cases where we have to repeatedly i int=1;
execute a set of statements by varying Begin
a variable from one value to another.
for i in 1..10 loop

FOR counter IN [REVERSE] lowerrange dbms_output.put_line(i);


.. upperrange
end loop;
LOOP
end;
Statements;
END LOOP;

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

FOR continued
If REVERSE option is used the following steps will take place:

1. Counter is set to upper range.


2. If counter is greater than or equal to lower range then statements are
executed otherwise loop is terminated.
3. Counter is decremented by one.
4. Go to step 2

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Sequential Control Structure


• The GOTO statement, which goes to a specified
statement, is seldom needed. Occasionally, it simplifies
logic enough to warrant its use.
• The NULL statement, which does nothing, can improve
readability by making the meaning and action of
conditional statements clear.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

GOTO Statement

• The GOTO statement transfers control to a label


unconditionally. The label must be unique in its scope
and must precede an executable statement or a PL/SQL
block. When run, the GOTO statement transfers control to
the labeled statement or block.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example
• DECLARE p VARCHAR2(30);
• n PLS_INTEGER := 37;
• BEGIN FOR j in 2..ROUND(SQRT(n)) LOOP IF n MOD j = 0
THEN p := ' is not a prime number';
• GOTO print_now; END IF; END LOOP;
• p := ' is a prime number'; <<print_now>>
DBMS_OUTPUT.PUT_LINE(TO_CHAR(n) || p);
• END; /
Output
37 is a prime number

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

NULL Statement

• The NULL statement only passes control to the next


statement. Some languages refer to such an instruction as
a no-op (no operation).
• Some uses for the NULL statement are:
• To provide a target for a GOTO statement,
• To improve readability by making the meaning and
action of conditional statements clear,
• To create placeholders and stub subprograms,
• To show that you are aware of a possibility, but that no
action is necessary,

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example : NULL Statement Showing No Action


• DECLARE v_job_id VARCHAR2(10);
• v_emp_id NUMBER(6) := 110;
• BEGIN SELECT job_id INTO v_job_id FROM employees
WHERE employee_id = v_emp_id;
• IF v_job_id = 'SA_REP' THEN UPDATE employees SET
commission_pct = commission_pct * 1.2;
• ELSE
NULL; -- Employee is not a sales rep END IF;
END; /

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

PL/SQL Control Structure


Cursor
DECLARE
name varchar2(20);
Cursor c1 is select t.name from table t where date is not null;
BEGIN
OPEN c1;
LOOP
FETCH c1 into name;
exit when c1%NOTFOUND;
END LOOP;
CLOSE c1;
END;

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

The %TYPE Attribute


– Declare a variable according to:
• A database column definition
• Another previously declared variable
– Prefix %TYPE with:
• The database table and column
• The previously declared variable name

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

DBMS_OUTPUT.PUT_LINE
– An Oracle-supplied packaged procedure
– An alternative for displaying data from a PL/SQL block – Must be
enabled in SQL*Plus with SET SERVER OUTPUT ON

University Institute of Engineering (UIE)


30 Department of Computer Science and Engineering (CSE)

HOME WORK

1. In the PL/SQL block below, how many rows will be inserted in the messages table?

DECLARE
v_start_sales NUMBER := 2;
v_end_sales NUMBER := 100;
BEGIN
FOR i IN v_start_sales..v_end_sales LOOP
INSERT INTO messages(msgid)
VALUES v_start_sales;
END LOOP;
END;
a. 1
b. 0
c. 99
d. 100
2. Which statements execute a sequence of statements multiple times?
a. EXIT
b. LOOP
c. Both A & B
d. None of the above
Answer:1.C, 2.b

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

References
Other References
• Control Structures | DBMS | Tutorialink.com
• PL/SQL Control Structures - Oracle PL/SQL Tutorial | Intellipaat.com
• Writing PL/SQL Control Structures(Introduction) (relationaldbdesign.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