Commit 5a252196 authored by michael lundquist's avatar michael lundquist
Browse files

week 3 homework

parent 1b099aed
# hands on 3
Before you start working on each chapter's exercises, you need to run a script to rebuild the database(s). This step ensures that the database(s) is in the correct state. Use the script above to rebuild the DoGood Donor database (DD_create). Use the DoGood Donor database for this assignment.
Instructions: Write appropriate PL/SQL programs to complete Hands-On Assignments 3.9-3.11 (Part II) for Chapter 3. For each program:
## 3_9 Retrieving Pledge Totals
Create a PL/SQL block that retrieves and displays information for a specific project based on Project ID. Display the following on a single row of output: project ID, project name, number of pldeges mads, total dollars pledged, and the average pledge amount.
```plsql
declare
lv_projid_num dd_project.idproj%TYPE := 501;
lv_pledgesum_num dd_pledge.pledgeamt%TYPE;
lv_pledgeavg_num dd_pledge.pledgeamt%TYPE;
lv_pledgecount_num NUMBER(8);
lv_projname_txt dd_project.projname%TYPE;
begin
select count(*), sum(pledgeamt), avg(pledgeamt)
into lv_pledgecount_num, lv_pledgesum_num, lv_pledgeavg_num
from dd_pledge join dd_project using (idproj)
where idproj = lv_projid_num
group by idproj;
select projname
into lv_projname_txt
from dd_project
where idproj = lv_projid_num
;
dbms_output.put_line( 'project name: ' || lv_projname_txt );
dbms_output.put_line( 'pledge count: ' || lv_pledgecount_num );
dbms_output.put_line( 'pledge average: ' || lv_pledgeavg_num );
dbms_output.put_line( 'pledge total for project: ' || lv_pledgesum_num );
end;
/
```
![3.9](./pics/3_9.PNG)
## 3.10
Create a PL/SQL block to handle adding a new project. Create and use a sequence named `DD_PROJID_SEQ` to handle generating and populating the project ID. The first number issued by this sequence should be 530, and no caching should be used. Use a record variable to handle the data to be added. Data for the new row should be the following: project name = HK Animal Shelter Extension, start = 1/1/2013, end = 5/31/2013, and fundraising goal = $65,000. Any columns not addressed in the data list are currently unnown
```plsql
--DOESN'T WORK IN THE BLOCK
CREATE SEQUENCE DD_PROJID_SEQ
MINVALUE 530
START WITH 530
INCREMENT BY 1;
declare
first_proj_var dd_project%ROWTYPE;
begin
select DD_PROJID_SEQ.NEXTVAL
into first_proj_var.idproj
FROM DUAL;
first_proj_var.projname := 'HK Animal Shelter Extension';
first_proj_var.PROJSTARTDATE := '1-JAN-2013';
first_proj_var.PROJENDDATE := '31-MAY-2013';
first_proj_var.projfundgoal := 65000;
INSERT INTO dd_project
VALUES first_proj_var;
end;
/
```
![3.10](./pics/3_10.PNG)
## 3.11 Retrieving and Displaying Pledge Data
Create a PL/SQL block to retrieve and display data for all pledges made in a specified month. One row of output should be displayed for each pledge. Include the following in each row of output:
- Pledge ID, donor ID, and pledge amount
- If the pledge is being paid in a lump sum, display "Lump Sum."
- If the pledge is being paid in monthly payments, display "Monthly - #"
- The list should be sorted to display all lump sum pledges first.
```plsql
declare
--using a table of records
TYPE type_pledge IS TABLE OF dd_pledge%ROWTYPE
index by binary_integer;
lv_pledges_var type_pledge;
lv_month_date number(2) := 10; -- a month to select pledges from
lv_paymentmonths_txt varchar2(30) := 'one lump Sum.';
begin
--getting info
select * BULK COLLECT
into lv_pledges_var
from dd_pledge
where extract(month from pledgedate) = lv_month_date;
--displaying it
FOR i in lv_pledges_var.first..lv_pledges_var.last loop
if lv_pledges_var(i).paymonths > 0
then lv_paymentmonths_txt := 'Monthly - ' || lv_pledges_var(i).paymonths;
end if;
dbms_output.put_line( 'pledge ID: ' || lv_pledges_var(i).IDPLEDGE || ' donor id ' || lv_pledges_var(i).IDDONOR || ' pledge amount ' || lv_pledges_var(i).pledgeamt || ' paid in ' || lv_paymentmonths_txt);
end loop;
end;
/
```
![3.11](./pics/3_11.PNG)
\ No newline at end of file
/* /*
# hands on 3
Before you start working on each chapter's exercises, you need to run a script to rebuild the database(s). This step ensures that the database(s) is in the correct state. Use the script above to rebuild the DoGood Donor database (DD_create). Use the DoGood Donor database for this assignment. Before you start working on each chapter's exercises, you need to run a script to rebuild the database(s). This step ensures that the database(s) is in the correct state. Use the script above to rebuild the DoGood Donor database (DD_create). Use the DoGood Donor database for this assignment.
Instructions: Write appropriate PL/SQL programs to complete Hands-On Assignments 3.9-3.11 (Part II) for Chapter 3. For each program: Instructions: Write appropriate PL/SQL programs to complete Hands-On Assignments 3.9-3.11 (Part II) for Chapter 3. For each program:
*/ */
\ No newline at end of file
/*
## 3_9 Retrieving Pledge Totals
Create a PL/SQL block that retrieves and displays information for a specific project based on Project ID. Display the following on a single row of output: project ID, project name, number of pldeges mads, total dollars pledged, and the average pledge amount.
*/
-- dd_pledge
declare
lv_projid_num dd_project.idproj%TYPE := 501;
lv_pledgesum_num dd_pledge.pledgeamt%TYPE;
lv_pledgeavg_num dd_pledge.pledgeamt%TYPE;
lv_pledgecount_num NUMBER(8);
lv_projname_txt dd_project.projname%TYPE;
begin
select count(*), sum(pledgeamt), avg(pledgeamt)
into lv_pledgecount_num, lv_pledgesum_num, lv_pledgeavg_num
from dd_pledge join dd_project using (idproj)
where idproj = lv_projid_num
group by idproj;
select projname
into lv_projname_txt
from dd_project
where idproj = lv_projid_num
;
dbms_output.put_line( 'project name: ' || lv_projname_txt );
dbms_output.put_line( 'pledge count: ' || lv_pledgecount_num );
dbms_output.put_line( 'pledge average: ' || lv_pledgeavg_num );
dbms_output.put_line( 'pledge total for project: ' || lv_pledgesum_num );
end;
/
/*
## 3.10
Create a PL/SQL block to handle adding a new project. Create and use a sequence named `DD_PROJID_SEQ` to handle generating and populating the project ID. The first number issued by this sequence should be 530, and no caching should be used. Use a record variable to handle the data to be added. Data for the new row should be the following: project name = HK Animal Shelter Extension, start = 1/1/2013, end = 5/31/2013, and fundraising goal = $65,000. Any columns not addressed in the data list are currently unnown
*/
--DOESN'T WORK IN THE BLOCK
CREATE SEQUENCE DD_PROJID_SEQ
MINVALUE 530
START WITH 530
INCREMENT BY 1;
declare
first_proj_var dd_project%ROWTYPE;
begin
select DD_PROJID_SEQ.NEXTVAL
into first_proj_var.idproj
FROM DUAL;
first_proj_var.projname := 'HK Animal Shelter Extension';
first_proj_var.PROJSTARTDATE := '1-JAN-2013';
first_proj_var.PROJENDDATE := '31-MAY-2013';
first_proj_var.projfundgoal := 65000;
INSERT INTO dd_project
VALUES first_proj_var;
end;
/
/*
## 3.11 Retrieving and Displaying Pledge Data
Create a PL/SQL block to retrieve and display data for all pledges made in a specified month. One row of output should be displayed for each pledge. Include the following in each row of output:
- Pledge ID, donor ID, and pledge amount
- If the pledge is being paid in a lump sum, display "Lump Sum."
- If the pledge is being paid in monthly payments, display "Monthly - #"
- The list should be sorted to display all lump sum pledges first.
*/
declare
--using a table of records
TYPE type_pledge IS TABLE OF dd_pledge%ROWTYPE
index by binary_integer;
lv_pledges_var type_pledge;
lv_month_date number(2) := 10; -- a month to select pledges from
lv_paymentmonths_txt varchar2(30) := 'one lump Sum.';
begin
--getting info
select * BULK COLLECT
into lv_pledges_var
from dd_pledge
where extract(month from pledgedate) = lv_month_date;
--displaying it
FOR i in lv_pledges_var.first..lv_pledges_var.last loop
if lv_pledges_var(i).paymonths > 0
then lv_paymentmonths_txt := 'Monthly - ' || lv_pledges_var(i).paymonths;
end if;
dbms_output.put_line( 'pledge ID: ' || lv_pledges_var(i).IDPLEDGE || ' donor id ' || lv_pledges_var(i).IDDONOR || ' pledge amount ' || lv_pledges_var(i).pledgeamt || ' paid in ' || lv_paymentmonths_txt);
end loop;
end;
/
\ No newline at end of file
practiceProblems/week9/pics/3_1b.PNG

28.6 KB | W: | H:

practiceProblems/week9/pics/3_1b.PNG

32.6 KB | W: | H:

practiceProblems/week9/pics/3_1b.PNG
practiceProblems/week9/pics/3_1b.PNG
practiceProblems/week9/pics/3_1b.PNG
practiceProblems/week9/pics/3_1b.PNG
  • 2-up
  • Swipe
  • Onion skin
# week 9 pp answers
From now on, before you start working through each chapter's examples, you need to run a script to rebuild the database. This step ensures that the database is in the correct state to achieve the coding results shown in the chapter and includes the new objects required to for chapter examples. Use the script file provided above to rebuild the database.
Complete Hands-On Assignments 3.1-3.5 and 3-8 (Part I) for Chapter 3 given in the textbook. Use the script files provided above when appropriate
## 3.1 Querying Data in a Block
- A Brewbean's application page is being developed for employees to enter a basket number and view shipping information for the order including:
- date
- shipper
- shipping number
- An IDSTAGE of 5 means the order has shipped
- There are steps we have to follow here. Code is below
```plsql
DECLARE
lv_ship_date bb_basketstatus.dtstage%TYPE;
lv_shipper_txt bb_basketstatus.shipper%TYPE;
lv_ship_num bb_basketstatus.shippingnum%TYPE;
lv_back_num bb_basketstatus.idbasket%TYPE := 7;
begin
SELECT dtstage, shipper, shippingnum
INTO lv_ship_date, lv_shipper_txt, lv_ship_num
FROM bb_basketstatus
WHERE idbasket = lv_back_num
AND idstage = 5;
dbms_output.put_line( 'Date Shipped: ' || lv_ship_date );
dbms_output.put_line( 'Shipper: ' || lv_shipper_txt );
dbms_output.put_line( 'Shipping #: ' || lv_ship_num );
end;
/
```
![3.1a](./pics/3_1a.png)
![3.1a](./pics/3_1b.png)
## 3.2
A Brewbean's application page is being developed for employees to enter a basket number and view shipping information for the order. The page needs to display all column values from the BB_BASKETSTATUS table. An INSTAGE value of 5 in the BB_BASKETSTATUS table indicates that the order has been shipped. They tell you to run the following code
```plsql
DECLARE
rec_ship bb_basketstatus%ROWTYPE;
lv_back_num bb_basketstatus.idbasket%TYPE := 3;
begin
select *
into rec_ship
FROM bb_basketstatus
WHERE idbasket = lv_back_num
AND idstage = 5;
dbms_output.put_line( 'Date Shipped: ' || rec_ship.dtstage );
dbms_output.put_line( 'Shipper: ' || rec_ship.shipper );
dbms_output.put_line( 'Shipping #: ' || rec_ship.shippingnum );
dbms_output.put_line( 'Notes: ' || rec_ship.notes );
end;
/
```
![3.2](./pics/3_2.png)
## 3.3 Processing Database Data with IF Statements
The Brewbean's application needs a block to determine whether a customer is HIGH, MID or LOW based on his or her total purchases. The block needs to select the total amount of orders for a specified customer, determine the rating, and then display the results onscreen. The code rates the customer HIGH if total purchases are greater than $200, MID if greater than $100, and LOW if lower than $100. Use an initialized variable to provide the shopper ID.
```plsql
DECLARE
lv_total_num NUMBER(6,2);
lv_rating_txt VARCHAR2(4) := 'LOW';
lv_shop_num bb_basket.idshopper%TYPE := 22;
BEGIN
SELECT SUM(total), idshopper
into lv_total_num, lv_shop_num
FROM bb_basket
WHERE idShopper = 22
AND orderplaced = 1
GROUP BY idshopper;
IF lv_total_num > 200
THEN lv_rating_txt := 'HIGH';
elsif lv_total_num > 100
THEN lv_rating_txt := 'MID';
END IF;
DBMS_OUTPUT.PUT_LINE('Shopper '||lv_shop_num||' is rated '||lv_rating_txt);
END;
/
```
![3.3](./pics/3_3.png)
## 3.4
same as 3.3, but use a searched case statement.
```plsql
DECLARE
lv_total_num NUMBER(6,2);
lv_rating_txt VARCHAR2(4) := 'LOW';
lv_shop_num bb_basket.idshopper%TYPE := 22;
BEGIN
SELECT SUM(total), idshopper
into lv_total_num, lv_shop_num
FROM bb_basket
WHERE idShopper = 22
AND orderplaced = 1
GROUP BY idshopper;
CASE
WHEN lv_total_num > 200
THEN lv_rating_txt := 'HIGH';
WHEN lv_total_num > 100
THEN lv_rating_txt := 'MID';
END CASE;
DBMS_OUTPUT.PUT_LINE('Shopper '||lv_shop_num||' is rated '||lv_rating_txt);
END;
/
```
![3.4](./pics/3_4.png)
## 3.5
Brewbean's wants to include a feature in its application that calculates the total amount (quantity) of a specified item that can be purchased with a given amount of money. Create a block with a WHILE loop to increment the item's cost until the dollar value is met. Test first with a total spending amount of $100 and product ID 4. Then test with an amount and a product of your choice. Use initialized variables to provide the total spending amount and product ID.
```plsql
DECLARE
lv_balance_num NUMBER(6,2) := 100;
lv_qty_txt NUMBER(4) := 0;
lv_prodid_num bb_product.idproduct%TYPE := 4;
lv_price_num bb_product.price%TYPE := 4;
BEGIN
SELECT price
into lv_price_num
FROM bb_product
WHERE idproduct = lv_prodid_num;
WHILE lv_balance_num >= lv_price_num LOOP
lv_balance_num := lv_balance_num - lv_price_num;
lv_qty_txt := lv_qty_txt + 1;
end loop;
DBMS_OUTPUT.PUT_LINE('you can buy ' || lv_qty_txt || ' items at ' || lv_price_num || ' each.');
END;
/
```
![3.5](./pics/3_5.png)
## 3.8
The Brewbean's application contains a page displaying order summary info, including IDBASKET, SUBTOTAL, SHIPPING, TAX, and TOTAL columns from the BB_BASKET table. Create a PL/SQL block with a record variable to retrieve this data and display it onscreen. An initialized variable should provide the IDBASKET value. Test the block usign the basket ID 12.*/
```plsql
DECLARE
lv_basket_var bb_basket%ROWTYPE;
BEGIN
SELECT *
into lv_basket_var
FROM bb_basket
WHERE idbasket = 12;
DBMS_OUTPUT.PUT_LINE('Basket ID 12 has: ');
DBMS_OUTPUT.PUT_LINE('subtotal: ' || lv_basket_var.subtotal);
DBMS_OUTPUT.PUT_LINE('shipping: ' || lv_basket_var.shipping);
DBMS_OUTPUT.PUT_LINE('tax: ' || lv_basket_var.tax);
DBMS_OUTPUT.PUT_LINE('total: ' || lv_basket_var.total);
END;
/
```
![3.8](./pics/3_8.png)
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment