Databases Course Book PDF
Databases Course Book PDF
course book
Version 4.1 (24 September 2014)
Free University of Bolzano Bozen Paolo Coletti
Introduction
This book contains the relational databases and Access courses lessons held at the Free University of
Bolzano Bozen. The book is divided into levels, the level is indicated between parentheses after each
sectionstitle:
studentsofInformationSystemsandDataManagement27000courseuselevel1;
studentsofInformationSystemsandDataManagement27006courseuselevels1,2and3;
studentsofComputerScienceandInformationProcessingcourseuselevels1,2and3;
studentsofAdvancedDataAnalysiscourseuselevels2and5.
This book refers to Microsoft Access 2010, with referrals to 2007 and 2003 in footnotes, to MySQL
CommunityServerversion5.5andtoHeidiSQLversion7.0.0.
Thisbookisincontinuousdevelopment,pleasetakealookatitsversionnumber,whichmarksimportant
changes.
Disclaimers
This book is designed for novice database designers. It contains simplifications of theory and many
technicaldetailsarepurposelyomitted.
Table of Contents
INTRODUCTION.......................................................1 2.5. REPORTS(LEVEL3).......................................22
TABLEOFCONTENTS................................................1 3. MYSQL(LEVEL5)............................................23
1. RELATIONALDATABASES(LEVEL2)..................2 3.1. HEIDISQL...................................................23
1.1. DATABASEINNORMALFORM............................2 3.2. INSTALLINGMYSQLSERVER...........................25
1.2. RELATIONS....................................................3 4. SQLLANGUAGEFORMYSQL(LEVEL5)............29
1.3. ONETOMANYRELATION.................................5 4.1. BASICOPERATIONS.......................................29
1.4. ONETOONERELATION....................................6 4.2. SIMPLESELECTIONQUERIES............................29
1.5. MANYTOMANYRELATION...............................7 4.3. INNERJOINS................................................31
1.6. FOREIGNKEYWITHSEVERALRELATIONS...............9 4.4. SUMMARYQUERIES......................................33
1.7. REFERENTIALINTEGRITY.................................10 4.5. MODIFYINGRECORDS....................................34
1.8. TEMPORALVERSUSSTATICDATABASE................11 4.6. EXTERNALDATA...........................................34
1.9. NONRELATIONALSTRUCTURES........................11 4.7. TABLES.......................................................35
1.10. ENTITYRELATIONSHIPMODEL(LEVEL9)............12 5. DESIGNINGADATABASE(LEVEL2).................38
2. MICROSOFTACCESS(LEVEL1)........................14 5.1. PAPERDIAGRAM..........................................38
2.1. BASICOPERATIONS........................................14 5.2. BUILDINGTHETABLES....................................39
2.2. TABLES(LEVEL1)..........................................15 5.3. INSERTINGDATA...........................................41
2.3. FORMS(LEVEL3)..........................................18 6. TECHNICALDOCUMENTATION(LEVEL9)........42
2.4. QUERIES(LEVEL1)........................................19 6.1. MYFARMEXAMPLE.......................................42
PaoloColetti Databasescoursebook
Page2of44 Version4.1(24/09/2014)
Databasescoursebook PaoloColetti
6. cellsdonotcontainvalueswhichcanbedirectlycalculatedfromcellsofthesamerow,suchasin
thelastcolumnofthistable:
Studentnumber Name Surname Tax1stsemester Tax2ndsemester Totaltax
2345 Mary Smith 550 430 980
2376 John McFlurry 450 0 450
Databaserowsarecalledrecordsanddatabasecolumnsarecalledfields.
Singletabledatabasescanbeeasilyhandledbymanyprogramsandbyhumanbeings,evenwhenthetable
isverylongorwithmanyfields.Therearehoweversituationsinwhichasingletableisnotanefficientway
tohandletheinformation.
1.2. Relations
1.2.1. Information redundancy
Insomesituationstryingtoputtheinformationweneedinasingletabledatabasecausesaduplicationof
identicaldatawhichcanbecalledinformationredundancy.Forexample,ifweaddtoourstudentstable
the information on who is the reference secretary for each student, together with other secretarys
informationsuchasofficetelephonenumber,officeroomandtimetables,wegetthistable:
Studentnumber Name Surname Secretary Telephone Office Time
2345 Mary Smith AnneBoyce 0471222222 C340 1418
2376 John McFlurry JessyCodd 0471223334 C343 911
2382 Elena Burger JessyCodd 0471223334 C343 911
2391 Sarah Crusa AnneBoyce 0471222222 C340 1418
2393 Bob Fochs JessyCodd 0471223334 C343 911
Informationredundancyisnotaproblembyitself,but:
storing several times the same information is a waste of computer space (hard disk and memory),
whichforaverylargetable,hasabadimpactonthesizeofthefileandonthespeedofeverysearch
orsortingoperation;
wheneverweneedtoupdatearepeatedinformation(e.g.thesecretarychangesoffice),weneedto
doalotofchanges;
manually inserting the same information several times can lead to typing (or copying&pasting)
mistakes,whichdecreasethequalityofthedatabase.
Version4.1(24/09/2014) Page3of44
PaoloColetti Databasescoursebook
Inordertoavoidthissituation,itisacommonproceduretosplitthetableintotwodistincttables,onefor
thestudentsandanotheroneforthesecretaries.Toeachsecretaryweassignauniquecodeandtoeach
studentweindicatethesecretaryscode.
Students
Studentnumber Name Surname Secretary
2345 Mary Smith 1
2376 John McFlurry 2
2382 Elena Burger 2
2391 Sarah Crusa 1
2393 Bob Fochs 2
Secretaries
Secretarycode Name Surname Telephone Office Time
1 Anne Boyce 0471222222 C340 1418
2 Jessy Codd 0471223334 C343 911
In this way the information on each secretary is written and stored only once and can be updated very
easily.Thepriceforthisisthateverytimeweneedtoknowwhoisastudentssecretarywehavetolookat
its secretary code and find the corresponding code in the Secretaries table: this can be a long and
frustratingprocedureforahumanbeingwhentheSecretariestablehasmanyrecords,butisveryfasttask
foracomputerprogramwhichisdesignedtoquicklysearchthroughtables.
Page4of44 Version4.1(24/09/2014)
Databasescoursebook PaoloColetti
People Telephones
Personcode Name Surname Owner Number
1 Mary Smith 1 0412345
2 John McFlurry 2 0412375
3 Elena Burger 2 3396754
4 Sarah Crusa 3 0412976
5 Bob Fochs 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 persons 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
appearasmanytimesasmanytelephonestheyhave,andpeoplewithnotelephonedonotappearatall.
Thedrawbackisthateverytimewewanttogettoknowtelephonenumberswehavetogothroughthe
entire Telephones table searching for the persons code, but again this procedure is very fast for an
appropriatecomputerprogram.
Version4.1(24/09/2014) Page5of44
PaoloColetti Databasescoursebook
Students Secretaries
Studentnumber 1 ID
Name Name
Surname Surname
Secretary Telephone
Office
Time
Fortheexampleofsection1.2.2,thetablesareinsteadindicatedinthisway,meaningthatforeachperson
therecanbenone,oneorseveraltelephonenumbersandforeachnumberthereisonlyonecorresponding
owner.Thisrelationiscalledonetomanyrelation.
People Telephones
1
Personcode Owner
Name Number
Surname
Clearlyonetomanyandmanytoonearethesamerelation,theonlydifferencebeingtheorderofdrawn
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
therearemanypeople,asituationwhichispossibleuptothe90s,whentherewasonlyonetelephonefor
awholefamilyusedbyallitscomponents,butwhichisnotwhatwewanttodescribewiththecurrent21st
centurysdatabase.Moreover,reversingtherelationalsoneedtochangealittlethestructureofthetables,
puttingtheforeignkeyTelephoneinthePeopletableinsteadoftheforeignkeyPersonintheTelephones
table,suchas
People
Personcode Telephones
1 Number
Name
Surname
Telephone
Page6of44 Version4.1(24/09/2014)
Databasescoursebook PaoloColetti
Countries
Name
Countries Flags Size
Name 1 1 ID
Population
Size Shape Continent
Population Picture Flagshape
Continent Flagpicture
Each owner can therefore have many property acts and each house can have many property acts which
refertothathouse.Ontheotherhandeachpropertyacthaswrittenonitonlyoneownerandonehouse.
Thisisthetypicalstructureofthejunctiontable:itcontainstwoormoreforeignkeysonthemanysideof
the relation. An example where the junction table contains four foreign keys is this database of car
competitionswithCarTypes,Tires,Races,Drivers.
Version4.1(24/09/2014) Page7of44
PaoloColetti Databasescoursebook
Drivers CarTypes
Taxcode 1 1 Cartype
Name Brand
Surname Participants Enginecc
Address Carplate Speed
Cartype
Driver
Tires
Race
Tires 1 1 Races
Racetime
Tirename Racename
Arrivalposition
Radial Date
Type Length
Width
However,whileitiscorrectthatforeachorderthereisoneandonlyonecustomer,foreachorderthereis
alsooneandonlyoneproduct,whichisnotwhatusuallyhappensinrealapplicationswhereacustomer
ordersseveralproductsatthesametimeandwantsalsotopaythemalltogetherwithcombinedshipping
costs.
In order to deal with this situation, we need a details table. We leave all the orders administrative
information,includingthecustomerrelation,intheOrderstableandwemovethelistoforderedproducts
intothedetailstable,whichwilllookliketheTelephonestableofsection1.2.2.
Customers Products
CustomerID 1 Orders OrderDetails 1 ProductID
OrderID 1 ID
Name Description
Date
Surname OrderID UnitPrice
Address CustomerID ProductID Category
Shippingcost Quantity Weight
Page8of44 Version4.1(24/09/2014)
Databasescoursebook PaoloColetti
EachrecordintheOrderDetailstablerepresentsaproductwhichisorderedwithitsamountandclearlyan
ordercanhaveseveraldetails.InthiswayanentireordercanberepresentedtakingfromtheCustomers
tabletheinformationonwhoorderedit,fromtheProductsthroughtheOrderDetailstabletheinformation
ontheproductsandfromtheOrderstableitselftheadministrativeinformation.
Usingqueriesandreports(explainedinsections2.4and2.5forAccess)allthesedatacanbeconveniently
put together, taking them from the tables and automatically joining them following the relations, into a
reportlikethisone.
Orders
OrderID,Date
Customers
Name,Surname,Address
OrderDetails
Products
Product,Description,WeightAmount
Products
Product,Description,WeightAmount
Products
Product,Description,WeightAmount
Orders
Shippingcost
Adetailstableisingeneralusedeverytimethejunctiontable,evenwithseveralforeignkeys,isnotenough
todescribetherelation.Insomecasesfurthersubdetailtablesmaybeevennecessary.
Version4.1(24/09/2014) Page9of44
PaoloColetti Databasescoursebook
Companies People
1 1 ID
ID Phones
Name Number Name
Type Owner Surname
Administrator Birthdate
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 inorderto find the phones owners name.Therefore, if thedatabase designerdoes nothave a
good experience, it is better to avoid this second schema and to choose, according to the problem, the
moreappropriatebetweenthefirstoneorthisthirdone:
PeopleCompanies
Phones 1 ID
Number Company(yes/no)
Owner
Name
Personsurname
Personbirthdate
Companytype
Companyadministrator
filling intononappropriatefields(suchasPersonsurname andPerson birthdatewhenrecordreferstoa
company)anemptyvalue,technicallycalledNull.
Students Secretaries
Studentnumber 1 ID
Name Name
Surname Surname
Secretary Telephone
Office
Secretaries
Secretarycode Name Surname Telephone Office Time
1 Anne Boyce 0471222222 C340 1418
2 Jessy Codd 0471223334 C343 911
Page10of44 Version4.1(24/09/2014)
Databasescoursebook PaoloColetti
Students
Studentnumber Name Surname Secretary
2345 Mary Smith 1 orphan
2376 John McFlurry 2
2382 Elena Burger 2
2391 Sarah Crusa 1 orphan
2393 Bob Fochs 2
Ontheotherhand,ifatablehasonlymanysiderelations,itsrecordscanbefreelydeletedandmodified
withoutbreakingthereferentialintegrity.
Somedatabasemanagementprogram,asAccess,correctlycheckreferentialintegrityifinstructedtodoso
andforbidsdangerousoperations.Others,asMySQLuptoversion5.6,doesnotcheckandwemusttake
specialcarewhendeletingrecords.
People
President Personcode Roles
1 Rolename
Name
Surname Dependson
Director1 Director2 Director3 Role
Version4.1(24/09/2014) Page11of44
PaoloColetti Databasescoursebook
1.9.2. Process
Arelationaldatabasecannotmodelaprocess,suchasasequenceofproductionstepsoractivitiesstatuses.
Again processes can be somehow modeled by a relationaldatabasemimickingthe sequencewitha field
statusandwitharelationissubsequentof.
Products
Productcode Stages
First Second Third 1 Stagename
Entrydate
stage stage stage Description Follows
Status
1
P.P.Chen,TheEntityRelationshipModel:TowardaUnifiedViewofData,ACMTransactionsonDatabaseSystems,
1976,vol.1,pp.936.
Page12of44 Version4.1(24/09/2014)
Databasescoursebook PaoloColetti
St number ID
is
Secretaries
Name Student assigned Name
to
Surname Surname
1.10.1. Enhanced entityrelationship model
Anenhancedentityrelationshipsmodelisanimprovementwhichpermitsalsotheexistenceofsubclass,an
entity which inherits allthe attributes and relationshipsof anotherentity called superclass,adding some
extraattributesandrelationshipsofitsown.Forexample,adatabasezoocanhaveentityanimalswith
attributesscientificnameandcommonname,whichcanhaveassubclasstheentitybirdswiththe
sametwoattributesandtheextraoneaveragewingspan.Thispermitsthemodelingofsomehierarchical
structures(seesection1.9.1onpage11)andsolvestheproblemofforeignkeyswithseveralrelations(see
section1.6onpage9).
Version4.1(24/09/2014) Page13of44
PaoloColetti Databasescoursebook
2
ForAccess2007OfficebuttonSave.
3
InAccess2007OfficebuttonSaveAsSavethedatabaseinanotherformatOfficebuttonManageBackUp
Database,whileinMicrosoftAccess2003thereisonlytheoptionFileBackupDatabase.
4
InAccess2003itisafloatingwindowwiththeobjecttypesmenuontheleftandAllAccessObjectsdoesnotneedto
beselected.
5
Also Access 2007 and 2010 have a Northwind 2007 database, but it must be downloaded or generated from a
templatefile.Moreover, it has a much morecomplicated structure which can misleadthe novice user. Office 2007
usersshouldcopytheNorthwind.mdbfilefromanOffice2003systeminC:\MicrosoftOffice\Office11\Samples,or,ifa
computerwithOffice2003isnotavailable,searchforthisfileontheInternetwiththehelpofasearchengine.
6
WithAccess2007clickonOptionsEnablethisContent,whileAccess2003displaysinsteadthreepopupwindows
to which No then Yes then Open should be answered; they can be permanently disabled choosing Tools
MacroSecurityLow.
7
Access2003displaystwosplashwindows.
Page14of44 Version4.1(24/09/2014)
Databasescoursebook PaoloColetti
8
ForAccess2007DatabaseToolsShow/HideRelationships,forAccess2003FileRelationships.
9
ForAccess2003insteadchooseCreatetableinDesignViewfromthemaindatabasewindow.
10
ForAccess2003ViewDesignView.
Version4.1(24/09/2014) Page15of44
PaoloColetti Databasescoursebook
Text, which contains up to 255 alphanumeric characters. This type is proper for names, addresses
andeveryshorttext;
Memo,whichcontainsupto65,536alphanumericcharacters.Thistypeisproperforlongtexts,such
ascurricula,abstractsandsmallarticles;
Number,whichcontainsnumberswhichcanbemanipulatedthroughmathematicaloperations.This
typemustbeusedonlyfornumericalinformation.Itisaverycommonmistaketouseitfornumeric
codes,suchastelephonenumbers,versionnumbersandZIPcodes.Numericcodesmustusethetext
type,sincetheymaystartwith0(telephone0471012343orZIP00100)orhavea0aslastdecimal
digit(version7.10)and,inanycase,mathematicaloperationswiththemmustbeforbidden;
Date/Time, which contains dates and times. Exactly like Excel, Access memorizes date and time
together,usingintegernumbersfordays.Thereforedatescanbesubtractedtoobtainthedifference
in days, or numbers can be added or subtracted to them to go ahead in the future or back in the
past;
Currency,whichcontainsnumberswithautomaticallyacurrencysymbol;
Autonumber,atypewhichisusedonlybyAccesstocreateIDs;
Yes/No,atypewithonlytwovalues;
OLEobject,whichcontainsotherfiles,suchasimagesordocuments.Thesefilesmaybeembedded,
whichmeans that the database file automaticallycontainsaduplicateofthis file(thus making the
databasefilelarger)orlinked,whichmeanthatthedatabasefilesimplycontainsthelinktothefile
(thusmakingitunusablewhentheexternalfileisnotavailable);
Hyperlink,whichcontainsanhyperlinkusuallytoawebpageortoanemailaddress;
LookupWizardisnotarealfieldtypebutthepossibilitytotakefieldsvaluesfromapredetermined
listorfromothertables.
11
For Access 2003 and 2007:this option does not appear in the last screen and thus you should add manually the
referentialintegrityfromtherelationshipsdiagram(seesection3.3).
12
ForAccess2003and2007:thisoptionisnotavailable,mandatorylistsmustbebuiltonlyusinganothertable.
Page16of44 Version4.1(24/09/2014)
Databasescoursebook PaoloColetti
Products
Code Categories
1 Categoryname
Name
Price
Category
With this solution the user may not choose values which are not in the list; however, he can add other
values to the list simply adding more records to the second table, without having to modify the fields
featuresinthefirsttable.
13
ForAccess2003ViewProperties.
Version4.1(24/09/2014) Page17of44
PaoloColetti Databasescoursebook
Ifmorerulesareneeded,theymustbecombinedwiththeAndoperator,forexample
( [Departure Date] <= [Arrival Date] ) And ( [Booking Date] <= [Arrival Date] ).
Unfortunatelythevalidationtextisonlyoneanditisnotpossibletotelltheuserexactlytowhichpartof
therulehisdatadonotadhere.
MorecomplexexpressionscanbebuiltwiththeExpressionBuilder(seesection2.4.2).
14
ForAccess2007ExternalDataImportExcel,forAccess2003FileExternaldataImport.
For Access 2007 Create Forms Other Forms Create using Wizard, for Access 2003 we instead choose
15
CreateFormusingwizardfromthedatabasemainwindow.
16
ForAccess2003ViewProperties.
Page18of44 Version4.1(24/09/2014)
Databasescoursebook PaoloColetti
For Access 2007 Create Other Query Wizard, for Access 2003, we select the Queries objects in the main
17
databasewindowandchooseBuildQueryusingWizard.
Version4.1(24/09/2014) Page19of44
PaoloColetti Databasescoursebook
However, when the condition becomes too complicated, it is better to use the Expression Builder (see
section2.4.2).Iftwocriteriaareputonthesamefield,typingthemontwospacesoneabovetheother,
theyautomaticallyarealternative(itisenoughthatoneofthembevalidforthosedatatobedisplayed);on
theotherhandiftwocriteriaareputondifferentfields,typingthemontwospacesofthesamerow,they
areconsideredtogether(bothmustbevalidforthosedatatobedisplayed).Again,complicatedconditions
withlogicaloperatorsaremoreeasilybuiltwiththeExpressionBuilder.
Virtual fields
Other fields can be automatically generated taking values from any field, even fields not present in the
query (but their tables must be present in the querys window upper part), and applying mathematical,
logicalortextualoperations.Wesimplytypeinthefieldsnamespaceofanexistingquerythevirtualfields
namefollowedbyacolonandbyitsexpression,usingtheExpressionBuilder(seesection2.4.2)ortyping
fields names involved in the operation enclosed in square brackets. For example, to build virtual field
ToPaywhichcontainsthepriceofanorderwithasingleproduct,wesimplytypeinthefieldsnamespace
ToPay: [Price] * [Quantity].
If we instead type in the names space or in the criterias space something between square parenthesis
whichdoesnotcorrespondtoanyfieldinthepresenttables,Accessstopstheexecutionofthequeryand
asks usthevalue ofthatthing. This is a goodtrick toforceAccessto askthe user avalue.For example,
putting in the criteria space of the EnrolmentYear field [Please, tell me the enrolment year] and switching to
DatasheetView,forcesAccesstostoptheexecutionofthequery,realizethatafieldwithnamePlease,tell
me the enrolment year does not exist, and display a dialog box which says exactly Please, tell me the
enrolmentyear.
Page20of44 Version4.1(24/09/2014)
Databasescoursebook PaoloColetti
18
InAccess2003itissimplythebutton.
19
InAccess2003fromtheQuerymenu.
20
ForAccess2003QueryRun.
Version4.1(24/09/2014) Page21of44
PaoloColetti Databasescoursebook
21
In Access 2003, we select the Reports objects in the main database window and choose Build Report using
Wizard.
22
InAccess2003itisopenedautomaticallyinReportView.
23
ThishappensonlyinAccess2010.ForAccess2003and2007,closingthePrintPreviewjumpstoanotherview.
24
ForAccess2003and2007DataWord
Page22of44 Version4.1(24/09/2014)
Databasescoursebook PaoloColetti
3. MySQL (level 5)
MySQL is a free SQL database management program, a program which is in charge of storing data and
lettingusersextract,modify,insertthemusingSQLlanguage(seesection0).
UnlikeMicrosoftAccess,MySQLprogramrunsasaserveronacomputerandreceivesSQLconnectionson
port3306.Theseconnectionsmaycomefromthesamecomputerorfromexternalonescalledclients,in
caseport3306isopenforexternalconnections.Toeachclientrequest,MySQLserverreturnsananswer
always intextual form which may be atable, in case of data request, a simple acknowledge in case ofa
commandrequestoranerrormessage.
UnlikeMicrosoftAccess2010,MySQLusesuserauthentication,i.e.theusermustprovideavalidusername
andpasswordtoopenaconnectionfromhisclienttoMySQLserverandmusthavetheprivilegesforthe
operation he is trying to do. For example, an user without the ALTER TABLE privilege will be unable to
changethestructureofatable.
3.1. HeidiSQL
Being a server, MySQL requires a client program to connect on the
usersside.Thesimplestclientprogramisacommandlineinterface,
as the one in the picture, where the user types its commands on a
cursorline.However,thiskindofclientisnotuserfriendly,especially
whenextracteddatahavetheformofacomplextable.
HeidiSQL is one of the many graphical clients for
MySQL server. It handles the connection
procedure and all the commands the user may
send to the database, displaying the result in a
more user friendly graphical format. It is freely
available on www.heidisql.com, but a
preconfigured portable version, where sessions
information are already inserted, can be
downloadedfromthecourseswebsite.
The first thing that HeidiSQL displays is the
Session Manager, where, when not using a
preconfigured portable version, we should build
yourmostcommonlyusedsessionsfillinginthese
details:
NetworkType:wechooseMySQL(TCP/IP);
Hostname/IP: we type here localhost if MySQL server is running on your current computer,
otherwisetheIPnumberor,better,theInternetNameofthecomputeronwhichMySQL server is
running(onthecourseswebsitethereareinstructionsonhowtoconnecttounibzMySQLserver)
flag Prompt for Credentials, unless we want the client to remember our username and our
password;
Port:3306;
wedonotflagCompressedclient/serverprotocol.
Wesavethesessionwehavejustbuiltandweassignameaningfulname.ThenweclickOpentoopenthe
connection.
Remember that, in order to connect to MySQL server at unibz from outside unibz's LAN, we must first
activate Virtual Private Network on our computer, otherwise the connection will be rejected by unibz
Version4.1(24/09/2014) Page23of44
PaoloColetti Databasescoursebook
3.1.1. Connection not working
Ifweareconnectingtounibzserverfromanunibzsinternalcomputer:
wemistypedusernameorpassword;
ouruserdoesnothaveanaccountonthatdatabase;
MySQLserverisdown.
Ifweareconnectingtounibzserverfromanexternalcomputer:
wearenotusingVirtualPrivateNetwork(seeabove,section3.1);
wemistypedusernameorpassword;
wemistypedunibzsserversInternetname;
ouruserdoesnothaveanaccountonthatdatabase;
MySQLserverisdown;
Page24of44 Version4.1(24/09/2014)
Databasescoursebook PaoloColetti
wearenotconnectedtotheInternet;
ourfirewallpreventsoutgoingconnectionsfromport3306;
If we are connecting to another server on your same computer: in this case we should try to use the
commandlineinterfacetocheckwhetherserverisupandourpasswordworks,asdescribedattheendof
procedureinsection3.2).
Ifweareconnectingtoanotherexternalserver:
wemistypedusernameorpassword;
wemistypedtheserversInternetname;
ouruserdoesnothaveanaccountonthatdatabase;
MySQLserverisdown;
our user does not have the privilege to connect to MySQL server from outside (a check on which
computerweareisperformedeverytimeweconnectbyMySQLserver);
wearenotconnectedtotheInternet;
ourfirewallpreventsoutgoingconnectionsfromport3306;
thefirewallonwhichMySQLisrunningpreventsincomingconnectionsonport3306.
Version4.1(24/09/2014) Page25of44
PaoloColetti Databasescoursebook
3)ifwehavedownloadedthelastversion,wemay 4)wechooseCustomsetuptype;
skipthedownloadofupdates;
5) the suggested features are MySQL Server, 6) according to what we have selected in the
MySQLNotifierandMySQLDocumentation; previous step, the installer might ask us the
permissiontoautomaticallyinstallotherproducts;
Page26of44 Version4.1(24/09/2014)
Databasescoursebook PaoloColetti
Version4.1(24/09/2014) Page27of44
PaoloColetti Databasescoursebook
Weshouldnowbeinsidethedatabaseasroot,with
all privileges. We check which databases we have
typingthecommandSHOWDATABASES;.
Sakila database should already be present inside the server. To load other databases, the fastest way is
loadingtheSQLcodeonthecourseswebsitewhich,onceexecuted,buildsthemautomaticallyandfillsthe
tables.
Page28of44 Version4.1(24/09/2014)
Databasescoursebook PaoloColetti
Version4.1(24/09/2014) Page29of44
PaoloColetti Databasescoursebook
ThebasicSQLsyntaxforaselectionqueryis
SELECT {fields} FROM {table};
where{fields}isalistoffieldsseparatedbycommasand{table}isthenameofatable.Forexample,
SELECT FirstName, LastName, BirthDate FROM Students;
produces a temporary table with 3 columns extracted from permanent table Students, which probably
containsmanymorefields.
Toaddafilteringorsortingconditionwejustneedtoadd,afterthetablesnameandbeforethesemicolon,
WHERE {condition} ORDER BY {field} {ASC|DESC}
Forexample,
SELECT FirstName, LastName FROM Students WHERE ( Enrolment_Date >= 2013-01-01 ) ORDER BY
LastName ASC ORDER BY FirstName ASC;
producesalistofstudentsenrolledfrom2013,orderedfirstbysurnameand,incasetwostudentshavethe
samesurname,byfirstname.
Query
SELECT FirstName, LastName, Residence_address, Residence_Country FROM Students WHERE ( (
Enrolment_Date >= 2013-01-01 ) AND ( Residence_country = Germany ) );
produces a list of students with their addresses enrolled from 2013 and resident in Germany, in no
particularorder.Notethattheconditionmayinvolveafieldwhichisorwhichisnotinthequeryfields,but
the field must obviously be in the selected table. Spaces in the condition and parenthesis around the
condition are not mandatory, but improve readability and are very helpful when conditions become
complicated.
Thereareotheroperatorsandfunctionswhicharehelpfulinconditions:
theusualmathematicaloperators +,-,*,/
theusualmathematicalcomparisons=,<,>,<=,=>,<>
{field} BETWEEN {value1} AND {value2},forexamplePrice BETWEEN 20 and 50
{condition1} AND {condition2},forexample( Price > 50 ) AND ( Quantity < 20 )
{condition1} OR {condition2},forexample( Price > 50 ) OR ( Quantity < 20 )
NOT {condition},whichreversestheconditionafterit,forexampleNOT (Country = Germany)
IS NULL and IS NOT NULL, to check whether a value is empty or not, such as (End date IS NOT
NULL)
{field} IN ({list}), very useful when you need to filter in values in a list, for example Country IN
(Germany,Italy,Austria,France)
{field} LIKE {expression}, for approximated matching filters, such as Surname LIKE Col%, which
selectsonly surnamesstarting with Col,or Surname LIKE Col_t which selectsonly surnamessuch
asColet,Colit,Coltt,Colt,Col4t,etc.
CURDATE(), to be used in expression where current date (with midnight time) is needed, for
example to select students enrolled today Enrolment_Date = CURDATE(). Pay special attention
whenyoucomparethisfunctionwithadateincludingtime,asthisfunctiongivesyouthecurrent
date with midnight time and can give you the wrong result; for example Enrolment_Time <=
CURDATE()doesnotreturnpeopleenrolledtoday
DATE_ADD({date},{interval})toadd(orsubtractwhentheintervalisnegative)days,months,years
fromadate,forexampleDATE_ADD('2012-01-09', INTERVAL 14 MONTH)resultsin2013-03-09
YEAR({date})togettheyearfromadate,suchasYEAR(Enrolment_Date) = 2012
MONTH({date})togetthenumericalmonthfromadate,suchasMONTH(Enrolment_Date) = 9
DAY({date})togetthedayofthemonthfromadate,suchas DAY( CURDATE() ) = 1,whichistrue
onlywhentodayisthefirstdayofthemonth,
unfortunatelySQLdoesnothave,asAccess,afunctiontodirectlycalculatethedifferenceinyears
between two dates, since function DATEDIFF() returns the difference in days and not in years.
Therefore,thetwowaystogetthedifferenceinyearsbetweentwodatesistheapproximateway
ROUND(DATEDIFF({date2},{date1})/365.25) and this trick for the exact difference YEAR({date2}) -
YEAR({date1}) - ( DATE_FORMAT( {date2}, '%m%d' ) < DATE_FORMAT( {date1}, '%m%d' ) ).
Page30of44 Version4.1(24/09/2014)
Databasescoursebook PaoloColetti
4.2.2. Views
Incaseweplantousethequeryagaininthefutureorwesimplywanttosaveitinsidethedatabaseorwe
plantouseitstemporarytableasasourcetableforanotherquery,itisagoodideatomakeitpermanent
creatingaviewwith
CREATE VIEW {name} AS {query};
Forexample,
CREATE VIEW List_Of_Discounted_Products AS SELECT ProductName, ProductDescription, Price * 0.9 AS
Discounted_Price FROM Products;
Thisquerycanbeusedlaterbyanotherqueryasasourcetable,forexample
SELECT ProductName, Discounted_Price FROM List_Of_Discounted_Products ORDER BY ProductName ASC;
Todestroyaview,usuallybecausewewanttorebuilditinanotherway:
DROP VIEW {name};
Version4.1(24/09/2014) Page31of44
PaoloColetti Databasescoursebook
25
Incasethissymboldoesnotappearonthekeyboard,inHeidiSQLanytableorfieldnamecanbeeasilyproduced,
together with the enclosing symbol, double clicking on the tables or fields name. Producing it directly withgout
copyingandpastingitfromanotherpartofthecodeisrathercomplicated:wepressWindowskey+R,thenwetype
charmap,thenwegothroughthecharactermapuntilwefindthegraveaccent,wecopyandpasteit.
Page32of44 Version4.1(24/09/2014)
Databasescoursebook PaoloColetti
Version4.1(24/09/2014) Page33of44
PaoloColetti Databasescoursebook
To modify records in a table it is, as in the previous case, a good idea to manually edit them using a
graphicalclient,butwhenalotofsimilarmodificationisrequested,thesyntaxthatcanbeusedis
UPDATE {table} SET {field} = {value} WHERE {condition};
Forexample,tosetlanguageGermantoallstudentswithresidencecountryGermany,thecommandis
UPDATE Students SET language = German WHERE residence_country = Germany;
Anupdatequeryisoftenappliedtoonetablebuttheconditionusesfieldsfromotherrelatedtables.Inthis
caseaninnerjoininnecessary,andthesyntaxbecomesslightlymorecomplicated
UPDATE {table} SET {field} = {value} FROM {tables with inner join} WHERE {condition};
For example, to set the grade of all exams to 30 (in table Exams) for students whose surname (in table
Students)startswithCol,thecommandis
UPDATE Exams SET grade = 30 FROM ( Exams INNER JOIN Students ON exams.StudentNumber =
Students.StudentNumber ) WHERE surname LIKE Col%;
Toinsertrecordsinatablethefastesttoolisimportingthemfromatextfile,asexplainedinsection4.6,or
insertingthemmanuallyrecordbyrecordinagraphicalclient.However,thesyntaxtomanuallyinsertthem
usingaqueryis
INSERT INTO {table} ({fields}) ({values}), , ({values});
Forexample,toinsertthreerecordsintableStudents
INSERT INTO Students ( StudentNumber, FirstName, Surname, BirthDate, EnrolmentYear ) ( 8712, John,
Smith, 1994-04-01, 2012 ), ( 8713, Vanda, Black, 1994-12-10, 2011 ), ( 8717, Mary,White, 1995-05-01,
2012 );
It is not necessary to specify all the fields of the table: any field not specified in the {fields} list will be
assignedanautomaticnumberifithastheAUTO_INCREMENToption(seesection4.7.1),otherwiseMySQL
willassignaNullvalue.
Page34of44 Version4.1(24/09/2014)
Databasescoursebook PaoloColetti
appearsamongtheexportedvaluesotherwiseitwillcreateconfusion.Iffindingsuchacharacterisdifficult
since the table contains long textual fields, which may have any character including tab, then the best
choice is using tab but inserting also an Encloser character, usually single or double quote, which will
surroundalltheexportedvalues.
To import using HeidiSQL, two operations must be performed before beginning. First, the table must
alreadyexist;ifitdoesnot,wemustprepareitsstructurebeforeimporting,asexplainedinsection4.7.2.
Thenweneedtoopenthedatafilewithaplaintexteditor(suchasNotepad)andcarefullycheckwhichis
thefieldterminationcharacterandwhethervaluesareenclosedinquotationsornot.
Finally, we open the database, we click on the
table into which we plan to insert the data in
the left window, then we choose Tools
Import CSV file. It is better, to avoid possible
incompatibilities, to choose Client parses file
contentsasaMethod.Wechoosecarefullythe
Fields terminator and the Fields encloser, if it
exist in the file. In the fields list, we unselect
thefieldswhicharenotpresentinthetextfile
andwerearrangethefieldsincasetheyareina
differentorderinthetextfile.
The entire importing procedure is very tricky,
very often it needs to be repeated several
times, choosing REPLACE (duplicates) in the
further attempts, before a complete import
withouterrorsisreached.
4.7. Tables
EachtableinaMySQLdatabasehasalistoffields,eachonebeingabletostoreonlyaspecificdatatype.To
seethestructureofatableweusethesyntax DESCRIBE {table}.Moreover,inordertodiscoverthesyntax
whichhasbeenusedtoproducethestructureofatable,SQLoffersthesyntax SHOW CREATE {table}.In
this way it is possible, even by novice users, see an example of a similar table and partially copy it to
produce other structures using the following syntax, where newlines are inserted only for readability
purposes,
CREATE TABLE {table} (
{field} {field type} {options},
{field} {field type} {options},
PRIMARY KEY ({field}),
INDEX({field})
);
Forexample,toproduceatableStudents,
CREATE TABLE Students(
`student number` INTEGER PRIMARY KEY,
name VARCHAR(45) NOT NULL,
surname VARCHAR(45) NOT NULL KEY,
`enrolment date` DATE KEY,
Notes TEXT,
PRIMARY KEY ( `student number` ),
INDEX ( `surname` )
);
Theindexindicationisanindicationthatthatfieldisfrequentlyusedinsearch,sortingorjoiningoperations
and MySQL will prepare itself; every foreign key is obviously an index, while for primary key it is not
Version4.1(24/09/2014) Page35of44
PaoloColetti Databasescoursebook
necessarytoindicateitasitisimplicitintheprimarykeyindication.Concerningotherfields,forsomeitis
obvious that they must be indexes, for example field surname in every peoples table, for others the
decisionisuptothedatabasedesignedwhileforsomeothersitisevidentthattheyarenotindexes,such
asfieldsNotesorPicture.
Wecanaddsomecheckconstraintstoatabletolimitthepossibilityofwrongdatainsertions,addingto
thesyntax CHECK {condition}.Forexample,toadmitonlystudentswithanumberlargerthan1000andan
enrolmentdatenotinthefuture
CREATE TABLE Students(
`student number` INTEGER,
name VARCHAR(45) NOT NULL,
surname VARCHAR(45) NOT NULL KEY,
`enrolment date` DATE KEY,
Notes TEXT,
PRIMARY KEY ( student number ),
INDEX ( `surname` ),
CHECK ( `student number` >1000 ),
CHECK ( `enrolment date` <=CURDATE() )
);
Unfortunately,uptoversion5.6MySQLacceptsanyCHECKcommandbutdoesnotreallyimplementthe
CHECKconstraint!
Even though it is never a good idea to do it (with the exception of adding other fields, a rather safe
operation),tochangeatablestructureafterithasbeencreatedthefollowingsyntaxescanbeused:
DROP TABLE {table};
ALTER TABLE {table} ADD {field} {field type} {options};
ALTER TABLE {table} DROP {field};
ALTER TABLE {table} ADD PRIMARY KEY {field};
ALTER TABLE {table} DROP PRIMARY KEY;
Page36of44 Version4.1(24/09/2014)
Databasescoursebook PaoloColetti
Version4.1(24/09/2014) Page37of44
PaoloColetti Databasescoursebook
Nowwechecktherelations.Sincewehavetherestrictionthatacropmustgointotheoneandnotmore
than one storage, we automatically have a manytoone relation between Crops and Storages, meaning
thatCropsinthisdatabaseisnotanexternaltablebutadependenttable.Cropsneedstheforeignkeyto
therelation,whichcanbeafieldcalledWhereStored.
NowweanalyzetherelationbetweenFieldsandCrops,whichisclearlymanytomanyandthereforeneeds
ajunctiontable.
belikethis,forexampleintherelationbetweenSecretariesandStudents)anddoesnotinstead
needamanytomanyrelation.
Inourexample,wethereforeanalyzecarefullythefactthatputtingamanytoonerelationbetweenCrops
andStoragesautomaticallyimpliesthateachcropisstoredinonlyonestoragewhileastoragecanaccept
differentcrops.PlantingstableisajunctiontablebetweenFieldsandCrops:infactinthesamefieldwecan
plantseveralcrops(eitherdividingthefieldinpartsorconsideringanhistoricaldatabasewhereeachfield
can be used for different crops in different years) and, clearly, the same crop can be planted in several
fields.
Nowwecan enrichthe diagram putting all theother fields and assigning primarykeys,checkingthat no
duplicatevaluesoftheprimarykeyexist.Alsoforfieldsspecialattentionmustbetaken,inparticular:
no field may exist which can be calculated automatically, either using the fields of the same
tableorthroughcountingorsummingoraveragingoperationsperformedonothertables,
fields must be put in the appropriate table as changing the table in which a field is located
completely changes the meaning of that field. Usually, a field in an external table is strictly
bound to that object and rarely changes, while fields in the junction tables are bound to the
actionwhichisexpressedbythattable.
Inourexample,weassignprimarykeytoNamefieldforFieldsandStoragestables,keepinginmindthat
neithertwoFieldsnortwoStorageswiththesamenamemayexits.ForPlantingswedecidetouseanID,
whileforCropsacompositeprimarykeycouldbeTypeandSubtype,butinordertoavoidproblemswith
compositeprimarykeywechoosetouseanID.
NotethatfieldIrrigationSystemisintableFields,meaningthatitisafeatureofthefieldregardlessofwhich
cropsareplantedinthefield.IfitwereintablePlantings,itwouldmeanthattheirrigationsystemchanges
accordingtowhichkingofcropandtothefield,ifitwereintableCropsitwouldmeanthatitisboundto
thespecifictypeofCropsandisthustransportedinthefieldaccordingtowhichcropispresent.
Version4.1(24/09/2014) Page39of44
PaoloColetti Databasescoursebook
Forexample,inthepreviousdatabase:
fortableFields:
o Name,Town,andStreetcontaintext(textforAccess; VARCHAR(30) VARCHAR(50) VARCHAR(50) for
MySQL);
o Slope, MinimumPH, MaximumPH and Size contain numbers (number for Access; DECIMAL(4,2)
DECIMAL(3,2) INTEGER forMySQL);
o AccessibilityandIrrigationSystemcontainyes/no(yes/noforAccess; ENUM(yes,no)or INTEGERfor
Mysql);
fortableStorages:Name,Town,andAddresscontaintext(textforAccess; VARCHAR(30) VARCHAR(50)
VARCHAR(50) forMySQL).
fortableCrops:
o IDisanautomaticincrementingnumber(autonumberforAccess, INTEGERwith AUTO_INCREMENT
forMySQL);
o Type, Subtype, Plant Type contain text (text for Access; VARCHAR(30) VARCHAR(30) VARCHAR(10)
forMySQL);
o HarvestMonth can contain text (text for Access; VARCHAR(9) or ENUM(January, , December) for
MySQL)ornumber(numberforAccess;INTEGERforMySQL);
o MinimumPH, MaximumPH, PlantsPerHectare, ProductionPerPlant, CostPerPlant, and
CropValuePerKg contain numbers (number for Access; DECIMAL(3,2) DECIMAL(3,2) INTEGER FLOAT
DECIMAL(6,2) FLOAT forMySQL);
fortablePlantings:
o IDisanautomaticincrementingnumber(autonumberforAccess, INTEGERwith AUTO_INCREMENT
forMySQL);
o Number(numberforAccess;INTEGERforMySQL);
o Percentagecontainnumbers(number,singlewith2decimaldigitsandpercentageformat,forAccess;
DECIMAL(5,2)forMySQL)
o PlantingDate,MaturityDate,andRemovingDatearedates(date/timeforAccess,DATEforMySQL).
ForeignkeysmustbesetinMySQLtothesametypeofthecorrespondingprimarykey.
ForeignkeyscanbesetinAccess
to the same type of the corresponding primary key and then the relation can be built in the
relationshipsdiagram,rememberingtoenforcereferentialintegrity;
asLookupWizardtypechoosingasdisplayedvaluestheappropriatemeaningfulfieldsfromtherelated
table, hiding the primary key when it is an ID (and thus not meaningful for a human operator) or
unhidingitwhenitismeaningful.Thenreferentialintegrityisenforcedintherelationshipsdiagram.
default value to 0 and option required (in MySQL DEFAULT 0 and NOT NULL), since it is important to
alwaysknowhowmuchfieldeachplantinguses.
Somefieldsclearlyadmitpredeterminedvalues.InAccessadropdownmenucanbeeasilycreatedusing
the Lookup Wizard with values directly typed in. For example, to Plant Type we add a dropdown menu
withanonmandatorypredeterminedlistcontainingthemostcommonplanttypes(tree,bush,vine,herb);
wechooseanonmandatorylistsinceothervaluescanexist.SimilarthingfortheHarvestMonth,weinsert
thetwelvemonthsinamandatorypredeterminedlistthroughLookupWizard.InMySQLitisnotpossible
toinsertanonmandatorypredeterminedlist,soonlytheHarvestfieldcanbearrangedinthiswayusing
ENUMtype.
However,ifthesevaluesaremandatoryandwearesurethatnoothervalueisallowed,weshouldinstead
for any database management program build another table with the predetermined list of values and
converttheconsideredfieldintoaforeignkey.Forexample,forHarvestMonththelistismandatorysince
onlytwelvemonthsexists.ThuswebuildaMonthstableandweconnecttheHarvestMonthtoitbuilding
the relation and the dropdown menu using the Lookup Wizard displaying simply the Month, always
rememberingtoenforcereferentialintegrity.
Crops
WhereStored
ID
Type
Subtype
Months
PlantType 1 Month
HarvestMonth
5.2.2. Table validation rule
WhenapplyingtablevalidationrulesinAccessoraconditioninMySQLwemustalwayskeepinmindthata
rulecanusuallyinvolveonlyfieldswhicharerequired.Ifweinvolvefieldswhicharenotrequired,wemust
always add to the validation rule the possibility that the nonrequired fields be empty otherwise the
validationrulewillautomaticallyfail.
Forexample,avalidationrulefortablePlantingswhichconditionthatMaturityDateandRemovingDatebe
not before PlantingDate, keeping in mind that MaturityDate and PlantingDate are not required, is for
Access
( [MaturityDate] Is Null Or [PlantingDate] <= [MaturityDate] ) And ( [RemovingDate] Is Null Or [PlantingDate] <=
[RemovingDate] )
WhileforMySQLitisverysimilar
( MaturityDate IS NULL OR PlantingDate <= MaturityDate ) AND ( RemovingDate IS NULL OR PlantingDate
<= RemovingDate )
EachpartoftheruleistruewhenthefieldiseitheremptyorafterPlantingDate.
Version4.1(24/09/2014) Page41of44
PaoloColetti Databasescoursebook
6.1.1. Schema
TheschemahasthemaintablesStorages,whichcontainsstoragesplacesdata,Fields,whichcontainsthe
physicaldataofthefields,andCrops,whichcontainsthebiologicalandcommercialfeaturesofthecrops.
Moreover, a junction table Plantings is used to describe what, where and when is planted: this table
connects Crops and Fields via a manytomany relation. The other relation is a onetomany between
StoragesandCrops,whichunderlinesthefactthateachcropcanbeputonlyinonestorage.
WhereStored
Page42of44 Version4.1(24/09/2014)
Databasescoursebook PaoloColetti
Version4.1(24/09/2014) Page43of44
PaoloColetti Databasescoursebook
Subtype: text limited to 50 characters, required. This contains the specific type of fruit, for example
PearKaiser,AppleGolden;
Plant type: text limited to 50 characters, nonmandatory predetermined list with values Tree, Bush,
Vine,Herb;
HarvestMonth: text limited to 50 characters, nonmandatory predetermined list with the twelve
monthsasvalues,thereisanextratableMonths,containingthetwelvemonths,forwhichthisfield
isaforeignkey;
MinimumPH, MaximumPH: number, single with 2 decimal digits, required, default value 5, must be
between0and10;
PlantsPerHectare:number,integerwith0decimaldigits,defaultvalue0,mustbepositive;
ProductionPerPlant:number,singlewith2decimaldigits,defaultvalue0,mustbenonnegative;
CostPerPlant:currency,euroformat,singlewith2decimaldigits,defaultvalue0;
CropValuePerKg:currency,euroformat,singlewith2decimaldigits,defaultvalue0;
WhereStored:foreignkey,takesvaluesfromStoragestable.
TablePlantingscontains
ID:autonumber,primarykey;
Crop:foreignkey,takesvaluesfromCropstable.
Field:foreignkey,takesvaluesfromFieldstable.
Number: number, integer with 0 decimal digits, default value 0, must be nonnegative. This field
indicateshowmanyplantsofCroptypeareplantedinField;
PlantingDate:date/time,formatdd/mm/yyyy,required,indexed(duplicatesallowed);
MaturityDate,RemovingDate:date/time,formatdd/mm/yyyy;
Percentage: number, percentage format, single with 2 decimal digits, default value 0%, must be
between0%and100%.Thisindicateswhichpercentageofthefieldisusedforthiscrop.
To table Plantings there is also a table validation rule that checks that MaturityDate and RemovingDate,
whentheyexist,arenotbeforePlantingDate.
6.1.4. Forms
Form Insert/Modify Crops shows the crops with their storage, using the relation between Storages and
Crops.Itispossibletomodifythemortoinsertnewones,butdeletionofcropsisforbidden.
6.1.5. Queries
QueryType+Subtype+FieldWithoutIrrigationQueryshowsthefieldwithoutanIrrigationSystemwiththe
croptypeandcropsubtypethatareandwereplantedinthatfield.ThisqueryretrievesthefieldswithaNo
in the IrrigationSystem field and then, thanks to the junction table Plantings and following the two
relations,retrievesthecropstypeandsubtype.
6.1.6. Reports
Report Crops by Harvest Month displays, grouped by HarvestMonth and then grouped by crop type, the
cropsubtypeandthecorresponding field andtownwherethecropisorwasplanted.Ittakes datafrom
tablesCropsandFields,connectedthroughthejunctiontablePlantingsandthetworelations.
Page44of44 Version4.1(24/09/2014)