0% found this document useful (0 votes)
4 views

Plsql1

PL/SQL is a procedural extension of SQL developed by Oracle to enhance database capabilities, integrating SQL with programming features. It supports various programming structures, error handling, and object-oriented programming, making it essential for database developers. PL/SQL is widely used in applications such as database security, XML management, and automating database tasks.

Uploaded by

whytujay
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Plsql1

PL/SQL is a procedural extension of SQL developed by Oracle to enhance database capabilities, integrating SQL with programming features. It supports various programming structures, error handling, and object-oriented programming, making it essential for database developers. PL/SQL is widely used in applications such as database security, XML management, and automating database tasks.

Uploaded by

whytujay
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

PL SQL

PL/SQL, which stands for Procedural Language extensions to the Structured Query Language (SQL). It is a
combination of SQL along with the procedural features of programming languages. It was developed by Oracle
Corporation in the early 90's to enhance the capabilities of SQL. PL/SQL is one of three key programming
languages embedded in the Oracle Database, along with SQL itself and Java.

Purpose of PL/SQL

The purpose of PL/SQL is to merge database commands with procedural programming language. It offers
more complete programming solutions for building critical applications that operate on the Oracle database.

Features of PL/SQL

PL/SQL has the following features −

 PL/SQL is tightly integrated with SQL.


 It offers extensive error checking mechanisms.
 It supports numerous data types for flexible data handling.
 Includes a variety of programming structures, such as loops and conditionals. Includes a variety of
programming structures, such as loops and conditionals.
 It supports structured programming through functions and procedures.
 It supports object-oriented programming, enabling more complex data handling and manipulation.
 It supports the web application development and server pages.

Why to learn PL/SQL?

Learning PL/SQL is an essential skill for persons who are interested in databases and other advanced RDBMS
technologies. PL/SQL offers various benefits, making it an essential skill for database developers −

 Ease of Use: PL/SQL is straightforward to write and read, featuring block-structured syntax which
simplifies programming and debugging.
 Portability: Programs written in PL/SQL are fully portable across different Oracle databases, ensuring
consistency and ease of migration.
 Tight SQL Integration: PL/SQL is tightly integrated with SQL, allowing for efficient querying,
transforming, and updating of data within a database.
 High Performance: It reduces network traffic by sending entire blocks of statements to the database
at once, thus improving performance.
 Security: It includes robust security features to protect database integrity.
 Object-Oriented Support: It supports object-oriented programming, and allows you to define object
types that can be used in object-oriented designs.

PL/SQL Block Structured

PL/SQL follows a block-structured approach, dividing programs into logical blocks of code. Each block consists
of three main sections −

 Declarations: This section, starting with the keyword DECLARE, is optional and used for
defining variables, cursors, subprograms, and other elements required within the block.
 Executable Commands: Enclosed between the keywords BEGIN and END, this mandatory section
contains executable PL/SQL statements. It must include at least one executable line of code, even if
it's just a NULL command indicating no action.
 Exception Handling: This starts with the keyword EXCEPTION, this optional section deals with
handling errors in the program through defined exceptions.

PL/SQL statements are terminated with a semicolon(;). Additionally, blocks can be nested within each other
using BEGIN and END keywords.

Applications of PL/SQL

PL/SQL is widely used in various applications, including −

 Database Security: It implements robust security measures within the database.


 XML Management: Generating and managing XML documents within the database.
 Linking Databases to Web Pages: Integrates databases with web applications.
 Automation: Automating database administration tasks for efficient management.

Every PL/SQL statement ends with a semicolon (;). PL/SQL blocks can be nested within other PL/SQL blocks
using BEG

PL/SQL Delimiters
PL/SQL Comments

Program comments are explanatory statements that can be included in the PL/SQL code that you write and
helps anyone reading its source code. All programming languages allow some form of comments.

The PL/SQL supports single-line and multi-line comments. All characters available inside any comment are
ignored by the PL/SQL compiler. The PL/SQL single-line comments start with the delimiter -- (double hyphen)
and multi-line comments are enclosed by /* and */.

PL / SQL Programming Units

Variable Declaration in PL/SQL


Variable Scope in PL/SQL
Declaring a Constant

A constant is declared using the CONSTANT keyword. It requires an initial value and does not allow that value
to be changed.

PL/SQL – Operators

PL/SQL - Conditions
PL/SQL Loop

PL/SQL - Strings
 Fixed-length strings − In such strings, programmers specify the length while declaring the string. The
string is right-padded with spaces to the length so specified.
 Variable-length strings − In such strings, a maximum length up to 32,767, for the string is specified
and no padding takes place.
 Character large objects (CLOBs) − These are variable-length strings that can be up to 128 terabytes.

Declaring String Variables

PL/SQL – Arrays
Creating a Varray Type

A varray type is created with the CREATE TYPE statement. You must specify the maximum size and the type of elements
stored in the varray.

The basic syntax for creating a VARRAY type at the schema level is −

Example

Output

PL/SQL - Procedures
A subprogram is a program unit/module that performs a particular task. These subprograms are combined to
form larger programs. This is basically called the 'Modular design'. A subprogram can be invoked by another
subprogram or program which is called the calling program.

 Functions − These subprograms return a single value; mainly used to compute and return a value.
 Procedures − These subprograms do not return a value directly; mainly used to perform an action.
Creating a Procedure

A procedure is created with the CREATE OR REPLACE PROCEDURE statement. The simplified syntax for the
CREATE OR REPLACE PROCEDURE statement is as follows −

Parameter Modes in PL/SQL Subprograms


Methods for Passing Parameters

 Positional notation
 Named notation
 Mixed notation

Positional Notation

In positional notation, you can call the procedure as −

Named Notation

In named notation, the actual parameter is associated with the formal parameter using the arrow symbol (
=>). The procedure call will be like the following −

Mixed Notation

In mixed notation, you can mix both notations in procedure call; however, the positional notation should
precede the named notation.

The following call is legal –


PL/SQL – Functions

Creating a Function

A standalone function is created using the CREATE FUNCTION statement. The simplified syntax for the CREATE
OR REPLACE PROCEDURE statement is as follows −

 function-name specifies the name of the function.


 [OR REPLACE] option allows the modification of an existing function.
 The optional parameter list contains name, mode and types of the parameters. IN represents the
value that will be passed from outside and OUT represents the parameter that will be used to return a
value outside of the procedure.
 The function must contain a return statement.
 The RETURN clause specifies the data type you are going to return from the function.
 function-body contains the executable part.
 The AS keyword is used instead of the IS keyword for creating a standalone function.

Output
Calling a Function

Example

The following example demonstrates Declaring, Defining, and Invoking a Simple PL/SQL Function that
computes and returns the maximum of two values.

PL/SQL Recursive Functions

We have seen that a program or subprogram may call another subprogram. When a subprogram calls itself, it
is referred to as a recursive call and the process is known as recursion.

To illustrate the con


Examples – Online platform - https://onecompiler.com/plsql
1. Program to Calculate Factorial

Program to Check Even or Odd

Program to Find the Maximum of Two Numbers

Program to Concatenate Strings


Program to Count from 1 to 10

Arithmetic operator

Relational Operators

Logical Operators
Assignment Operators

Using Operators in PL/SQL Collections

Creating of Table

Conditional statements
IF Statement with Multiple Conditions

CASE Statement

Nested IF Statement
Using Conditions in Loops

FOR Loop

WHILE LOOP
LOOP with EXIT Condition

Nested Loops

Using CONTINUE Statement


Strings

ARRAYS

You might also like