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

Assignment 4

DBMS practical

Uploaded by

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

Assignment 4

DBMS practical

Uploaded by

harshp.aidsioe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Group A

Assignment No: 4

Aim: Write a PL/SQL block to calculate fine for a library book by accessing borrower
information from the database.

Problem Statement:
Unnamed PL/SQL code block: Use of Control structure and Exception handling is
mandatory. Write a PL/SQL block of code for the following requirements:-
Schema: 1. Borrower(Rollin, Name, DateofIssue, NameofBook, Status)
2. Fine(Roll_no,Date,Amt)
 Accept roll_no & name of book from user.
 Check the number of days (from date of issue), if days are between 15 to 30 then fine
amount will be Rs 5per day.
 If no. of days>30, per day fine will be Rs 50 per day & for days less than 30, Rs. 5 per day.
 After submitting the book, status will change from I to R.
 If condition of fine is true, then details will be stored into fine table

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

In this section, we will discuss some differences between SQL and PL/SQL

SQL PL/SQL

 SQL is a single query that is used to  PL/SQL is a block of codes that used to
write the entire program blocks/ procedure/
perform DML and DDL operations. function, etc.

 It is declarative, that defines what needs to  PL/SQL is procedural that defines how the
be done, rather than how things need to be things needs to be done.
done.

 Execute as a single statement.  Execute as a whole block.

 Mainly used to manipulate data.  Mainly used to create an application.

 Interaction with Database server.  No interaction with the database server.

 Cannot contain PL/SQL code in it.  It is an extension of SQL, so it can contain


SQL inside it.

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 suucessfully implemented PL/SQL block to retrieve fine for issued library book by
reading borrower information from the database.
Problem Statement:
Unnamed PL/SQL code block: Use of Control structure and Exception handling is
mandatory. Write a PL/SQL block of code for the following requirements:-
Schema: 1. Borrower(Rollin, Name, DateofIssue, NameofBook, Status)
2. Fine(Roll_no,Date,Amt)
 Accept roll_no & name of book from user.
 Check the number of days (from date of issue), if days are between 15 to 30 then fine
amount will be Rs 5per day.
 If no. of days>30, per day fine will be Rs 50 per day & for days less than 30, Rs. 5 per day.
 After submitting the book, status will change from I to R.
 If condition of fine is true, then details will be stored into fine table

mysql>desc borrower;

Field Type Null Key Default Extra


Rollin Int(11) No PRI Null
Name Char(20) No Null
dateofIssue Date Yes Null
Bname Char(20) Yes Null
Status Char(1) Yes Null

5 rows in set(0.00 sec)

mysql>desc fine;

Field Type Null Key Default Extra


Rollno Int(11) YES MUL Null
Fdate Date Yes Null
Amt Int(11) Yes Null

3 rows in set(0.00 sec)


mysql>select * from borrow;

rollin name dateofIssue bname status


1 a 2023-11-01 Java I
2 b 2023-10-15 Networking I
3 c 2023-10-01 DBMS I
4 d 2023-09-22 CN I

mysql> delimiter $
->create procedure fine_calculation(IN rno int(3),bname char(20))
->begin
->declare i_date date;
->declare diff int;
->declare fine_amt int;
->declare exit handler for sqlexception select 'Table not Found';
->select dateofissue into i_date from borrower where rollin=rno and bname=bname;
->select datediff(curdate(),i_date) into diff;
->if (diff>15 and diff<=30) then
->set fine_amt=diff*5;
->insert into fine values(rno,curdate(),fine_amt);
->elseif(diff>30) then
->set fine_amt=15*5+(diff-30)*50;
->insert into fine values(rno,curdate(),fine_amt);
->end if;
->update borrower set status='R' where rollin=rno and bname=bname;
->end;
->$
Query OK, 0 rows affected (0.02 sec)
mysql> call fine_calculation (3,'DBMS');
-> $
Query OK, 1 row affected (0.42 sec)
mysql> select * from Fine;
->$
Rollno Fdate amt
3 2023-10-04 16850
1 row in set(0.00 sec)

You might also like