What is PL/SQL?

PL/SQL is an extension of Structured Query Language (SQL) that is used in Oracle. Unlike SQL, PL/SQL allows the programmer to write code in a procedural format. Full form of PL/SQL is "Procedural Language extensions to SQL".

It combines the data manipulation power of SQL with the processing power of procedural language to create super powerful SQL queries.

PL/SQL means instructing the compiler 'what to do' through SQL and 'how to do' through its procedural way.

Similar to other database languages, it gives more control to the programmers by the use of loops, conditions and object-oriented concepts.

Architecture of PL/SQL

The PL/SQL architecture mainly consists of following three components:

1.PL/SQL block

2.PL/SQL Engine

3.Database Server

PL/SQL block:

• This is the component which has the actual PL/SQL code.

• This consists of different sections to divide the code logically (declarative section for declaring purpose, execution section for processing statements, exception handling section for handling errors)

• It also contains the SQL instruction that used to interact with the database server.

• All the PL/SQL units are treated as PL/SQL blocks, and this is the starting stage of the architecture which serves as the primary input.

• Following are the different type of PL/SQL units.

o Anonymous Block

o Function

o Library

o Procedure

o Package Body

o Package Specification

o Trigger

o Type

o Type Body

PL/SQL Engine

• PL/SQL engine is the component where the actual processing of the codes takes place.

• PL/SQL engine separates PL/SQL units and SQL part in the input (as shown in the image below).

• The separated PL/SQL units will be handled by the PL/SQL engine itself.

• The SQL part will be sent to database server where the actual interaction with database takes place.

• It can be installed in both database server and in the application server.

Database Server:

• This is the most important component of Pl/SQL unit which stores the data.

• The PL/SQL engine uses the SQL from PL/SQL units to interact with the database server.

• It consists of SQL executor which parses the input SQL statements and execute the same.

Advantage of Using PL/SQL

1.Better performance, as SQL is executed in bulk rather than a single statement

2.High Productivity

3.Tight integration with SQL

4.Full Portability

5.Tight Security

6.Support Object Oriented Programming concepts.

Types of PL/SQL block

PL/SQL blocks are of mainly two types.

1.Anonymous blocks

2.Named Blocks

Anonymous blocks:

Anonymous blocks are PL/SQL blocks which do not have any names assigned to them. They need to be created and used in the same session because they will not be stored in the server as database objects.

Since they need not store in the database, they need no compilation steps. They are written and executed directly, and compilation and execution happen in a single process.

Below are few more characteristics of Anonymous blocks.

• These blocks don't have any reference name specified for them.

• These blocks start with the keyword 'DECLARE' or 'BEGIN'.

• Since these blocks do not have any reference name, these cannot be stored for later purpose. They shall be created and executed in the same session.

• They can call the other named blocks, but call to anonymous block is not possible as it is not having any reference.

• It can have nested block in it which can be named or anonymous. It can also be nested in any blocks.

• These blocks can have all three sections of the block, in which execution section is mandatory, the other two sections are optional.

Named blocks:

Named blocks have a specific and unique name for them. They are stored as the database objects in the server. Since they are available as database objects, they can be referred to or used as long as it is present on the server. The compilation process for named blocks happens separately while creating them as a database objects.

Below are few more characteristics of Named blocks.

• These blocks can be called from other blocks.

• The block structure is same as an anonymous block, except it will never start with the keyword 'DECLARE'. Instead, it will start with the keyword 'CREATE' which instruct the compiler to create it as a database object.

• These blocks can be nested within other blocks. It can also contain nested blocks.