unitII
unitII
UNIT II
PL/SQL, DATA MODEL AND
QUERY PROCESSING
Syllabus
UNITII PL/SQL, DATA MODEL AND QUERY PROCESSING 9
⮚ Block Structure
⮚ Procedural Language capability
⮚ Better Performance
⮚ Error Handling
⮚ Portability
⮚ Security
PL/SQL Block Structure
• A PL/SQL block has up to four different sections, only one
of which is mandatory:
❑ Header-Used only for named blocks. The header
determines the way the named block or program must be
called. Optional.
❑ Declaration section-Identifies variables, cursors, and
subblocks that are referenced in the execution and
exception sections. Optional.
❑ Execution section-Statements the PL/SQL runtime engine
will execute at runtime. Mandatory.
❑ Exception section-Handles exceptions to normal
processing (warnings and error conditions). Optional.
PL/SQL Block Structure
PL/SQL Block Structure-Example
PL/SQL Language Constructs
• SQL supports constructs that gives it almost all the power of a
general-purpose programming language.
– Warning: most database systems implement their own variant of
the standard syntax below.
• Compound statement: begin … end,
– May contain multiple SQL statements between begin and end.
– Local variables can be declared within a compound statements
• While and repeat statements:
– while boolean expression do
sequence of statements ;
end while
• Loop
sequence of statements ;
Exit when boolean expression;
end Loop;
PL/SQL Conditional Statement
• Conditional statements (if-then-else)
if boolean expression
then statement or compound statement
elseif boolean expression
then statement or compound statement
else statement or compound statement
end if
PL/SQL – For Loop
• For loop
– Permits iteration over all results of a query
• Example: Find the budget of all departments
Procedure created.
Executing a Standalone Procedure
• A Standalone Procedure can be called in two ways:
❑ Using the EXECUTE keyword
❑ Calling the name of the procedure from a PL / SQL block
Output:
6 customers updated
PL/SQL procedure successfully completed.
Explicit Cursor
• The Explicit cursors are defined by the programmers to
gain more control over the context area.
• In
Instructor relation
Join Operation
• The Cartesian-Product
instructor X teaches
associates every tuple of instructor with every tuple of
teaches.
• Most of the resulting rows have information about
instructors who did NOT teach a particular course.
• To get only those tuples of “instructor X teaches “ that
pertain to instructors and the courses that they taught, we
write:
σ instructor.id = teaches.id (instructor x teaches ))
We get only those tuples of “instructor X teaches” that pertain
to instructors and the courses that they taught.
• The result of this expression, shown in the next slide
Union Operation
• The union operation allows us to
combine two relations
• Notation: r ∪ s
• For r ∪ s to be valid.
1. r, s must have the same arity (same
number of attributes)
2. The attribute domains must be
compatible (example: 2nd column of r
deals with the same type of values as
does the 2nd column of s)
• Example: to find all courses taught in
the Fall 2017 semester, or in the
Spring 2018 semester, or in both
∏course_id (σ semester=“Fall” Λ year=2017
(section)) ∪
∏course_id (σ semester=“Spring” Λ year=2018
(section))
Union Operation
• Result of:
∏course_id (σ semester=“Fall” Λ year=2017 (section)) ∪
∏course_id (σ semester=“Spring” Λ year=2018 (section))
Set Intersection Operation
• The set intersection operation
allows us to find tuples that are in
both the input relations.
• Notation: r ∩ s
• Assume:
– r, s have the same arity
– attributes of r and s are
compatible
• Example: Find the set of all courses
taught in both the Fall 2017 and
the Spring 2018 semesters.
∏course_id (σ semester=“Fall” Λ year=2017 (section))
∩
∏course_id (σ semester=“Spring” Λ year=2018
(section))
• Result
Set Difference Operation
• The set-difference operation
allows us to find tuples that
are in one relation but are not
in another.
• Notation r – s
• Set differences must be taken
between compatible
relations.
– r and s must have the same
arity
– attribute domains of r and s
must be compatible
Set Difference Operation (Cont.)
• Example: to find all courses taught in the Fall 2017
semester, but not in the Spring 2018 semester
• Result
Assignment Operation
• It is convenient at times to write a relational-algebra expression
by assigning parts of it to temporary relation variables.
• The expression:
ρx (E)
returns the result of expression E under the name x.
• For example:
{ R| ∃T ∈ Authors(T.article='database' AND R.name=T.name)}
• Evaluation
– The query-execution engine takes a query-evaluation
plan, executes that plan, and returns the answers to the
query.
Basic Steps in Query Processing:
Optimization
• A relational algebra expression may have many equivalent
expressions
– E.g., σsalary<75000(∏salary(instructor)) is equivalent to
∏salary(σsalary<75000(instructor))
• Each relational algebra operation can be evaluated using one of
several different algorithms
– Correspondingly, a relational-algebra expression can be
evaluated in many ways.
• Annotated expression specifying detailed evaluation strategy is
called an evaluation-plan. E.g.,:
– Use an index on salary to find instructors with salary < 75000,
– Or perform complete relation scan and discard instructors
with salary ≥ 75000
Basic Steps in Query Processing(Cont.)