Académique Documents
Professionnel Documents
Culture Documents
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
)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.
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.
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.
<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.
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.
(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.
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.
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.
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