Top Banner
CS 4604: Introduction to Database Management Systems B. Aditya Prakash Lecture #2: The Relational Model and Relational Algebra
79

CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Jul 13, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

CS4604:IntroductiontoDatabaseManagementSystems

B.AdityaPrakashLecture#2:TheRelationalModeland

RelationalAlgebra

Page 2: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

CourseOutline§  Weeks1–4:Query/

ManipulationLanguagesandDataModeling–  RelationalAlgebra–  Datadefinition–  ProgrammingwithSQL–  Entity-Relationship(E/R)approach

–  SpecifyingConstraints–  GoodE/Rdesign

§  Weeks5–8:Indexes,ProcessingandOptimization–  Storing–  Hashing/Sorting–  QueryOptimization–  NoSQLandHadoop

§  Week9-10:RelationalDesign–  FunctionalDependencies–  Normalizationtoavoidredundancy

§  Week11-12:ConcurrencyControl–  Transactions–  LoggingandRecovery

§  Week13–14:Students’choice–  PracticeProblems–  XML–  Dataminingandwarehousing

Prakash2018 VTCS4604 2

Page 3: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

DataModel§  ADataModelisanotationfordescribingdataorinformation.

–  Structureofdata(e.g.arrays,structs)•  Conceptualmodel:Indatabases,structuresareatahigherlevel.

–  Operationsondata(ModificationsandQueries)•  LimitedOperations:Easeofprogrammersandefficiencyofdatabase.

–  Constraintsondata(whatthedatacanbe)§  Examplesofdatamodels

–  TheRelationalModel–  TheSemistructured-DataModel

•  XMLandrelatedstandards–  Object-RelationalModel

Prakash2018 VTCS4604 3

Page 4: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

TheRelationalModel

§  Structure:Table(likeanarrayofstructs)§  Operations:Relationalalebgra(selection,projection,conditions,etc)

§  Constraints:E.g.,gradescanbeonly{A,B,C,F}

Prakash2018 VTCS4604

Student Course Grade

HermioneGrainger Potions A

DracoMalfoy Potions B

HarryPotter Potions A

RonWeasley Potions C

4

Page 5: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

TheSemi-structuredmodel

§  Structure:Treesorgraphs,tagsdefineroleplayedbydifferentpiecesofdata.

§  Operations:Followpathsintheimpliedtreefromoneelementtoanother.

§  Constraints:E.g.,canexpresslimitationsondatatypes

Prakash2018 VTCS4604

<CoursesTaken>

<Student>Hermione Grainger</Student>

<Course>Potions</Course>

<Grade>A</Grade>

<Student>Draco Malfoy</Student>

<Course>Potions</Course>

<Grade>B</Grade> ...

</CoursesTaken>

5

Page 6: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Comparingthetwomodels

§  Flexibility:XMLcanrepresentgraphs§  Easeofuse:SQLenablesprogrammertoexpresswishesathighlevel.

Prakash2018 VTCS4604 6

Page 7: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

TheRelationalModel§  Simple:Builtaroundasingleconceptformodelingdata:therelationortable.– Arelationaldatabaseisacollectionofrelations. – Eachrelationisatablewithrowsandcolumns.

§  Supportshigh-levelprogramminglanguage(SQL).– Limitedbutveryusefulsetofoperations

§  Hasanelegantmathematicaldesigntheory.§  MostcurrentDBMSarerelational(Oracle,IBMDB2,MSSQL)

Prakash2018 VTCS4604 7

Page 8: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Relations§  Arelationisatwo-dimensionaltable:

–  Relation==table.–  Attribute==columnname.–  Tuple==row(nottheheaderrow).

§  Database==collectionofrelations.§  Arelationhastwoparts:

–  Schemadefinescolumnheadsofthetable(attributes).–  Instancecontainsthedatarows(tuples,rows,orrecords)ofthetable.

Student Course Grade

HermioneGrainger Potions A

DracoMalfoy Potions B

HarryPotter Potions A

RonWeasley Potions CPrakash2018 VTCS4604 8

Page 9: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

SchemaCoursesTaken:§  Theschemaofarelationisthenameoftherelationfollowed

byaparenthesizedlistofattributes. CoursesTaken(Student, Course, Grade)

§  Adesigninarelationalmodelconsistsofasetofschemas.§  Suchasetofschemasiscalledarelationaldatabaseschema.

Student Course Grade

HermioneGrainger Potions A

DracoMalfoy Potions B

HarryPotter Potions A

RonWeasley Potions C

Prakash2018 VTCS4604 9

Page 10: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Relations:EquivalentRepresentationsCoursesTaken:

CoursesTaken(Student, Course, Grade)

§  Relationisasetoftuplesandnotalistoftuples.–  Orderinwhichwepresentthetuplesdoesnotmatter.–  Veryimportant!

§  Theattributesinaschemaarealsoaset(notalist).–  Schemaisthesameirrespectiveoforderofattributes. CoursesTaken(Student, Grade, Course)

–  Wespecifya“standard”orderwhenweintroduceaschema.§  Howmanyequivalentrepresentationsarethereforarelationwith

mattributesandntuples?

Student Course Grade

HermioneGrainger Potions A

DracoMalfoy Potions B

HarryPotter Potions A

RonWeasley Potions C

m! n! Prakash2018 VTCS4604 10

Page 11: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

DegreeandCardinalityCoursesTaken:§  Degree/Arityisthenumberoffields/attributesinschema(=3

inthetableabove)§  Cardinalityisthenumberoftuplesinrelation(=4inthetable

above)

Student Course Grade

HermioneGrainger Potions A

DracoMalfoy Potions B

HarryPotter Potions A

RonWeasley Potions C

Prakash2018 VTCS4604 11

Page 12: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

KeysofRelations§  Keysareoneformofintegrityconstraints(IC)

– Nopairoftuplesshouldhaveidenticalkeys§ WhatisthekeyforCoursesTaken?

– Studentifonlyonecourseintherelation– Pair(Student,Course)ifmultiplecourses– Whatifstudenttakessamecoursemanytimes?

Prakash2018 VTCS4604

Student Course Grade

HermioneGrainger Potions A

DracoMalfoy Potions B

HarryPotter Potions A

RonWeasley Potions C12

Page 13: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

KeysofRelations

§  Keyshelpassociatetuplesindifferentrelations

Prakash2018 VTCS4604

SID Student GPA

123 HermioneGrainger

3.9

111 DracoMalfoy 3.0

234 HarryPotter 3.7

456 RonWeasley 3.1

SID CID Grade

123 15-401 A

111 15-401 B

123 14-501 B

…. …. ….

13

Page 14: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Example§  Create a database for managing class enrollments in a single

semester. You shouldkeep trackofall students (theirnames, Ids,and addresses) and professors (name, Id, department). Do notrecord the address of professors but keep track of their ages.Maintainrecordsofcoursesalso.Likewhatclassroomisassignedtoa course, what is the current enrollment, and which departmentoffersit.Atmostoneprofessorteacheseachcourse.Eachstudentevaluates the professor teaching the course. Note that all courseofferings in the semester are unique, i.e. course names andnumbers do not overlap. A course can have ≥ 0 pre-requisites,excludingitself.Astudentenrolledinacoursemusthaveenrolledin all its pre-requisites. Each student receives a grade in eachcourse.Thedepartmentsarealsounique,andcanhaveatmostonechairperson(ordept.head).Achairpersonisnot allowedtoheadtwoormoredepartments.

Prakash2018 VTCS4604 14

Page 15: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Example§  Create a database for managing class enrollments in a single

semester.Youshouldkeep trackofallstudents (theirnames, Ids,and addresses) and professors (name, Id, department). Do notrecord the address of professors but keep track of their ages.Maintain recordsofcourses also. Likewhat classroom is assignedtoacourse,whatisthecurrentenrollment,andwhichdepartmentoffersit.Atmostoneprofessorteacheseachcourse.Eachstudentevaluates the professor teaching the course.Note that all courseofferings in the semester are unique, i.e. course names andnumbers do not overlap. A course can have ≥ 0 pre-requisites,excludingitself.Astudentenrolledinacoursemusthaveenrolledin all its pre-requisites. Each student receives a grade in eachcourse. The departments are also unique, and can have at mostonechairperson (ordept.head).A chairperson isnot allowed toheadtwoormoredepartments.

Prakash2018 VTCS4604 15

Page 16: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

RelationalDesignfortheExample§  Students(PID:string,Name:string,Address:string)

§  Professors(PID:string,Name:string,Office:string,Age:integer,DepartmentName:string)

§  Courses(Number:integer,DeptName:string,CourseName:string,Classroom:string,Enrollment:integer)

§  Teach(ProfessorPID:string,Number:integer,DeptName:string)

§  Take(StudentPID:string,Number: integer,DeptName:string,Grade:string,ProfessorEvaluation:integer)

§  Departments(Name:string,ChairmanPID:string)

§  PreReq(Number:integer,DeptName:string,PreReqNumber:integer, PreReqDeptName:string)

Prakash2018 VTCS4604 16

Page 17: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

RelationalDesignExample:Keys?§  Students(PID:string,Name:string,Address:string)

§  Professors(PID:string,Name:string,Office:string,Age:integer,DepartmentName:string)

§  Courses(Number:integer,DeptName:string,CourseName:string,Classroom:string,Enrollment:integer)

§  Teach(ProfessorPID:string,Number:integer,DeptName:string)

§  Take(StudentPID:string,Number: integer,DeptName:string,Grade:string,ProfessorEvaluation:integer)

§  Departments(Name:string,ChairmanPID:string)

§  PreReq(Number:integer,DeptName:string,PreReqNumber:integer, PreReqDeptName:string)

Prakash2018 VTCS4604 17

Page 18: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

RelationalDesign:Keys?§  Students(PID:string,Name:string,Address:string)

§  Professors(PID:string,Name:string,Office:string,Age:integer,DepartmentName:string)

§  Courses(Number:integer,DeptName:string,CourseName:string,Classroom:string,Enrollment:integer)

§  Teach(ProfessorPID:string,Number:integer,DeptName:string)

§  Take(StudentPID:string,Number: integer,DeptName:string,Grade:string,ProfessorEvaluation:integer)

§  Departments(Name:string,ChairmanPID:string)

§  PreReq(Number:integer,DeptName:string,PreReqNumber:integer, PreReqDeptName:string)

Prakash2018 VTCS4604 18

Page 19: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

IssuestoConsiderintheDesign§  CanwemergeCoursesandTeachsinceeachprofessorteachesatmostonecourse?

§  Doweneedaseparaterelationtostoreevaluations?§  Howcanwehandlepre-requisitesthatare“or”s,e.g.,youcantakeCS4604ifyouhavetakeneitherCS3114orCS2606?

§  Howdowegeneralizethisschematohandledataovermorethanonesemester?

§  Whatmodificationsdoestheschemaneedifmorethanoneprofessorcanteachacourse?

Prakash2018 VTCS4604 19

Page 20: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Formalquerylanguages

§  Howdowecollectinformation?§  Eg.,findssn’sofpeoplein415§  (recall:everythingisaset!)§  Onesolution:Rel.algebra,ie.,setoperators§  Q1:Whichones??§  Q2:whatisaminimalsetofoperators?

20

Page 21: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

§  .§  .§  .§  setunionU§  setdifference‘-’

Relationaloperators

21

Page 22: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Example:

FT-STUDENTSsn Name

129 peters main str239 lee 5th ave

PT-STUDENTSsn Name Address

123 smith main str234 jones forbes ave

§  Q:findallstudents(partorfulltime)§  A:PT-STUDENTunionFT-STUDENT

22

Page 23: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Observations:

§  twotablesare‘unioncompatible’iftheyhavethesameattributes(‘domains’)

§  Q:howaboutintersectionU

23

Page 24: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Observations:

§  A:redundant:§  STUDENTintersectionSTAFF=

STUDENT STAFF

24

Page 25: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Observations:

§  A:redundant:§  STUDENTintersectionSTAFF=

STUDENT STAFF

25

Page 26: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Observations:

§  A:redundant:§  STUDENTintersectionSTAFF=STUDENT-(STUDENT-STAFF)

STUDENT STAFF

26

Page 27: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Observations:

§  A:redundant:§  STUDENTintersectionSTAFF=STUDENT-(STUDENT-STAFF)

Doublenegation:We’llseeitagain,later…

27

Page 28: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

§  .§  .§  .§  setunion§  setdifference‘-’

Relationaloperators

U

28

Page 29: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Otheroperators?

§  eg,findallstudentson‘Mainstreet’§  A:‘selection’

)('' STUDENTstrmainaddress=σ

STUDENTSsn Name Address

123 smith main str234 jones forbes ave

29

Page 30: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Otheroperators?

§  Notice:selection(andrestofoperators)expecttables,andproducetables(->canbecascaded!!)

§  Forselection,ingeneral:

)(RELATIONconditionσ

30

Page 31: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Selection-examples

§  Findall‘Smiths’on‘MainSt.’

σ name='Smith '∧ address='Main st. ' (STUDENT )

‘condition’canbeanybooleancombinationof‘=‘,‘>’,‘>=‘,‘<>’,...

31

Page 32: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

§  selection§  .§  .§  setunion§  setdifferenceR-S

Relationaloperators

)(Rconditionσ

RUS

32

Page 33: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

§  selectionpicksrows-howaboutcolumns?§  A:‘projection’-eg.:findsallthe‘ssn’-removingduplicates

Relationaloperators

)(STUDENTssnπ

STUDENTSsn Name Address

123 smith main str234 jones forbes ave

33

Page 34: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Cascading:‘findssnofstudentson‘mainst.’

Relationaloperators

π ssn (σ address='main st ' (STUDENT ))

STUDENTSsn Name Address

123 smith main str234 jones forbes ave

34

Page 35: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

§  selection§  projection§  .§  setunion§  setdifferenceR-S

Relationaloperators

)(Rconditionσ

)(Rlistatt−π

RUS

35

Page 36: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Arewedoneyet?Q:Giveaquerywecannotansweryet!

Relationaloperators

36

Page 37: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

A:anyqueryacrosstwoormoretables,eg.,‘findnamesofstudentsin4604’

Q:whatextraoperatordoweneed??

Relationaloperators

STUDENTSsn Name Address

123 smith main str234 jones forbes ave

SSN c-id grade123 4604 A234 5614 B

37

Page 38: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

A:anyqueryacrosstwoormoretables,eg.,‘findnamesofstudentsin4604’

Q:whatextraoperatordoweneed??A:surprisingly,cartesianproductisenough!

Relationaloperators

STUDENTSsn Name Address

123 smith main str234 jones forbes ave

SSN c-id grade123 4604 A234 5614 B

38

Page 39: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Cartesianproduct

§  eg.,dog-breeding:MALExFEMALE§  givesallpossiblecouples

MALEnamespikespot

FEMALEnamelassieshiba

x =M.name F.namespike lassiespike shibaspot lassiespot shiba

39

Page 40: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

sowhat?

§  Eg.,howdowefindnamesofstudentstaking4604?

40

Page 41: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Cartesianproduct

§  A:

Ssn Name Address ssn cid grade123 smith main str 123 4604 A234 jones forbes ave 123 4604 A123 smith main str 234 5614 B234 jones forbes ave 234 5614 B

)(......... .. TAKESxSTUDENTssnTAKESssnSTUDENT =σ

41

Page 42: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Cartesianproduct

..σ cid=4604(σ STUDENT .ssn=TAKES.ssn (STUDENT x TAKES))

Ssn Name Address ssn cid grade123 smith main str 123 4604 A234 jones forbes ave 123 4604 A123 smith main str 234 5614 B234 jones forbes ave 234 5614 B

42

Page 43: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

π name(σ cid=4604(σ STUDENT .ssn=TAKES.ssn (STUDENT x TAKES)))

Ssn Name Address ssn cid grade123 smith main str 123 4604 A234 jones forbes ave 123 4604 A123 smith main str 234 5614 B234 jones forbes ave 234 5614 B

43

Page 44: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

§  selection§  projection§  cartesianproductMALExFEMALE§  setunion§  setdifferenceR-S

FUNDAMENTALRelationaloperators

)(Rconditionσ)(Rlistatt−π

RUS

44

Page 45: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Relationalops

§  Surprisingly,theyareenough,tohelpusansweralmostanyquerywewant!!

§  derived/convenienceoperators:– setintersection–  join(thetajoin,equi-join,naturaljoin)– ‘rename’operator– division

)(' RRρSR ÷

45

Page 46: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Joins

§  Equijoin:SR bSaR .. =▹◃ )(.. SRbSaR ×= =σ

46

Page 47: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Cartesianproduct

§  A: )(......... .. TAKESxSTUDENTssnTAKESssnSTUDENT =σ

Ssn Name Address ssn cid grade123 smith main str 123 4604 A234 jones forbes ave 123 4604 A123 smith main str 234 5614 B234 jones forbes ave 234 5614 B

47

Page 48: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Joins

§  Equijoin:§  theta-joins:generalizationofequi-join-anycondition

SR bSaR .. =▹◃ )(.. SRbSaR ×= =σSR θ▹◃

θ

48

Page 49: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Joins

§  verypopular:naturaljoin:RS§  likeequi-join,butitdropsduplicatecolumns:STUDENT(ssn,name,address)TAKES(ssn,cid,grade)

49

Page 50: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Joins

§  nat.joinhas5attributes TAKESSTUDENT ▹◃

TAKESSTUDENT ssnTAKESssnSTUDENT .. =▹◃equi-join:6

Ssn Name Address ssn cid grade123 smith main str 123 4604 A234 jones forbes ave 123 4604 A123 smith main str 234 5614 B234 jones forbes ave 234 5614 B

50

Page 51: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

NaturalJoins-nit-picking

§  ifnoattributesincommonbetweenR,S:nat.join->cartesianproduct

51

Page 52: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Overview-rel.algebra

§  fundamentaloperators§  derivedoperators

–  joinsetc–  rename– division

§  examples

52

Page 53: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Renameop.

§  Q:why?§  A:shorthand;self-joins;…§  forexample,findthegrand-parentsof‘Tom’,givenPC(parent-id,child-id)

)(BEFOREAFTERρ

53

Page 54: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Renameop.

§  PC(parent-id,child-id) PCPC▹◃

PCp-id c-idMary TomPeter MaryJohn Tom

PCp-id c-idMary TomPeter MaryJohn Tom

54

Page 55: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Renameop.

§  first,WRONGattempt:§  (why?howmanycolumns?)§  SecondWRONGattempt:

PCPC▹◃

PCPC idpPCidcPC −=− ..▹◃

55

Page 56: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Renameop.

§  weclearlyneedtwodifferentnamesforthesametable-hence,the‘rename’op.

PCPC idpPCidcPCPC −=− ..11 )( ▹◃ρ

56

Page 57: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Overview-rel.algebra

§  fundamentaloperators§  derivedoperators

–  joinsetc–  rename– division

§  examples

57

Page 58: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Division

§  Rarelyused,butpowerful.§  Example:findsuspicioussuppliers,ie.,suppliersthatsuppliedallthepartsinA_BOMB

58

Page 59: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Division

SHIPMENTs# p#s1 p1s2 p1s1 p2s3 p1s5 p3

ABOMBp#p1p2

BAD_Ss#s1÷ =

59

Example:findsuspicioussuppliers,ie.,suppliersthatsuppliedallthepartsinA_BOMB

Page 60: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Division

§  Observations:~reverseofcartesianproduct§  Itcanbederivedfromthe5fundamentaloperators(!!)

§  How?

60

Page 61: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Division

§  Answer:

§  Observation:find‘good’suppliers,andsubtract!(doublenegation)

]))([()( )()()( rsrrsr SRSRSR −×−=÷ −−− πππ

61

Page 62: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Division

§  Answer:

§  Observation:find‘good’suppliers,andsubtract!(doublenegation)

]))([()( )()()( rsrrsr SRSRSR −×−=÷ −−− πππ

SHIPMENTs# p#s1 p1s2 p1s1 p2s3 p1s5 p3

ABOMBp#p1p2

BAD_Ss#s1÷ =

62

Page 63: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Division

§  Answer:

]))([()( )()()( rsrrsr SRSRSR −×−=÷ −−− πππ

SHIPMENTs# p#s1 p1s2 p1s1 p2s3 p1s5 p3

ABOMBp#p1p2

BAD_Ss#s1÷ =

Allsuppliers

Allbadparts

63

Table‘r’Table‘s’

Page 64: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Division

§  Answer:

]))([()( )()()( rsrrsr SRSRSR −×−=÷ −−− πππ

SHIPMENTs# p#s1 p1s2 p1s1 p2s3 p1s5 p3

ABOMBp#p1p2

BAD_Ss#s1÷ =

allpossiblesuspiciousshipments

64

Table‘r’Table‘s’

Page 65: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Division

§  Answer:

]))([()( )()()( rsrrsr SRSRSR −×−=÷ −−− πππ

SHIPMENTs# p#s1 p1s2 p1s1 p2s3 p1s5 p3

ABOMBp#p1p2

BAD_Ss#s1÷ =

allpossiblesuspiciousshipmentsthatdidn’thappen

65

Table‘r’Table‘s’

Page 66: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Division

§  Answer:

]))([()( )()()( rsrrsr SRSRSR −×−=÷ −−− πππ

SHIPMENTs# p#s1 p1s2 p1s1 p2s3 p1s5 p3

ABOMBp#p1p2

BAD_Ss#s1÷ =

allsupplierswhomissedatleastonesuspiciousshipment,

i.e.:‘good’suppliers

66

Table‘r’Table‘s’

Page 67: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Overview-rel.algebra

§  fundamentaloperators§  derivedoperators

–  joinsetc–  rename– division

§  examples

67

Page 68: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Sampleschema

STUDENTSsn Name Address

123 smith main str234 jones forbes ave

CLASSc-id c-name units

4513 s.e. 24512 o.s. 2

SSN c-id grade123 4513 A234 4513 B

findnamesofstudentsthattake4604

68

TAKES

Page 69: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Examples

§  findnamesofstudentsthattake4604

69

Page 70: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Examples

§  findnamesofstudentsthattake4604

π name[σ c−id=4604 (STUDENT ▹◃TAKES)]

70

Page 71: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Sampleschema

STUDENTSsn Name Address

123 smith main str234 jones forbes ave

CLASSc-id c-name units

4613 s.e. 24612 o.s. 2

SSN c-id grade123 4613 A234 4613 B

findcoursenamesof‘smith’

71

TAKES

Page 72: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Examples

§  findcoursenamesof‘smith’

π c−name[σ name='smith ' (

STUDENT ▹◃TAKES ▹◃CLASS)]

72

Page 73: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Examples

§  findssnof‘overworked’students,ie.,thattake4612,4613,4604

73

Page 74: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Examples

§  findssnof‘overworked’students,ie.,thattake4612,4613,4604:almostcorrectanswer:

σ c−name=4612 (TAKES)∩σ c−name=4613(TAKES)∩σ c−name=4604 (TAKES)

74

Page 75: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Examples

§  findssnof‘overworked’students,ie.,thattake4612,4613,4604-Correctanswer:

π ssn[σ c−name=4612 (TAKES)]∩π ssn[σ c−name=4613(TAKES)]∩π ssn[σ c−name=4604 (TAKES)]

75

Page 76: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Examples

§  findssnofstudentsthatworkatleastashardasssn=123,ie.,theytakeallthecoursesofssn=123,andmaybemore

76

Page 77: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Sampleschema

STUDENTSsn Name Address

123 smith main str234 jones forbes ave

CLASSc-id c-name units

4613 s.e. 24612 o.s. 2

SSN c-id grade123 4613 A234 4613 B

77

TAKES

Page 78: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Examples

§  findssnofstudentsthatworkatleastashardasssn=123(ie.,theytakeallthecoursesofssn=123,andmaybemore

)]([)]([ 123, TAKESTAKES ssnidcidcssn =−− ÷ σππ

78

Page 79: CS 4604: Introduction to Database Management Systemscourses.cs.vt.edu/~cs4604/Fall18/lectures/lecture-2.pdf · 2018-12-07 · • Conceptual model: In databases, structures are at

Prakash2018 VTCS4604

Conclusions

§  Relationalmodel:onlytables(‘relations’)§  relationalalgebra:powerful,minimal:5operatorscanhandlealmostanyquery!

79