Sei sulla pagina 1di 13

Create Relationships

In Access, you store data in multiple tables and then use relationships to join the tables. After you have created
relationships, you can use data from all of the related tables in a query, form, or report.
A primary key is a field or combination of fields that uniquely identify each record in a table. A foreign key is a
value in one table that must match the primary key in another table. You use primary keys and foreign keys to join tables
togetherin other words, you use primary keys and foreign keys to create relationships.
here are two valid types of relationships! one"to"one and one"to"many. In a one"to"one relationship, for every
occurrence of a value in table A, there can only be one matching occurrence of that value in table #, and for every
occurrence of a value in table #, there can only be one matching occurrence of that value in table A. $ne"to"one
relationships are rare because if there is a one"to"one relationship, the data is usually stored in a single table. %owever, a
one"to"one relationship can occur when you want to store the information in a separate table for security reasons, when
tables have a large number of fields, or for other reasons. In a one"to"many relationship, for every occurrence of a value in
table A, there can be &ero or more matching occurrences in table #, and for every one occurrence in table #, there can
only be one matching occurrence in table A.
'hen tables have a one"to"many relationship, the table with the one value is called the primary table and the
table with the many values is called the related table. Referential integrity ensures that the validity of the relationship
between two tables remains intact. It prohibits changes to the primary table that would invalidate an entry in the related
table. (or e)ample, a school has students. *ach student can make several payments, but each payment can only be from
one student. he +tudents table is the primary table and the ,ayments table is the related table.
+tudents
+tudent I- .ast /ame (irst /ame
,rimary 0ey
1 2ohn +mith
3 4ark Adams
5 6alerie 0ilm

,ayments
,ayment I- +tudent I- Amount -ue Amount ,aid
,rimary key (oreign key
1 1 788 788
3 3 988 588
5 5 788 378
: 3 :88 588
7 5 378 378
If you delete +tudent I- 1 from the +tudents table, +tudent I- 1 is no longer valid in the ,ayments table. ;eferential
integrity prevents you from deleting +tudent I- 1 from the +tudents table. Also, if the only valid +tudent I-s are 1, 3, and
5, referential integrity prevents you from entering a value of : in the +tudent I- field in the ,ayments table. A foreign key
without a primary key reference is called an orphan. ;eferential integrity prevents you from creating orphans.
To create relationships:
1. <lose all tables and forms. =;ight"click on the tab of any $bject. A menu appears. <lick <lose All.>
3. Activate the -atabase ools tab.
5. <lick the ;elationships button in the +how?%ide group. he ;elationships window appears.
:. If anything appears in the relationships window, click the <lear .ayout button in the ools group. If you are
prompted, click Yes.
7. <lick the +how able button in the ;elationships group. he +how able dialog bo) appears.
@. Activate the ables tab if your relationships will be based on tables, activate the Aueries tab if your relationships
will be based on queries, or activate the #oth tab if your relationships will be based on both.
9. -ouble"click each table or query you want to use to build a relationship. he tables appear in the ;elationships
window.
B. <lick the <lose button to close the +how able dialog bo).
C. -rag the ,rimary tableDs primary key over the related tableDs foreign key. After you drag the primary key to the
related tableDs bo), the cursor changes to an arrow. 4ake sure the arrow points to the foreign key. he *dit
;elationships -ialog bo) appears.
18. <lick the *nforce ;eferential Integrity checkbo).
11. <lick <reate. Access creates a one"to"many relationship between the tables.
13. <lick the +ave button on the Auick Access toolbar to save the relationship.
Tip: 'hen you create a relationship, you can view the related table as a subdatasheet of the primary table.
$pen the primary table and click the plus =E> in the far left column. he plus sign turns into a minus ="> sign. If the Insert
+ubdatasheet dialog bo) opens, click the table you want to view as a subdatasheet and then click $0. Access displays
the subdatasheet each time you click the plus sign in the far left column. <lick the minus sign to hide the subdatasheet.
Tip: After a relationship has been created between two tables, you must delete the relationship before you can
make modifications to the fields on which the relationship is based. o delete a relationship!
1. <lick the line that connects the tables.
3. ,ress the -elete key.
Tip: 'hen you create a lookup column, Access creates a relationship between the tables.
Lesson 5: Creating Microsoft Access Queries
You can use a query to view a subset of your data or to answer questions about your data. (or e)ample, if you want to
view a list of student names and email addresses, but you do not want to see addresses and other data, you can create a
query that displays the studentDs first name, last name, and email address only. Alternatively, if you want to know which
students live in -*, you can restrict your list to those students. his lesson teaches you how to create a query.
Open Tables or Queries in Query Design View
A query can be based on tables or on other queries. o create a query, you open the tables or queries on which you are
going to base your query in Auery -esign view, and then use the options in -esign view to create your query. You then
click the ;un button to display the results. You can save queries for later use.
To open tables or ueries in Query Design !iew:
1. Activate the <reate tab.
3. <lick the Auery -esign button in the $ther group. he +how able dialog bo) appears.
5. Activate the ables tab if you want to base your query on tables, activate the Aueries tab if you want base your
query on queries or activate the #oth tab if you want to base your query on both tables and queries.
:. <lick to choose the table or query on which you want to base your query.
7. <lick Add. he table appears in the window.
a. <lick to choose the ne)t table or query on which you want to base your query.
b. <ontinue clicking tables or queries until you have all the tables and queries you plan to use.
@. <lick <lose. Access changes to Auery -esign view.
Display All Recor"s an" All #iel"s
In Auery -esign view, each table has an option that allows you to display all of the fields and all of the records in a table.
his option appears on the field line on the drop"down menu as the table name followed by a period and an asterisk
=tablename.*).
To "isplay all recor"s an" all fiel"s:
1. $pen a table or query in Auery -esign view.
3. <lick the down"arrow in the first field on the (ield row and then select the tablename.* option. he table name
appears on the table line.
5. <lick the ;un button. Access retrieves all of the fields and records for the table and displays them in -atasheet
view.

Change fro$ Datasheet View to Query Design View
After you run a query, you can easily change back to Auery -esign view and make modifications to your query or create a
new query.
To change to Query Design !iew:
1. Activate the %ome tab.
3. <lick the down"arrow below 6iew in the 6iews group. A menu appears.
5. <lick -esign 6iew. Access changes to Auery -esign view. You can modify your query.
Tip: You can also click the -esign button in the lower"right corner of the Access window to change to
-esign view.
Retrie!e a %ingle Colu$n
You can use an Access query to retrieve a single column of data. Instead of choosing the tablename.* option on the (ield
line in Auery -esign view, choose the name of the field you want to retrieve.
To retrie!e a single colu$n:
1. $pen a table or query in Auery -esign view.
3. <hoose the field name you want to display in the field line.
5. <lick the ;un button. Access retrieves the column you chose.
Retrie!e Multiple Colu$ns
You can use an Access query to retrieve multiple columns of data. $n the (ield line in Auery -esign view, choose the field
name of each field you want to retrieve in the order you want to retrieve them.
To retrie!e $ultiple colu$ns:
1. $pen a table or query in Auery -esign view.
3. <hoose the field names you want to retrieve in the order you want to retrieve them.
5. <lick the ;un button. Access retrieves the columns you chose.
%ort a Query
'hen creating a query, you can sort the rows you retrieve in ascending or descending order by choosing the option you
want on the +ort row in Auery -esign view.
To perfor$ a sort:
1. $pen a table or query in Auery -esign view.
3. <hoose the field names you want to retrieve in the order you want to retrieve them.
5. Fnder the field you want to sort, click the down"arrow and then choose Ascending or -escending.
:. <lick the ;un button. Access retrieves the columns you chose and displays the rows in the order you specified.
%ort Multiple Colu$ns in a Query
As you learned in the previous section, you can sort the rows your query returns. You can also create sorts within a sort.
(or e)ample, you can sort by state and then within a state, you can sort by last name and then by first name. You specify
the sort in the order you want the sort to occur. If you want to sort by state and then by last name within a state and then
by first name within last name, you enter the sort in the following order! city, last name, first name. Your sort order may not
agree with the order in which you want to display fields. In such a case, you can use fields that do not display to enter
your sort order. o prevent a field from displaying, deselect the +how bo) on the +how row.
To sort $ultiple colu$ns:
1. $pen a table or query in Auery -esign view.
3. <hoose the field names you want to retrieve in the order you want to retrieve them.
5. <hoose the field names you want to sort by in the order you want to sort.
:. Fnder the fields you want to sort by, choose Ascending or -escending.
7. -eselect the +how button for the columns you do not want to display.
@. <lick the ;un button. Access retrieves the columns you chose and displays the rows in the order you specified.
Retrie!e %pecific Recor"s
In the e)amples so far, you have been retrieving all of the records =rows> in your table. You can, however, specify which
records you wish to retrieve. (or e)ample, you can retrieve only those students who live in -*, only the student whose
student number is 7, or only those students whose birth date is 3?1@?BB.
You use logical operators such as G =equal>, HI =not equal>, I =greater than>, or H =less than> to restrict the records you
retrieve. (or e)ample, if you only want to display students who live in -*, enter & 'D(' in the +tate column on the <riteria
line. Access will only retrieve records where the value in the +tate column is equal to -*. +election criteria are not case"
sensitive, so Access will retrieve records where the entry is -*, de, -e, or d*.
.ogical $perators
$perator 4eaning (ield ype *ntry (ormat
G *qual to <haracter
/umber
-ate
G J-*J
G 7
G K3?1@?BBK
HI /ot equal to <haracter
/umber
HI J-*J
HI 7
-ate HI K3?1@?BBK
I Lreater than <haracter
/umber
-ate
I J-*J
I 7
I K3?1@?BBK
IG Lreater than or
equal to
<haracter
/umber
-ate
I G J-*J
I G 7
I G K3?1@?BBK
H .ess than <haracter
/umber
-ate
H J-*J
H 7
H K3?1@?BBK
HG .ess than or
equal to
<haracter
/umber
-ate
HG J-*J
HG 7
HG K3?1@?BBK
In *qual to any item
in a list
<haracter
/umber
-ate
In =J-*J, J/2J>
In =7, C, 19>
In =K3?1@?BBK, K3?5?C8K,
K13?17?BBK>
/ot In /ot equal to any
item in a list
<haracter
/umber
-ate
/ot In =J-*J, J/2J>
/ot In =7, C, 19>
/ot In =K3?1@?BBK, K3?5?C8K,
K13?17?BBK>
#etween #etween two
values,
greater than or
equal to one and
less than or equal
to the other
<haracter
/umber
-ate
#etween J<J And J(J
#etween 7 And 18
#etween K1?1?BBK And
K13?51?BBK
/ot #etween /ot between two
values
<haracter
/umber
-ate
/ot #etween J<J And J(J
/ot #etween 7 And 18
/ot #etween K1?1?BBK And
K13?51?BBK
Is /ull he value is
missing from the
field
<haracter
/umber
-ate
Is /ull
Is /ull
Is /ull
Is /ot /ull he value is not
missing from the
field
<haracter
/umber
-ate
Is /ot /ull
Is /ot /ull
Is /ot /ull
.ike .ike a specified
pattern.
M means any
series of
characters.
N means any
single character.
<haracter
/umber
-ate
.ike J+MJ
.ike J1MJ
/ot Applicable
/ot .ike /ot like a
specified pattern.
M means any
series of
characters.
N means many
single character.
<haracter
/umber
-ate
.ike J+MJ
.ike J1MJ
/ot Applicable
'hen using the .ike and /ot .ike criteria, where you place the asterisk=M> or question mark =N> determines the type of
search Access performs. Li)e '*o+' finds all records in the field that begin with Jo. It would find 2ones, 2ohnson, and
2ordan. Li)e '+son' finds all records in the field that end with son. It would find +tevenson, 2ackson, and ,eterson.Li)e
'+,5-+' finds all records that contain :7@ anywhere in the field. It would find :7@1359BC, 135:7@9BC, and 1359BC:7@. he
sequence Li)e '.en' finds all three character field entries where the second and third characters are en. It would find
#en, .en, and 2en. he sequence Li)e '*o.' finds all three character field entries where the first and second characters
are Jo. It would return 2oe, 2oy, and 2on. he sequence Li)e 'T.$' finds all three character field entries where the first
and third characters are T and m. It would return im, om, and am.
To retrie!e specific recor"s:
1. $pen a table or query in Auery -esign view.
3. <hoose the field names you want to retrieve in the order you want to retrieve them.
5. <hoose the field names you want to sort by in the order you want to sort.
:. Fnder the fields you want to sort by, choose Ascending or -escending.
7. -eselect the +how button for columns you do not want to display.
@. *nter your selection criteria on the <riteria line.
9. <lick the ;un button. Access retrieves the columns you chose and displays the rows in the order you specified.
Apply Multiple Criteria
You can apply multiple criteria to the same table. If you place two criteria on the same line, Access will only retrieve
records where both criteria are met. (or e)ample, if you want all records where the +tate is equal to J-*J and the .ast
/ame is equal to +mith, you would set the +tate field to G J-*J and the .ast /ame field to G J+mithJ and you would place
both criteria on the same line.
If you place one set of criteria on the <riteria line and the second set of criteria on the $r line, Access will retrieve records
if either criteria are met. (or e)ample, you want all records where the +tate is equal to J-*J or the .ast /ame is equal to
+mith. You would set the +tate field to G J-*J and the .ast /ame field to G J+mithJ and you would place one set of criteria
on the <riteria line and the other set of criteria on the $r line. Access will bring back all records where the state equals
-* and all records where the .ast /ame is equal to +mith no matter what the +tate is. You can add
additional and and or statements by using the lines below the $r line. (or And clauses, place the criteria on the same lineO
for $r clauses, place the criteria on separate lines.
To apply $ultiple criteria:
1. $pen a table or query in Auery -esign view.
3. <hoose the field names you want to retrieve in the order you want to retrieve them.
5. <hoose the field names you want to sort by in the order you want to sort.
:. Fnder the fields you want to sort by, choose Ascending or -escending.
7. *nter your selection criteria on the <riteria line and the $r line, as needed.
@. -eselect the +how button for columns you do not want to display.
9. <lick the ;un button. Access retrieves the columns you chose and displays the rows in the order you specified.
Create a Query That /ses Two or More Tables
If you want to view data from two or more tables or queries, you can create a query that pulls the data from multiple tables
or queries. he tables and queries from which you pull your data should have a relationship.
To create a uery that uses two or $ore tables:
1. $pen the tables and?or queries you want to use in Auery -esign view.
3. <hoose the field names you want to retrieve in the order you want to retrieve them.
5. <hoose the field names you want to sort by in the order you want to sort. Fnder the fields you want to sort by,
choose Ascending or -escending.
:. *nter your selection criteria, if necessary =/ot applicable in this e)ample>.
7. -eselect the +how button for columns you do not want to display =/ot applicable in this e)ample>.
@. <lick the ;un button. Access retrieves the columns you chose and displays the rows in the order you specified.
%a!e a Query
After you create a query, you can save it. You can rerun a saved query at any time. If you change the data on which the
saved query is based, you will see the changes when you rerun the query.
To sa!e a uery:
1. <lick the +ave button on the Auick Access toolbar. Access saves the query unless you are saving for the first
time. If you are saving for the first time, the +ave As dialog bo) appears.
3. ype the name you want to give your query.
5. <lick $0. Access saves the query. You can now access the query by using the /avigation pane.
Tip: You can also save by right"clicking a queryDs tab and then selecting +ave from the menu that appears.
Access saves the query unless you are saving for the first time. If you are saving for the first time, the +ave As dialog bo)
appears. ype the name you want to give the query and then click $0. Access saves the query. You can now access the
query by using the /avigation pane.
Tip: After you have saved a query, you can run it by opening the /avigation pane and then clicking the name of
the query.
Mo"ify a Query
$nce created, a query can be modified. +imply open the query in Auery -esign view and make the changes. You can add
columns, change the sort order, change the criteria, and make other changes.
In Auery -esign view, the Auery +etup group offers several options that can assist you. Fse the Insert ;ows button to
insert a row in the criteria area. <lick anywhere in the row before which you want to insert a new row and then click the
Insert ;ows button.
Fse the Insert <olumns button to insert a column. <lick anywhere in the column before which you want to insert a
column and then click the Insert <olumn button .
Fse the -elete ;ows button to delete a row in the criteria area. <lick anywhere in the row you want to delete and
then click the -elete ;ow button.
Fse the -elete <olumns button to delete a column. <lick anywhere in the column you want to delete and then click
the -elete <olumn button .
/se a Query to Ma)e a Table
You can use a query to create a table. his is useful when you want to create a new table that includes the fields and data
from an e)isting table.
To create a table:
1. $pen the table or query on which you want to base your new table on in Auery -esign view.
3. *nter the criteria on which you want to base your new table.
5. <lick the 4ake able button. he 4ake able dialog bo) appears.
:. ype the name you want to give your new table.
7. <lick $0.
@. <lick ;un. You see the following prompt.
9. <lick Yes.
B. <lose the query. =;ight"click the queryDs tab and then click <lose.>
C. -ouble"click the new tableDs name in the /avigation pane to view the new table.
.

Potrebbero piacerti anche