Relate Tables in a One-to-Many Relationship

Access 2016 Level 1
Unit 1 Creating Tables and Queries
Chapter 2 Creating Relationships between
Tables
© Paradigm Publishing, Inc.
1
Relate Tables
 A flat file management system stores all data in a
single directory and cannot contain multiple tables.
 This type of management system is a simple way to
store data but becomes inefficient as more data is
added.
 In a relational database management system, like
Access, relationships are defined between sets of
data, allowing greater flexibility in manipulating data
and eliminating data redundancy
© Paradigm Publishing, Inc.
2
Determine Relationships
 Taking time to plan a database is extremely important.
 Creating a database with related tables takes even
more consideration.
 Determine how to break down the required data and
what tables to create to eliminate redundancies.
© Paradigm Publishing, Inc.
3
Determine Relationships (continued)
 Along with determining the necessary tables,
determine the relationship between those tables.
database
diagram
© Paradigm Publishing, Inc.
4
Set the Primary Key Field
 A database table can contain two different types of key
fields: a primary key field and a foreign key field.
 An asterisk indicates a primary key field, which is a
field that holds data that uniquely identifies each
record in a table.
 A table can have only one primary key field and it is
the field by which the table is sorted whenever the
table is opened.
 Access expects a value in each record in the table and
this is referred to as entity integrity.
© Paradigm Publishing, Inc.
5
Set the Primary Key Field (continued)
To define a primary
key field:
1. Open table.
2. Click View button.
3. Click field.
4. Click Primary Key
button.
5. Click Save button.
4
© Paradigm Publishing, Inc.
6
Set the Primary Key Field (continued)
 By default, Access includes the ID field as the first field
in a table, assigns the AutoNumber data type, and
identifies the field as the primary key field.
 The AutoNumber data type assigns the first record a
field value of 1 and each new record is assigned the
next sequential number.
© Paradigm Publishing, Inc.
7
Set the Primary Key Field (continued)
 Typically, a primary key field in one table becomes the
foreign key field in a related table.
primary key and
foreign key
© Paradigm Publishing, Inc.
8
Relate Tables in a One-to-Many Relationship
 When tables with a common field are joined, data can
be extracted from both tables as if they were one large
table.
 Relating tables helps to ensure the integrity of the
data by avoiding entering the same data in multiple
tables.
 In a one-to-many relationship, the table containing the
“one” is referred to as the primary table and the table
containing the “many” is referred to as the related
table.
© Paradigm Publishing, Inc.
9
Relate Tables in a One-to-Many Relationship
(continued)
To create a one-to-many
relationship:
1. Click Database Tools tab.
2. Click Relationships
button.
3. Click first table name
and click Add button.
4. Add all necessary tables.
5. Click Close button.
3
continues on next slide…
© Paradigm Publishing, Inc.
10
Relate Tables in a One-to-Many Relationship
(continued)
…continued from previous slide
6. Drag “one” field from primary table to “many” field in
related table.
continues on next slide…
6
© Paradigm Publishing, Inc.
11
Relate Tables in a One-to-Many Relationship
(continued)
…continued from previous slide
7. Click Create button.
8. Click Save button.
9. Click Close button.
7
© Paradigm Publishing, Inc.
12
Relate Tables in a One-to-Many Relationship
(continued)
This is an example of a one-to-many relationship,
where 1 identifies the “one” side of the relationship
and the infinity symbol ( ) identifies the “many” side.
© Paradigm Publishing, Inc.
13
Relate Tables in a One-to-Many Relationship
(continued)
 Choose Enforce Referential Integrity to ensure that the
relationships between records in related tables are
valid.
 Select Cascade Update Related Fields and, if changes
are made to the primary key field value in the primary
table, Access will automatically update the matching
value in the related table.
 Choose Cascade Delete Related Records and, if a
record is deleted in the primary table, Access will
delete any related records in the related table.
© Paradigm Publishing, Inc.
14
Relate Tables in a One-to-Many Relationship
(continued)
To create and print a relationship report:
1. Click Database Tools tab.
2. Click Relationships button.
3. Click Relationship Report button.
4. Click Print button.
4
© Paradigm Publishing, Inc.
15
Relate Tables in a One-to-Many Relationship
(continued)
To display the Show Table dialog box:
1. Click Relationships button.
2. Click Show Table button.
2
© Paradigm Publishing, Inc.
16
Relate Tables in a One-to-Many Relationship
(continued)
To edit a relationship:
1. Click Database Tools tab.
2. Click Relationships button.
3. Click Edit Relationships
button.
4. Make changes at Edit
Relationships dialog box.
5. Click OK.
5
© Paradigm Publishing, Inc.
17
Relate Tables in a One-to-Many Relationship
(continued)
To delete a relationship:
1. Click Database Tools tab.
2. Click Relationships button.
3. Right-click middle portion of join line.
4. Click Delete.
5. Click Yes.
© Paradigm Publishing, Inc.
18
4
Create a One-to-One Relationship
 A one-to-one relationship can be created between
tables in which each record in the first table matches
only one record in the second table and one record in
the second table matches only one record in the first
table.
one-to-one
relationship
© Paradigm Publishing, Inc.
19
Create a One-to-One Relationship (continued)
To create a one-to-one
relationship:
1. Click Database Tools tab.
2. Click Relationships button.
3. Add tables.
4. Drag “one” field from
primary table to “one”
field in related table.
5. Click required check boxes.
6. Click Create button.
7. Click Save button.
© Paradigm Publishing, Inc.
20
6
Display Related Records in Subdatasheets
 When a relationship is established, records in related
tables can be viewed and edited with a subdatasheet.
subdatasheet
© Paradigm Publishing, Inc.
21
Display Related Records in Subdatasheets
(continued)
To display a subdatasheet:
1. Open table.
2. Click expand indicator
at left side of record.
3. Click table at Insert
Subdatasheet dialog
box.
4. Click OK.
2
© Paradigm Publishing, Inc.
22
Display Related Records in Subdatasheets
(continued)
 If a table has more than
one relationship defined,
clicking the expand
indicator will display the
Insert Subdatasheet
dialog box.
 At this dialog box, click
the desired table in the
Tables list box and click
OK.
Insert Subdatasheet
dialog box
© Paradigm Publishing, Inc.
23
Display Related Records in Subdatasheets
(continued)
To display subdatasheets for all records:
1. Open table.
2. Click Home tab.
3. Click More button.
4. Point to Subdatasheet.
5. Click Expand All.
3
© Paradigm Publishing, Inc.
24
Display Related Records in Subdatasheets
(continued)
To remove a
subdatasheet:
1. Open table.
2. Click Home tab.
3. Click More button.
4. Point to
Subdatasheet.
5. Click Remove.
3
© Paradigm Publishing, Inc.
25
Summary: Creating Relationships between
Tables






Define a primary key field in a table
Create a one-to-many relationship
Specify referential integrity
Print, edit, and delete relationships
Create a one-to-one relationship
View and edit a subdatasheet
© Paradigm Publishing, Inc.
26