Chapter 2, 3, 4, Sample Solutions - Marshall University Personal

advertisement
ANSWERS FOR:
Chapter 2 Minicase 1
Chapter 3 Exercise 2
Chapter 4 Exercise 2
=====================================================================================
CHAPTER 2 MINICASE 2
1. Draw an entity-relationship diagram that describes the following business environment.
Happy Cruise Lines has several ships and a variety of cruise itineraries, each involving several ports-of-call. The
company wants to maintain information on the sailors who currently work on each of its ships. It also wants
to keep track of both its past and future cruises and of the passengers who sailed on the former and are
booked on the latter.
Each ship has at least one and, of course, normally many sailors on it. The unique identifier of each ship is its
ship number. Other ship attributes include ship name, weight, year built, and passenger capacity. Each sailor
has a unique sailor identification number, as well as a name, date-of-birth, and nationality. Some of the sailors
are in supervisory positions, supervising several other sailors. Each sailor reports to just one supervisor. A
cruise is identified by a unique cruise serial number. Other cruise descriptors include a sailing date, a return
date, and a departure port (which is also the cruise’s ending point.) Clearly, a cruise involves exactly one ship;
over time a ship sails on many cruises, but there is a requirement to be able to list a new ship that has not as
yet sailed on any cruises, at all. Each cruise stops at at least one and usually several ports-of-call, each of
which is normally host to many cruises, over time. In addition, the company wants to maintain information
about ports that it has not as yet used in its cruises but may use in the future. A port is identified by its name
and the country that it is in. Other information about a port includes its population, whether a passport is
required for the passengers to disembark there, and its current docking fee, which is assumed to be the same
for all ships. Passenger information includes a unique passenger number, name, home address, nationality,
and date-of-birth. A cruise typically has many passengers on it (certainly at least one). Hoping for return
business, the company assumes that each passenger may have sailed on several of its cruises (and/or may be
booked for a future cruise.) For a person to be of interest to the company, he or she must have sailed on or be
booked on at least one of the company’s cruises. The company wants to keep track of how much money each
passenger paid for (or will pay for) each of their cruises, as well as their satisfaction rating of the cruise, if it has
been completed.
PORT
PK
PK
Port Name
Country
Population
Passport Req.
Docking Fee
Visits
Stops at
VISIT
PK
PK
PK
PASSENGER
Cruise Number
Port Name
Country
PK
Passenger Number
Name
Address
Nationality
Date of Birth
Visits
Visited by
Sails on
Had on
CRUISE
PK
VOYAGE
Cruise Number
Sailing Date
Return Date
Departure Port
Had on
PK
PK
Passenger Number
Cruise Number
Price Paid
Satisfaction Rating
Was on
Used in
Uses
SHIP
PK
SAILOR
Ship Number
Name
Weight
Year Built
Passenger Capacity
PK
Works on
Employs
Sailor Number
Name
Date of Birth
Nationality
Supervises
Supervised by
CHAPTER 3 EXERCISE 2
1. The Dynamic Chemicals Corp. keeps track of its customers and its orders. Customers typically have several
outstanding orders while each order was generated by a single customer. Each customer has a unique
customer number, a customer name, address, and telephone number. An order has a unique order
number, a date, and a total cost.
a. What kind of relationship is there between customers and orders?
b. Develop sample customer and order data and construct two relational tables in the style of Figure 3.5 in
which to store your sample data.
c. Do any fields have to be added to one or the other of the two tables to record the relationship between
customers and orders? Explain.
d. Merge these two tables into one, as in the style of Figure 3.6. Does this create any problems with the
data? Explain.
a. One-to-many.
b.
Customer Customer
Number
Name
Address
Telephone
116
Ace Corp.
456 Fir Dr.
555-8372
158
ABC Inc.
83 Curl Dr.
555-2600
209
Basic Corp.
600 Ivy Ln.
555-0049
(a) Customer file.
Order
Customer
Number
Date
Cost Number
1643
10/02/2003
3,450.00
209
1850
10/02/2003
2,259.00
116
1994
10/06/2003
4,675.00
158
2076
10/09/2003
2,846.00
158
2188
10/11/2003
538.00
209
2231
10/11/2003
1,759.00
158
(b) Order file.
c. The Customer Number field has to be added to the Order file in order to store the one-to-many
relationship between customers and orders.
d. Merging these two tables creates considerable data redundancy because all of the data for a particular
customer has to be repeated as many times as the number of orders it has. Note, for example, in the
table below that Customer Number 158’s name, address, and telephone number are all repeated three
times because it has three orders.
Order
Customer
Cost Number
Customer
Customer
Number
Name
Number
Date
Address
Telephone
1643
10/02/2003
3,450.00
209
209
Basic
Corp.
600 Ivy Ln.
555-0049
1850
10/02/2003
2,259.00
116
116
Ace Corp.
456 Fir Dr.
555-8372
1994
10/06/2003
4,675.00
158
158
ABC Inc.
83 Curl Dr.
555-2600
2076
10/09/2003
2,846.00
158
158
ABC Inc.
83 Curl Dr.
555-2600
2188
10/11/2003
538.00
209
209
Basic
Corp.
600 Ivy Ln.
555-0049
2231
10/11/2003
1,759.00
158
158
ABC Inc.
83 Curl Dr.
555-2600
CHAPTER 4 EXERCISE 2
1. Consider the following relational database for the Quality Appliance Manufacturing Co. The database is
designed to track the major appliances (refrigerators, washing machines, dishwashers, etc.) that Quality
manufactures. It also records information about Quality’s suppliers, the parts they supply, the buyers of
the finished appliances, and the finished goods inspectors. Note the following facts about this
environment:






Suppliers are the companies that supply Quality with its major components, such as electric motors, for the
appliances. Supplier number is a unique identifier.
Parts are the major components that the suppliers supply to Quality. Each part comes with a part number
but that part number is only unique within a supplier. Thus, from Quality’s point of view, the unique
identifier of a part is the combination of part number and supplier number.
Each appliance that Quality manufactures is given an appliance number that is unique across all of the
types of appliances that Quality makes.
Buyers are major department stores, home improvement chains, and wholesalers. Buyer numbers are
unique.
An appliance may be inspected by several inspectors. There is clearly a many-to-many relationship
between appliances and inspectors, as indicated by the INSPECTION table.
There are one-to-many relationships between suppliers and parts (Supplier Number is a foreign key in the
PART table,) parts and appliances (Appliance Number is a foreign key in the PART table,) and appliances
and buyers (Buyer Number is a foreign key in the APPLIANCE table.)

SUPPLIERNUM
SUPPLIERNAME
CITY
COUNTRY
TELEPHONE
SUPPLIER Table
PARTNUM
PART Table
SUPPLIERNUM
PARTTYPE
COST
APPLIANCENUM
APPLIANCENUM
APPLIANCETYPE
DATEMANUF
BUYERNUM
PRICE
APPLIANCE Table
BUYERNUM
BUYERNAME
CITY
COUNTRY
CREDITRATING
BUYER Table
INSPECTORNUM
INSPECTORNAME
SALARY
DATEHIRE
INSPECTOR Table
APPLIANCENUM
INSPECTORNUM
DATEINSPECTION
SCORE
INSPECTION Table
Write SQL SELECT commands to answer the following queries.
a. List the names, in alphabetic order, of the suppliers located in London, Liverpool, or Manchester, UK.
SELECT SUPPLIERNAME
FROM SUPPLIER
WHERE CITY IN (‘London’, ‘Liverpool’, ‘Manchester’)
AND COUNTRY=’UK’
ORDER BY SUPPLIERNAME;
b. List the names of the suppliers that supply motors (see PARTTYPE) costing between $50 and $100.
SELECT SUPPLIERNAME
FROM SUPPLIER, PART
WHERE SUPPLIER.SUPPLIERNUM=PART.SUPPLIERNUM
AND PARTTYPE=’Motor’
AND COST BETWEEN 50 AND 100;
c.
Find the average cost of the motors (see PARTTYPE) supplied by supplier number 3728.
SELECT AVG(COST)
FROM PART
WHERE PARTTYPE=’Motor’
AND SUPPLIERNUM=3728;
d. List the names of the inspectors who were inspecting refrigerators (see APPLIANCETYPE) on April 17,
2003.
SELECT INSPECTORNAME
FROM INSPECTOR, INSPECTION, APPLIANCE
WHERE INSPECTOR.INSPECTORNUM=INSPECTION.INSPECTORNUM
AND INSPECTION.APPLIANCENUM=APPLIANCE.APPLIANCENUM
AND APPLIANCETYPE=’Refrigerator’
AND DATEINSPECTION=’APR-17-2003’;
e. What was the highest inspection score achieved by a refrigerator on November 3, 2003?
SELECT MAX(SCORE)
FROM INSPECTION, APPLIANCE
WHERE INSPECTION.APPLIANCENUM=APPLIANCE.APPLIANCENUM
AND APPLIANCETYPE=’Refrigerator’
AND DATEINSPECTION=’NOV-03-2003’;
f. Find the total amount of money spent on Quality Appliance products by each buyer from Mexico,
Venezuela, and Argentina.
SELECT BUYERNUM, SUM(PRICE)
FROM APPLIANCE, BUYER
WHERE APPLIANCE.BUYERNUM=BUYER.BUYERNUM
AND COUNTRY IN (‘Mexico’, ‘Venezuela’, ‘Argentina’)
GROUP BY BUYERNUM;
g. Find the total cost of the parts used in each dishwasher manufactured on February 28, 2004. Only
include in the results those dishwashers that used at least $200 in parts.
SELECT APPLIANCENUM, SUM(COST)
FROM APPLIANCE, PART
WHERE APPLIANCE.APPLIANCENUM=PART.APPLIANCENUM
AND APPLIANCETYPE=’Dishwasher’
AND DATEMANUF=’FEB-28-2004’
GROUP BY APPLIANCENUM
HAVING SUM(COST)>=200;
h. List the highest paid inspectors.
SELECT INSPECTORNUM
FROM INSPECTOR
WHERE SALARY=
(SELECT MAX(SALARY)
FROM INSPECTOR);
i.
List the highest paid inspectors who were hired in 2002.
SELECT INSPECTORNUM
FROM INSPECTOR
WHERE DATEHIRE BETWEEN ’JAN-01-2002’ AND ’DEC-31-2002’
AND SALARY=
(SELECT MAX(SALARY)
FROM INSPECTOR
WHERE DATEHIRE BETWEEN ’JAN-01-2002’ AND ’DEC-31-2002’);
j.
Among all of the inspectors, list those who earn more money than the highest paid inspector who was
hired in 2002.
SELECT INSPECTORNUM
FROM INSPECTOR
WHERE SALARY>
(SELECT MAX(SALARY)
FROM INSPECTOR
WHERE DATEHIRE BETWEEN ’JAN-01-2002’ AND ’DEC-31-2002’);
Download