Assignment 4
Assignment 4
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:
1. PL/SQL block
2. PL/SQL Engine
3. Database Server
PL/SQL block:
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.
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.
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;
DECLARE
BEGIN
dbms_output.put_line(message);
END;
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.
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.
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);
The below example declares two variables, one of which is a not null.
DECLARE
salary number(4);
The below example declares two variables, one of which is a not null.
DECLARE
salary number(4);
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;
mysql>desc fine;
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)