Vous êtes sur la page 1sur 39

DATABASE RELATIONSHIP

OBJECTIVES
At the end of this practical session, participants should know how to

• Create Multiple database using Ms Access 2010

• Link tables together using database relationship


Contents

 Introduction
 Types of table relationships
 Why create table relationships?
 Understanding referential integrity
 View table relationships
 Create a table relationship
 Delete a table relationship
 Change a table relationship
 Enforce referential integrity
Introduction
Access 2010: Building Relationships Among Database Tables

One of the advantages of Relational Database Management System such as Access


2010 is to easily setup tables and relationships with constraints to make them more
tightly followed. One feature is being the ability to create and maintain relationships
between tables.

To start off with, Launch Access, create tables for defining relationships among them.
For Instance, I am using an existing database with tables (primary keys assigned and
data types with constraints applied ). Launch Access 2010, navigate to Database Tools
on new Access ribbon and click Relationships button.
Database Relationship
One of the goals of good database design is to remove data redundancy (duplicate

data). To achieve that goal, you divide your data into many subject-based tables so

that each fact is represented only once. You then provide Microsoft Office Access with

the means by which to bring the divided information back together — you do this by

placing common fields in tables that are related. To do this step correctly, however,

you must first understand the relationships between your tables, and then specify

these relationships in your Office Access database.


After you have created a table for each subject in your database, you must provide
Office Access 2010 with the means by which to bring that information back
together again when needed. You do this by placing common fields in tables that
are related, and by defining relationships between your tables. You can then create
queries, forms, and reports that display information from several tables at once.
For example, the form shown here includes information drawn from several tables:

Orders form displaying related information from five tables at once


 Information in this form comes from the Customers table...
 2 ...the Orders table...
 3 ...the Products table...
 4 ...and the Order Details table.
The customer name in the Bill To box is retrieved from the Customers table, the Order
ID and the Order Date values come from the Orders table, the Product name comes
from the Products table, and the Unit Price and Quantity values come from the Order
Details table. These tables are linked to each other in a variety of ways to bring
information from each into the form.

In the preceding example, the fields in the tables must be coordinated so that they
show information about the same order. This coordination is accomplished by using
table relationships. A table relationship works by matching data in key fields — often a
field with the same name in both tables. In most cases, these matching fields are the
primary key 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
for which they are responsible by creating a table relationship between the
EmployeeID fields in the Employees and the Orders tables.
 Employee ID appears in both tables — as a
primary key ...
 2 ... and as a foreign key.
Types of table relationships

There are three types of table relationships.

 A one-to-many relationship

Consider an order tracking database that includes a Customers table and an Orders
table. A customer can place any number of orders. It follows that for any customer
represented in the Customers table, there can be many orders represented in the
Orders table. The relationship between the Customers table and the Orders table is,
therefore, a one-to-many relationship.

To represent a one-to-many relationship in your database design, take the primary


key on the "one" side of the relationship and add it as an additional field or fields to
the table on the "many" side of the relationship. In this case, for example, you add a
new field — the ID field from the Customers table — to the Orders table and name
it Customer ID. Access can then use the Customer ID number in the Orders table to
locate the correct customer for each order.
 A many-to-many relationship

Consider the relationship between a Products table and an Orders table. A single order
can include more than one product. On the other hand, a single product can appear on
many orders. Therefore, for each record in the Orders table, there can be many records in
the Products table. In addition, for each record in the Products table, there can be many
records in the Orders table. This type of relationship is called a many-to-many relationship
because, for any product, there can be many orders and, for any order, there can be many
products. Note that to detect existing many-to-many relationships between your tables, it
is important that you consider both sides of the relationship.

To represent a many-to-many relationship, you must create a third table, often called a
junction table, that breaks down the many-to-many relationship into two one-to-many
relationships. You insert the primary key from each of the two tables into the third table.
As a result, the third table records each occurrence, or instance, of the relationship. For
example, the Orders table and the Products table have a many-to-many relationship that
is defined by creating two one-to-many relationships to the Order Details table. One
order can have many products, and each product can appear on many orders.
 A one-to-one relationship

In a one-to-one relationship, each record in the first table can have only one matching
record in the second table, and each record in the second table can have only one
matching record in the first table. This type of relationship is not common because,
most often, the information related in this way is stored in the same table. You might
use a one-to-one relationship to divide a table with 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. When you do identify such a relationship, both tables must share a
common field.
Why create table relationships?
You can create table relationships explicitly by using the Relationships window, or
by dragging a field from the Field List pane. Office Access uses table relationships
to how to join tables when you need to use them in a database object. There are
several reasons why you should create table relationships before you create other
database objects, such as forms, queries and reports.

Table relationships inform your query designs


To work with records from more than one table, you often must create a query that
joins the tables. The query works by matching the values in the primary key field of the
first table with a foreign key field in the second table. For example, to return rows that
list all of the orders for each customer, you construct a query that joins the Customers
table with the Orders table based on the Customer ID field. In the Relationships
window, you can manually specify the fields to join. However, if you already have a
relationship defined between the tables, Office Access supplies the default join, based
on existing the table relationship. In addition, if you use one of the query wizards,
Access uses the information it gathers from the table relationships you have already
defined to present you with informed choices and to prepopulate property settings
with appropriate default values.
Table relationships inform your form and report designs

When you design a form or report, Office Access 2010 uses the information it gathers
from the table relationships you have already defined to present you with informed
choices and to prepopulate property settings with appropriate default values.

Table relationships are the foundation upon which you can enforce referential
integrity to help prevent orphan records in your database. An orphan record is a
record with a reference to another record that does not exist — for example, an order
record that references a customer record that does not exist.

When you design a database, you divide your information into tables, each of which
has a primary key. You then add foreign keys to related tables that reference those
primary keys. These foreign key-primary key pairings form the basis for table
relationships and multi-table queries. It is important, therefore, that these foreign key-
primary key references stay synchronized. Referential integrity helps ensure that
references stay synchronized and is dependent upon table relationships.
Understanding referential integrity

The purpose of referential integrity is to prevent orphans and keep references in


sync so that this hypothetical situation never occurs.
You enforce referential integrity by enabling it for a table relationship (see
Enforce referential integrity for step-by-step instructions). Once enforced, Access
rejects any operation that would violate referential integrity for that table
relationship. This means Access will reject both updates that change the target
of a reference, and deletions that remove the target of a reference. However, it
is possible you might have a perfectly valid need to change the primary key for a
shipper that has orders in the Orders table. For such cases, what you really need
is for Access to automatically update all the effected rows as part of a single
operation. That way,
Access ensures that the update is completed in full so that your database is
not left in an inconsistent state, with some rows updated and some not. For
this reason Access supports the Cascade Update Related Fields option.
When you enforce referential integrity and choose the Cascade Update
Related Fields option, and you then update a primary key, Access
automatically updates all fields that reference the primary key.
It's also possible you might have a valid need to delete a row and all related
records — for example, a Shipper record and all related orders for that
shipper. For this reason, Access supports the Cascade Delete Related
Records option. When you enforce referential integrity and choose the
Cascade Delete Related Records option, and you then delete a record on the
primary key side of the relationship, Access automatically deletes all records
that reference the primary key.
View Table Relationships

To view your table relationships, click Relationships on the Database Tools tab.
The Relationships window opens and displays any existing relationships. If no
table relationships have yet been defined and you are opening the Relationships
window for the first time, Access prompts you to add a table or query to the
window.
Open the Relationships window

 Click file Tab, and then click Open.

 In the Open dialog box, select and open the database.


 On the Database Tools tab, in the Show/Hide group, click Relationships.

 If the database contains relationships, the Relationships window appears. If


the database does not contain any relationships and you are opening the
Relationships window for the first time, the Show Table dialog box appears.
Click Close to close the dialog box.
 On the Design tab, in the Relationships group, click All Relationships.
1 The primary key
2 This line represents the relationship
3 The foreign key

A table relationship is represented by a relationship line drawn between tables in the


Relationships window. A relationship that does not enforce referential integrity
appears as a thin line between the common fields supporting the relationship. When
you select the relationship by clicking its line, the line thickens to indicate it is
selected.
If you enforce referential integrity for this relationship, the line appears thicker at
each end. In addition, the number 1 appears over the thick portion of the line on
one side of the relationship, and the infinity symbol (∞) appears over the thick
portion of the line on the other side.

When the Relationships window is active, you can select from the following
commands on the Ribbon, part of the Microsoft Office Fluent user interface:

On the Design tab, in the Tools group:


• Edit Relationships Opens the Edit Relationships dialog box. When you select a
relationship line, you can click Edit Relationships to change the table relationship. You
can also double-click the relationship line.
• Clear Layout Removes all tables and relationships from display in the Relationships
window. Note that this command only hides the tables and relationships — it does not
delete them.
• Relationships Report Creates a report that displays the tables and relationships in
your database. The report shows only the tables and relationships that are not hidden
in the Relationships window.
On the Design tab, in the Relationships group:

• Show Table Opens the Show Table dialog box so that you can select tables
and queries for viewing in the Relationships window.

• Hide Table Hides the selected table in the Relationships window.


• Direct Relationships Displays all relationships and related tables for the selected
table in the Relationships window, if they are not already displayed.
• All Relationships Displays all of the relationships and related tables in your
database in the Relationships window. Note that hidden tables (tables for which the
Hidden check box in the table's Properties dialog box is selected) and their
relationships will not be shown unless Show Hidden Objects is selected in the
Navigation Options dialog box.
Create a table relationship
You can create a table relationship by using the Relationships window, or by dragging a
field onto a datasheet from the Field List pane. When you create a relationship between
tables, the common fields are not required to have the same names, although it is often
the case that they do. Rather, those fields must have the same data type. If the primary
key field is an AutoNumber field, however, the foreign key field can be a Number field if
the FieldSize property of both fields is the same. For example, you can match an
AutoNumber field and a Number field if the FieldSize property of both fields is Long
Integer. When both common fields are Number fields, they must have the same FieldSize
property setting.

Create a table relationship by using the Relationships window

 Click the File button and then click Open.


 In the Open dialog box, select and open the database.
 On the Database Tools tab, in the Show/Hide group, click Relationships.
 If you have not yet defined any relationships, the Show Table dialog box
automatically appears. If it does not appear, on the Design tab, in the Relationships
group, click Show Table.
The Show Table dialog box displays all of the tables and queries in the database. To
see only tables, click Tables. To see only queries, click Queries. To see both tables and
queries, click Both.
 Select one or more tables or queries and then click Add. When you have finished
adding tables and queries to the Relationships window, click Close.
 Drag a field (typically the primary key) from one table to the common field (the
foreign key) in the other table. To drag multiple fields, press the CTRL key, click each
field, and then drag them.

 The Edit Relationships dialog box appears.

 Verify that the field names shown are the common fields for the relationship. If a field
name is incorrect, click the field name and select a new field from the list.

 To enforce referential integrity for this relationship, select the Enforce Referential
Integrity check box. For more information about referential integrity, see the
Understanding Referential Integrity and the Enforce Referential Integrity sections.
 Click Create.

The relationship line is drawn between the two tables. If you selected the Enforce
Referential Integrity check box, the line appears thicker at each end. In addition, again
only if you selected the Enforce Referential Integrity check box, the number 1 appears
over the thick portion of the line on one side of the relationship, and the infinity
symbol (∞) appears over the thick portion of the line on the other side.
Notes

 To create a one-to-one relationship Both of the common fields (usually the primary
key and foreign key fields) must have a unique index. This means the Indexed
property for these fields should be set to Yes (No Duplicates). If both fields have a
unique index, Access creates a one-to-one relationship.
 To create a one-to-many relationship The field on the "one" side (typically the
primary key) of the relationship must have a unique index. This means the Indexed
property for this field should be set to Yes (No Duplicates). The field on the "many"
side should not have a unique index. It can have an index, but it must allow
duplicates. This means the Indexed property for this field should be set to either No,
or Yes (Duplicates OK). When one field has a unique index and the other does not,
Access creates a one-to-many relationship
Access 2010: Building Relationships Among Database Tables

One of the advantages of Relational Database Management System such as Access


2010 is to easily setup tables and relationships with constraints to make them more
tightly followed. One feature is being the ability to create and maintain relationships
between tables.

To start off with, Launch Access, create tables for defining relationships among
them.For Instance, I am using an existing database with tables (primary keys assigned
and data types with constraints applied ). Launch Access 2010, navigate to Database
Tools on new Access ribbon and click Relationships button
Access will open Relationships tab with Show Table Dialog box. Select tables, queries
or both, and hit Add.
You will see selected tables/queries small windows with columns names in Relationships
window. Now sit back start pondering over the selected tables to relate them properly. In
this example I am using only four tables
Am now creating one-many-relationship between AddictiveTips Example (Student) and
Courses table. Click & hold the primary key of AddictiveTips Example, drag it and drop
over course name in small window of Courses. Immediately Edit Relationships dialog
window will pop-up. Here you can create new relationship between tables, Specify Join
Type, and Enforce Referential Integrity. For demonstration purpose, I am going to ignore
all options and hit Create.
You will see one-many relationship created between AddictiveTips Example’s ID and
course code.
To create one-one relationship with enforcing referential Integrity, hold AddictiveTips
Example’s ID, drag and drop it over Mark Sheet’s Id. Edit Relationship dialog box will
appear. Now enable Enforce Referential Integrity (ensure consistency & validates
corresponding entries) & Cascade Delete Related Records checkbox (ensures removal of
all related data from Mark Sheet when AddictiveTips Example’s ID is deleted ). Click Join
Type to open it’s dialog box.
These three options are similar to SQL’s inner join, outer join and right outer join. Select
a join type and Hit OK.
Click Create to make 1-1 relationship between AddictiveTips Example’s ID and Mark
Sheet’s Id
This is the simplest form of relationships among tables/queries. It becomes much complex
and elusive as it grows.
Relationships make it possible to co-relate and ensure data consistency from table to
table. Now you can unleash power of Access 2010 by creating more tables and
making relationships with them.
Steps
1. Click the Database Tools tab on the Ribbon, then click the Relationships button
2. In the Show Table dialog box, double-click Customers, double-click Comments, then
click Close Each table is represented by a small field list window that displays the table’s
field names. A key symbol identifies the primary key field in each table. To relate the two
tables in a one-to-many relationship, you connect them using the common field, which is
always the primary key field on the “one” side of the relationship.
3. Drag CustID in the Customers field list to the CustID field in the Comments field list
The Edit Relationships dialog box opens. Referential integrity, a set of Access rules that
governs data entry, helps ensure data accuracy.
4. Click the Enforce Referential Integrity check box in the Edit Relationships dialog box,
then click Create
The one-to-many line shows the link between the CustID field of the Customers table
(the “one” side) and the CustID field of the Comments table (the “many” side,
indicated by the infinity symbol),The linking field on the “many” side is called the
foreign key field. Now that these tables are related, it is much easier to enter
comments for the correct customer.
5. Click the Close button on the Design tab, click Yes to save changes, then double-
click the Customers table in the Navigation Pane to open it in Datasheet View
When you relate two tables in a one-to-many relationship, expand buttons appear to
the left of each record in the table on the “one” side of the relationship. In this case,
this is the Customers table.
6. Click the expand button to the left of the first record, then drag the pointer to
widen the Comment field A sub datasheet shows the related comment records for each
customer. In other words, the sub datasheet shows the records on the “many” side of a
one-to-many relationship. The expand button also changed to the collapse button for
the first customer. Widening the Comment field allows you to see the entire entry
in the Comments sub datasheet. Now the task of entering comments for the right
customer is much more straightforward.
7. Enter two more comments ,Interestingly, the Cust ID field in the Comments table
(the foreign key field) is not displayed in the sub datasheet.
Behind the scenes, Access is entering the correct Cust ID value in the Comments table,
which is the glue that ties each comment to the right customer.
8. Close the Customers table, then click Yes if prompted to save changes

Vous aimerez peut-être aussi