Commit 1b099aed authored by michael lundquist's avatar michael lundquist
Browse files

Adding all the changes for ch3 practice problems. Still need to put them into .md though.

parent 66ecd384
DROP TABLE dd_donor CASCADE CONSTRAINTS;
DROP TABLE dd_project CASCADE CONSTRAINTS;
DROP TABLE dd_status CASCADE CONSTRAINTS;
DROP TABLE dd_pledge CASCADE CONSTRAINTS;
DROP TABLE dd_payment CASCADE CONSTRAINTS;
CREATE TABLE DD_Donor (
idDonor number(4),
Firstname varchar2(15),
Lastname varchar2(30),
Typecode CHAR(1),
Street varchar2(40),
City varchar2(20),
State char(2),
Zip varchar2(9),
Phone varchar2(10),
Fax varchar2(10),
Email varchar2(25),
News char(1),
dtentered date DEFAULT SYSDATE,
CONSTRAINT donor_id_pk PRIMARY KEY(idDonor) );
CREATE TABLE DD_Project (
idProj number(6),
Projname varchar2(60),
Projstartdate DATE,
Projenddate DATE,
Projfundgoal number(12,2),
ProjCoord varchar2(20),
CONSTRAINT project_id_pk PRIMARY KEY(idProj),
CONSTRAINT project_name_uk UNIQUE (Projname) );
CREATE TABLE DD_Status (
idStatus number(2),
Statusdesc varchar2(15),
CONSTRAINT status_id_pk PRIMARY KEY(idStatus) );
CREATE TABLE DD_Pledge (
idPledge number(5),
idDonor number(4),
Pledgedate DATE,
Pledgeamt number(8,2),
idProj number(5),
idStatus number(2),
Writeoff number(8,2),
paymonths number(3),
Campaign number(4),
Firstpledge char(1),
CONSTRAINT pledge_id_pk PRIMARY KEY(idPledge),
CONSTRAINT pledge_idDonor_fk FOREIGN KEY (idDonor)
REFERENCES dd_donor (idDonor),
CONSTRAINT pledge_idProj_fk FOREIGN KEY (idProj)
REFERENCES dd_project (idProj),
CONSTRAINT pledge_idStatus_fk FOREIGN KEY (idStatus)
REFERENCES dd_status (idStatus));
CREATE TABLE DD_Payment (
idPay number(6),
idPledge number(5),
Payamt number(8,2),
Paydate DATE,
Paymethod char(2),
CONSTRAINT payment_id_pk PRIMARY KEY(idPay),
CONSTRAINT pay_idpledge_fk FOREIGN KEY (idPledge)
REFERENCES dd_pledge (idPledge) );
INSERT INTO dd_donor
VALUES (301, 'Mary', 'Treanor', 'I','243 main St.', 'Norfolk', 'VA','23510',NULL,NULL,'mtrea492@mdv.com','Y','01-SEP-2012');
INSERT INTO dd_donor
VALUES (302, 'Patrick', 'Lee', 'I','11 Hooper St.', 'Norfolk', 'VA','23510','7572115445',NULL,'pleeNorf@gmail.com','N','09-SEP-2012');
INSERT INTO dd_donor
VALUES (303, 'Terry', 'Venor', 'I','556 Loop Lane.', 'Chesapeake', 'VA','23320',NULL,NULL,'tervenr@drw.edu','Y','18-SEP-2012');
INSERT INTO dd_donor
VALUES (304, 'Sherry', 'Pane', 'I','Center Blvd.', 'Virginia Beach', 'VA','23455',NULL,NULL,'toppane@yahoo.com','Y','21-SEP-2012');
INSERT INTO dd_donor
VALUES (305, 'Thomas', 'Sheer', 'I','66 Train St.', 'Chesapeake', 'VA','23322','7579390022',NULL,'tls3488@sheer.com','Y','01-MAR-2013');
INSERT INTO dd_donor
VALUES (306, NULL, 'Coastal Developers', 'B','3667 Shore Dr.', 'Virginia Beach', 'VA','23450','8889220004',NULL,'coastVA@cdev.com','Y','30-SEP-2012');
INSERT INTO dd_donor
VALUES (307, NULL, 'VA Community Org', 'G','689 Bush Dr.', 'Norfolk', 'VA','23513','7578337467','7578337468','vacmorg@biz.com','Y','03-OCT-2012');
INSERT INTO dd_donor
VALUES (308, 'Betty', 'Konklin', 'I','11 Shark Ln.', 'Virginia Beach', 'VA','23455','7574550087',NULL,'shark11@cox.net','N','04-OCT-2012');
INSERT INTO dd_donor
VALUES (309, 'Jim', 'Tapp', 'I','200 Pine Tree Blvd.', 'Chesapeake', 'VA','23320','',NULL,'','N','08-OCT-2012');
INSERT INTO dd_donor
VALUES (310, NULL, 'Unique Dezigns', 'B','Connect Circle Unit 12', 'Chesapeake', 'VA','23320','7574442121',NULL,'UDezigns@cox.net','Y','11-SEP-2012');
INSERT INTO dd_project
VALUES (500,'Elders Assistance League', '01-SEP-2012','31-OCT-2012',15000,'Shawn Hasee');
INSERT INTO dd_project
VALUES (501,'Community food pantry #21 freezer equipment', '01-OCT-2012','31-DEC-2012',65000,'Shawn Hasee');
INSERT INTO dd_project
VALUES (502,'Lang Scholarship Fund', '01-JAN-2013','01-NOV-2013',100000,'Traci Brown');
INSERT INTO dd_project
VALUES (503,'Animal shelter Vet Connect Program', '01-DEC-2012','30-MAR-2013',25000,'Traci Brown');
INSERT INTO dd_project
VALUES (504,'Shelter Share Project 2013', '01-FEB-2013','31-JUL-2013',35000,'Traci Brown');
INSERT INTO dd_status
VALUES (10,'Open');
INSERT INTO dd_status
VALUES (20,'Complete');
INSERT INTO dd_status
VALUES (30,'Overdue');
INSERT INTO dd_status
VALUES (40,'Closed');
INSERT INTO dd_status
VALUES (50,'Hold');
INSERT INTO dd_pledge
VALUES (100,303,'18-SEP-2012',80,500,20,NULL,0,738,'Y');
INSERT INTO dd_pledge
VALUES (101,304,'21-SEP-2012',35,500,20,NULL,0,738,'Y');
INSERT INTO dd_pledge
VALUES (102,310,'01-OCT-2012',500,501,20,NULL,0,749,'Y');
INSERT INTO dd_pledge
VALUES (103,307,'03-OCT-2012',2000,501,20,NULL,0,749,'N');
INSERT INTO dd_pledge
VALUES (104,308,'04-OCT-2012',240,501,10,NULL,12,749,'Y');
INSERT INTO dd_pledge
VALUES (105,309,'08-OCT-2012',120,501,10,NULL,12,749,'Y');
INSERT INTO dd_pledge
VALUES (106,301,'12-OCT-2012',75,500,20,NULL,0,738,'N');
INSERT INTO dd_pledge
VALUES (107,302,'15-OCT-2012',1200,501,10,NULL,24,749,'Y');
INSERT INTO dd_pledge
VALUES (108,308,'20-JAN-2013',480,503,10,NULL,24,790,'N');
INSERT INTO dd_pledge
VALUES (109,301,'01-FEB-2013',360,503,10,NULL,12,790,'N');
INSERT INTO dd_pledge
VALUES (110,303,'01-MAR-2013',300,504,10,NULL,12,756,'N');
INSERT INTO dd_pledge
VALUES (111,306,'01-MAR-2013',1500,504,20,NULL,0,756,'Y');
INSERT INTO dd_pledge
VALUES (112,309,'16-MAR-2013',240,504,10,NULL,12,756,'N');
INSERT INTO dd_payment
VALUES (1425,100,80,'18-SEP-2012','CC');
INSERT INTO dd_payment
VALUES (1426,101,35,'21-SEP-2012','DC');
INSERT INTO dd_payment
VALUES (1427,102,500,'01-OCT-2012','CH');
INSERT INTO dd_payment
VALUES (1428,103,2000,'03-OCT-2012','CH');
INSERT INTO dd_payment
VALUES (1429,106,75,'12-OCT-2012','CC');
INSERT INTO dd_payment
VALUES (1430,104,20,'01-NOV-2012','CC');
INSERT INTO dd_payment
VALUES (1431,105,10,'01-NOV-2012','CC');
INSERT INTO dd_payment
VALUES (1432,107,50,'01-NOV-2012','CC');
INSERT INTO dd_payment
VALUES (1433,104,20,'01-DEC-2012','CC');
INSERT INTO dd_payment
VALUES (1434,105,10,'01-DEC-2012','CC');
INSERT INTO dd_payment
VALUES (1435,107,50,'01-DEC-2012','CC');
INSERT INTO dd_payment
VALUES (1436,104,20,'01-JAN-2013','CC');
INSERT INTO dd_payment
VALUES (1437,105,10,'01-JAN-2013','CC');
INSERT INTO dd_payment
VALUES (1438,107,50,'01-JAN-2013','CC');
INSERT INTO dd_payment
VALUES (1439,104,20,'01-FEB-2013','CC');
INSERT INTO dd_payment
VALUES (1440,105,10,'01-FEB-2013','CC');
INSERT INTO dd_payment
VALUES (1441,107,50,'01-FEB-2013','CC');
INSERT INTO dd_payment
VALUES (1442,108,20,'01-FEB-2013','CC');
INSERT INTO dd_payment
VALUES (1443,109,30,'01-FEB-2013','CC');
INSERT INTO dd_payment
VALUES (1444,104,20,'01-MAR-2013','CC');
INSERT INTO dd_payment
VALUES (1445,105,10,'01-MAR-2013','CC');
INSERT INTO dd_payment
VALUES (1446,107,50,'01-MAR-2013','CC');
INSERT INTO dd_payment
VALUES (1447,108,20,'01-MAR-2013','CC');
INSERT INTO dd_payment
VALUES (1448,109,30,'01-MAR-2013','CC');
INSERT INTO dd_payment
VALUES (1449,110,25,'01-MAR-2013','CC');
INSERT INTO dd_payment
VALUES (1450,111,1500,'01-MAR-2013','CH');
INSERT INTO dd_payment
VALUES (1451,104,20,'01-APR-2013','CC');
INSERT INTO dd_payment
VALUES (1452,105,10,'01-APR-2013','CC');
INSERT INTO dd_payment
VALUES (1453,107,50,'01-APR-2013','CC');
INSERT INTO dd_payment
VALUES (1454,108,20,'01-APR-2013','CC');
INSERT INTO dd_payment
VALUES (1455,109,30,'01-APR-2013','CC');
INSERT INTO dd_payment
VALUES (1456,110,25,'01-APR-2013','CC');
INSERT INTO dd_payment
VALUES (1457,112,20,'01-APR-2013','CC');
INSERT INTO dd_payment
VALUES (1458,104,20,'01-MAY-2013','CC');
INSERT INTO dd_payment
VALUES (1459,105,10,'01-MAY-2013','CC');
INSERT INTO dd_payment
VALUES (1460,107,50,'01-MAY-2013','CC');
INSERT INTO dd_payment
VALUES (1461,108,20,'01-MAY-2013','CC');
INSERT INTO dd_payment
VALUES (1462,109,30,'01-MAY-2013','CC');
INSERT INTO dd_payment
VALUES (1463,110,25,'01-MAY-2013','CC');
INSERT INTO dd_payment
VALUES (1464,112,20,'01-MAY-2013','CC');
COMMIT;
\ No newline at end of file
/*
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:
*/
\ No newline at end of file
DECLARE
lv_ship_date bb_basketstatus.dtstage%TYPE;
lv_shipper_txt bb_basketstatus.shipper%TYPE;
lv_ship_num bb_basketstatus.shippingnum%TYPE;
lv_bask_num bb_basketstatus.idbasket%TYPE := 3;
BEGIN
SELECT dtstage, shipper, shippingnum
INTO lv_ship_date, lv_shipper_txt, lv_ship_num
FROM bb_basketstatus
WHERE idbasket = lv_bask_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;
DECLARE
rec_ship bb_basketstatus%ROWTYPE;
lv_bask_num bb_basketstatus.idbasket%TYPE := 3;
BEGIN
SELECT *
INTO rec_ship
FROM bb_basketstatus
WHERE idbasket = lv_bask_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;
\ No newline at end of file
DECLARE
lv_total_num NUMBER(6,2);
lv_rating_txt VARCHAR2(4);
lv_shop_num bb_basket.idshopper%TYPE := 22;
BEGIN
SELECT SUM(total)
FROM bb_basket
WHERE idShopper =
AND orderplaced = 1
GROUP BY idshopper;
IF lv_total_num > 200 THEN
END IF;
DBMS_OUTPUT.PUT_LINE('Shopper '||lv_shop_num||' is rated '||lv_rating_txt);
END;
\ No newline at end of file
This diff is collapsed.
/*
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 applicatio npage 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
*/
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.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
*/
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.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.
*/
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.4
same as 3.3, but use a searched case statement.
*/
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.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.
*/
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.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.*/
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;
/
\ 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