Академический Документы
Профессиональный Документы
Культура Документы
PL/SQL
INTRODUCTION
PL/SQL supports variables, conditions, loops and exceptions. Arrays are also supported,
though in a somewhat unusual way, involving the use of PL/SQL collections. PL/SQL
collections are a slightly advanced topic.
PL/SQL program units (essentially code containers) can be compiled into the Oracle
database. Programmers can thus embed PL/SQL units of functionality into the database
directly. They also can write scripts containing PL/SQL program units that can be read
into the database using the Oracle SQL*Plus tool.
Once the program units have been stored into the database, they become available for
execution at a later time.
While programmers can readily embed Data Manipulation Language (DML) statements
directly into their PL/SQL code using straight forward SQL statements, Data Definition
Language (DDL) requires more complex "Dynamic SQL" statements to be written in the
PL/SQL code. However, DML statements underpin the majority of PL/SQL code in
typical software applications.
In the case of PL/SQL dynamic SQL, early versions of the Oracle Database required the
use of a complicated Oracle DBMS_SQL package library. More recent versions have
however introduced a simpler "Native Dynamic SQL", along with an associated
EXECUTE IMMEDIATE syntax.
This tutorial will begin with an introduction of the Oracle SQL plus utility and a short
tutorial on the proper techniques for developing SQL queries. We will also introduce the
architecture of SQL as a vehicle for extracting data from a relational Oracle database
management system. The topics will include a getting-started section on using SQL*Plus
and developing SQL queries, the history and evolutions of SQL to show the student how
SQL has evolved to meet changing demands from relational databases. This tutorial will
also explore the evolution of PL/SQL and show how PL/SQL is used within Oracle as an
alternative to traditional procedural languages.
The SQL section of this tutorial will deal primarily with a theoretical evolution of SQL as
a data access method for relational databases. We will examine the declarative nature of
SQL statements and understand how the select, project and join operators can be used in
order to get data out of any relational database. We will also take a look at the concept of
the SQL optimizer and understand how the optimizer chooses the appropriate access path
to the data within the database.
We will also introduce SQL tuning and understand the basic goals of SQL tanning as
they relate to the optimization of SQL within any relational database management
system. This will include a discussion of database design as it relates to SQL
performance and a discussion of the barriers to SQL tuning, especially with regard to
vendor-based SQL applications.
We will also take a look at techniques for identifying the offensive SQL within the
database management system and examine techniques for extracting and tuning
individual SQL statements.
The PL/SQL section of this tutorial will start with the basic introduction to the PL/SQL
programming language. We begin by taking a look at the origins of the PL/SQL
language, as well as an understanding of the different incarnation of PL/SQL, and how
the different versions contain progressively more powerful programming features.
We also take a look at stored procedures, functions, and packages within the database
programming environment. We will review the concept of modular coding; introduce
PL/SQL block structures, and understanding the differences between the stored
procedures and functions in an operational environment. We also examine the concept of
tutorial overloading, and how to use forward declarations within stored procedures.
We wrap up the PL/SQL section of this text and by examining PL/SQL packages.
PL/SQL packages are an important construct and a database environment, and this
section will introduce the package structure and how you can take stored procedures and
functions and encapsulated them into Oracle packages
FEATURES
• Block Structure:
PL/SQL is a block structured language meaning that the programs can be divided
into logical blocks. PL/SQL blocks can be nested within each other. A PL/SQL
block consists of upto three sections: declarative (optional), executable (required)
and exception handling (optional). The structure of PL/SQL block is as:
• Declare
• Begin
Executable section: This is the only executable section and is the mandatory section.
All SQL and PL/SQL statements go here.
• Exception:
• End:
Keywords declare, begin and exception are not followed by a semicolon (;) while End
and all other PL/SQL statements require a semicolon (:) to terminate the statement.
The executable section must contain at least one executable statement.
• Error handling:
Exception handling section is the error handling section which is the optional
section. It specifies the actions to perform when errors and abnormal conditions arise
in the executable section. By separating the error handling code from the main body
of the program, the structure of the program itself is clear.
• Variables:
• Looping:
• Conditionals:
The logical flow of statements within the PL/SQL block can be changed with a
number of control structures. The various control structures are
• Cursor:
A cursor is a private SQL work areas where all SQL statements get executed.
Cursor can be of implicit and explicit types. Implicit cursors are automatically
defined by the oracle server while explicit cursor are declared and defined by the
programmer.
Subprograms, along with variables and types, can be grouped together into a
package. A package has two parts specification and body.
Advantages of PL/SQL