day1 - rhodeskirlinp/courses/db/f16/lectures/day1.pdf · equivalent representations of a relation...

42

Upload: others

Post on 29-Jun-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •
Page 2: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •
Page 3: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •
Page 4: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

Databases

Page 5: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

Standardstuff

• Classwebpage:cs.rhodes.edu/db• Textbook:getitsomewhere;usedisfine– Stayupwithreading!

• Prerequisite:CS241• Coursework:– Homework,groupproject,midterm,final

• Bepreparedtobringlaptopseverysooften.

Page 6: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

Groupproject• Youwilldesignandimplementyourowndatabase-drivenwebsite.

• Ideas:shopping,auctions,writeabetterBannerWeb,library/bibliographysystem,reviewsalaYelp,bank,finance/stocks,jobpostings,socialnetworkingalaFacebook,recipes,movies,apartments,…

• Groups:probably4-5people,formedonyourown.

• Spreadoutoverthewholesemester;check-insalongtheway.

Page 7: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

Whystudydatabases?

• Academicreasons• Programmingreasons• Business(getajob)reasons• Studentreasons

Page 8: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

Whatwillyoulearn?

• Databasedesign– Howdoyoumodelyourdatasoitcanbestoredinadatabase?

• Databaseprogramming– HowdoIuseadatabasetoaskitquestions?

• Databaseimplementation– Howdoesthedatabaseitselfwork;i.e.,howdoesitstore,find,andretrievedataefficiently?

Page 9: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

Whatisthegoalofadatabase?

• Electronicrecord-keeping,enablingfast andconvenient accesstotheinformationinside.

• DBMS=Databasemanagementsystem– Softwarethatstoresindividualdatabasesandknowshowtosearchtheinformationinside.

– RDBMS=RelationalDBMS– Examples:Oracle,MSSQLServer,MSAccess,MySQL,PostgreSQL,IBMDB2,SQLite

Page 10: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

DBMSFeatures

• Supportmassiveamountsofdata– Giga-,tera-,petabytes

• Persistentstorage– Datacontinuestolivelongafterprogramfinishes.

• Efficientandconvenientaccess– Efficient:don'tsearchtheentirethingtoansweraquestion!

– Convenient:allowuserstoaskquestionsaseasilyaspossible.

• Secure,concurrent,andatomicaccess

Page 11: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

Example:buildabetterBannerWeb

• Professorsofferclasses,studentssignup,getgrades

• Whataresomequestionswe(studentsorfaculty)couldask?– FindmyGPA.– …

• Whyaresecurity,concurrency,andatomicityimportanthere?

Page 12: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

Obvioussolution:Folders

• Advantages?

• Disadvantages?

Page 13: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

Obvioussolution++

• TextfilesandPython/C++/Javaprograms

Page 14: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

Obvioussolution++

• Let'suseCSV:

Hermione,Granger,R123,Potions,ADraco,Malfoy,R111,Potions,BHarry,Potter,R234,Potions,ARonald,Weasley,R345,Potions,C

Page 15: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

Hermione,Granger,R123,Potions,ADraco,Malfoy,R111,Potions,BHarry,Potter,R234,Potions,ARonald,Weasley,R345,Potions,CHarry,Potter,R234,Herbology,BHermione,Graner,R123,Herbology,A

Page 16: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

File1:Hermione,Granger,R123 Draco,Malfoy,R111 Harry,Potter,R234 Ronald,Weasley,R345File2:R123,Potions,AR111,Potions,BR234,Potions,AR345,Potions,CR234,Herbology,BR123,Herbology,A

Page 17: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

Problems

• Inconvenient– needtoknowPython/C++/Javatogetatdata!

• Redundancy/inconsistency• Integrityproblems• Atomicityproblems• Concurrentaccessproblems• Securityproblems

Page 18: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

Whyarethereproblems?

• Twomainreasons:– ThedescriptionofhowthefilesarelaidoutisburiedwithinthePython/C++/Javacodeitself(ifit'sdocumentedatall)

– Thereisnosupportfortransactions (supportingconcurrency,atomicity,integrity,andrecovery)

• DBMSshandleexactlythesetwoproblems.

Page 19: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

Relationaldatabasesystems

• EdgarF.Codd wasaresearcheratIBMwhoconceivedanewwayoforganizingdatabasedonthemathematicalconceptofarelation.

• Relation:asetoforderedtuples(oh,no,CS172stuff…)

Page 20: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

• RDBMS=Relationaldatabasemanagementsystem.

• Therelationalmodelusesrelations(akatables)tostructuredata.

• Gradesrelation:First Last Course Grade

Hermione Granger Potions A

Draco Malfoy Potions B

Harry Potter Potions A

Ronald Weasley Potions C

Page 21: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

• Relationalmodelisanabstraction.• Separatesthelogicalview(asviewedbytheDBuser)fromthephysicalview(DB'sinternalrepresentationofthedata)

First Last Course Grade

Hermione Granger Potions A

Draco Malfoy Potions B

Harry Potter Potions A

Ronald Weasley Potions C

Page 22: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

• Structuredquerylanguage(SQL)foraccessing/modifyingdata:

• FindallstudentswhoaregettingaB.– SELECT First, Last FROM Grades WHERE Grade = "B"

First Last Course Grade

Hermione Granger Potions A

Draco Malfoy Potions B

Harry Potter Potions A

Ronald Weasley Potions C

Page 23: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •
Page 24: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •
Page 25: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

Transactionprocessing• OneormoreDBoperationscanbegroupedintoatransaction.

• ForaDBMStoproperlyimplementtransactions:• Atomicity:All-or-nothingexecutionoftransactions.

• Consistency:ADBcanhaveconsistencyrulesthatshouldnotbeviolated.

• Isolation:Eachtransactionmustappear tobeexecutedasifnoothertransactionsarehappeningsimultaneously.

• Durability:Anychangesatransactionmakesmustneverbelost.

Page 26: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

Ontotherealstuffnow…

Page 27: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

DataModels

• Awayofdescribingdata.– Better:adescriptionofhowtoconceptuallystructurethedata,whatoperationsarepossibleonthedata,andanyconstraintsonthedata.

• Structure:howweviewthedataabstractly• Operations:whatispossibletodowiththedata?

• Constraints:howcanwecontrolwhatdataislegalandwhatisnot?

Page 28: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

Relationalmodel

• Structure:relation(table)• Operations:relationalalgebra(selectcertainrows,certaincolumns,wherepropertiesaretrue/false)

• Constraints:canenforcerestrictionslikeGrademustbein{A,B,C,D,F}

First Last Course Grade

Hermione Granger Potions A

Draco Malfoy Potions B

Harry Potter Potions A

Ronald Weasley Potions C

Page 29: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

Othermodels• Semi-structureddatathatisstill“structured”butnotinrelationalformat.– XML,JSON

• Objectdatabases,orobject-relational• Graphdatabases• NoSQL,NewSQL

Page 30: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

Semi-structuredmodel

• Structure:Treesorgraphs– e.g.,XML

• Operations:Followpathsintheimpliedtreefromoneelementtoanother.– e.g.,XQuery

• Constraints:canconstraindatatypes,possiblevalues,etc.– e.g.,DTDs(documenttypedefinition),XMLSchema

Page 31: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

Object-relational

• Similartorelational,but– Valuesinatablecanhavetheirownstructure,ratherthanbeingsimplestringsorints.

– Relationscanhaveassociatedmethods.

Page 32: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

Relationalmodelismostcommon

• Simple:builtaroundasingleconceptformodelingdata:therelationortable.– Arelationaldatabaseisacollectionofrelations.– Eachrelationisatablewithrowsandcolumns.– AnRDBMScanmanagemanydatabasesatonce.

• Supportshigh-levelprogramminglanguage(SQL)– Limitedbutusefulsetofoperations.

• Haselegantmathematicaltheorybehindit.

Page 33: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

RelationTerminology

• Relation==2Dtable– Attribute ==columnname– Tuple ==row(nottheheaderrow)

• Database==collectionofrelationsFirst Last Course Grade

Hermione Granger Potions A

Draco Malfoy Potions B

Harry Potter Potions A

Ronald Weasley Potions C

Page 34: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

RelationTerminology

• Arelationincludestwoparts:– Therelationschema definesthecolumnheadingsofthetable(attributes/fields)

– Therelationinstance definesthedatarows(tuples,rows,orrecords)ofthetable.

First Last Course Grade

Hermione Granger Potions A

Draco Malfoy Potions B

Harry Potter Potions A

Ronald Weasley Potions C

Page 35: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

Schema

• Aschemaiswrittenbythenameoftherelationfollowedbyaparenthesizedlistofattributes.– Grades(First, Last, Course, Grade)

• ArelationaldatabaseschemaisthesetofschemasforalltherelationsinaDB.

First Last Course Grade

Hermione Granger Potions A

Draco Malfoy Potions B

Harry Potter Potions A

Ronald Weasley Potions C

Page 36: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

Domains

• ArelationalDBrequiresthateverycomponentofarow(tuple)haveaspecificelementarydatatype,ordomain.– string,int,float,date,time(nocomplicatedobjects!)

Grades(First:string, Last:string, Course:string, Grade:char)

Page 37: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

Equivalentrepresentationsofarelation

Grades(First, Last, Course, Grade)• Relationisaset oftuples,notalist.• Attributesinaschemaareaset aswell.– However,theschemaspecifiesa"standard"orderfortheattributes.

• Howmanyequivalentrepresentationsarethereforarelationwithm attributesandn tuples?

First Last Course Grade

Hermione Granger Potions A

Draco Malfoy Potions B

Harry Potter Potions A

Ronald Weasley Potions C

Page 38: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

Degreeandcardinality

• Degree/arity ofarelationisthenumberofattributesinarelation.

• Cardinality isthenumberoftuplesinarelation.

First Last Course Grade

Hermione Granger Potions A

Draco Malfoy Potions B

Harry Potter Potions A

Ronald Weasley Potions C

Page 39: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

Keystoagoodrelation(ship)

Page 40: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

Keysofarelation

• Keysareakindofintegrityconstraint.• AsetofattributesKformsakeyforarelationRif:– weforbidtwotuplesinaninstanceofRtohavethesamevaluesforallattributesofK.

First Last Course Grade

Hermione Granger Potions A

Draco Malfoy Potions B

Harry Potter Potions A

Ronald Weasley Potions C

Grades(First, Last, Course, Grade)

Page 41: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

Keysofarelation

• Keyshelpassociatetuplesindifferentrelations.

SID CRN Grade

123 777 A

111 777 B

234 777 A

345 777 C

SID First Last

123 Hermione Granger

111 Draco Malfoy

234 Harry Potter

345 Ronald Weasley

CRN Name Semester Year

777 Potions Fall 1997

888 Potions Spring 1997

999 Transfiguration Fall 1996

789 Transfiguration Spring 1996

Page 42: day1 - Rhodeskirlinp/courses/db/f16/lectures/day1.pdf · Equivalent representations of a relation Grades(First, Last, Course, Grade) • Relation is a setof tuples, not a list. •

Example

• Let'sexpandtheserelationstohandlethekindsofthingsyou'dliketoseeinBannerWeb.

• Keeptrackofstudents,professors,courses,whoteacheswhat,enrollments,pre-requisites,grades,departments&theirchairs.– Onlyonechairperdepartment.– Studentcannotenrollinmultiplecopiesofthesamecourseinonesemester.

– Otherconstraintsthatarelogical.