Phase9 PLSQL
Phase9 PLSQL
Submission Guidelines:
1. This assignment is worth 35 points for all students.
2. It is your responsibility to make sure all files are readable and submitted on time.
Submission:
• Task 1. Submit a single file named Phase9_Procedure.sql containing the procedure for 15
points.
• Task 2. Submit a single file named Phase9_Triggers.sql containing both (two) triggers for
a total of 20 points.
Task 1. PL/SQL Procedures (15 points)
Introduction. Your task is to write a procedure that generates a report for all the houses in the
If bot, delimiter outp ut with “x”
price range for a given buyer’s SSN. You will use DBMS_OUTPUT.PUT_LINE to print the
report. Keep in mind that it may be easier to start by building an anonymous block that you can
later store as a procedure.
Testing. The next page contains example procedure calls along with the report they return. Your
formatting does not need to match mine exactly, but it must contain the relevant information
listed above.
Hint: You will need to use a cursor, but not more than one. If you find yourself writing several
cursors, 1) look at how you could improve your cursor and 2) ask if you could solve the problem
with a SELECT INTO.
Hint: You should be able to write this procedure in ~45 - 55 lines of code. If your solution uses
more than 70 lines, I encourage you to ask questions in class or office hours.
Hint: If you want to format your output like mine, you can use the LPAD() and RPAD()
functions you have probably seen in other programming languages.
call HouseReport('987-65-4322');
call HouseReport('987-65-4323');
call HouseReport('987-65-4324');
No available houses!
Task 2. PL/SQL Triggers (20 points)
Introduction. Your task is to write two triggers that will maintain the number of houses an agent
is listing. While you are not required to use DBMS_OUTPUT.PUT_LINE, I highly recommend
using it for testing and debugging your code. Note, this is an example of a computed column that
we saw in Chapter 6: E-R Modeling.
First, add a column to the Agent table to store this value using the following command:
ALTER TABLE Agent ADD HousesListed NUMBER DEFAULT 0;
If you make a mistake along the way, you can reset this column with a simple update:
UPDATE Agent SET HousesListed = 0;
Trigger #1. Write a trigger that will fire when you INSERT a row into the House table. This
trigger will check the value of Agent.HousesListed for the corresponding Agent’s ID. If
use error # -21234.
Agent.HousesListed is less than 4, then an agent can list more houses. If Agent.HousesListed is
name the trigger seller status
equal to 4, then the agent is too busy so your trigger will cancel the INSERT and display a
make comment “forbidden seller”
• Run your .sql script containing your INSERT statements for the House table.
• The insert for House H20 should produce an error message that looks like:
INSERT INTO house VALUES('H20', '600 Poydras', 'Central Business
District', 335000, 1400, '723-45-6789', 'A01')
Error report -
ORA-20201: Agent is too busy!
ORA-06512: at "JAYSTUDENT.ADDHOUSE", line 15
ORA-04088: error during execution of trigger 'JAYSTUDENT.ADDHOUSE'
• The query SELECT * FROM Agent should return the following output:
ID NAME PHONE ASS HOUSESLISTED
--- ------------------------------ ------------ --- ------------
A10 Shreya Banerjee 504-125-4567 0
A02 James Wagner 504-123-5567 3
A08 David Pace 504-124-4567 0
A05 Vassil Roussev 504-323-4567 A02 3
A04 Ben Samuel 504-123-4667 A02 2
A07 Yasin Nur 504-123-4597 A02 0
A03 Chris Summa 504-223-4567 A10 3
A09 Tamjid Hoque 504-423-4567 A10 2
A01 Kathy Johnson 504-123-4567 A03 4
A06 Ted Holmberg 504-123-4767 A04 2
• The House table should only have 19 records in it since the insert for H20 was rejected.
Trigger #2. Write a trigger that will fire when a user attempts to DELETE one or more rows
Use seller table not h ouse
from House. This trigger will update the Agent.HousesListed column for the corresponding
update buyer.houses_b ought, not the agent table. Decrease when removed from seller, not h ouse
Agent’s ID by decreasing the value by one each time a row is removed from House.
• SELECT * FROM Agent should return the following output if you delete H07:
ID NAME PHONE ASS HOUSESLISTED
--- ------------------------------ ------------ --- ------------
A10 Shreya Banerjee 504-125-4567 0
A02 James Wagner 504-123-5567 3
A08 David Pace 504-124-4567 0
A05 Vassil Roussev 504-323-4567 A02 3
A04 Ben Samuel 504-123-4667 A02 2
A07 Yasin Nur 504-123-4597 A02 0
A03 Chris Summa 504-223-4567 A10 3
A09 Tamjid Hoque 504-423-4567 A10 2
A01 Kathy Johnson 504-123-4567 A03 4
A06 Ted Holmberg 504-123-4767 A04 1