Microsoft Access 2013 ® ® Tutorial 2 Building a Database and Defining Table Relationships
Microsoft Access 2013® ®
Tutorial2BuildingaDatabase
andDefiningTableRelationships
XPXPXPObjectives• Session2.1– Learntheguidelinesfordesigningdatabasesandsettingfieldproperties
– CreateatableinDesignview– Definefields,setfieldproperties,andspecifyatable’sprimarykey
– Modifythestructureofatable– ChangetheorderoffieldsinDesignview– AddnewfieldsinDesignview– ChangetheFormatpropertyforafieldinDatasheetview– ModifyfieldpropertiesinDesignview
NewPerspectivesonMicrosoftAccess2013 2
XPXPXPObjectives(Cont.)• Session2.2– ImportdatafromanExcelworksheet– Createatablebyimportinganexistingtablestructure– AddfieldstoatablewiththeDataTypegallery– Deleteandrenamefields– ChangethedatatypeforafieldinDesignview– SettheDefaultValuepropertyforafield– Adddatatoatablebyimportingatextfile– Definearelationshipbetweentwotables
NewPerspectivesonMicrosoftAccess2013 3
XPXPXPGuidelinesforDatabaseDesign• Case- ChathamCommunityHealthServices– Databasecurrentlycontainsonetable(Visittable)– Userwantstotrackinformationabouttheclinic’spatientsandtheinvoicessenttothemforservicesprovided• Thisinformationincludessuchitemsaseachpatient’snameandaddress,andtheamountandbillingdateforeachinvoice
– Createtwonewtables—namedBillingandPatient—tocontaintheadditionaldatatheuserwantstotrack
NewPerspectivesonMicrosoftAccess2013 4
XPXPXPGuidelinesforDatabaseDesign(Cont.)• Case- ChathamCommunityHealthServices– Afteraddingrecordstothetables,youwilldefinethenecessaryrelationshipsbetweenthetablesintheChathamdatabasetorelatethetables
NewPerspectivesonMicrosoftAccess2013 5
XPXPXPGuidelinesforDatabaseDesign(Cont.)
NewPerspectivesonMicrosoftAccess2013 6
XPXPXPGuidelinesforDatabaseDesign(Cont.)
NewPerspectivesonMicrosoftAccess2013 7
XPXPXPGuidelinesforDatabaseDesign(Cont.)• Identifyallthefieldsneeded
toproducetherequiredinformation
• Organizeeachpieceofdataintoitssmallestusefulpart
• Grouprelatedfieldsintotables• Determineeachtable’s
primarykey• Includeacommonfieldin
relatedtables• Avoiddataredundancy
NewPerspectivesonMicrosoftAccess2013 8
XPXPXPGuidelinesforDatabaseDesign(Cont.)
NewPerspectivesonMicrosoftAccess2013 9
XPXPXPGuidelines forSettingFieldProperties
• NamingFieldsandObjects– Youmustnameeachfield,table,andotherobjectinanAccessdatabase
• AssigningFieldDataTypes– Eachfieldmusthaveadatatype
• DatatypesareassignedautomaticallybyAccessorspecificallybythetabledesigner
• Thedatatypedetermineswhatfieldvaluesyoucanenter forthefieldandwhatotherproperties thefieldwillhave
NewPerspectivesonMicrosoftAccess2013 10
XPXPXPGuidelines forSettingFieldProperties (Cont.)
NewPerspectivesonMicrosoftAccess2013 11
XPXPXPGuidelines forSettingFieldProperties (Cont.)
• SettingFieldSizes– TheFieldSizepropertydefinesafieldvalue’smaximumstoragesizeforShortText,Number,andAutoNumberfieldsonly
– TheotherdatatypeshavenoFieldSizepropertybecausetheirstoragesizeiseitherafixed,predeterminedamountorisdeterminedautomaticallybythefieldvalueitself
• SettingtheCaptionPropertyforFields– TheCaptionpropertyforafieldspecifieshowthefieldnameisdisplayedindatabaseobjects
– Ifyoudon’tsettheCaptionproperty,Accessdisplaysthefieldnameasthecolumnheadingorlabelforafield
NewPerspectivesonMicrosoftAccess2013 12
XPXPXP
• CreatingatableinDesignviewinvolvesenteringthefieldnamesanddefiningthepropertiesforthefields,specifyingaprimarykeyforthetable,andthensavingthetablestructure
• DefiningFields– Whenyoufirstcreate atableinDesignview, theinsertionpointis
located inthefirst row’sFieldNamebox,ready foryoutobegindefining thefirstfieldinthetable
– Entervalues fortheFieldName,DataType,andDescription fieldproperties, andthenselectvalues forallotherfieldproperties intheFieldProperties pane
– Theseotherproperties willappearwhenyoumovetothefirstrow’sDataTypebox
CreatingaTableinDesignView
NewPerspectivesonMicrosoftAccess2013 13
XPXPXPCreatingaTableinDesignView(Cont.)
NewPerspectivesonMicrosoftAccess2013 14
XPXPXPCreatingaTableinDesignView(Cont.)
NewPerspectivesonMicrosoftAccess2013 15
XPXPXPCreatingaTableinDesignView(Cont.)
NewPerspectivesonMicrosoftAccess2013 16
XPXPXPCreatingaTableinDesignView(Cont.)
NewPerspectivesonMicrosoftAccess2013 17
XPXPXPCreatingaTableinDesignView(Cont.)
NewPerspectivesonMicrosoftAccess2013 18
XPXPXP
• SpecifyingthePrimaryKey– Aprimarykeyuniquelyidentifies eachrecord inatable– Accessdoesnotallowduplicatevalues intheprimarykeyfield– Whenaprimarykeyhasbeenspecified, Access forces youtoentera
value fortheprimarykeyfieldinevery record inthetable(entityintegrity)
– Youcanenter records inanyorder,butAccessdisplaysthembydefaultinorderoftheprimarykey’sfieldvalues
– Access responds faster toyourrequests forspecific recordsbasedontheprimarykey
• SavingtheTableStructure– Thelaststepin
creatingatableistonamethetableandsave thetable’s structure
CreatingaTableinDesignView(Cont.)
NewPerspectivesonMicrosoftAccess2013 19
XPXPXPModifyingtheStructureofanAccessTable
• MovingaFieldinDesignView– Tomoveafield,youusethemousetodragittoanewlocationintheTableDesigngrid• YoucanmoveafieldinDatasheet viewbydraggingitscolumnheadingtoanewlocation,doingsorearranges onlythedisplayofthetable’s fields;thetablestructure isnotchanged
• Tomoveafieldpermanently,move thefieldinDesignview
• AddingaFieldinDesignView– Toaddanewfieldbetweenexistingfields,youmustinsertarow
– Beginbyselectingtherowbelowwhereyouwantthenewfieldtobeinserted
NewPerspectivesonMicrosoftAccess2013 20
XPXPXPCreatingaTableinDesignView(Cont.)
NewPerspectivesonMicrosoftAccess2013 21
XPXPXPCreatingaTableinDesignView(Cont.)
NewPerspectivesonMicrosoftAccess2013 22
XPXPXPModifyingFieldProperties
• ChangingtheFormatPropertyinDatasheetView– TheFormattinggroupontheFIELDStabinDatasheetviewallowsyoutomodifyformattingforcertainfieldtypes
– Whenyouformatafield,youchangethewaydataisdisplayed,butnottheactualvaluesstoredinthetable
NewPerspectivesonMicrosoftAccess2013 23
XPXPXPModifyingFieldProperties(Cont.)
• ChangingPropertiesinDesignView– EachoftheShortTextfieldshasthedefaultfieldsizeof255,whichistoolargeforthedatacontainedinthesefields
NewPerspectivesonMicrosoftAccess2013 24
XPXPXPUnderstandingTableRelationships
NewPerspectivesonMicrosoftAccess2013 25
XPXPXP
• AddingRecordstoaNewTable• TheBillingtabledesigniscompleteandyouwouldliketo
addrecordstothetablesoitwillcontaintheinvoicedata• AddrecordstoatableinDatasheetviewbytypingthe
fieldvaluesintherowsbelowthecolumnheadingsforthefields
AddingRecordstoaNewTable
NewPerspectivesonMicrosoftAccess2013 26
XPXPXPAddingRecordstoaNewTable(Cont.)
NewPerspectivesonMicrosoftAccess2013 27
XPXPXPImportingDatafromanExcelWorksheet
NewPerspectivesonMicrosoftAccess2013 28
• WhendatayouwanttoaddtoanAccesstableexistsinanotherfile-- likeWordorExcel-- youcanbringthedatafromotherfilesintoAccessindifferentways• Copyandpastethedatafromanopenfile• Importthedata,whichisaprocessthatallows
youtocopythedatafromasourcewithouthavingtoopenthesourcefile
XPXPXPImportingDatafromanExcelWorksheet(Cont.)
NewPerspectivesonMicrosoftAccess2013 29
XPXPXPImportingDatafromanExcelWorksheet(Cont.)
NewPerspectivesonMicrosoftAccess2013 30
XPXPXPImportingDatafromanExcelWorksheet(Cont.)
NewPerspectivesonMicrosoftAccess2013 31
XPXPXPCreatingaTablebyImportinganExistingTableStructure
NewPerspectivesonMicrosoftAccess2013 32
• IfanotherAccessdatabasecontainsatable—orevenjustthedesign,orstructure,ofatable—thatyouwanttoincludeinyourdatabase,youcanimportthetableandanyrecordsitcontainsorimportonlythetablestructureintoyourdatabase
XPXPXPCreatingaTablebyImportinganExistingTableStructure(Cont.)
NewPerspectivesonMicrosoftAccess2013 33
XPXPXPAddingFieldstoaTableUsingtheDataTypeGallery
NewPerspectivesonMicrosoftAccess2013 34
• TheDataTypegallery(intheAdd&DeletegroupontheFIELDStab)allowsyoutoaddagroupofrelated
fieldstoatableatthesametime,ratherthanaddingeachfieldtothetableindividually
XPXPXPModifyingtheImportedTable
NewPerspectivesonMicrosoftAccess2013 35
• DeletingFieldsfromaTableStructure– Afteryou’vecreatedatable,youmightneedtodeleteoneormorefields (whichalsodeletesallthevaluesforthatfieldfromthetable)
– Beforeyoudeleteafield,youshouldmakesurethatyouwanttodosoandthatyouchoosethecorrectfieldtodelete
– FieldscanbedeletedineitherDatasheetvieworDesignview
XPXPXPModifyingtheImportedTable(Cont.)
NewPerspectivesonMicrosoftAccess2013 36
• RenamingFieldsinDesignView– TomatchthedesignforthePatienttable,youneedtorenametheStateProvinceandZIPPostalfields
– FieldscanberenamedinDatasheetvieworDesignview
XPXPXPModifyingtheImportedTable(Cont.)
NewPerspectivesonMicrosoftAccess2013 37
• ChangingtheDataTypeforaFieldinDesignView– AllofthefieldsinthePatienttable,exceptBirthDate,shouldbeShortTextfields
– ThetablestructureyouimportedspecifiestheNumberdatatypeforthePhonefield-- itshouldbeShortText
– TheDataTypecanbechangedinDatasheetvieworDesignview
XPXPXPSettingtheDefaultValuePropertyforaField
NewPerspectivesonMicrosoftAccess2013 38
• TheDefaultValuepropertyforafieldspecifieswhatvaluewillappear,bydefault,forthefieldineachnewrecordyouaddtoatable
XPXPXPAddingDatatoaTablebyImportingaTextFile
NewPerspectivesonMicrosoftAccess2013 39
• ManywaystoimportdataintoanAccessdatabase– ImportinganExcelspreadsheet– Createdanewtablebyimportingthestructureofanexistingtable
– Importdatacontainedintextfiles
XPXPXPDefiningTableRelationships
NewPerspectivesonMicrosoftAccess2013 40
XPXPXPDefiningTableRelationships(Cont.)
NewPerspectivesonMicrosoftAccess2013 41
• One-to-ManyRelationships– Aone-to-manyrelationshipexistsbetweentwotableswhenonerecordinthefirsttablematcheszero,one,ormanyrecordsinthesecondtable,andwhenonerecordinthesecondtablematchesatmostonerecordinthefirsttable
• ReferentialIntegrity– AsetofrulesthatAccessenforcestomaintainconsistencybetweenrelatedtableswhenyouupdatedatainadatabase
XPXPXPDefiningTableRelationships(Cont.)
NewPerspectivesonMicrosoftAccess2013 42
• ReferentialIntegrity– Whenyouaddarecord toarelated table,amatchingrecordmust
alreadyexist intheprimarytable,preventing thepossibilityoforphaned records
– Ifyouattempttochangethevalueoftheprimarykeyintheprimarytable,Accessprevents thischangeifmatchingrecords existinarelatedtable
– WiththeCascadeUpdateRelatedFields option,Accesspermits thechangeinvalue totheprimarykeyandchangestheappropriateforeignkeyvalues intherelated table
– Ifyouattempttodeletearecord intheprimarytable,Accesspreventsthedeletion ifmatchingrecords existinarelated table.However, ifyouchoose theCascadeDeleteRelatedRecordsoption,Accessdeletestherecord intheprimarytableandalsodeletes allrecords inrelatedtables thathavematchingforeignkeyvalues
XPXPXPDefiningTableRelationships (Cont.)
NewPerspectivesonMicrosoftAccess2013 43