You are on page 1of 5

Below is the ERD diagram for a database called ‘Book Store’ which stores information about

books and authors. Script for creating the tables and to populate the data is given with the
assignment.

- Run the script provided with this assignment to create the tables and insert the data.
- Save all your code for this assignment in Asg3_code.sql and all output in Asg3_output.txt.
Q1.
Create a PL/SQL block that declares a cursor called B_CAT with a parameter for selecting all books
for a certain category from BOOK table. Use a substitution variable B_CAT for passing a category
code to the cursor and printing the details of all books in the given category.
Use the following to test your code:

Page 1 of 5
DEFINE B_CAT = COM

Sample Output:
ISBN: 3957136468
Title: HOLY GRAIL OF ORACLE
Publication Date: 31-DEC-01
Publisher: AMERICAN PUBLISHING
Cost: 47.25
Retail: 75.95

ISBN: 1915762492
Title: HANDCRANKED COMPUTERS
Publication Date: 21-JAN-01
Publisher: AMERICAN PUBLISHING
Cost: 21.8
Retail: 25
………

Q2.
Create a copy of the BOOK table named LST_BOOK, populating the table with information from
BOOK published only in a given year. Use the following substitution variable to run your script:
DEFINE B_YEAR = 2000

a. Write a block that prints out the ISBN, title and author name for all books in LST_BOOK.

Raise an exception if the book is not published in the year specified in the substitution
variable B_YEAR. Handle the exception with an appropriate exception handler that prints
out ‘Book#... skipped since not published in the specified year’. Use a counter as the book
# in above printed message.

Raise another exception if the book does not belong to ‘fitness’ or ‘family life’ category.
Handle the exception with an appropriate exception handler that prints out ‘book #...
skipped since not of interest’.

b. Test the block with DEFINE B_YEAR = 2000. You should get the books published in 2000.

c. Test the block with DEFINE B_YEAR = 2002. You should get an output that skips over all
books that were displayed in part (c). For example (this is just a sample output):

Page 2 of 5
PL/SQL procedure successfully completed.
Book #1 skipped since not published in 2002.

Q3.
a. Create a procedure, named RAND_SELECT with the following parameters:
- An input parameter, N, indicating the number of books to select,
- Two output parameters, NCN and NFL, to indicate the ‘number of books belonging to
computer’ and ‘number of books in the family life’ songs selected.
In this procedure,
- Create an INDEX BY table named NUM_TABLE to store a type matching B_NUM.
- Create a cursor to query all rows of BOOK table.
- LOOP through all rows of above cursor
o Write the B_NUM values into consecutive elements in NUM_TABLE
- Delete all rows in LST_BOOK
- In a second loop, LOOP for N times
o Generate a random number r between 1 and the number of values in
NUM_TABLE
Hint 1: To generate an integer between 1 and 1000, you can use:
“select trunc(dbms_random.value(1,1000)) num from dual;”
Hint 2: NUM_TABLE.COUNT returns the number of books in the table
o Use the rth item in the NUM_TABLE to SELECT the book with that number
from the BOOK table. INSERT this song into the LST_BOOK table
o Update the NCN and NFL parameters, if the selected book belong to the
computer category or the family life category respectively.

b. Write a PL/SQL code that calls the above procedure to select 3 random books from BOOK
into LST_BOOK table. Display the books, and then display the number of ‘computer
related’ and ‘family life related’ books in this set.

Q4.

a. Create a stored procedure called CHECK_COST as follows:


- The procedure accepts three parameters, one for book ISBN of type string and the
other two numeric values one for retail price and one for cost.

Page 3 of 5
- The procedure checks if the given cost is less than the retail price, for cost higher
than retail price it should raise an application exception, with the message “Price
of the book <ISBN> should be higher than the <cost>”

b. Create a trigger called CHECK_COST_TRG on the BOOK table that fires before an INSERT
or UPDATE operation on each row.

c. Test the CHECK_COST_TRG using the following cases:

1) Add a new employee with the following data:


i. ISBN:1285196147
ii. Title: Database Systems - Design, Implementation & Management
iii. PubDate: 01-Feb-2006
iv. CatCode: COM
v. Cost: 95.99
vi. Retail: 80

- If you get an error message, fix the cost and try to add the record again with cost
equal to $69.

2) Update the cost of the book with title "DATABASE IMPLEMENTATION" to $60

LAB
A. Create a new SQL worksheet in SQL Developer, save as Lab10.sql. Use these two lines at
the beginning of your script:
SET SERVEROUTPUT ON
SET VERIFY OFF

Part I: Exceptions
1- Write a PL/SQL block that ask the user for a first name and selects the row from the
employees table with the given first name and displays the information. For example, given
‘Lex’ as the user input, the output is:

Employee 102, Lex De Haan, hired on 13-JAN-93

If there are no employees with that name, for example, given ‘John’ as the user input, handle
the predefined exception with the following message displayed:

Page 4 of 5
No employees with the given first name.

If there are more than one rows returned, handle the predefined exception with the following
message displayed:

More than one employee with this name.

Part II: Procedures and Functions


2- Write a function that takes a department ID as input and returns the number of employees
that manage others in that department. If the ID does not match any departments, return
NULL.
3- Write a PL/SQL block that loops though departments 10, 20, and 30 and displays the number
of mangers for these departments using the function in part (a).

Part III: Triggers


4- Create a trigger that is fired when an employee is deleted, ignoring the delete and raising an
error, prompting that an employee cannot be deleted.
5- Delete employee with employee ID 200. Did the above trigger run? Check to make sure the
employee is not deleted.

Page 5 of 5

You might also like