Вы находитесь на странице: 1из 6

EXPERIMENT-12.

AIM Introduction to PL/SQL.

PL/SQL

PL/SQL (Procedural Language/Structured Query Language) is Oracle Corporation's


procedural extension language for SQL and the Oracle relational database.

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.

Implementations from version 8 of Oracle Database onwards have included features


associated with object-orientation.

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.

Oracle Corporation customarily extends package functionality with each successive


release of the Oracle Database.

Architecture of the SQL Language and PL/SQL

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

Declarative Section: Contains variables, constants, cursors and exception. This is


optional section

• Begin

Executable section: This is the only executable section and is the mandatory section.
All SQL and PL/SQL statements go here.

• Exception:

Exception handling Section: Error handling statements go here. It is the optional


section.

• 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:

A variable is used to transmit the information between PL/SQL and database.


Variables are temporary storage of data can be used for calculations and for other data
manipulations without accessing the database. All variables have a specific datatype
which specifies storage format and valid range of values. The datatype can be of
scalar, composite, reference and LOB (large object).

• Looping:

A loop allows to execute the same sequence of statements repeatedly. PL/SQL


supports different kinds of loops which are Basic loop, while loop and for loop.

• 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

IF-THEN-END IF, IF-THEN-ELSE-END IF and IF-THEN-ELSEIF-END IF

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

• Procedures and functions:

A procedure is a type of sub programme that performs an action. A procedure can


be stored in the data base as a schema object for repeated execution. In other words
that can accept parameter and be invoked. A procedure has a header, declaration
section, executable section and an optional exception handling section. A function
is a type of subprogramme and is similar to procedure except that it must return a
value.
• Package:

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

These are the advantages of PL/SQL.

• Block Structures: PL SQL consists of blocks of code, which can be nested


within each other. Each block forms a unit of a task or a logical module. PL/SQL
Blocks can be stored in the database and reused.
• Procedural Language Capability: PL SQL consists of procedural language
constructs such as conditional statements (if else statements) and loops like (FOR
loops).
• Better Performance: PL SQL engine processes multiple SQL statements
simultaneously as a single block, thereby reducing network traffic.
• Error Handling: PL/SQL handles errors or exceptions effectively during the
execution of a PL/SQL program. Once an exception is caught, specific actions can
be taken depending upon the type of the exception or it can be displayed to the
user with a message.

Вам также может понравиться