You are on page 1of 11

Ken Goldberg Database Lab Notes

Lab 3 Relationships in ER Diagram and Relationships in MS Access MS Access Lab 3 Summary


Introduction to Relationships Why Define Relationships? Relationships in ER Diagram vs. Relationships in MS Access Creating Relationships et!een "a#les o $ne%to%Many Relationship &'()* o Many%to%Many Relationship &M()* Creating a +ield,Column that loo-s up or lists values in ta#les o Creating a field that loo-s up data from another ta#le o Creating a field that lists values from another ta#le

1. Introduction:

hat is a Relationship!

Definition in class( An association #et!een . &or more* separate entities. Definition in MS Access( An association #et!een . common fields &column* in t!o ta#les. "here are three types of relationships( "ne#to#"ne $1:1% "ne#to#Many $1:&% Many#to#Many $M:&%.

'.

hy De(ine Relationships!

After you/ve set up different ta#les for each su#0ect in your Microsoft Access Data#ase1 you need a !ay of telling Microsoft Access ho! to #ring that information #ac- together again. "he first step in this process is to define relationships #et!een your ta#les. After you/ve done that1 you can create 2ueries1 forms1 and reports to display information from several ta#les at once. +or e3ample1 the form in )ig. 1 includes information from five ta#les(

Ken Goldberg Database Lab Notes )ig. 1: A )orm *sing In(ormation (rom )i+e ,ables

-o. do relationships .or/!


In the previous e3ample in )ig. 11 the fields in five ta#les must #e coordinated so that they sho! information a#out the same order. "his coordination is accomplished !ith relationships #et!een ta#les. A relationship works by matching data in key fields usually a field with the same name in both tables. In most cases1 these matching fields are the primary /ey &see footnote of page 4* from one ta#le1 !hich provides a uni2ue identifier for each record1 and a (oreign /ey in the other ta#le. +or e3ample1 employees can #e associated !ith orders they/re responsi#le for #y creating a relationship #et!een the 5 Employees6 ta#le and the 5"rders6 ta#le using the EmployeeID fields &!hich !e !ill sho! later*.

3. Relationships in ER Diagram +s MS Access

3.1 Relationships in ER Diagram


7et8s ta-e a loo- at the relationship #et!een the 5 EM0L"1EE2 entity and the 3"RDER2 entity in )ig. '. 9"a-es8 is a one%to%many relationship. "he 9"a-es8 relationship can #e converted into an MS Access relationship as sho!n in )ig 3.

)ig. ': Relationships in ER diagram

3.' Relationships in MS Access


:lease refer to )ig. 31 !hich corresponds to the relationship in ER diagram sho!n in )ig. '.

4. 5reating Relationships 6et.een ,ables


Again1 !e !ill #e using &orth.ind Sample Database. $pen it as #efore from ;elp menu.

4.1 "ne#to#"ne relationship

Ken Goldberg Database Lab Notes

)ig. 3: Relationships in MS Access Relationship 7ie. In a one%to%one relationship1 each record in "a#le A can have only one matching record in "a#le and each record in "a#le can have only one matching record in "a#le A. "his type of relationship is )$" common1 #ecause most information related in this !ay !ould #e in one ta#le. <ou might use a one%to%one relationship to divide a ta#le !ith too many fields1 to isolate part of a ta#le for security reasons1 or to store information that applies only to a su#set of the main ta#le. +or e3ample1 you might !ant to create a ta#le to trac- employees participating in a fundraising soccer game.

4.' "ne#to#many Relationship


A one%to%many relationship is the most common type of relationship. In a one#to#many relationship1 a record in "a#le A can have many matching records in "a#le 1 #ut a record in "a#le has only one matching record in "a#le A. Refer to )ig. 4 for Supplier ta#le as A1 and :roducts ta#le as .

De(ining a "ne#to#many Relationships bet.een ,ables


'. Close any ta#les you have open. <ou can/t create or modify relationships #et!een any open ta#les. .. If you haven/t already done so1 s!itch to the Database indo.. <ou can press +'' to s!itch to the Data#ase !indo! from any other !indo!. 3. Clic- on menu ,ools Relationships &&ote( !hen you do this1 the tool#ar in the !indo! !ill loo- different1 refer to )ig.3* If the relationships are already defined for the data#ase1 a relationship vie! of the current data#ase !ill sho! up and loo- li-e )ig. 8. 4. If your data#ase does not have any relationships defined1 the Sho. ,able dialog #o3 !ill automatically #e displayed &)ig. 9*. Add the ta#les that you !ant to relate. When the Sho. ,able dialog #o3 isn/t displayed1 in the Relationships 7ie. &!hich you originally accessed via the menu ,ool Relationships*1 clic- on menu relationships Sho. ,able or right%clicand select Sho. ,able.

Ken Goldberg Database Lab Notes

)ig. 4 "ne#to#Many Relationship

)ig. 8 Relationships 7ie. o( a &orth.ind Database

)ig. 9 Sample 3Sho. ,able2 Dialog 6o:

Ken Goldberg Database Lab Notes

5. $nce in the relationship vie! you have all the ta#les you !ant to relate1 then you need to
define the relationship #et!een any t!o ta#les #y dragging the field that you !ant to relate from one ta#le to the related field in the other ta#le &refer to )ig. ;*. "o drag multiple (ields1 press the C"R7 -ey and clic- each field #efore dragging them. In most cases1 you drag the primary /ey1 field &!hich is displayed in #old te3t* from one ta#le to a similar field &often !ith the same name* called the (oreign /ey' in the other ta#le. "he related fields are )$" re2uired to have the same names &)ote it is good practice to do so since it reminds you !here the relationship comes from*1 #ut they M=S" have the same domain &or data type3* and contain the same -ind of information. In addition1 !hen the matching fields are &umber fields1 they must have the same +ieldSi>e property setting. "he t!o e3ceptions to matching data types( you can match an Auto&umber field !ith a &umber field !hose )ieldSi<e property is set to Long Integer you can also match an Auto&umber field !ith a &umber field if #oth fields have their )ieldSi<e property set to ReplicationID. ?. $nce you have created the relationships1 the Edit Relationships dialog #o3 is displayed as sho!n in )ig. =. Chec- the field names displayed in the t!o columns to ensure they are correct. <ou can change them if necessary. Set the relationship options if necessary. +or information a#out a specific item in the Relationships dialog #o31 clic- on the 2uestion mar#utton &the cursor !ould no! have a floating 2uestion mar- ne3t to it* and then clic- on the item. @. Clic- the 5reate #utton to create the relationship. A. Repeat steps 4 through A for each pair of ta#les you !ant to relate.

)ig. ;: 5reating a relationship bet.een ' tables &otes


1

Primary key: one or more fields w ose !al"e or !al"es "ni#"ely identify ea$ re$ord in a table. %n a relations i&' a &rimary key is "sed to refer to s&e$ifi$ re$ords in one table from anot er table.
2

(oreign key: one or more table fields t at refer to t e &rimary key field or fields in anot er table. ) foreign key indi$ates ow t e tables are related. * e data in t e foreign key and &rimary key fields m"st mat$ . (or e+am&le' t e Prod"$ts table in t e Nort wind sam&le database $ontains t e foreign key ,"&&lier%D' w i$ refers to t e ,"&&lier%D &rimary key of t e ,"&&liers table. -sing t is relations i&' t e Prod"$ts table dis&lays a s"&&lier name from t e ,"&&liers table for ea$ &rod"$t.
3

Data ty&e: t e attrib"te of a !ariable or field t at determines w at kind of data it $an old. (or e+am&le' t e *e+t and .emo field data ty&es allow t e field to store eit er te+t or n"mbers' b"t t e N"mber data ty&e will allow only n"mbers to be stored in t e field. N"mber data ty&e fields store n"meri$al data t at will be "sed in mat emati$al $al$"lations. -se t e /"rren$y data ty&e to dis&lay or $al$"late $"rren$y !al"es. ,"&&orted data ty&es in$l"de field data ty&es' 0is"al 1asi$ data ty&es' and #"ery &arameter data ty&es.

Ken Goldberg Database Lab Notes

<ou can edit the relationship #et!een the t!o ta#les later on1 #y 0ust dou#le%clic-ing on the Relationship Line $sho!n in #oth )ig. 8 and )ig. ;* connecting the t!o ta#les in Relationships 7ie.. Please keep in mind that when you delete table from the Relationship Views, it only changes the layout you see but not the relationship itself. When you close the Relationships !indo!1 Microsoft Access as-s if you !ant to save the layout. Whether you save the layout or not1 the relationships you create are saved in the data#ase. If you need to vie! all the relationships defined in the data#ase1 clic- Sho. All Relationships on the tool#ar. "o vie! only the relationships defined for a particular

ta#le1 clic- the ta#le1 and then clic- Sho. Direct Relationships on the tool#ar. If you need to ma-e a change to the design of a ta#le1 you can right%clic- the ta#le you !ant to change1 and then clic- Design ,able. <ou can create relationships using 2ueries as !ell as ta#les. ;o!ever1 re(erential integrity4 isn/t enforced !ith 2ueries. "o create a relationship #et!een a ta#le and itself1 add that ta#le t!ice. "his is useful in situations !here you need to perform a loo-up !ithin the same ta#le. +or e3ample1 in the Employees ta#le in the )orth!ind sample data#ase1 a relationship has #een defined #et!een the EmployeeID and Reports,o fields1 so that the Reports,o field can display employee data from a matching EmployeeID.

)ig. =: 3Edit Relationships2 Dialog 6o:

4.3 Many#to#many Relationship


In a many#to#many relationship1 a record in "a#le A can have many matching records in "a#le 1 and a record in "a#le can have many matching records in "a#le A. "his type of relationship is only possible #y defining a third ta#le1 called a 0unction ta#le1 !hose primary -ey consists of t!o fields( the primary /eys (rom both ,ables A and 6 . A many%to%many relationship is really t!o one%to%many relationships !ith a third ta#le. +or e3ample1 the "rders ta#le and the 0roducts ta#le in )ig. > have a many#to#many relationship that/s defined #y creating t!o one#to#many relationships !ith the "rder Details ta#le.
4

2eferential integrity: 2"les t at yo" follow to &reser!e t e defined relations i&s between tables w en yo" enter or delete re$ords. %f yo" enfor$e referential integrity' .i$rosoft )$$ess &re!ents yo" from adding re$ords to a related table w en t ere is no asso$iated re$ord in t e &rimary table' $ anging !al"es in t e &rimary table t at wo"ld res"lt in or& an re$ords in a related table' and deleting re$ords from t e &rimary table w en t ere are mat$ ing related re$ords in a related table.

Ken Goldberg Database Lab Notes

)ig. >: Many#to#many Relationship

De(ining a Many#to#many Relationship bet.een ,ables


)ig. 'B sho!s a relationship vie! for )orth!ind data#ase containing a many%to%many relationship #et!een :roducts and $rders. :lease refer to it !hile you understand the follo!ing steps. '. Create the t!o ta#les that !ill have a many#to#many relationship. .. Create a third ta#le1 called a ?unction table1 and add fields !ith the same definitions as the primary -ey fields from each of the other t!o ta#les to this ta#le. In the 0unction ta#le1 the primary /ey fields function as (oreign /eys. <ou can add other fields to the 0unction ta#le1 0ust as you can to any other ta#le. 3. In the ?unction table1 set the primary /ey to include the primary /ey fields from the other t!o ta#les. +or e3ample1 in an "rder Details 0unction ta#le1 the primary -ey !ould #e made up of the "rderID and 0roductID fields. &)ote( <ou can set multiple fields as the primary /ey #y highlighting multiple ro!s &!hich correspond to fields* !hich you !ant to #e part of your primary /ey in Design 7ie.@ and then clic- on Edit 0rimary Aey. Alternatively you can also use the primary /ey #utton in the tool #ar* C. Define a one#to#many relationship #et!een each of the t!o primary tables and the ?unction table. 4. "o add data to the ta#les1 create a (orm8 that !or-s !ith more than one ta#le. &ote: In the )orth!ind sample data#ase1 a many#to#many relationship e3ists #et!een the "rders and 0roducts ta#les. $ne order in the "rders ta#le can include multiple products from the 0roducts ta#le. In addition1 a single product can appear in many orders. In the sample data#ase1 the "rder Details ta#le is a ?unction table #et!een the "rders table and the 0roducts table.

(orm: a .i$rosoft )$$ess database ob4e$t on w i$ yo" &la$e $ontrols for taking a$tions or for entering' dis&laying' and entering data in fields.

Ken Goldberg Database Lab Notes

)ig. 1B: Cunction ,able in a Many#to#many Relationship in &orth.ind.mdb

8. 5reating a (ield that loo/s up or lists +alues in tables


8.1 Loo/up i<ard
When you set up the fields for the ta#le1 you can create a field that displays either of t!o -inds of lists to make data entry simpler #y means of Loo/up i<ard( Loo/up list that displays values loo-ed up from an e3isting ta#le or 2uery 7alue list that displays a fi3ed set of values that you enter !hen you create the field We !ill introduce ho! to use the loo-up !i>ard in design vie! to generate loo-up list and value in the follo!ing t!o sections.

8.' 5reating a (ield that loo/s up data (rom another table in Design +ie.
Loo/up List
"he most common Loo/up list displays values loo-ed up from a related ta#le. +or e3ample1 the SupplierID field in the 0roducts ta#le of the &orth.ind sample data#ase displays the Loo/up list as sho!n in )ig. 11.

)ig. 11: Loo/ up List

Ken Goldberg Database Lab Notes <ou can add a ne. 7oo-up field in either design vie! or datasheet vie! of ta#les. ;o!ever1 if the field you !ant to use as the foreign -ey for a 7oo-up field already e3ists1 you must open that field/s ta#le in Design +ie. to define the 7oo-up field. +or e3ample1 assume you already have a Suppliers ta#le filled !ith data1 no! you !ant to create a 0roducts ta#le containing a SupplierID field !hose contents should come form the Suppliers ta#le. <ou can ma-e the data entry simpler for this #y changing it to a 7oo-up field1 so that a drop%do!n list containing all supplier IDs from Suppliers ta#le !ill #e availa#le !hen you start to enter data into :roducts ta#le. In order to do this1 you must open the 0roducts ta#le in Design +ie. to change SupplierID to a 7oo-up field. ;ere are the steps to follo!( '. $pen the ta#le in Design +ie.. .. Do one of the follo!ing( If the SupplierID field is not defined yet1 clic- in the ro! #elo! !here you !ant to add the field1 and then clic- Insert Ro.s on the tool#ar1 or to add a ne! field at the end of the ta#le1 clic- in the first #lan- ro!. "ype SupplierID in the +ield )ame column1 follo!ing Microsoft Access o#0ect%naming rules. If the SupplierID is already defined1 clic- the corresponding ro!. D. In the 9Data ,ypeD column1 clic- the do!n arro! and select ELoo/up i<ardD from the list. C. Clic- the option that indicates you !ant the 7oo-up field to loo- up the values in a ta#le or 2uery. 4. Clic- &e:t and follo! the directions in the remaining 7oo-up Wi>ard dialog #o3es. When you clic- the )inish #utton1 Microsoft Access creates the 7oo-up field ? and sets certain field properties #ased on the choices you made in the !i>ard. +or information on the properties that the Loo/up i<ard sets1 go #ac- to the field that !as set #y the Loo/up i<ard1 and clicon the Loo/up ta# in the )ield 0roperties !indo! &)ig. 1'*.

)ig. 1': 0roperties Listed in the Loo/up ,ab in the )ield 0roperties
3

indo.

Look"& list field and form: on$e yo"7!e $reated a Look"& list field' if yo" add t e field to a form' .i$rosoft )$$ess $o&ies its definition into t e form. 8o" won7t a!e to $reate t e $ombo or list bo+ and its Look"& or !al"e list definition for t e form. 9owe!er' if yo" $ ange t e definition of a Look"& or !al"e list field in t e table after adding it to a form' t ose $ anges will N:* be refle$ted in t at form. *o $orre$t t is' delete t e field from t e form and t en add it again.

Ken Goldberg Database Lab Notes &ote: It is also possi#le to add a 7oo-up field to a ta#le that displays values from the same ta#le that contains the 7oo-up field. +or e3ample1 in the Employees ta#le of the )orth!ind sample data#ase the Reports,o field is a 7oo-up field that displays data from the )irst&ame and Last&ame fields #y loo-ing up the corresponding EmployeeID in the same ta#le. )o! let8s go #ac- to )ig. 11. It is created #y loo-ing up the SupplierID values in the Suppliers ta#le and #y displaying the corresponding Supplier names. :ic-ing a value from a Loo/up list sets the (oreign /ey value in the current record &SupplierID in the 0roducts ta#le* to the primary /ey value of the corresponding record in the original ta#le & SupplierID in the Suppliers ta#le*. "his creates an association to the related ta#le to display &#ut )$" store* the Supplier names in the record. "he foreign -ey & SupplierID* is stored #ut is not displayed. +or this reason1 any updates made to the data in the Suppliers ta#le !ill #e reflected in #oth the list and records in the 0roducts ta#le. <ou must define a 7oo-up field in the ta#le that !ill contain the (oreign /ey and display the 7oo-up list. In this e3ample1 the Loo/up list field !ould #e defined in the 0roducts ta#le.

8.3 5reate a +alue list (ield in Design +ie.


7alue List
A 7alue list loo-s the same as a Loo/up list1 #ut consists of a fi3ed set of values you type in !hen you create it. A value list should only #e used for values that !ill not change very often and don/t need to #e stored in a ta#le. +or e3ample1 a list for a Salutation field containing Mr.1 Mrs.1 or Ms. !ould #e a good candidate for a value list. Choosing a value from a value list !ill store that value in the record. It doesn/t create an association to a related ta#le. +or this reason1 if you change any of the original values in the value list later1 they !ill not #e reflected in records added #efore this change !as made. '. $pen the ta#le in Design +ie.. 2. "o insert the field !ithin the ta#le1 clic- in the ro! #elo! !here you !ant to add the field1 and then clic- Insert Ro.s on the tool#ar. D. "o add the field to the end of the ta#le1 clic- in the first #lan- ro!. C. In the )ield &ame column1 type the name for the field1 follo!ing Microsoft Access o#0ect% naming rules. 4. In the Data ,ype column1 clic- the arro! and select Loo/up i<ard. ?. In the first Loo/up i<ard dialog #o31 clic- the option that indicates you !ill type in the values that you !ant. @. Clic- &e:t and follo! the directions in the remaining 7oo-up Wi>ard dialog #o3es. &ote:

When you use the Loo/up i<ard to create a fi3ed value list@1 Microsoft Access sets certain field properties #ased on the choices you made in the !i>ard. EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE WE77 D$)E FF <$= ;AGE H=S" 7EAR)ED ;$W "$ CREA"E DI++ERE)" RE7A"I$)S;I:S E"WEE) "A 7ES1 A)D A7S$ "$ E);A)CE <$=R "A 7ES =SI)I 7$$J=: K GA7=E 7IS"S EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE

Fui< 3
5

0al"e list field and form: on$e yo"7!e $reated t e field' if yo" add it to a form' .i$rosoft )$$ess $o&ies its definition into t e form. 8o" won7t a!e to $reate t e !al"e list definition for t e form. 9owe!er' if yo" $ ange t e definition of t e !al"e list field in t e table after adding it to a form' t ose $ anges will not be refle$ted in t at form. *o $orre$t t is' delete t e field from t e form and t en add it again.

1<

Ken Goldberg Database Lab Notes &Due at the start of ne3t class* '. Create a data#ase car.mdb1 and #uild D ta#les for a Car Company &choose your o!n name*1 including Customer1 GehicleInstance1 and Dealership. +or each ta#le1 create the fields as follo!s( Customer( CustomerID1 Customer)ame1 City1 Discount GehicleInstance( GehicleID1 Gehicle)ame1 :rice1 Discount Dealership( DealerID1 Dealer)ame1 City1 Commission .. Enter as many tuples,ro!s of data for the 9GehicleInstance8 ta#le as you li-e &L'B instances* and 'B tuples,ro!s of data for the 9Customer81 and 9Dealership8 ta#les. D. Create relationships that connect the different entities according to the follo!ing three re2uirements( &Hint: efore you can create a one!to!many relationship between two tables, you need to add the primary key of the table on the one!side to the table on the many!side so that the two tables are related by some common field."or many!to!many relationship, build a #unction table and then create two one!to!many relationships* :lease enter the data for the ne! fields too. a. A dealer can have multiple vehicles and multiple customers #. A Customer can o!n multiple vehicles and can also #e a client of multiple dealers c. Each vehicle can #elong to only one dealer or one customer. )urther instructions: +or the 2ui>1 please provide( screenshots for A77 records in each ta#le1 and try to fit D ta#les in a page screenshot for A77 relationships in the Relationship !indo! 0lease sa+e car.mdb (or use in (uture labs. "o do a screenshot capture1 open the !indo! !hich you !ant to capture1 ma3imi>e it &or si>e it appropriately* and the do AltG0rint Screen. "hen use the 0aste command in your !ord processing soft!are to import the image into your document.

11

You might also like