build a database iii: build relationships for a new access database overview: you can relate this...

17
Build a database III: Build relationships for a new Access database Overview: You can relate This course teaches you how to build relationships between your new tables. Why are relationships essential? They bring the data in your tables together so you can extract meaningful information. This course explains the different types of relationships you can use in a database, and shows you how to create the most common type: a one-to-many relationship.

Upload: marian-thornton

Post on 14-Jan-2016

221 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Build a database III: Build relationships for a new Access database Overview: You can relate This course teaches you how to build relationships between

Build a database III: Build relationships for a new Access database

Overview: You can relate

This course teaches you how to build relationships between your new tables.

Why are relationships essential? They bring the data in your tables together so you can extract meaningful information.

This course explains the different types of relationships you can use in a database, and shows you how to create the most common type: a one-to-many relationship.

Page 2: Build a database III: Build relationships for a new Access database Overview: You can relate This course teaches you how to build relationships between

Build a database III: Build relationships for a new Access database

Course goals

• Create one-to-many relationships between database tables.

• Set referential integrity for each relationship and prevent accidental data removal.

• Set cascading updates and deletes, a move that walks any changes through your data.

• Turn indexing on or off for primary and foreign key fields, a move that helps speed your database and create a type of relationship called a one-to-one relationship.

Page 3: Build a database III: Build relationships for a new Access database Overview: You can relate This course teaches you how to build relationships between

Build a database III: Build relationships for a new Access database

Create relationships

In a database, relationships are how your tables “talk” to each other.

If your tables don’t talk, you can’t get answers from your data.

You create relationships by including a field from one table as a field in a related table.

Page 4: Build a database III: Build relationships for a new Access database Overview: You can relate This course teaches you how to build relationships between

Build a database III: Build relationships for a new Access database

Create relationships

In a database, relationships are how your tables “talk” to each other.

If your tables don’t talk, you can’t get answers from your data.

1

2

The field you include is called a primary key. Each of the tables in your database has a primary key, and you include it as a new field in another table to create a relationship with that other table.

When you share a primary key, the new field in the second table is called a foreign key.

Page 5: Build a database III: Build relationships for a new Access database Overview: You can relate This course teaches you how to build relationships between

Build a database III: Build relationships for a new Access database

Create relationships

In a database, relationships are how your tables “talk” to each other.

If your tables don’t talk, you can’t get answers from your data.

3 The picture illustrates one way this can work. In order to associate assets with suppliers, the foreign key field in the Assets table provides a list of company names. Behind the scenes, Access uses the values in the primary and foreign keys to synchronize the two fields.

Page 6: Build a database III: Build relationships for a new Access database Overview: You can relate This course teaches you how to build relationships between

Build a database III: Build relationships for a new Access database

Types of relationships

Your tables can participate in three types of relationships, shown in the picture.

1

2

You have a one-to-one relationship when a single record in one table is related to a single record in another table, and vice versa.

You have a one-to-many relationship when one record in a table is related to many records in another table.

Page 7: Build a database III: Build relationships for a new Access database Overview: You can relate This course teaches you how to build relationships between

Build a database III: Build relationships for a new Access database

Types of relationships

Your tables can participate in three types of relationships, shown in the picture.

3 You have a many-to-many relationship when several records in one table are related to several records in another table.

Page 8: Build a database III: Build relationships for a new Access database Overview: You can relate This course teaches you how to build relationships between

Build a database III: Build relationships for a new Access database

Create a relationship

So let’s create a one-to-many relationship between the Suppliers and Assets tables.

Before you start, think of the relationship as having “sides.”

In this case, one supplier can provide many assets, so the Suppliers table resides on the “one” side of the relationship, while the Assets table sits on the “many” side.

Page 9: Build a database III: Build relationships for a new Access database Overview: You can relate This course teaches you how to build relationships between

Build a database III: Build relationships for a new Access database

Create a relationship

Next, remember this rule: To create a one-to-many relationship, you use the primary key from the “one” side as a foreign key on the “many” side.

One of the easiest ways to do this is to run a tool called the Lookup Wizard, which creates the relationship and the foreign key field.

What’s more, the foreign key works as a drop-down list, so you can select data from the table on the “one” side quickly and accurately.

Page 10: Build a database III: Build relationships for a new Access database Overview: You can relate This course teaches you how to build relationships between

Build a database III: Build relationships for a new Access database

Set referential integrity

The relationship you created in the previous section doesn’t become a one-to-many relationship until you set referential integrity.

Referential integrity ensures that your primary and foreign key fields stay in synch whenever you add, change, or remove data. In turn, that keeps your data accurate.

Page 11: Build a database III: Build relationships for a new Access database Overview: You can relate This course teaches you how to build relationships between

Build a database III: Build relationships for a new Access database

Set referential integrity

You set referential integrity by editing a relationship.

1

2

Close any open tables, click the Database Tools tab, and in the Show/Hide group, click Relationships.

Click the line that represents the relationship you want to edit. The line becomes thicker.

Page 12: Build a database III: Build relationships for a new Access database Overview: You can relate This course teaches you how to build relationships between

Build a database III: Build relationships for a new Access database

Set referential integrity

You set referential integrity by editing a relationship.

3 Right-click the line and click Edit Relationship. The Edit Relationship dialog box appears.

4 Select the Enforce Referential Integrity check box.

Page 13: Build a database III: Build relationships for a new Access database Overview: You can relate This course teaches you how to build relationships between

Build a database III: Build relationships for a new Access database

Set referential integrity

You set referential integrity by editing a relationship.

Optionally, you can change data in one place and have that change appear in other related data. To do so, select the Cascade Update Related Fields and Cascade Delete Related Records check boxes. When you finish, click OK.

5

Page 14: Build a database III: Build relationships for a new Access database Overview: You can relate This course teaches you how to build relationships between

Build a database III: Build relationships for a new Access database

A look at one-to-one relationships

You may not use one-to-one relationships very often, but you should know how to create them.

One-to-one relationships use a primary key and a foreign key, just like one-to-many relationships.

The difference lies in how you set indexes for each key field. Indexing a field allows Access to search for data faster. It can also prevent users from adding duplicate records.

Page 15: Build a database III: Build relationships for a new Access database Overview: You can relate This course teaches you how to build relationships between

Build a database III: Build relationships for a new Access database

A look at one-to-one relationships

You use Design View to turn on indexing for your foreign key fields. It’s easiest to do this with blank tables.

1

2

3

In the Navigation Pane, right-click the table that contains the foreign key field, then click Design View.

In the designer, click the foreign key field.

Under Field Properties, on the General tab, click the list next to Indexed and select Yes (No Duplicates).

The picture shows the process.

Page 16: Build a database III: Build relationships for a new Access database Overview: You can relate This course teaches you how to build relationships between

Build a database III: Build relationships for a new Access database

A look at many-to-many relationships

You have a many-to-many relationship when a single record in one table can relate to many records in another, and a single record in that second table can also relate to many in the first.

For example, say your company has several types of computers and several technicians, with each technician certified to work on some, but not all, of the computers.

Each technician can be related to more than one computer, and in turn, each computer can be related to more than one technician.

Page 17: Build a database III: Build relationships for a new Access database Overview: You can relate This course teaches you how to build relationships between

Build a database III: Build relationships for a new Access database

A look at many-to-many relationships

To track who can work on a given machine, you create a many-to-many relationship by adding the primary keys from both sides of the relationship to a third table, called a junction or link table.

The picture shows a typical junction table, and you can see that its structure is relatively simple — a pair of primary key fields and some related information.

Now your data can answer questions such as, “Which computers did a given technician work on last month?”