Lab 3.4

Lab 3.4
Modifying a Database Schema
In the previous lab, we covered viewing and modifying the data inside a database, the
most common everyday tasks when using a database. In this lab, we move on to a less
frequent but equally important database maintenance task. From the skills we learned in
the last lab, we know how to add a new movie or director to our movie database. In
database terminology, we know how to add rows to an existing database table. Suppose
we wanted to make a different kind of addition to the database: a way to store each
movie’s Motion Picture Association of America (MPAA) rating (e.g., G, PG, PG-13,
etc.). You cannot accomplish this by simply adding rows to a table, so we’ll see how to
do this in this lab. We’ll also learn how to modify the movie database to store new
entities, e.g., movie reviews. There is a fundamental difference between the database
modifications discussed in this lab, compared to those in the previous lab: In this lab, we
will be making changes to the database schema, rather than just changing the contents of
the database. To put it another way, in the previous lab, we saw how to add and change
entities stored in the database, but we didn’t change the database to store new kinds of
entities or change how the entities are stored.
Post-lab Questions
Write your answers after completing the lab, but read them carefully now and keep them
in mind during the lab.
1. Suppose you removed a movie from the movie database. Would this be an
example of changing the database schema? Answer yes, no, or it depends and
explain your answer.
2. In this lab, we add a field to the Movie table for MPAA rating. Think of another
piece of data you associate with movies that could be stored in a new field in the
Movie table.
Be careful when answering this question. You might, for instance, think that cast
members would be a natural addition to the Movie table. However, you can
easily imagine wanting to store more information than just the names of
actors/actresses in the movie database, and this suggests that the right way to add
them to the database is using a separate table, rather than just adding a field to the
Movie table. (We store data about directors in a separate table for the same
reason.). There’s an additional twist, in fact, because actors/actresses and movies
have a many-to-many relationship, which, in fact, we haven’t learned how to
represent.
1
Lab 3.4: Modifying a Database Schema
3. Give an example of a modification to the movie database (other than those you’ve
seen in lab) that does not require changing the schema.
4. Give an example of a modification to the movie database (other than those you’ve
seen in lab) that does require changing the schema.
5. What advantages do spreadsheets have over databases for storing, maintaining,
and viewing data?
What advantages do databases have over spreadsheets?
Discussion and Procedure
We’ll continue using the same database as in the previous labs: movies.mdb, which
contains data on movies and directors.
Part 1. Adding a new field to a table
First, we’ll see how to modify the database to store a new movie attribute for MPAA
rating. In a previous lab, we opened the Movie table in Design View to see the details of
how the table stores movie attributes. We’ll use the same view to add a new attribute
now.
1. Open the Movie table in Design View.
2. Add a new movie attribute, MPAARating. (Recall that Access calls attributes
“fields.”) Adding a field is very simple in Design View. Just fill in the blank row
below the last field, which should be ReleaseYear. MPAA movie ratings are
represented using letters and numbers (e.g., PG-13). Which Data Type do you
think you should choose?
Although MPAARating is a pretty self-explanatory name, you might enter more
details for Description.
At the bottom of the window, you’ll notice a bunch of options on the General and
Lookup tabs. We’ll return to these later for fine-tuning our newly added field, but this is
already enough to try out the modified table in Datasheet View.
3. Switch to Datasheet View. You should now see that the Movie table has a new
column at the far right called MPAARating. It would be convenient if the
database magically figured out what the ratings for the movies already in the
Movie table are and enter them, but you’ll notice that all of the existing movies
have nothing stored in the MPAARating field. This is generally what happens
with existing table rows when you add a new field to a table.
2
Lab 3.4: Modifying a Database Schema
4. Try entering ratings for one or two movies. You can just guess at the ratings for
now or use the Internet Movie Database (a real life and full-featured version of
our movie database on the web) at www.imdb.com.
5. Try entering invalid ratings for a movie. Try entering a nonsensical movie rating
like A or even your name in the MPAARating column for a movie. Does an error
message result?
In the next steps, we’ll refine the table design to make it more convenient to enter valid
values for the MPAARating field.
6. Return to Design View for the Movie table and select the MPAARating field.
7. On the General tab, set the Field Size appropriately. The MPAA’s movie rating
system has the following ratings: G, PG, PG-13, R, and NC-17. None of these
ratings are longer than five characters (counting the dash in PG-13 and NC-17).
You can specify that the value for the MPAARating field be up to five characters
by setting the Field Size.
What is the field size that Access automatically sets for new fields (i.e., the
default field size)?
Why is it a good idea to reduce the field size for MPAARating to 5?
8. On the Lookup tab, set the Display Control, Row Source Type, Row Source, and
Limit to List appropriately. Unlike other fields in the Movie table, the
MPAARating field should really only be set to one of a small number of valid
values: G, PG, PG-13, R, NC-17. Thinking ahead, we’d also allow it to be set to
NR (to stand for “not yet rated”). Access allows you to set up a field so that a
user can set its value by picking from a combo box of options.
To do this, set the Display Control to Combo Box and the Row Source Type to
Value List. This tells Access that you will specify a list of valid values to show in
the combo box, and you will do this using the Row Source setting. In this box,
fill the list of valid values separated by semicolons:
G;PG;PG-13;R;NC-17;NR
Finally, set Limit to List to Yes, which prevents anyone from entering a value
other than one of those in the list you just entered.
9. Switch back to Datasheet View and try editing the MPAARating field. What
happens when you click to select the field?
What happens when you type in a valid rating, rather than choose it from the list?
What happens when you type in an invalid rating?
3
Lab 3.4: Modifying a Database Schema
Part 2. Adding the new field to a form
In the previous lab, we saw how convenient forms are for working with tables, especially
for specialized tasks like editing and viewing. In this part of the lab, we’ll see how to
update one of the movie forms in the database so that it includes the newly added field
(MPAARating). We’ll start with the form you created for editing movies, EditMovie.
You’ll see that adding the field to a form is greatly simplified, because we created the
field with combo box settings.
10. Open the EditMovie form in Design View.
11. Open the Field List window. You can do this from the View menu or by clicking
the Field List button on the Form Design toolbar.
12. Make some space for a control for the MPAARating field. Resize the form and
rearrange controls as necessary to make room for a combo box and label.
13. Add the field to the form. Starting in the Field List window (which should be
titled “Movie”), drag the MPAARating item onto the form where you want its
control to be. A new combo box with label “MPAARating” should appear on the
form.
14. Clean up the appearance of the form. Resize and rearrange the form controls and
consider changing the label for the MPAARating combo box (e.g., just “Rating”).
You might want to shorten the MPAARating combo box, since ratings are at most
five characters long. You will probably find it helpful to switch back and forth
between Design and Form views to see exactly how your form will look to a user.
…and that’s all you need to do. The combo box already lists the proper values, and the
rules about valid values for MPAARating apply for the form just like they did for editing
the Movie table directly in Datasheet View.
Part 3. Designing a new table
In the last part of this lab, we’ll round out your fundamental skills in database
maintenance by discussing how to create a table from scratch. Before we start clicking
around in Access, however, we should have a clear plan for what we’re going to add to
the database.
4
Lab 3.4: Modifying a Database Schema
Suppose we want to enhance the database by adding movie reviews. More specifically,
you want to allow users to save some brief comments and personal rating (e.g., on a scale
from 0 to 5) of movies in the database. In addition, you want to store some basic
information about each review author (at least their name, and maybe a little personal
profile). This suggests two new entities for the database to keep track of: reviews and
reviewers. We’ll end up creating a table for each one of these, but let’s take a moment to
discuss the attributes of these entities and how they’re related, not only to each other, but
also to the existing entities in the database: movies and directors.
15. Recall that there is a “one-to-many” relationship between directors and movies
(i.e., each director can be related to multiple movies, but not vice versa). How
would you best characterize the relationship between reviews and movies?
How would you best characterize the relationship between reviews and
reviewers?
For the sake of time, we will initially include only the most essential attributes for the
Review and Reviewer entities. Each Review record will have an author, the review
author’s comments, and the review author’s rating of the movie on a scale of 0 to 5. Each
Reviewer will have a last name, first name, and e-mail address. In addition to these
fields, the Review and Reviewer tables will each need a primary key field, as well as any
additional ID fields necessary to represent the relationships discussed above.
Adding a new table to a database involves creating the table, setting up the fields in each
table, and setting up the relationships between new and existing tables. As with most
tables, you will probably want to follow up by creating forms for editing and viewing
newly added tables, but we won’t cover that in this lab.
Before you start, you might want to make backup copy of your movies.mdb database file.
Especially because Access doesn’t allow you to undo very many changes, just in case
you end up messing up your database in some way, it might be convenient to be able to
start over from a pre-lab copy of the database.
16. Create a new table called Reviewer. One way to do this is from the menu bar, by
selecting Insert \ Table. Select Design View, because you are already familiar
with this interface for adding and modifying table fields.
17. Begin by creating a field for the Reviewer’s ID. For consistency with the way
other tables’ primary key fields are named, name this field ReviewerID. Its type
should be AutoNumber.
18. Make ReviewerID a primary key field. You can do this by right-clicking the field
row and selecting Primary Key or by clicking the toolbar button with a key on it.
19. Create additional fields for first and last name, and e-mail address.
20. Close and save the table as Reviewer.
5
Lab 3.4: Modifying a Database Schema
In the same way, create a table for Review. In addition to its own primary key ID field,
Review will probably need additional field(s) for representing relationships to rows in the
Movie and Reviewer tables. Make sure to create these fields, along with the fields for
reviewer comments and rating (the reviewer’s rating, different from the MPAA rating
stored with each movie). For the review comments field, use the data type Memo instead
if Text. The Memo type is useful for text whose maximum length is hard to predict
ahead of time (unlike with first names, for instance, which are usually no more than
twenty characters or so). For the reviewer’s rating of the movie, try setting up the field to
use a combo box (like you did for the ) with choices 0;1;2;3;4;5.
With the Review and Reviewer tables set up and saved, the last steps are for setting up
the relationships among the tables.
21. Open the Relationships window. You can do this from the menu bar by
selecting Tools \ Relationships... or clicking the Relationships button on
the toolbar.
What table(s) are displayed in the Relationships window?
In each table, one of the fields is shown in bold face. What makes this field
different from the other fields in the table?
Recall that in the Relationships window, relationships are depicted by lines
between table fields. What relationship(s) are displayed in the Relationships
window?
Each relationship connects two tables. For each relationship, which field from
each table is used to represent the relationship? That is, which fields are
connected by the lines?
22. Add the Review and Reviewer tables to the Relationships window. One way to do
this is by right-clicking on the background (i.e., not on a table or relationship line)
in the Relationships window and selecting Show Table... from the pop-up
menu. You can also click the toolbar button for Show Table.
TROUBLESHOOTING:
If you accidentally add a table more than once or want to
remove a table from the Relationships window for any other reason, you can
right-click it and select Hide Table.
23. Set up the relationships for the newly added tables. Recall we have two new
relationships to add: between movies and reviews and between reviewers and
reviews. Let’s start with the first: movies and reviews. Identify the field in each
table that will be used to represent the relationship. One of them should be a
primary key.
Which Movie field will be used to represent the movie-review relationship? Is
this a primary key?
6
Lab 3.4: Modifying a Database Schema
Which Review field will be used to represent the movie-review relationship? Is
this a primary key?
Adding a relationship involves dragging a field from one table to the
corresponding field in the other table. This pops up the Edit Relationships dialog
box, which will look something like this:
Double check that the tables and fields selected for the relationship are correct.
Make sure to select Enforce Referential Integrity, as shown above. Click the
Create button when you’re done, and the tables should be connected with a new
relationship line, with endpoints labeled with 1 and the infinity symbol to indicate
the one-to-many nature of the relationship.
TROUBLESHOOTING:
If you accidentally set up a relationship incorrectly, you can
delete it by right-clicking it and selecting Delete.
Enforcing referential integrity. With this option selected, Access will prevent you from
deleting a database record if it is related to other records in the database. For example, if
you have movies in the database by director Spike Lee, if you delete the record for Spike
Lee, records for his movies will be left related to a nonexistent director record. This is an
example violation of referential integrity. Access will watch over changes to your tables
and warn you if you are about to violate your database’s referential integrity.
Once you have finished adding both the movie-review and reviewer-review relationships,
you can close and save the layout for the Relationships window. Your instructor may ask
you to take a screenshot of your Relationships window for submission. (The easy way to
do this is by pressing Alt-PrtScn and doing a paste in a new WordPad document. You’ll
get a snapshot of the Access window, including the Relationships window and other
subwindows that are open and visible.)
7
Lab 3.4: Modifying a Database Schema
Optional Additional Activities
 Update the form for viewing movies with the new MPAA rating field. Note that
combo boxes can be locked, just like other controls, but they continue to allow the
user to view the drop down list. (The user can’t select a new value from the list,
however—the control is still indeed locked.) If you don’t want to show the list,
you can change the combo box to a simple text box. In Design View, right click
the combo box and select Change To.
 Create forms for editing the Review and Reviewer tables.
 You now know how to create and modify tables and forms. For practice’s sake, try
recreating the movie database from scratch, i.e., starting from an empty, new
Access database. You can always open the existing movie database as a reference.
Start by creating the tables, then try reproducing the forms.
8