Lab 3 Relationships in ER Diagram and Relationships in MS Access
Lab 3 Relationships in ER Diagram and Relationships in MS Access
1
Ken Goldberg Database Lab Notes
2
Ken Goldberg Database Lab Notes
page 5) from one table, which provides a unique identifier for each record, and a foreign key in
the other table. For example, employees can be associated with orders they're responsible for by
creating a relationship between the “Employees” table and the “Orders” table using the
EmployeeID fields (which we will show later).
3
Ken Goldberg Database Lab Notes
4
Ken Goldberg Database Lab Notes
In a one-to-one relationship, each record in Table A can have only one matching record in Table
B and each record in Table B can have only one matching record in Table A. This type of
relationship is NOT common, because most information related in this way would be in one table.
You might use a one-to-one relationship to divide a table with too many fields, to isolate part of a
table for security reasons, or to store information that applies only to a subset of the main table.
For example, you might want to create a table to track employees participating in a fundraising
soccer game.
5
Ken Goldberg Database Lab Notes
6
Ken Goldberg Database Lab Notes
7
Ken Goldberg Database Lab Notes
1
Primary key: one or more fields whose value or values uniquely identify each record in a table. In a
relationship, a primary key is used to refer to specific records in one table from another table.
2
Foreign key: one or more table fields that refer to the primary key field or fields in another table. A
foreign key indicates how the tables are related. The data in the foreign key and primary key fields must
match. For example, the Products table in the Northwind sample database contains the foreign key
SupplierID, which refers to the SupplierID primary key of the Suppliers table. Using this relationship, the
Products table displays a supplier name from the Suppliers table for each product.
3
Data type: the attribute of a variable or field that determines what kind of data it can hold. For example,
the Text and Memo field data types allow the field to store either text or numbers, but the Number data type
will allow only numbers to be stored in the field. Number data type fields store numerical data that will be
used in mathematical calculations. Use the Currency data type to display or calculate currency values.
Supported data types include field data types, Visual Basic data types, and query parameter data types.
8
Ken Goldberg Database Lab Notes
• you can also match an AutoNumber field with a Number field if both fields have
their FieldSize property set to Replication ID.
6. Once you have created the relationships, the Edit Relationships dialog box is displayed as
shown in Fig. 8. Check the field names displayed in the two columns to ensure they are
correct. You can change them if necessary. Set the relationship options if necessary. For
information about a specific item in the Relationships dialog box, click on the question mark
button (the cursor would now have a floating question mark next to it) and then click on the
item.
7. Click the Create button to create the relationship.
8. Repeat steps 5 through 8 for each pair of tables you want to relate.
4
Referential integrity: Rules that you follow to preserve the defined relationships between tables when you
enter or delete records. If you enforce referential integrity, Microsoft Access prevents you from adding
records to a related table when there is no associated record in the primary table, changing values in the
primary table that would result in orphan records in a related table, and deleting records from the primary
table when there are matching related records in a related table.
9
Ken Goldberg Database Lab Notes
Employees table in the Northwind sample database, a relationship has been defined
between the EmployeeID and ReportsTo fields, so that the ReportsTo field can display
employee data from a matching EmployeeID.
10
Ken Goldberg Database Lab Notes
11
Ken Goldberg Database Lab Notes
primary key fields function as foreign keys. You can add other fields to the junction table,
just as you can to any other table.
3. In the junction table, set the primary key to include the primary key fields from the other
two tables. For example, in an Order Details junction table, the primary key would be made
up of the OrderID and ProductID fields. (Note: You can set multiple fields as the primary
key by highlighting multiple rows (which correspond to fields) which you want to be part of
your primary key in Design View, and then on the Ribbon click on EditDesign Primary
Key. Alternatively you can also use the primary key button in the tool bar)
4. Define a one-to-many relationship between each of the two primary tables and the
junction table.
5. To add data to the tables, create a form5 that works with more than one table.
Note:
In the Northwind sample database, a many-to-many relationship exists between the Orders and
Products tables. One order in the Orders table can include multiple products from the Products
table. In addition, a single product can appear in many orders. In the sample database, the Order
Details table is a junction table between the Orders table and the Products table.
5
Form: a Microsoft Access database object on which you place controls for taking actions or for entering,
displaying, and entering data in fields.
12
Ken Goldberg Database Lab Notes
13
Ken Goldberg Database Lab Notes
We will introduce how to use the lookup wizard in design view to generate lookup list and value in
the following two sections.
Creating a field that looks up data from another table in Design view
Lookup List
The most common Lookup list displays values looked up from a related table. For example, the
SupplierID Employee field in the Products Orders table of the Northwind sample database
displays the Lookup list as shown in Fig. 11.
14
Ken Goldberg Database Lab Notes
3. In the ‘Data Type’ column, click the down arrow and select ‘Lookup Wizard’ from the list.
4. Click the option that indicates you want the Lookup field to look up the values in a table or
query.
5. Click Next and follow the directions in the remaining Lookup Wizard dialog boxes.
When you click the Finish button, Microsoft Access creates the Lookup field6 and sets certain
field properties based on the choices you made in the wizard. For information on the properties
that the Lookup Wizard sets, go back to the field that was set by the Lookup Wizard, and click
on the Lookup tab in the Field Properties window (Fig. 12).
6
Lookup list field and form: once you've created a Lookup list field, if you add the field to a form,
Microsoft Access copies its definition into the form. You won't have to create the combo or list box and its
Lookup or value list definition for the form. However, if you change the definition of a Lookup or value list
field in the table after adding it to a form, those changes will NOT be reflected in that form. To correct this,
delete the field from the form and then add it again.
15
Ken Goldberg Database Lab Notes
Fig. 12: Properties Listed in the Lookup Tab in the Field Properties Window
Note:
• It is also possible to add a Lookup field to a table that displays values from the same table
that contains the Lookup field. For example, in the Employees table, which has FirstName,
LastName and ReportTo fields, of the Northwind sample database the ReportsTo field can
be created as is a Lookup field that displays data from the FirstName and LastName fields
by looking up the corresponding EmployeeID in the same table.
16
Ken Goldberg Database Lab Notes
Now let’s go back to Fig. 11. It is created by looking up the SupplierID EmployeeID values in the
Suppliers Employees table and by displaying the corresponding Supplier Employee names.
Picking a value from a Lookup list sets the foreign key value in the current record (SupplierID
EmployeeID in the Products Employees table) to the primary key value of the corresponding
record in the original table (SupplierID EmployeeID in the Suppliers Employees table). This
creates an association to the related table to display (but NOT store) the Supplier Employee
names in the record. The foreign key (SupplierIDEmployeeID) is stored but is not displayed. For
this reason, any updates made to the data in the Suppliers Employees table will be reflected in
both the list and records in the Products Orders table. You must define a Lookup field in the
table that will contain the foreign key and display the Lookup list. In this example, the Lookup
list field would be defined in the Products Orders table.
Value List
A Value list looks the same as a Lookup list, but consists of a fixed set of values you type in
when you create it. A value list should only be used for values that will not change very often and
don't need to be stored in a table. For example, a list for a Salutation field containing Mr., Mrs.,
or Ms. would be a good candidate for a value list. Choosing a value from a value list will store that
value in the record. It doesn't create an association to a related table. For this reason, if you
change any of the original values in the value list later, they will not be reflected in records added
before this change was made.
7
Value list field and form: once you've created the field, if you add it to a form, Microsoft Access
copies its definition into the form. You won't have to create the value list definition for the form.
However, if you change the definition of the value list field in the table after adding it to a form, those
changes will not be reflected in that form. To correct this, delete the field from the form and then add it
again.
17
Ken Goldberg Database Lab Notes
1. Create a database car.mdb, and build 3 tables for a Car Company (choose your own name),
including Customer, VehicleInstance, and Dealership. For each table, create the fields as
follows:
• Customer: CustomerID, CustomerName, City, Discount
• VehicleInstance: VehicleID, VehicleName, Price, Discount
• Dealership: DealerID, DealerName, City, Commission
2. Enter as many tuples/rows of data for the ‘VehicleInstance’ table as you like (>10 instances)
and 10 tuples/rows of data for the ‘Customer’, and ‘Dealership’ tables.
3. Create relationships that connect the different entities according to the following three
requirements:
(Hint: Before you can create a one-to-many relationship between two tables, you need to add
the primary key of the table on the one-side to the table on the many-side so that the two tables
are related by some common field.For many-to-many relationship, build a junction table and then
create two one-to-many relationships) Please enter the data for the new fields too.
Further instructions:
For the quiz, please provide:
• screenshots for ALL records in each table, and try to fit 3 tables in a page
• screenshot for ALL relationships in the Relationship window
Please save car.mdb for use in future labs.
To do a screenshot capture, open the window which you want to capture, maximize it (or size it
appropriately) and the do Alt+Print Screen. Then use the Paste command in your word
processing software to import the image into your document.
18