1. Trang chủ
  2. » Công Nghệ Thông Tin

databases course book

44 202 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 44
Dung lượng 1,33 MB

Nội dung

Databasescoursebook Version4.1(8 October2013) FreeUniversityofBolzanoBozen–PaoloColetti Introduction This book contains the relational databases and Access course’s lessons held at the Free University of Bolzano Bozen. The book is divided into levels, the level is indicated between parenthesis after each section’stitle:  studentsofInformationSystemsandDataManagement3creditscourseuselevel1;  studentsofInformationSystemsandDataManagement5creditscourseuselevels1,2and3;  studentsofComputerScienceandInformationProcessingcourseuselevels1,2and3;  studentsofAdvancedDataAnalysiscourseuselevels2and5. This book refers to Microsoft Access 2010, with referrals to 2007 and 2003 in footnotes, to MySQL CommunityServerversion5.5andtoHeidiSQLversion7.0.0. Thisbookisincontinuousdevelopment,pleasetakealookatitsversionnumber,whichmarksimportant changes. Disclaimers This book is designed for novice database designers. It contains simplifications of theory and many technicaldetailsarepurposelyomitted. TableofContents INTRODUCTION 1 TABLEOFCONTENTS 1 1. RELATIONALDATABASES(LEVEL2) 2 1.1. DATABASEINNORMALFORM 2 1.2. RELATIONS 3 1.3. ONETOMANYRELATION 5 1.4. ONETOONERELATION 6 1.5. MANYTOMANYRELATION 7 1.6. FOREIGNKEYWITHSEVERALRELATIONS 9 1.7. REFERENTIALINTEGRITY 10 1.8. TEMPORALVERSUSSTATICDATABASE 11 1.9. NONRELATIONALSTRUCTURES 11 1.10. ENTITYRELATIONSHIPMODEL(LEVEL9) 12 2. MICROSOFTACCESS(LEVEL1) 14 2.1. BASICOPERATIONS 14 2.2. TABLES(LEVEL1) 15 2.3. FORMS(LEVEL3) 18 2.4. QUERIES(LEVEL1) 19 2.5. REPORTS(LEVEL3) 22 3. MYSQL(LEVEL5) 23 3.1. HEIDISQL 23 3.2. INSTALLINGMYSQLSERVER 25 4. SQLLANGUAGEFORMYSQL(LEVEL5) 29 4.1. BASICOPERATIONS 29 4.2. SIMPLESELECTIONQUERIES 29 4.3. INNERJOINS 31 4.4. SUMMARYQUERIES 33 4.5. MODIFYINGRECORDS 34 4.6. EXTERNALDATA 34 4.7. TABLES 35 5. DESIGNINGADATABASE(LEVEL2) 38 5.1. PAPERDIAGRAM 38 5.2. BUILDINGTHETABLES 39 5.3. INSERTINGDATA 41 6. TECHNICALDOCUMENTATION(LEVEL9) 42 6.1. MYFARMEXAMPLE 42 PaoloColettiDatabasescoursebook Page2of44Version4.1(08/10/2013) 1. Relationaldatabases(level2) Thischapterpresentsthebasicideasandmotivationswhichliebehindtheconceptofrelationaldatabase. Readerswithpreviousexperienceinbuildingschemasforrelationaldatabasescanskipthispart. Arelationaldatabaseisdefinedasacollectionoftablesconnectedviarelations.Itisalwaysagoodideato have thistableorganizedinastructuredwasthatiscallednormalform. 1.1. DatabaseinNormalForm The easiest form of database, which can behandled even by Microsoft Excel, is a single table. To be a databaseinnormalform,thetablemustsatisfysomerequisites: 1. the first line contains the headers of the columns, which univocally define the content of the column.Forexample: Studentnumber Name Surname Telephone 2345 Mary Smith 0471234567  2. eachcolumn containsonlywhatisindicatedinitsheader. Forexample,inacolumn with header “telephonenumber”wemaynotputtwonumbersorindicationonthepreferredcallingtime,such asinthesecondrowofthistable: Studentnumber Name Surname Telephone 2345 Mary Smith 0471234567 2348 John McFlurry 0471234567or3378765432   3. eachrowreferstoasingleobject.Forexample,theremaynotbearowwithinformationonseveral objectsoronagroupofobjects,suchasinthesecondrowofthistable: Studentnumber Name Surname Degreecourse 2345 Mary Smith EconomicsandManagement Startingwith5  LogisticsandProductionEngineering    4. rowsareindependent,i.e.nocellhasreferencestootherrows,suchasinthesecondrowofthis table: Studentnumber Name Surname Notes 2345 Mary Smith 2376 John Smith isthebrotherof2345   5. rowsandcolumnsaredisordered,i.e.theirorderisnotimportant.Forexample,thesefourtables arethesameone: Studentnumber Name Surname Studentnumber Name Surname 2345 Mary Smith 2376 John McFlurry 2376 John McFlurry 2345 Mary Smith  Name Studentnumber Surname Surname Studentnumber Name Mary 2345 Smith McFlurry 2376 John John 2376 McFlurry Smith 2345 Mary   Databasescoursebook PaoloColetti  Version4.1(08/10/2013) Page3of44 6. cellsdonotcontainvalueswhichcanbedirectlycalculatedfromcellsofthesamerow,suchasin thelastcolumnofthistable: Studentnumber Name Surname Tax1 st semester Tax2 nd semester Totaltax 2345 Mary Smith 550€ 430€ 980€ 2376 John McFlurry 450€ 0€ 450€     Databaserowsarecalledrecordsanddatabasecolumnsarecalledfields. Singletabledatabasescanbeeasilyhandledbymanyprogramsandbyhumanbeings,evenwhenthetable isverylongorwithmanyfields.Therearehoweversituationsinwhichasingletableisnotanefficientway tohandletheinformation. 1.1.1. Primarykey Eachtableshouldhaveaprimarykey,whichmeansafieldwhosevalueisdifferentforeveryrecord.Many timesprimarykeyhasanaturalcandidate,asforexamplestudentnumberforastudents’table,taxcode foracitizenstable,telephonenumberforatelephonestable.Othertimesagoodprimarykeycandidateis difficulttodetect,forexampleinacars’tablethecarnameisnotaprimarykeysincetherearedifferent seriesanddifferentmotortypesofthesamecar.Inthesecasesitispossibletoaddanextrafield,calledID orsurrogatekey,withaprogressivenumber, tobeusedasprimarykey.Inmanydatabaseprogramsthis progressivenumberishandleddirectlybytheprogramitself. Itis also possibletodefine asprimarykey several fieldstogether, for examplein a people table thefirst nametogetherwiththelastname,togetherwithplaceanddateofbirthformauniquesequenceforevery person. In this case the primary key is also called composite key or compound key. On some database managementprogramshoweverhandlingacompositekeycancreateproblemsandthereforeitisabetter ideatouse,inthiscase,anID. 1.2. Relations 1.2.1. Informationredundancy Insomesituationstryingtoputtheinformationweneedinasingletabledatabasecausesaduplicationof identicaldatawhichcan becalledinformationredundancy.Forexample,ifweaddtoourstudents’table the information on who is the reference secretary for each student, together with other secretary’s informationsuchasofficetelephonenumber,officeroomandtimetables,wegetthistable: Studentnumber Name Surname Secretary Telephone Office Time 2345 Mary Smith AnneBoyce 0471222222 C340 1418 2376 John McFlurry JessyCodd 0471223334 C343 911 2382 Elena Burger JessyCodd 0471223334 C343 911 2391 Sarah Crusa AnneBoyce 0471222222 C340 1418 2393 Bob Fochs JessyCodd 0471223334 C343 911   Informationredundancyisnotaproblembyitself,but:  storing several times the same information is a waste of computer space (hard disk and memory), whichforaverylargetable,hasabadimpactonthesizeofthefileandonthespeedofeverysearch orsortingoperation;  wheneverweneedtoupdatearepeatedinformation(e.g.thesecretarychangesoffice),weneedto doalotofchanges;  manually inserting the same information several times can lead to typing (or copying&pasting) mistakes,whichdecreasethequalityofthedatabase. PaoloColettiDatabasescoursebook Page4of44Version4.1(08/10/2013) Inordertoavoidthissituation,itisacommonproceduretosplitthetableintotwodistincttables,onefor thestudentsandanotheroneforthesecretaries.Toeachsecretaryweassignauniquecodeandtoeach studentweindicatethesecretary’scode. Students Studentnumber Name Surname Secretary 2345 Mary Smith 1 2376 John McFlurry 2 2382 Elena Burger 2 2391 Sarah Crusa 1 2393 Bob Fochs 2   Secretaries Secretarycode Name Surname Telephone Office Time 1 Anne Boyce 0471222222 C340 1418 2 Jessy Codd 0471223334 C343 911   In this way the information on each secretary is written and stored only once and can be updated very easily.Thepriceforthisisthateverytimeweneedtoknowwhoisastudent’ssecretarywehavetolookat its secretary code and find the corresponding code in  the Secretaries table: this can be a long and frustratingprocedureforahumanbeingwhentheSecretariestablehasmanyrecords,butisveryfasttask foracomputerprogramwhichisdesignedtoquicklysearchthroughtables. 1.2.2. Emptyfields Another typical problem which arises with single table databases is the case of many empty fields.For example,ifwewanttobuildanaddressbookwiththetelephonenumbersofallthepeople,wewillhave somebodywithnotelephonenumbers,manypeoplewithafewtelephonenumbers,andsomepeoplewith a lot of telephone numbers. Moreover, we must also take into consideration that new numbers  will probablybeaddedinthefuturetoanybody. Ifwereserveafieldforeverytelephone,thetablelookslikethis: Name Surname Phone1 Phone2 Phone3 Phone4 Phone5 Phone6 Phone7 Mary Smith 0412345   John McFlurry 0412375 3396754  Elena Burger 0412976 3397654 0436754 3376547 0487652 3387655 0463456 Sarah Crusa 0418765 0412345  Bob Fochs 0346789 0765439 3376543      Asitisclear,ifwereserveseveralfieldsforthetelephonenumbers,alotofcellsareempty.Theproblems ofemptycellsare:  anemptycellisawasteofcomputerspace;  thereisafixedlimitoffieldswhichmaybeused.Ifarecordneeds anotherfield(forexample,Elena Burgergetsanothertelephonenumber)theentirestructureofthetablemustbechanged;  since all these fields contain the same type of information, it is difficult to search whether an informationispresentsinceitmustbelookedforineveryfield,including thecellswhichareempty. In order toavoid this situation, we again split the table into two distinct tables, one for the people and anotheronefortheirtelephonenumbers.Thistime,however,weassignauniquecodetoeachpersonand webuildthesecondtablewithcombinationsofpersontelephone. Databasescoursebook PaoloColetti  Version4.1(08/10/2013) Page5of44 People Personcode Name Surname 1 Mary Smith 2 John McFlurry 3 Elena Burger 4 Sarah Crusa 5 Bob Fochs    Telephones Owner Number 1 0412345 2 0412375 2 3396754 3 0412976 3 3397654 3 0436754 3 3376547 3 0487652 3 3387655 3 0463456 4 0418765 4 0412345 5 0346789 5 0765439 5 3376543  Even thoughit seemsstrange, each person’scode appearsseveral timesin the Telephonestable. This is correct, since Telephones table uses the exact amount of records to avoid having empty cells: people appearasmanytimesasmanytelephonestheyhave,andpeoplewithnotelephonedonotappearat all. Thedrawbackisthateverytimewewanttogetto know telephone numberswe haveto gothroughthe entire Telephones table searching for the person’s code, but again this procedure is very fast for an appropriatecomputerprogram. 1.2.3. Foreignkey Whenafield,whichisnottheprimarykey,isusedinarelationwithanothertablethisfieldiscalledforeign key.This field isimportantforthe database managementprogram,such asAccess,whenithas tocheck referentialintegrity(seesection1.6). For example, in the previous examples Owner is a foreign key for Telephones table and Secretary is a foreignkeyforStudentstable. 1.3. Onetomanyrelation ArelationisaconnectionbetweenafieldoftableA(whichbecomesaforeignkey)andtheprimarykeyof tableB:ontheBsidetherelationis“1”,meaningthatforeachrecordoftableAthereisoneandonlyone corresponding record of table B, while on the A side the relation is “many” (indicated with the mathematical symbol) meaning that for each record of table B there can be none, one or more correspondingrecordsintableA. Fortheexampleofsection1.2.1,thetablesareindicatedinthisway,meaningthatfor eachstudentthereis exactlyonesecretaryandforeachsecretarytherearemanystudents.Thisrelationiscalledmanytoone relation. PaoloColettiDatabasescoursebook Page6of44Version4.1(08/10/2013)  Fortheexampleofsection1.2.2,thetablesareinsteadindicatedinthisway,meaningthatforeachperson therecanbenone,oneorseveraltelephonenumbersandforeachnumberthereisonlyonecorresponding owner. Thisrelationiscalledonetomanyrelation.  Clearlyonetomanyandmanytoonearethesamerelation,theonlydifferencebeingtheorderofdrawn tables. It is however very important to correctly identify the “1” side, since it has several implications on the correct working of the database. For example, in the previous example putting the “1” side on the Telephones table means that for each person there is only one telephone and that for each telephone therearemanypeople,asituationwhichispossibleuptothe90s,whentherewasonlyonetelephonefor awholefamilyusedbyallitscomponents, butwhichisnotwhatwewanttodescribewiththecurrent21 st  century’sdatabase.Moreover,reversingtherelationalsoneedtochangealittlethestructureofthetables, puttingtheforeignkeyTelephoneinthePeopletableinsteadoftheforeignkeyPersonintheTelephones table,suchas  1.4. Onetoonerelation Aonetoonerelationisadirectconnectionbetweentwoprimarykeys.Eachrecordofthefirsttablehas exactly onecorresponding record in the second table and vice versa. An example can be countries and nationalflags.Thisrelationcansometimesbeusefultoseparateintwotablestwoconceptuallydifferent objectswithalotoffields,butitshouldbeavoided,sincethetwotablescanbeeasilyjoinedtogetherina singletable.  1 Students Studentnumber Name Surname Secretary Secretaries ID Name Surname Telephone Office Time 1  People Personcode Name Surname Telephones Owner Number  1 People Personcode Name Surname Telephone Telephones Number Databases c  Version4.1 1.5. Ma Even thou g handled a u junctionta b tomany r e othertime s ameaningf u havealway s Forexampl (withdiffer dates),and manytom a “Whatiso w Each owne r refertotha Thisisthet the relatio n competitio n c oursebook (08/10/201 3 a n y tom a g h a manyt o u tomatically b le,whichis e lated; som e s itisonlya n u lnametot s clearlyin m e,webuild entpercent a ontheoth e a ny relation w nedbywh o r  can theref thouse.On ypicalstruc t n . An exam n swithCar T Countrie s Name Size Populati o Continen t Houses Address Square m Height Construc t 3 )  a nyrela t o manyrela t by relation a anextrata b e times this j n abstractre hejunction t m inditsmea n adatabase w a gesor,if w e rhandeac h  betweenh o m”or“Wh o ore have m a theotherh a t ureofthej u ple where t T ypes,Tires, 1 s  o n t  1 m eters t ionyear t ion t ionis very c a l database s b lewiththe unction tab presentatio n t able,often n ing. w ithhouses w earebuildi n h personma y o uses and o o ownswhat ” a ny propert y a ndeachpr o u nctiontabl e t he junctio n Races,Driv e 1 1  c ommonin s . In order t taskofcon n le has an c o n oftherela t usingaque s s andowner s n ganhistor i y ownseve r o wners we u ”or,usinga y  acts and e o pertyacth a e :itcontains n  table con t e rs. Flags ID Shape Picture Property Actnum b Percenta House Owner Beginda t Enddate  realapplica t t o deal wit h n ectingtoge o rrespondin g t ion.Inany c s tionforms u s .Eachhou s i caldatabas e r alportions o u se a juncti o moretangi b e ach house c a swritteno n twoormor ains four f o Acts b er ge t e t ions,unfor t h  them, rel a t herthetw o g  meaning i c ase,itisal w u chas“Wha t s emaybeo w e ,withdiffe r o fhouses.I n o n table wh i b lename,Pr o c an have m a n itonlyone e foreignke y o reign keys Cou n Na m Size Pop u Con t Flag Flag 1 O w Ta x Na Su r Bir t Bir t P t unately th e a tional data o fieldswhi c i n everyday w aysagood t isownedb wnedbyse v r entstartin g n ordertor e ich can be c opertyActs. a ny propert y ownerand o y sonthe“ m is this dat a n tries m e u lation t inent shape picture w ners x code me r name r thplace r thdate P aoloColett i Page7of4 4  ycannot b e bases use a c haremany  experience , ideatogiv e ywhom”,t o v eralpeopl e g andendin g e presentthi s c alled eithe r  y  actswhic h o nehouse. m any”sideo f a base of ca r i  4  e  a   ,  e  o  e  g  s  r  h  f  r  PaoloColettiDatabasescoursebook Page8of44Version4.1(08/10/2013)   1.5.1. Detailstable Manytimesineverydayapplicationstherelationissocomplicatedthatajunctiontableisnotenough.This is the case, for example, of a selling database, with table Customers and table Products. Clearly each customer may order different products and each products is hopefully ordered by several customers, thereforewe needanOrdersjunctiontable.Thistablecontainsalsoallthedetailsoftheorder,suchasthe amountofproducts,thedateandtheshippingcost.  However,whileitiscorrectthatforeachorderthereisoneandonlyonecustomer,foreachorderthereis alsooneandonlyoneproduct,whichisnotwhat usuallyhappensin real applicationswherea customer ordersseveralproductsatthesametimeandwantsalsotopaythemalltogetherwithcombinedshipping costs. In order to deal with this situation, we need a details table. We leave all the order’s administrative information,includingthecustomerrelation,intheOrderstableandwemovethelistoforderedproducts intothedetailstable,whichwilllookliketheTelephonestableofsection1.2.2.   1   1 1 CarTypes Cartype Brand Enginecc Speed  1 Drivers Taxcode Name Surname Address Tires Tirename Radial Type Width Races Racename Date Length Participants Carplate Cartype Driver Tires Race Racetime Arrivalposition 1 Products Productcode Description UnitPrice Category Weight  1 Customers CustomerID Name Surname Address Orders Ordernumber Date Customer Product Shippingcost Amount   1 1  Products ProductID Description UnitPrice Category Weight  1 Customers CustomerID Name Surname Address Orders OrderID Date CustomerID Shippingcost OrderDetails ID OrderID ProductID Quantity Databasescoursebook PaoloColetti  Version4.1(08/10/2013) Page9of44 EachrecordintheOrderDetailstablerepresentsaproductwhichisorderedwithitsamountandclearlyan ordercanhaveseveraldetails.InthiswayanentireordercanberepresentedtakingfromtheCustomers tabletheinformationonwhoorderedit,fromtheProductsthroughtheOrderDetailstabletheinformation ontheproductsandfromtheOrderstableitselftheadministrativeinformation. Usingqueriesandreports(explainedinsections2.4and2.5forAccess)allthesedatacanbeconveniently put together, taking them from  the tables and automatically joining them following the relations,into a reportlikethisone.  Adetailstableisingeneralusedeverytimethejunctiontable,evenwithseveralforeignkeys,isnotenough todescribetherelation.Insomecasesfurthersubdetailtablesmaybeevennecessary. 1.6. Foreignkeywithseveralrelations Consideradatabasewithpeopleandcompanies.Clearlythesetwoobjectsmustbeintwodifferenttables sincetheyrequiredifferentfields.Ifhoweverweneedtobuildatablecontainingphonesweeitherhaveto buildtwodistincttablesas:  An alternative schema is thefollowing, which uses two relations coming out from the same foreign key field: Orders OrderID,Date  Customers Name,Surname,Address  OrderDetails  Products Product,Description,WeightAmount  Products Product,Description,WeightAmount  Products Product,Description,WeightAmount     Orders Shippingcost People’s phones Number Owner  1 People ID Name Surname Birthdate  1 Companies ID Name Type Administrator Companies’ phones Number Owner PaoloColettiDatabasescoursebook Page10of44Version4.1(08/10/2013)  However this schema creates a technical problem: many database management programs which automatically follow relations, such as Access, do not know whether to follow the first or the second relationin order to findthe phone’sowner’s name.Therefore,if thedatabase designerdoes not havea good experience, it is better to avoid this second schema and to choose, according to the problem, the moreappropriatebetweenthefirstoneorthisthirdone:  fillingintononappropriatefields(suchasPersonsurnameandPersonbirthdatewhen recordrefertstoa company)anemptyvalue,technicallycalledNull. 1.7. Referentialintegrity If two tables are related via a manytoone relation, like the one between students and secretaries of section1.2.1,wearenomorefreetomodifythedataonthe“1”sideatourwill.Forexample,ifwedelete asecretaryofifwechangeitsID,there areprobablycorrespondingstudentsintheStudentstablewhich becomesorphans,i.e.theydonothavetheircorrespondingsecretaryanymoreandfollowingtheirrelation totheSecretariestableleadstoanonexistentID.Thisissueisknownasreferentialintegrity,whichisthe propertyofadatabase tohavealltheforeign key’sdatacorrectlyrelated toprimarykey’sdata.Whena record on the “1” side table is deleted, referential integrity can be broken and this results in a non consistentdatabase.    Secretaries Secretarycode Name Surname Telephone Office Time 1 Anne Boyce 0471222222 C340 1418 2 Jessy Codd 0471223334 C343 911  Phones Number Owner   1 People ID Name Surname Birthdate Companies ID Name Type Administrator 1 Phones Number Owner  1 PeopleCompanies ID Company(yes/no) Name Personsurname Personbirthdate Companytype Companyadministrator  1 Students Studentnumber Name Surname Secretary Secretaries ID Name Surname Telephone Office [...]... hotel booking table it is necessary to have departure dates not before arrival dates and therefore the condition here is [Departure Date] . 0412345 2 0412375 2 33 967 54 3 04129 76 3 339 765 4 3 04 367 54 3 33 765 47 3 048 765 2 3 338 765 5 3 0 463 4 56 4 0418 765  4 0412345 5 03 467 89 5 0 765 439 5 33 765 43  Even thoughit. Phone5 Phone6 Phone7 Mary Smith 0412345   John McFlurry 0412375 33 967 54  Elena Burger 04129 76 339 765 4 04 367 54 33 765 47 048 765 2 338 765 5 0 463 4 56 Sarah Crusa 0418 765  0412345. Databases course book Version4.1(8 October2013) FreeUniversityofBolzanoBozen–PaoloColetti Introduction This book contains the relational databases and Access course s

Ngày đăng: 18/10/2014, 16:08

TỪ KHÓA LIÊN QUAN