B.E.
(E&TC) – 2019 Pattern Database Management Lab
Group: B
Experiment No.6
Write PL/SQL block of code
Roll Number
Date of Performance
AIM: Write a PL/SQL, block of code for the following requirements:-
Schemas
1. Borrower(Rollin, Name, Date of Issue, Name of Book, Status)
2. Fine(Roll no, Date, Amt)
Accept toll 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 per day.
It ‘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.
OBJECTIVES: To understand and implement PL/SQL Control Structure that provides conditional
tests, loops, flow control and branches.
THEORY: -
The PL/SQL programming language was developed by Oracle Corporation in the late 1980s as
procedural extension language for SQL and the Oracle relational database. Following are certain notable
facts about PL/SQL
PL/SQL is a completely portable, high-performance transaction-processing language.
PL/SQL provides a built-in, interpreted and OS independent programming environment.
PL/SQL can also directly be called from the command-line SQL*Plus interface.
Direct call can also be made from external programming language calls to database.
PL/SQL's general syntax is based on that of ADA and Pascal programming language.
Apart from Oracle, PL/SQL is available in TimesTen in-memory database and IBM DB2.
Features of PL/SQL
29
B.E. (E&TC) – 2019 Pattern Database Management Lab
PL/SQL has the following features-
PL/SQL is tightly integrated with SQL.
. It offers extensive error checking.
It offers numerous data types.
It offers a variety of programming structures.
It supports structured programming through functions and procedures.
It supports object-oriented programming.
It supports the development of web applications and server pages.
Advantages of PL/SQL
PL/SQL has the following advantages –
SQL is the standard database language and PL/SQL is strongly integrated with SQL. PL/SQ supports both
static and dynamic SQL. Static SQL supports DML operations and transaction control from PL/SQL block.
In Dynamic SQL, SQL allows embedding DDL statements in PL/SQL blocks.
PL/SQL allows sending an entire block of statements to the database at one time. This reduc network
traffic and provides high performance for the applications.
PL/SQL gives high productivity to programmers as it can query, transform, and update data i a database.
PL/SQL saves time on design and debugging by strong features, such as exception handling
encapsulation, data hiding, and object-oriented data types.
Applications written in PL/SQL are fully portable.
PL/SQL provides high security level.
PL/SQL provides access to predefined SQL packages.
PL/SQL provides support for Object-Oriented Programming.
PL/SQL provides support for developing Web Applications and Server Pages.
DECLARE: - if you want to declare a variable in plsql program then it takes place in declare section
BEGIN: - is used to start the working of program and end is used to terminate the begin.
Delimiter is used to run (/)
SET SERVEROUTPUT ON: is run before every time when you compiled a program in a session.
SET ECHO ON: is optional
DBMS_OUTPUT.PUT_LINE command for e.g. if sal=10 and you want to print it Then it looks like
dbms_output.put_line(‘the salary is’ ||sal);
IF STATEMENT
30
B.E. (E&TC) – 2019 Pattern Database Management Lab
Common syntax
IF condition THEN
statement 1;
ELSE
statement 2;
END IF;
INTO command: is used to catch a value in variable from table under some while condition
Only one value must be returned For e.g. in the above example if there are two people who's name
is john then it shows error
Exception Handling:
An exception is an error condition during a program execution. PL/SQL supports programmers to
catch such conditions using EXCEPTION block in the program and an appropriate action is taken
against the error condition. There are two types of exceptions
System-defined exceptions
User-defined exceptions
Syntax for Exception Handling
The general syntax for exception handling is as follows. Here you can list down as many exceptions
as you can handle. The default exception will be handled using WHEN others THEN –
DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
<exception handling goes here >
WHEN exception! THEN
exception 1-handling-statements
WHEN exception2 THEN
Exception3-handling-statements
WHEN exception3 THEN
exception3-handling-statements
------------------
WHEN others THEN
Exception3-handling-statements
END;
31
B.E. (E&TC) – 2019 Pattern Database Management Lab
Problem Statement:
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, Date of Issue, Name of Book, 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.
Solution in Mysql:
Steps are:
1) Create borrower and fine table with primary and foreign keys
2) insert records in borrower table
3) create procedure to insert entries in fine table with exception handling
4) call procedure to calculate fine and display fine table.
mysql> create table borrower(rollin int primary key, name varchar(20), dateofissue date,
nameofbook varchar(20), status varchar(20));
Query OK, 0 rows affected (0.30 sec)
mysql> desc borrower;
mysql> create table fine(rollno int, foreign key(rollno) references borrower(rollin), returndate
date,amount int);
32
B.E. (E&TC) – 2019 Pattern Database Management Lab
Query OK, 0 rows affected (0.38 sec)
mysql> desc fine;
Mysql> insert into borrower values(1,’abc’,2017-08-01’,’SEPM’,’PEN’) $Query OK, 1row affected
(0.16 sec)
mysql insert into borrower values(2,'xyz','2017-07-01','DBMS','PEN') $Query OK, 1 row affected
(0.08 sec)
mysql> insert into borrower values(3,'pqr','2017-08-15','DBMS','PEN') $Query OK, I row affected
(0.03 sec)
mysql> delimiter $
mysql> create procedure calc_fine_lib6(in roll int)
begin
declare finel int;
declare noofdays int;
declare issuedate date;
declare exit handler for SQLEXCEPTION select'create table definition’;
select dateofissue into issuedate from borrower where rollin=roll;
select datediff(curdate(), issuedate) into noofdays;
if noofdays>15 and noofdays<=30 then
set finel-noofdays*5;
insert into fine values(roll, curdate(), fine1);
elseif noofdays>30 then
set finel=((noofdays-30)*50) + 15*5;
insert into fine values(roll,curdate(), finel);
else
insert into fine values(roll,curdate(),0);
end if;
update borrower set status=’return’ where rollin=roll;
33
B.E. (E&TC) – 2019 Pattern Database Management Lab
end $
mysql> call
calc_fine_lib6(1)$ Query OK, 0 rows
affected (0.09 sec)mysql> call
calc_fine_lib6(2)$ Query OK, 0 rows
affected (0.09 sec) mysql> call
calc_fine_lib6(3)$ Query OK, 0 rows
affected (0.09 sec)
mysql> select * from fine;
-> $
Mysql>drop table fine$
Query OK, 0 rows affected (0.21 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> create table fine(rollno int, foreign key(rollno) references borrower(rollin), returndate
date, amount int)$
Query OK, 0 rows affected (0.34 sec)
mysql> call
calc_fine_lib6(1)$ Query OK, 0 rows
affected (0.09 sec)
34
B.E. (E&TC) – 2019 Pattern Database Management Lab
CONCLUSION:
Questions
1. What is need of PL-SQL?
2. What is the structure of PL-SQL?
REFERENCES
[1] Database Systems Concepts, Design and Applications by S. K. Singh. Pearson, 2nd Edition,
Chapter- 24, Page No- 1278-1286
Marks (Out of 20) Signature of Faculty with Date
MR (6) MP (6) MU (8) Total (20)
MR – Marks for Regularity, MP – Marks for Presentation, MU – Marks for
Understanding
35