Locked lesson.
About this lesson
Learn how to lay out the Relationships Diagram to see what you have in the database.
Exercise files
Download this lesson’s related exercise files.
Services_18_Start_Relationships.accdb1.6 MB Services_18_Relationships.accdb
1.6 MB
Quick reference
Relationships and Relationships Diagram
Application Terminology
Add Tables to the Relationships Diagram
It is often quicker to drag tables from the Navigation Pane to the Relationships Diagram. You can drag them one-by-one, or select multiple tables and drag the selection.
You can also click the Show Table button on the RELATIONSHIP TOOLS, DESIGN ribbon to open the Show Table dialog box. Double-click to add tables, or select them and click Add. You can use the same keys to select multiple tables as in the Navigation Pane.
All Relationships
To see all relationships that are set up in the database, choose All Relationships from the RELATIONSHIP TOOLS, DESIGN ribbon.
Cascade Delete Related Records
Cascade Delete Related Records means that if a record is deleted in the main table and it has related records, they will be deleted at the same time.
As a general rule, do not check Cascade Delete Related Records. There are times, however, you may want to temporarily check this to clear out data quickly. Don’t forget to set it back as this can delete information that you really want to keep.
Cascade Update Related Fields
If changes are made to the value of the primary key in the main table, the same changes will automatically be made to related tables. When using AutoNumber for the Primary Key in the main table, this is usually not applicable.
Clear Layout
To clear the layout of the Relationships Diagram, click the Clear Layout icon on the RELATIONSHIP TOOLS, DESIGN ribbon. Clearing the layout does not delete relationships, it only clears the diagram.
Create Relationship
When creating relationships, the data types must match. This means Field Size too. By default, AutoNumbers are Long Integers.
Drag from a Primary Key to a Foreign Key. When you let go of the mouse, the Edit Relationships dialog box appears. Look at the field names listed to ensure they are correct. Check the Enforce Referential Integrity box.
While Access does not require you to drag from a Primary Key, that is the field you should use. At least use a field with a Unique index – otherwise, the relationship will not be a good one.
One reason to qualify ID for fieldnames (i.e., ServiceID, CustomerID, ImvoiceID) is so that you don't accidentally create relationships from one AutoNumber to another … I have seen this many times! Obviously, this is not right.
When a piece of data is the same, my preference is to keep the Primary and Foreign Key fieldnames the same. This alleviates confusion down the road.
Default Value
When creating key fields, make sure to delete the Default Value of 0 (zero) that Access automatically assigns for numeric data types … unless this is a value that matches up and is really what you want.
Delete Relationship
To delete a relationship, right-click on the link line and choose Delete from the shortcut menu.
You can also click a relationship to select it and press the DELETE key on your keyboard. I prefer the first method though, as it is safer. If you accidentally delete a table, you can drag it back on again.
Direct Relationships
If there are relationships already set up, it can be more convenient to add a table to the diagram and choose Direct Relationships from the RELATIONSHIP TOOLS, DESIGN ribbon. You can also right-click on a table on the Relationships Diagram and choose Show Direct from the shortcut menu.
Document the Relationships Diagram
To document the Relationships Diagram, press the Print Screen key to put an image of the screen on the Windows Clipboard. You can then paste the screen into another application such as Word or PowerPoint to print it out.
If you have a screen capture program such as Snagit (TechSmith), you can select just a rectangle of the screen. I like to then color the background white so when it is printed, I can add my own notes.
You can also choose Relationship Report on the RELATIONSHIP TOOLS, DESIGN ribbon. However, I prefer to put diagrams in PowerPoint or Word (use Landscape) for annotating and labeling.
Edit Relationship
To edit a relationship, right-click on it and choose Edit Relationship from the shortcut menu. You can also double-click a relationship to edit.
Enforce Referential Integrity
Enforce Referential Integrity is a checkbox in the Edit Relationships dialog box.
By enforcing Referential Integrity, you are telling Access that it is not okay to delete a record in the main table if there are related records. Also, values in a related table will not be allowed if they do not match any value in the main table.
Foreign Key
A Foreign Key is a field (or combination of fields) in a table that relates to a primary key in another table. Foreign Keys are often single fields using a Long Integer data type that relate to an AutoNumber field in the main table.
Maximize Window
A quick way to maximize a window is to double-click the Title Bar. The Title Bar is a much bigger target than clicking on the maximize button in the upper left, so faster!
Minimize Relationship Lines Crossing
To minimize relationship lines crossing, rearrange fields in the tables. Order does not matter. I like to put key fields on the top and tracking fields on the bottom.
Primary Key
A Primary Key is a field (or combination of fields) in a table that has a unique value for every record and is designated with a key icon in the row selector box.
You will also see an index in the Indexes window that has Primary and Unique properties set to Yes. The Index name does not matter, but is usually called Primary Key.
Referential Integrity
Referential Integrity is a property of a relationship that ensures there will be no foreign key values that do not exist in the main table (which should be the Primary Key). Unless you have a specific reason not to check Enforce Referential Integrity, then you should always choose it.
Relationships Diagram
As you build your database and define your key fields, lay out the Relationships Diagram. This is a good way to see what you have and a great help while you are building to see the field names.
To get a better view of what is in the database, stretch fieldlists to show all the fields. If you do not have long names, also make the fieldlists narrower. If you do have long names, you may have to make them wider. It is good to show all the names.
Arrange tables from left to right, as data must be entered.
Select Multiple Tables
To select multiple tables, click on the first table then shift-click on the last table. To toggle the selection status, ctrl-click.
Table Design
For a quick way to the table design, right-click a table on the Relationships Diagram and choose Table Design from the shortcut menu. When you save changes to a table design, the Relationships Diagram will change too.
If you want to change the data type of a field that is part of a relationship, you must first delete the relationship. You can, however, change field names and other properties without deleting relationships.
Table Indexes
To toggle the display of the table Indexes window, click the lightning bolt icon on the TABLE TOOLS, DESIGN ribbon
This window shows two indexes:
- Primary Key on ServiceID
- Multi-field Unique index on the combination of ServTyID (service type) and ServName (service name). This ensures that the same service will not be duplicated for a service type.
Unique Index
A Unique index can be set on a field that is not the Primary Key but must also be unique. A good example is when defining types and you do not want the text value of a type to be duplicated. When creating indexes, they should be on short fields – so set the Field Size accordingly.
Steps
Show Relationships Diagram
- To show the Relationships Diagram, click the Relationships icon on the DATABASE TOOLS ribbon tab
Add Tables to Relationships Diagram
- To add tables to Relationships Diagram, drag them from the Navigation Pane
- or click the Show Table command on the DESIGN ribbon tab of RELATIONSHIP TOOLS
Create Relationship
- To create a relationship, drag from a Primary Key to a Foreign Key. When you let go of the mouse, the Edit Relationships dialog box appears.
- Look at the field names listed to ensure they are correct.
- Check the Enforce Referential Integrity box.
When creating relationships, the data types must match. This means Field Size too. By default, AutoNumbers are Long Integers.
Document Relationships Diagram using Access
- To get a quick report of the Relationships Diagram from Access, click the Relationship Report on the RELATIONSHIP TOOLS, DESIGN ribbon
This report, however, is not as nice as the diagram itself. Better to document using a screen shot.
Document the Relationships Diagram using a Screen Shot
- To document the Relationships Diagram, press the Print Screen key to put an image of the screen on the Windows Clipboard.
- You can then paste the screen into another application such as Word or PowerPoint to modify it, annotate it, and print it out.
Edit Relationship
- To edit a relationship, right-click on the relationship line
- Choose Edit Relationship from the shortcut menu
- Alternately, double-click a relationship line to edit.
Lesson notes are only available for subscribers.