Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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(
Access Lab 1
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*.
Access Lab 1
)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.
Access Lab 1
4.
)ig. 9 Sample 3Sho. ,able2 Dialog 6o: $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
Access Lab 1
most cases1 you drag the primary /ey1 field &#efore !hich a -ey sign is displayed* 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 Replication ID. 4. $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. ?. Repeat steps 4 through @ for each pair of ta#les you !ant to relate.
)ig. ;: 5reating a relationship bet.een ' tables &otes ;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.
Primary key: one or more fields whose value or values uniquely identify each record in a table. In a relationship a primary key is used to refer to specific records in one table from another table.
2
!orei"n key: one or more table fields that refer to the primary key field or fields in another table. # forei"n key indicates how the tables are related. $he data in the forei"n key and primary key fields must match. !or e%ample the Products table in the &orthwind sample database contains the forei"n key 'upplierI( which refers to the 'upplierI( primary key of the 'uppliers table. )sin" this relationship the Products table displays a supplier name from the 'uppliers table for each product.
3
(ata type: the attribute of a variable or field that determines what kind of data it can hold. !or e%ample the $e%t and *emo field data types allow the field to store either te%t or numbers but the &umber data type will allow only numbers to be stored in the field. &umber data type fields store numerical data that will be used in mathematical calculations. )se the +urrency data type to display or calculate currency values. 'upported data types include field data types ,isual -asic data types and query parameter data types.
Access Lab 1
If you need to vie! all the relationships defined in the data#ase1 clic- Design Sho. All Relationships on the Ri##on. "o vie! only the relationships defined for a particular ta#le1 clic- the ta#le1 and then clic- Sho. Direct Relationships on the Ri##on. 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- ,able Design. ;ou can create relationships using 2ueries as !ell as ta#les. Ao!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.
/eferential inte"rity: /ules that you follow to preserve the defined relationships between tables when you enter or delete records. If you enforce referential inte"rity *icrosoft #ccess prevents you from addin" records to a related table when there is no associated record in the primary table chan"in" values in the primary table that would result in orphan records in a related table and deletin" records from the primary table when there are matchin" related records in a related table.
Access Lab 1
!orm: a *icrosoft #ccess database ob1ect on which you place controls for takin" actions or for enterin" displayin" and enterin" data in fields.
Access Lab 1
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 Employee field in the "rders 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 ;ou can add a ne. 7oo-up field in either design vie! or datasheet vie! of ta#les. Ao!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 an Employees ta#le filled !ith data1 no! you !ant to create an "rders ta#le containing an EmployeeID field !hose contents should come from the Employees ta#le. ;ou can ma-e
Access Lab 1
the data entry simpler for this #y changing it to a 7oo-up field1 so that a drop%do!n list containing all employee names from Employees ta#le !ill #e availa#le !hen you start to enter data into $rders ta#le. In order to do this1 you must open the "rders ta#le in Design +ie. to change EmployeeID to a 7oo-up field. Aere are the steps to follo!( '. $pen the ta#le in Design +ie.. .. Do one of the follo!ing( If the EmployeeID field is not defined yet1 right clic- in the ro! #elo! !here you !ant to add the field1 and then clic- InsertRo.s on the dropdo!n menu1 or to add a ne! field at the end of the ta#le1 clic- in the first #lan- ro!. "ype EmployeeID in the +ield )ame column1 follo!ing Microsoft Access o#0ect%naming rules. If the EmployeeID 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 clic- on the Loo/up ta# in the )ield 0roperties !indo! &)ig. 1'*.
)ig. 1': 0roperties Listed in the Loo/up ,ab in the )ield 0roperties indo. &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#le1 !hich has )irst&ame1 Last&ame and Report,o fields1 the Reports,o field can #e created as 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 EmployeeID values in the Employees ta#le and #y displaying the corresponding Employee names. :ic-ing a value from a Loo/up list sets the (oreign /ey value in the current record &EmployeeID in the Employees ta#le* to the primary /ey value of the corresponding record in the original ta#le &EmployeeID in the Employees ta#le*. "his creates an association to the related ta#le to display &#ut )$" store* the Employee names in the record. "he foreign -ey & EmployeeID* is stored #ut is not displayed. +or this reason1 any updates made to the data in the Employees ta#le !ill #e reflected in #oth the list and records in the
0
4ookup list field and form: once you5ve created a 4ookup list field if you add the field to a form *icrosoft #ccess copies its definition into the form. 6ou won5t have to create the combo or list bo% and its 4ookup or value list definition for the form. 7owever if you chan"e the definition of a 4ookup or value list field in the table after addin" it to a form those chan"es will &8$ be reflected in that form. $o correct this delete the field from the form and then add it a"ain.
Access Lab 1
"rders 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 "rders ta#le.
Fui< 3
'. Create a data#ase car1 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( "a#le'( Customer( CustomerID1 Customer)ame1 City1 Discount "a#le.( GehicleInstance( GehicleID1 Gehicle)ame1 :rice1 Discount "a#le D( 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:
2
,alue list field and form: once you5ve created the field if you add it to a form *icrosoft #ccess copies its definition into the form. 6ou won5t have to create the value list definition for the form. 7owever if you chan"e the definition of the value list field in the table after addin" it to a form those chan"es will not be reflected in that form. $o correct this delete the field from the form and then add it a"ain.
1:
Access Lab 1
+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
Access Lab 1
Objectives: #. 4aunch *icrosoft #ccess and create a new database -. )nderstand *icrosoft #ccess environment +. +reate tables and insert data A. Launch Microsoft Access and Create New Database 1. 4aunch *icrosoft #ccess usin" the Start menu 2. 'elect Blan Access database and click O! ;8f course in the future when you want to open an e%istin" database you should choose open e%istin" database< 3. +han"e the file name to "#first."db or any other name you want and click +reate 4. +on"ratulations= 6ou>ve 1ust created your first #ccess database when you see the window below: B. $nderstand Microsoft Access %nviron"ent 1. ?%plore the menu 2. ?%plain key ob1ects: tables queries forms reports and macros 3. )nderstand 8pen view ;to run an ob1ect< and (esi"n view ;to desi"n an ob1ect< C. Create &ables and 'nsert Data @e are "oin" to create a relation database accordin" to the lo"ical schema below. -ut to save time we will 1ust create the Employee Department Dept_Locations and Project tables and their relationship.
?mployee !&ame *init 4&ame -(ate #ddress 'e% 'alary (ep&o
Access Lab 1
2. $ype DNa"e as the !ield &ame and choose &e)t as the (ata $ype 3. In the Ceneral tab a. +han"e !ield 'iDe to 1* b. 'elect +es for /equired ;&ot &ull< c. 'elect +es ,No Du-licates. for Inde%ed ;)nique< 4. (o the similar for the other three attributes in the (epartment table accordin" to the requirements below. Attribute Data &#-e /ri"ar# 0orei(n Constraint (epartment &ame ,#/+7#/;1.< )&IE)? &8$ &)44 (epartment &umber I&$ &8$ &)44 *ana"er ''& +7#/;9< &8$ &)44 *ana"e 'tart (ate (#$? .. /i"htBclick the De-No field and select /ri"ar# !e# 0. Save the table as De-art"ent and close the table. 2. +reate the deptAlocations table similar accordin" the requirements below. Attribute Data &#-e /ri"ar# 0orei(n Constraint (epartment ;(ep&o< 8& (epartment &umber I&$ &8$ &)44 (?4?$? +#'+#(? (epartment 4ocation ,#/+7#/;1.< &8$ &)44 3. +reate relationship between the two tables. a. +lick 1elationshi-s under the $ool menu b. 'elect the two tables click Add and then Close c. 'elect De-No in the (eptA4ocations table hold the mouse left key dra( it onto the De-No in the (epartment table and release the left key. d. +heck %nforce 1eferential 'nte(rit# e. +heck Cascade Delete 1elated 1ecords f. +lick Create and you should be able to the window below
9. (oubleBclick to open the (epartment table enter the followin" records: &able Na"e: De-art"ent (&ame (ep&o *"r''& *"r(ate *anufacture #dministration 1 2 33300.... .43210239 19BF)&B21 :4BF#&B99 13
Access Lab 1
1:. +lose the (epartment table and enter the followin" records for (eptA4ocations table. &able Na"e: De-t2Locations (ep&o (4ocation 1 7ouston 1 +hica"o 2 &ew 6ork 3 'alt 4ake +ity $he system won>t allow you to enter the last record whyG I"nore the last record and carry on. +lose myfirst.dmb. 1. 4aunch *icrosoft #ccess and select HBlan Access databaseH 2. $hen chan"e the !ile &ame to "#odbc."db ;or any other name you want< 3. $o connect an 8racle table to #ccess select H 0ileH then H3et %)ternal DataH and then click on HLin &ablesH 4. # 4ink $able dialo"ue screen will pop up ;see the followin" screenshot<. $o indicate the table that you would like to link is from 8(-+ please select HODBC DatabasesH in the H!iles of $ypeH section and click Lin .. 'elect the Machine Data Source tab and select your data source ;e%ample: is4456<. If the data source if not available refer to the I7ow to setup 8(-+J instruction. 0. )pon clink on IO!J for data source an H8racle 8(-+ (river +onnectH dialo"ue screen will appear and ask you for H$ser Na"eH and H/asswordH for accessin" the 8racle database server. Please enter your user name and password ;when you lo" on to 'E4 Plus @orksheet< and then click on O! to continue.
2. Please select the tables you want to link which are all the tables you created in 4ab 2. /emember to check the HSave -asswordH. -y checkin" H'ave passwordH you are e%empted from reBenterin" your password when attemptin" to connect 8racle tables to #ccess databases in the future. +lick O! 3. 6ou should be able to see the new tables with H?arthBlikeH icon in the $ables section ;see the followin" screenshot<. If so you have successfully connected a table on an 8racle database server to the #ccess database environment. 9. (ouble click on one table you have 1ust linked and you should be able to see all the data ;records< stored in that table ;as shown in followin" screenshot<.
14
Access Lab 1
0. $se 7uer# b# %)a"-le 1. 4ist department number department name and number of employees in each department ordered by number of employees in each department. @e have done this in 8racle 'E4 Plus in #ssi"nment .. &ow let>s try it usin" *icrosoft #ccess E-?. 2. 'elect 7ueries under 8b1ects and double click Create 8uer# in Desi(n view 3. 'elect the De-art"ent and %"-lo#ee table and click Add 4. (esi"n the query as shown below. &ote: you can click the si"n to show or not to show the a""re"ate functionality &otal
1.
Access Lab 1
.. +lick the 9 si"n to run the query you should see the result below. D%/NO DNAM% SSNOfCount 1 2 3 4 *anufacture #dministration 7eadquarter !inance 1 1 1 3
. /esearch 4 0. 6ou can save this query as counte"-lo#ee for future use and you should see this query under the Eueries tab. 2. 4et>s do another query list the name of employees who both lives ;address< and works ;department location< in 'alt 4ake +ity. 3. 'elect 7ueries under 8b1ects and double click Create 8uer# in Desi(n view 9. 'elect the De-t2Locations and %"-lo#ee table and click Add. 1:. 6ou will find unlike the previous e%ample there is no connection between those two tables.
10
Access Lab 1
13. (ra" the D%/NO attribute in the (eptAlocations table unto the D%/NO in the ?mployee table to create the 1oin and then desi"n the query as below
12