Sei sulla pagina 1di 12

4002-360 KEY Final Exam Prep

Department of Information Sciences and Technologies

Part I. This prep will use the Book Trading Database that you used for HW #6. In this example, our Universe of Discourse is a portion of a (hypothetical) Book Trading database, used to capture information about books that are owned and traded among college students in IT. Figure 1. ERWin model of Book Trading database (foreign keys included for your convenience).

4002-360 Final Practical SQL Prep

-1-

Captured in English, the entities of this Universe of Discourse, and the relationships among them, can be described like this: Book .. (1) identified by an ISBN, (2) written by one primary BookAuthor and zero or more secondary BookAuthors, (3) categorized by subject matter Category, (4) published by Publisher, and (5) reviewed in a given BookReview .. individuals that are either the primary or secondary author(s) of a specific Book occurrence .. individuals that are primary or secondary authors .. the rating assigned to a specific Book occurrence by a given Reviewer .. individuals that review Books and assign a rating to it .. Books owned by Owners that are deemed to be in a certain Condition as rated by a ConditionEvaluator .. individuals that own OwnersBooks .. the set of Condition rankings (1..5, corresponding to bad, poor, average, good and excellent) that can be assigned to an OwnersBook .. individuals that evaluate the condition of a book and assign a Condition to it the set of BOOK subject matter classifications .. publishes BOOKs in a given U.S. State .. state codes and full state names

BookAuthor Author BookReview Reviewer OwnersBook Owners Condition

ConditionEvaluator Category Publisher State

4002-360 Final Practical SQL Prep

-2-

1. Given the following portion of an E-R diagram, create the AGENT table including appropriate constraints. Also, do what is necessary to implement the relationship between AUTHOR and AGENT. AgentID should be an integer and FirstName and LastName should be the same datatype as the name attributes in AUTHOR.

Create table agent( Agentid integer, FirstName varchar(50), LastName varchar(50), PhoneNumber varchar(12), Constraint agent_agentid_pk primary key (agentid)); Alter table author add column agentid integer not null; Alter table author add constraint author_agentid_fk foreign key (agentid) references AGENT(agentid);

2. What are the titles, authors names, and prices for pre-owned books that are priced at $50.00 or below? Your output should match the display given below:
mysql> select title, lastname, firstname, price -> from ownersbook, book, bookauthor, author -> where price <=50.00 and -> ownersbook.isbn=book.isbn and -> book.isbn=bookauthor.isbn and -> bookauthor.authorid=author.authorid; +-------------------------------------------------+------------+-----------+-------+ | title | lastname | firstname | price | +-------------------------------------------------+------------+-----------+-------+ | Yes! Networking is for Bills Fans | Lutz | Peter | 40.00 | | Yes! Networking is for Bills Fans | Leone | James | 40.00 | | Yes! Networking is for Bills Fans | Phelps | Andrew | 40.00 | | My Love's Last Longing | Heartthrob | Danielle | 50.00 | | How to Keep your Cellular Bill Down | Hartpence | Bruce | 25.00 | | ER, SOM, NF, DK/NF, SQL, JDBC, ODBC, and RELVAR | Stratton | Bill | 50.00 | | How to Keep your Cable Bill Down | Hartpence | Bruce | 45.00 | | My Lost Love's Long Last Lingering | Heartthrob | Danielle | 25.00 | | The Shortest Book in the World | Phelps | Andrew | 35.00 | | From the Shores of Lake Erie to IT | Stratton | Bill | 0.00 | +-------------------------------------------------+------------+-----------+-------+ 10 rows in set (0.00 sec)

4002-360 Final Practical SQL Prep

-3-

3. Count the number of publishers that are in each location. Your output should match the display given below: mysql> select city, statecode, count(*) "Count" -> from publisher -> group by city, statecode; +---------------+-----------+-------+ | city | statecode | Count | +---------------+-----------+-------+ | Boston | MA | 1 | | Chicago | IL | 3 | | Hartford | CT | 1 | | New York | NY | 4 | | Rochester | MN | 1 | | Rochester | NY | 2 | | San Francisco | CA | 1 | +---------------+-----------+-------+ 7 rows in set (0.00 sec) 4. Count the number of publishers that are in each location but display only those location that have at least 2 publishers. Your output should match the display given below: mysql> select city, statecode, count(*) "Count" -> from publisher -> group by city, statecode -> having count >2; +----------+-----------+-------+ | city | statecode | Count | +----------+-----------+-------+ | Chicago | IL | 3 | | New York | NY | 4 | +----------+-----------+-------+ 2 rows in set (0.00 sec) 5. Add yourself as a reviewer. Include a value for all attributes. mysql> insert into reviewer values(30, 'Weeden', 'RIT'); Query OK, 1 row affected (0.06 sec) 6. Update the record you just added changing EmployedBy to RIT. Update reviewer set employedby=RIT where reviewerid=30; 7. Delete the record you added for yourself as a reviewer. delete from reviewer where reviewerid=30;

4002-360 Final Practical SQL Prep

-4-

8. Show the titles of ALL books and if the book had a reviewer show the name of the reviewer(s). Your output should match the display given below: mysql> select title, name -> from book left join bookreview on book.isbn=bookreview.isbn left join reviewer -> on bookreview.reviewerid=reviewer.reviewerid; +--------------------------------------------------+-------------+ | title | name | +--------------------------------------------------+-------------+ | Women are From Venus ORACLE is from Beyond Pluto | Phelps | | Calculus for Phys Ed Majors | NULL | | My Love's Last Longing | Heartthrob | | From the Shores of Lake Erie to IT | Yacci | | My Love's Last Lingering Lost | Troell | | My Love's Last Lingering Lost | Phelps | | The Science of Literature Searching | NULL | | Yes! Networking is for Bills Fans | Kurtz | | Yes! Networking is for Bills Fans | Whittington | | Yes! Networking is for Bills Fans | Stratton | | How to Keep your Cellular Bill Down | Leone | | How to Keep your Cellular Bill Down | Whittington | | Tired of wired? Infrared instead! | NULL | | ER, SOM, NF, DK/NF, SQL, JDBC, ODBC, and RELVAR | Bills | | Master Wireless Through The Classic Comics | NULL | | How to Keep your Cable Bill Down | Leone | | I Lasted my Love's Last Lingering Longing | Weeden | | From Deep in the Heart of Texas to IT | Leone | | My Lost Love's Long Last Lingering | Stratton | | From Brockport to IT | Yacci | | From Brockport to IT | Perez-Hardy | | Master HTML Through The Classic Comics | NULL | | The Shortest Book in the World | Kurtz | | The Shortest Book in the World | Stratton | | My Love's at Long Last Lost his Lingering | Weeden | | How to add Class to your Programming | McVea | | Master C++ Through The Classic Comics | NULL | | A language without Pointers? Priceless. | NULL | | JAVA: It's more than Just a Programming Language | Whittington | +--------------------------------------------------+-------------+ 29 rows in set (0.00 sec)

4002-360 Final Practical SQL Prep

-5-

9. Show the title and its average rating for ALL books in descending order of average rating. Your output should match the display given below: mysql> select title, Avg(rating) "Average Rating" -> from book left join bookreview on book.isbn=bookreview.isbn -> group by title -> order by "average rating" desc; +--------------------------------------------------+----------------+ | title | Average Rating | +--------------------------------------------------+----------------+ | My Love's Last Longing | 10.0000 | | The Shortest Book in the World | 10.0000 | | Women are From Venus ORACLE is from Beyond Pluto | 10.0000 | | How to add Class to your Programming | 9.0000 | | JAVA: It's more than Just a Programming Language | 9.0000 | | My Lost Love's Long Last Lingering | 9.0000 | | My Love's Last Lingering Lost | 8.5000 | | From Brockport to IT | 8.5000 | | How to Keep your Cellular Bill Down | 7.5000 | | How to Keep your Cable Bill Down | 7.0000 | | I Lasted my Love's Last Lingering Longing | 7.0000 | | My Love's at Long Last Lost his Lingering | 7.0000 | | Yes! Networking is for Bills Fans | 6.3333 | | From Deep in the Heart of Texas to IT | 6.0000 | | From the Shores of Lake Erie to IT | 4.0000 | | ER, SOM, NF, DK/NF, SQL, JDBC, ODBC, and RELVAR | 4.0000 | | Calculus for Phys Ed Majors | NULL | | Master Wireless Through The Classic Comics | NULL | | Master HTML Through The Classic Comics | NULL | | The Science of Literature Searching | NULL | | Tired of wired? Infrared instead! | NULL | | Master C++ Through The Classic Comics | NULL | | A language without Pointers? Priceless. | NULL | +--------------------------------------------------+----------------+ 23 rows in set (0.00 sec) 10. Show the title and its average rating for all books that have the in the title in descending order by average rank and title. Your output should match the display given below: mysql> select title, Avg(rating) "Average Rating" -> from book left join bookreview on book.isbn=bookreview.isbn -> where title like '%the%' -> group by title -> order by "average rating" desc, title desc; +--------------------------------------------+----------------+ 4002-360 Final Practical SQL Prep -6-

| title | Average Rating | +--------------------------------------------+----------------+ | The Shortest Book in the World | 10.0000 | | From Deep in the Heart of Texas to IT | 6.0000 | | From the Shores of Lake Erie to IT | 4.0000 | | The Science of Literature Searching | NULL | | Master Wireless Through The Classic Comics | NULL | | Master HTML Through The Classic Comics | NULL | | Master C++ Through The Classic Comics | NULL | +--------------------------------------------+----------------+ 7 rows in set (0.00 sec)

4002-360 Final Practical SQL Prep

-7-

Part II. This prep will use the E-R diagram shown below.

Questions 1. List the strong entities that appear in the diagram? CONTRACT

STORE
REPAIRPERSON

2. List the weak entities that appear in the diagram? 4002-360 Final Practical SQL Prep -8-

ITEM

HOUSEHOLD

BUSINESS

SERVICECALL

3. List the supertype(s) that are in the diagram? CONTRACT

4. List the subtype(s) that are in the diagram? HOUSEHOLD BUSINESS

5. Can a CONTRACT be both a HOUSEHOLD and a BUSINESS contract? Please Explain. No. They are denoted by a disjoint constraint (d inside the subtype circle) so a CONTRACT is either HOUSEHOLD or BUSINESS but not both.

6. What is the identifier of CONTRACT? Explain how you can tell. ContractID. You can tell ContractID is the identifier because it appears above the line inside the rectangle.

7. What type of identifier does CONTRACT have: simple or composite? Please explain. 4002-360 Final Practical SQL Prep -9-

The identifier of CONTRACT is made up of only one attribute (ContractID), therefore it is a simple identifier.

8. Give an example of an entity instance of CONTRACT? Any dummy info will do. ContractID: 1234 EndDate: 12/31/10 StartDate: 1/1/10 ContractPrice: 1000.00

9. List the recursive relationships that appear in the diagram. List by relationship name. made up of / part of

10. List the binary relationships that appear in the diagram. List by relationship name. sells / sold by makes / is made by used in / uses assigned / taken by

11. Does a SERVICE CALL have to have an ITEM? How can you tell? No. In the uses/used in relationship the oval going into the ITEM entity indicates that a SERVICE CALL doesnt require an ITEM.

12. Explain the relationship including minimum and maximum cardinalites for the assigned / taken by relationship. 4002-360 Final Practical SQL Prep - 10 -

A SERVICE CALL has one and only one REPAIRPERSON. A REPAIRPERSON doesnt have to take a SERVICE CALL, but could take several SERVICE CALLs.

13. How many 1:1 relationships appear in the diagram? List them by relationship name. One. The recursive relationship called made up of / part of.

14. How many 1:N (N:1) relationships appear in the diagram? List them by relationship name. Four. assigned / taken by used in / uses makes / is made by sells / sold by

15. How many N:M relationships appear in the diagram? List them by relationship name. There are no N:M relationships in the diagram.

4002-360 Final Practical SQL Prep

- 11 -

Part III. This prep will use the E-R diagram shown below.
Transpose the following E-R diagram into a set of relations.

CUSTOMER(CustomerID, CustomerName, Address, Email) BUSINESS(CustomerID, PurchaseTerms) HOME(CustomerID, CreditCardNum, CardExpiration) CONTACT(CustomerID,ContactName, ContactPhone) OFFER(OfferCode, MinAmount, Discount, ExpirationDate) ORDER(OrderID, Total, OfferCode, CustomerID) CATEGORY(CategoryName, ShippingPerPound, DiscountsAllowed?) ITEM(ItemNumber, ItemName, Model#, Description, Price, CategoryName) LINE_ITEM(OrderID, ItemNumber, quantity, shipping_amount)

4002-360 Final Practical SQL Prep

- 12 -

Potrebbero piacerti anche