Locked lesson.
About this lesson
Create a third table to hold the company information and then create a link table to link the ticket table to the company table.
Exercise files
Download this lesson’s related exercise files.
6.03 setting-up-relationships - Exercise.docx43.2 KB 6.03 setting-up-relationships - Exercise solution.docx
101.6 KB TechGurusTicketing11.accdb
660 KB TechGurusTicketing11 - Complete.accdb
716 KB Company_List.txt
128 B
Quick reference
Setting Up Relationships
Create a table that holds the company information and a link table to link the ticket table to the company table.
When to use
Create link tables whenever you need to create relationships between tables that have no shared field.
Instructions
Creating Another Table
We are going to create a table that holds the 'Company' information. Download the file 'Company_List.txt'.
- From the Create tab, in the Tables group, click Table Design.
Our table will contain one field.
- In the Field Name, type 'Company'.
- In the Data Type field, select Short Text.
- In the Description field, type 'Company Name'.
Set the Primary Key field for the table.
- Select the 'Company' field.
- From the Table Design tab, in the Tools group, click Primary Key.
Set the Field Properties.
- Field Size: 30.
- Required: Yes.
- Allow Zero Length: No.
- Indexed: Yes (No Duplicates).
- Close the table and Save the changes.
- Name the table 'tblCompany'.
- Click OK
Adding Data to the Table
- Double-click to open the table 'tblCompany'.
- Use the file 'Company_List.txt' to populate the table.
- Close the table.
Creating Relationships
Next, we need to create relationships between our tables.
- From the Database Tools tab, in the Relationships group, click Relationships.
- From the Add Tables pane, select both tables and click Add Selected Tables.
The tables do not share a common field so we need to create a link table.
- From the Create tab, in the Tables group, click Table.
- Right-click on the tab and select Design View.
When we create a table in this way, an ID field is automatically created which provides a unique way of identifying each record.
- Add a description for the ID field, 'Unique ID for this table'.
- Save the table as 'tblTicketCompany'.
- Click OK.
We need to include a field from 'tblTicket' and a field from 'tblCompany' in our link table. The second field will be the ID field from 'tblTicket'. As we already have an 'ID' field in 'tblTicketCompany' we will call this something different.
- In the Field Name, type 'TicketID' in 'tblTicketCompany'.
- In the Data Type field, select Number.
- In the Description field, type 'Unique ID (Primary Key) of the ticket table'.
- In the Field Name, type 'Company'.
- In the Data Type field, select Short Text.
- In the Description field, type 'Unique ID (Primary Key) of the company table'.
- Set the field properties for each field.
Creating Relationships
We can now create a relationship between 'tblTicket' and 'tblCompany' using the 'tblTicketCompany' link table.
- From Database Tools, in the Relationships group, click Relationships.
- In the Add Tables pane, select 'tblCompany' and 'tblTicketCompay'.
- Click Add Selected Tables.
- Drag the 'ID' field from 'tblTicket' to the 'TicketID' field in 'tblTicketCompany'.
- In the Edit Relationships window, select Enforce Referential Integrity.
- Click Create.
- Drag the 'Company' field from 'tblCompany' to the 'Company' field in 'tblTicketCompany'.
- In the Edit Relationships window, select Enforce Referential Integrity.
- Click Create.
Lesson notes are only available for subscribers.