Introduction to Database using Microsoft Access 2013 Part 7

advertisement
Introduction to Database
using Microsoft Access 2013
Part 7
November 19, 2014
Types of Relationships
What is Relationship?
(in Microsoft Access)
•
In a relational database, relationships enable you to prevent redundant data. For
example, if you are designing a database that will track information about books,
you might have a table named "Titles" that stores information about each book,
such as the book’s title, date of publication, and publisher. There is also information
that you might want to store about the publisher, such as the publisher's telephone
number, address, and ZIP Code/Postal Code. If you were to store all this
information in the "Titles" table, the publisher’s telephone number would be
duplicated for each title that the publisher prints.
•
•
•
•
A better solution is to store the publisher's information only one time, in a separate table
that we will call "Publishers." You would then put a pointer in the "Titles" table that
references an entry in the "Publishers" table.
To make sure that you data stays synchronized, you can enforce referential integrity
between tables. Referential integrity relationships help make sure that information in one
table matches information in another.
For example, each title in the "Titles" table must be associated with a specific publisher in
the "Publishers" table. A title cannot be added to the database for a publisher that does not
exist in the database.
Logical relationships in a database enable you to efficiently query data and create reports.
Types of Relationships
One to One Relationship
•
•
•
•
•
•
In a one-to-one relationship, a row in table A can have no more than one matching row in table B, and vice
versa. A one-to-one relationship is created if both of the related columns are primary keys or have unique
constraints.
This kind of relationship is not common, because most information that is related in this manner would be
in one table. You might use a one-to-one relationship to take the following actions: Divide a table with
many columns.
Isolate part of a table for security reasons.
Store data that is short-lived and could be easily deleted by deleting the table.
Store information that applies only to a subset of the main table.
In Access, the primary key side of a one-to-one relationship is denoted by a key symbol. The foreign key side
is also denoted by a key symbol.
Types of Relationships
One to Many Relationships
•
•
•
A one-to-many relationship is the most common kind of relationship. In this kind of relationship,
a row in table A can have many matching rows in table B. But a row in table B can have only one
matching row in table A. For example, the "Publishers" and "Titles" tables have a one-to-many
relationship. That is, each publisher produces many titles. But each title comes from only one
publisher.
A one-to-many relationship is created if only one of the related columns is a primary key or has a
unique constraint.
In the relationship window in Access, the primary key side of a one-to-many relationship is
denoted by a number 1. The foreign key side of a relationship is denoted by an infinity symbol.
Types of Relationships
Many to Many Relationships
•
In a many-to-many relationship, a row in table A can have many matching rows in
table B, and vice versa. You create such a relationship by defining a third table that
is called a junction table. The primary key of the junction table consists of the
foreign keys from both table A and table B. For example, the "Authors" table and
the "Titles" table have a many-to-many relationship that is defined by a one-tomany relationship from each of these tables to the "TitleAuthors" table. The
primary key of the "TitleAuthors" table is the combination of the au_ID column
(the "Authors" table’s primary key) and the title_ID column (the "Titles" table’s
primary key).
How to Create One-toOne Relationship
Begin
• Click MS Access 2013
• On the MS Access Screen Pane, select Blank Database
• Create a name that will reserve as your file for the database
In the screen pane,
click blank desktop
database
In the screen box, click
the browse button to
save in any file that you
want
Click create if done
Write your name in the
box that you want to
save your database
In the MS Access Environment
Right Click the
“Table 1”.
Click Design
View
• Save first.
Write your name
inside the Table Name
Box
Click “OK” if done.
• You are now in the Design View.
How to add fields in the design view
• Write first your Field Name.
• Second, select from the data type that you want.
• Third, is to write a short description based on your field name.
•
If you're done. Click the General Properties (Field) to emphasize your field.
• Now you're sample will be like this. With the Field Properties.
• Create another table also and repeat slides 12-23.
• Now I created two tables on the screen.
• Go to “DATABASE VIEW” and click “Relationships”
• Click Relationships
• The “Show Table has arrived.
• Click any of the Tables that you want to relate with.
• I clicked “Relationship” and the overview is.
•
•
Just simply drag the “Item1” to “Item1”. It should be the same primary key. Same data type
and same name like “Item1” and “Item1”.
Then the message appears.
• Click “Enforce Referential Integrity”
• Or if you want to click “Cascade Update Related Fields” and “Cascade
Delete Related Records”
• And press the “Create Button”
• Then the Relationship appears.
How to Create One-toMany Relationship
• Repeat Slide 12-23.
• Create another table and repeat slide 12-23.
• Now I Created Three Tables.
•
•
•
•
•
In One-to-Many Relationship. A Primary Key must be related to a non-primary key. It should have the same
data type, and the same name also.
Drag the Name to its same Name also.
The message appears
Click Enforce Referential Integrity
And click Create if done.
• Then the relationship appears.
How to Create Many-toMany Relationship
• Repeat Slide 12-23.
• Create another table and repeat slide 12-23.
• Repeat slide 33-36.
• The relationship appears.
• You can select the “Join Properties” that your output will be added to.
• You can press the Create Button. If you want to have One to One
Relationship.
Deleting Relationships
• RIGHT Click the
Relationship Arrow.
• Then Click Delete.
Download