Database Systems Lab 7 PL/SQL Programming 1
Database Systems Lab 7 PL/SQL Programming 1
Database Systems
Lab 7
PL/SQL Programming 1
The objectives of this practical session is to write simple PL/SQL code using iSQL*Plus utility
and run the code against Oracle Server.
Three tables will be used in this session and the script to create and populate them is provided
on the shared folder. The file is called ‘author.sql’. Before completing any of these exercises
you should run this script in iSQLPlus to create the sample database
books_author1 authors
id NUMBER <pk>
books_author2
first_name VARCHAR2(20)
family_name VARCHAR2(20)
books_author3
books
isbn CHAR(10) <pk> fk_isbn inventory
category VARCHAR2(20) isbn CHAR(10) <pk,fk>
title VARCHAR2(100) status VARCHAR2(16)
num_pages NUMBER status_date DATE
price NUMBER quantity NUMBER
copyright NUMBER(4)
author1 NUMBER <fk1>
author2 NUMBER <fk2>
author3 NUMBER <fk3>
Task (1)
SET SERVEROUTPUT ON
DECLARE
max_author_no Authors.id%type;
BEGIN
SELECT MAX(id) INTO max_author_no
FROM authors;
DBMS_OUTPUT.PUT_LINE('The maximum author is : ' ||
max_author_no);
END;
/
1
PL/ SQL Programming 1
Task (2)
Write an anonymous block to display the date on which status was entered in ‘inventory’ table
for the book titled ‘Oracle DBA 101’
Hints:
Remember you need to set the serveroutput on to be able to print within a block. Your select
statement will need to join the inventory and books tables together.
SET SERVEROUTPUT ON
DECLARE
Date_entered inventory.status_date%type;
Book_Title books.title%type := 'Oracle DBA 101';
BEGIN
SELECT status_date INTO Date_entered
FROM inventory, books
WHERE books.isbn = inventory.isbn AND
Title = Book_Title;
DBMS_OUTPUT.PUT_LINE('Book : ' || Book_Title || ' entered on
'|| Date_entered);
END;
/
Task (3)
Using the same code developed for Task (2), change the title to ‘High-Performance SQL
Tuning’ and display the status from inventory table.
SET SERVEROUTPUT ON
DECLARE
pstatus inventory.status%type;
Book_Title books.title%type := 'Oracle High-Performance SQL
Tuning';
BEGIN
SELECT status INTO pstatus
FROM inventory, books
WHERE books.isbn = inventory.isbn AND
Title = Book_Title;
DBMS_OUTPUT.PUT_LINE('Book : ' || Book_Title || ' Status :
'|| pstatus);
END;
/
Task (4)
Write an anonymous block to calculate and display the full price of the book titled “Oracle
Database 10g A Beginner's Guide”.
The code should then check and apply the discount using the guidelines below t o produce the
discounted price which should also be outputted. You should ensure that your discounted price
is round up to 2 decimal places e.g. £12.99.
a) What is the discount price does you program give for Oracle Database 10g A Beginner's
Guide?
b) What is the discount price does you program give for Oracle 24x7 Tips and
Techniques?
SET SERVEROUTPUT ON
DECLARE
full_price books.price%type;
book_title VARCHAR2(100);
discount_price books.price%type;
BEGIN
book_title := 'Oracle 24x7 Tips and Techniques';