Relational Database and Queries Activity

Total Page:16

File Type:pdf, Size:1020Kb

Relational Database and Queries Activity

LSP 121 Relational Database and Queries Activity

This activity will give you practice at creating Access tables, performing normalization, and creating primary and foreign keys.

Given below are the fields for a database for cable television subscribers. The fields are not normalized. In particular, you should pay attention to the repeating premium channels and their associated charges.

Cable Television Subscribers CustomerID (make this a text field and designate it the Primary Key) Name Address City State Zip Phone Start (start date of service - make sure type is Date/Time) End (ending date of service, if any - likewise Date/Time) the following fields repeat 1 to n times: Premium Channel (e.g. HBO, Showtime, NGO, etc.) Premium Charge (each channel has its own cost)

In Word, list the two tables you would create to set up a proper relational database situation. Do NOT create any tables in Access at this point. The first table, which we will call Cable Customer Info, contains the fields ID down through End, and the second table, which we will call PremiumChannelInfo, should have fields for ChannelName and ChannelCost. You do not need to have a primary key for this table. (Recall that primary keys are not always necessary).

Note: if your Premium Channel Info table looks something like the following: CustomerID (foreign key) Premium Channel 1 Premium Charge 1 Premium Channel 2 Premium Charge 2 then you are not doing it correctly. This second table should not have multiple fields for Premium Channels and Premium Charges. You should instead be creating a table with a ID and one Premium Channel and one Premium Charge. If a customer has signed up for multiple Premium Channels, then we will create multiple records, one for each Premium Channel and associated Premium Charge.

Things will be easier if you are careful to name the shared key (the one for customer ID) exactly the same in the two tables. Also be sure that they are of the same data type.

1 Don’t forget that the ID in the second table is not a primary key. It is instead a foreign key (Access does not identify foreign keys –we do). But there will be a relationship between CustomerID in the first table and CustomerID in the second table. This is a default relationship, since CustomerID appears in both tables (same spelling), and they have the same data type.

Ask the instructor to check your table design before you proceed to the next step.

The data for the first table exists as an Excel spreadsheet (cable_customers.xls, on the class webpage) so you can import it into Access. After the import, you should verify that the data has been entered correctly. Be sure to go to ‘Design View’ and check that the data types are correct.  E.g. What data type would you choose for Start and End fields?  ‘ID’ is not a very informative field name. Rename it to CustomerID.  Choose Text for all other fields (including CustomerID)

Data records from the Cable Customer Info table:

ID Name Address City State Zip Phone Start End 1 Smith 3382 Armitage Chicago IL 60043 773 455-0923 4/5/02 2 Jones 5665 Wilbur Des Plaines IL 60061 847 443-0221 9/1/98 3 Chen 342 Oak Park Ridge IL 60032 847 333-6753 4/5/90 4 Shah 5544 Susan Oak Park IL 60631 708 339-0222 1/1/00 1/1/08 5 Purcel 4322 Orchard Des Plaines IL 60061 847 332-0932 5/1/02 6 Roberts 601 Stewart Lombard IL 60133 773 566-9123 10/1/01 7 Stein 565 Packard Oak Park IL 60632 708 445-0922 4/1/05 8 Leninger 100 Ford Chicago IL 60033 312 566-0933 12/10/97 9 Walsh 12233 Park Oak Park IL 60632 708 366-7618 5/9/00

Now let’s create a second table to enter all of the premium channels that the customer has subscribed to. Click Create  Table. Enter design mode. Name the table ‘PremiumChannelInfo’.

Once you have created the new table, enter the following records into your table. Each customer has the premium channels as listed. Note:  Do not enter the customer’s last name into this second table. Instead, use the customer’s ID. Recall from lecture how we create a foreign key based on the primary key from a different table…  Each channel should be in its own record. For example, Smith should have two records in this second table. The first record indicates HBO, and the second record indicates Showtime. Do not put both HBO and Showtime in one record.  This table should have three columns: CustomerID, ChannelName, ChannelCost. o Use ‘Currency’ data type for ChannelCost

Name Premium Channels Subscribed To Smith HBO, Showtime Jones HBO, StarZ, MovieZ

2 Chen none Shah HBO, HBO2, Showtime, Dogz Purcel Showtime Roberts StarZ, MovieZ Stein HBO Leninger StarZ, History, MovieZ Walsh Showtime

HBO costs $5.95 per month Showtime cost $5.95 per month MovieZ cost $4.95 per month HBO2 cost $4.95 per month StarZ cost $3.95 per month Dogz, cost $3.95 per month History cost $3.95 per month

Next: Create a relationship between the two tables  Click on Database Tools  Relationships  Select the two tables that you want to relate  Click and drag from the “parent” field (the primary key) to the “child” field (the foreign key). In this case, you would click from the CustomerID in the CustomerInfo table to the CustomerID in the PremiumChannelInfo table.  Select the ‘Enforce referential integrity’ checkbox and click ‘Create’.  Note: You may encounter some minor complaints from Access along the way. See if you can figure them out on your own. If you really can’t, ask for assistance. You should see something like the following:

When you’re done, call over your instructor to make sure your tables are correct. If they are, paste a copy of the contents of both tables into your activity document. The first three rows of your PremiumChannelInfo table should look something like this:

3 PremiumChannelInfo CustomerID ChannelName ChannelCost 1 HBO $5.95 1 Showtime $5.95 2 HBO $5.95

Question: Think about the design of the table we have just created. There is a significant problem with it related to normalization. Can you see it? Explain. (This is an important question!)

Finally, perform the following queries. Be sure that you include ONLY those fields that are asked for in each query. For example, do not display the CustomerID unless you are asked for it. As each query is performed, copy and paste the query results into your Word document. DO NOT hand type the results into your Word document.

1. Display Name, Address, City and Phone for all customers living in Des Plaines. Sort alphabetically by Name. (If you don’t recall how to set up this query, then either review the overhead lecture notes or read the handout Introduction to Database Systems found on my website under Week 1 Lecture Notes, Activities, and Homework.) Note that you only need to include one table in your query – the Cable Customer Info table. As a general rule, do not include any extra tables into a query if you are not going to use the fields from that table.

2. Display Name, City, and Phone for all customers that have HBO. Sort alphabetically by Name. For this query, you will need both tables in the query. Is there a line connecting ID in the first table to ID in the second table? If not, you do not have a relationship between the two tables and this query will not return the correct results.

3. Display Name and Phone, and channel of those customers with either HBO or Showtime. Sort alphabetically by Name.

4. Display Name and service Start date for those customers that have a start date earlier than 1/1/2000 (remember to use the mathematical ‘<’ symbol).

End of first part of activity.

Advanced Queries

Use the Access file real_estate.mdb (which you can find on the class webpage) for each of the following problems. Microsoft may also warn you that you may be opening a dangerous file. That’s Microsoft’s feeble attempt to provide security. Click on the Enable option.)

The database contains four tables: Listings, Agents, Agent Listing, and Rooms. The table Listings contains one record for each property currently for sale. The table Agents contains one

4 record for each real estate agent. The table Agent Listing contains one record for each agent assigned to a particular listing (there may be multiple agents working on a listing, and each agent may have multiple listings). The table Rooms has a record for each room in a listing (but not all listings have rooms).

Question: Look at these four tables. Why all these tables? Briefly explain giving at least one specific example.

Queries:

Important reminder: When constructing a query, only include those tables that will be necessary to perform that one query. If you include unnecessary tables, unpredictable results may occur.

Copy and paste the result of each query into a Word document and submit the Word document when finished. You must show the pasted results from your Access query to get full credit (don’t just type the answers into your Word document).

1. Using a query, compute the average width and average length of all rooms in all listings. You will only need one table: Rooms. Include Room Length and Room Width in the query. Add the Total row to the query by clicking on the ∑ button on the main toolbar. Change Group By to Avg for both Room Length and Room Width, then run (!) the query. The designed query should look something like the following:

The results from the query should look like the following:

5 Copy and paste the results of your query into your Word document.

2. Using a query, compute the average width and average length of all bedrooms in all listings. You will only require one table for this query – Rooms. Along with Room Length and Room Width, you will need to include Type of Room. Under Type of Room, use the criteria “bedroom”.

3. Using a query, calculate the average width and average length of each type of room individually. Similar to above but also must Group By Type of Room.

4. Using one query, compute the count of how many brick houses and how many frame houses there are currently on the market. You need only one table: Listings. You need to Group By Frame Type and then Count Frame Type in the same query (which means you have to include Frame Type twice in your query). Under the Group By Frame Type, enter the criteria “brick” OR “frame”.

5. Using a query, compute the count of how many homes have each type of heating. Similar to above but use Heating Type.

6. Using one query, what are the highest and lowest property taxes for all the listings in the database.

7. Repeat the above query, but based on the frame type of each house. E.g. What is the max and min for Brick vs Frame? (See questions 4 and 5 to help).

8. Using one query, what are the highest and lowest property taxes in River Forest?

9. List the Address, City, and State (of the Listing – not the agent) for all of Christopher Blake’s listings. For this query, you will need three tables: Listings, Agent Listing, and Agents.

6

Recommended publications