0% found this document useful (0 votes)
68 views5 pages

Assignment No 5

Uploaded by

Vaibhav Roman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views5 pages

Assignment No 5

Uploaded by

Vaibhav Roman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Group A

Assignment No: 5
Class: T.E. Computer Roll No:

Aim: Write a PL/SQL block to calculate the area of a circle for a value of radius
varying from 5 to 9.

Problem Statement:
Write a PL/SQL code block to calculate the area of a circle for a value of radius varying from 5 to 9.
Store the radius and the corresponding values of calculated area in an empty table named areas,
consisting of two columns, radius and area.
Objective:

1. To learn and understand PL/SQL in Oracle


Hardware requirements:
Any CPU with Pentium Processor or similar, 256 MB
RAM or more, 1 GB Hard Disk or more.
Software requirements:
Windows 7 Operating System, Oracle 11g, SQL developer
Theory:
PL/SQL stands for Procedural Language extension of SQL. PL/SQL 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.

Architecture of PL/SQL:

The PL/SQL architecture mainly consists of following 3 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 with 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 actually 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.

Basic Difference between SQL and PL/SQL

PL/SQL Block Structure:


Basic Syntax of PL/SQL which is a block-structured language; this means that the PL/SQL programs are
divided and written in logical blocks of code. Each block consists of three sub-parts −

S.No Sections & Description

Declarations
1 This section starts with the keyword DECLARE. It is an optional section and defines all variables,
cursors, subprograms, and other elements to be used in the program.

Executable Commands

2 This section is enclosed between the keywords BEGIN and END and it is a mandatory section. It
consists of the executable PL/SQL statements of the program. It should have at least one executable line
of code, which may be just a NULL command to indicate that nothing should be executed.
Exception Handling
3 This section starts with the keyword EXCEPTION. This optional section contains exception(s) that
handle errors in the program.
Every PL/SQL statement ends with a semicolon (;). PL/SQL blocks can be nested within other PL/SQL
blocks using BEGIN and END. Following is the basic structure of a PL/SQL block −

DECLARE

<declarations section>

BEGIN

<executable command(s)>

EXCEPTION

<exception handling>

END;

The 'Hello World' Example

DECLARE

message varchar2(20):= 'Hello, World!';

BEGIN

dbms_output.put_line(message);

END;

The end; line signals the end of the PL/SQL block.

PL/SQL Placeholders

Placeholders are temporary storage area. PL/SQL Placeholders can be any of Variables, Constants and
Records. Oracle defines placeholders to store data temporarily, which are used to manipulate data during
the execution of a PL SQL block.

 Define PL/SQL Placeholders

Depending on the kind of data you want to store, you can define placeholders with a
name and a datatype. Few of the datatypes used to define placeholders are as given below.
Number (n,m) , Char (n) , Varchar2 (n) , Date , Long , Long raw, Raw, Blob, Clob, Nclob, Bfile

 PL/SQL Variables

These are placeholders that store the values that can change through the PL/SQL Block.

General Syntax to declare a variable is


variable_name datatype [NOT NULL := value ];

 variable_name is the name of the variable.


 datatype is a valid PL/SQL datatype.
 NOT NULL is an optional specification on the variable.
 value or DEFAULT valueis also an optional specification, where you can initialize a variable.
 Each variable declaration is a separate statement and must be terminated by a semicolon.

For example, if you want to store the current salary of an employee, you can use a variable.

DECLARE
salary number (6);

The below example declares two variables, one of which is a not null.

DECLARE

salary number(4);

dept varchar2(10) NOT NULL := “HR Dept”;

The below example declares two variables, one of which is a not null.

DECLARE

salary number(4);

dept varchar2(10) NOT NULL := “HR Dept”;

The below example declares two variables, one of which is a not null.

DECLARE

salary number(4);

dept varchar2(10) NOT NULL := “HR Dept”;

Conclusion:
Thus we have successfully implemented PL/SQL block to calculate the area of a circle for a
value of radius varying from 5 to 9.

You might also like