CS 405G: Introduction to Database Systems

Slides:



Advertisements
Similar presentations
IS698: Database Management Min Song IS NJIT. The Relational Data Model.
Advertisements

Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Relational Algebra Tim Kaddoura CS157A. Introduction  Relational query languages are languages for describing queries on a relational database  Three.
1 CHAPTER 4 RELATIONAL ALGEBRA AND CALCULUS. 2 Introduction - We discuss here two mathematical formalisms which can be used as the basis for stating and.
D ATABASE S YSTEMS I R ELATIONAL A LGEBRA. 22 R ELATIONAL Q UERY L ANGUAGES Query languages (QL): Allow manipulation and retrieval of data from a database.
Relational Algebra Ch. 7.4 – 7.6 John Ortiz. Lecture 4Relational Algebra2 Relational Query Languages  Query languages: allow manipulation and retrieval.
INFS614, Fall 08 1 Relational Algebra Lecture 4. INFS614, Fall 08 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of.
 CS 405G: Introduction to Database Systems Lecture 7: Relational Algebra II Instructor: Chen Qian Spring 2014.
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
CS405G: Introduction to Database Systems Final Review.
1 Relational Algebra and Calculus Chapter 4. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.
MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 1 ©Akhilesh Bajaj, 2000, 2002, 2003, All.
Instructor: Jinze Liu Fall Basic Components (2) Relational Database Web-Interface Done before mid-term Must-Have Components (2) Security: access.
Relational Algebra  Souhad M. Daraghma. Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational.
1.1 CAS CS 460/660 Introduction to Database Systems Relational Algebra.
ICS 321 Fall 2011 The Relational Model of Data (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 8/29/20111Lipyeow.
Instructor: Jinze Liu Fall Phases of Database Design u Conceptual design begins with the collection of requirements and results needed from the.
 CS 405G: Introduction to Database Systems Lecture 6: Relational Algebra Instructor: Chen Qian.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
CS 405G: Introduction to Database Systems Relational Algebra.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2007.
CS 405G: Introduction to Database Systems SQL III.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu.
1 CSE 480: Database Systems Lecture 16: Relational Algebra.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2007.
Relational Algebra Instructor: Mohamed Eltabakh 1.
1 Relational Algebra and SQL. 2 Relational Query Languages Languages for describing queries on a relational database Relational AlgebraRelational Algebra.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 6 th Edition Chapter 8: Relational Algebra.
Relational Algebra & Calculus
Ritu CHaturvedi Some figures are adapted from T. COnnolly
CSE202 Database Management Systems
Database Systems Chapter 6
Chapter (6) The Relational Algebra and Relational Calculus Objectives
Basic Operations Algebra of Bags
Relational Algebra Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
Module 2: Intro to Relational Model
Relational Algebra Chapter 4 1.
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Relational Algebra Chapter 4, Part A
EECS 647: Introduction to Database Systems
CS 405G: Introduction to Database Systems
Translation of ER-diagram into Relational Schema
Database Management Systems (CS 564)
Database Management Systems (CS 564)
Relational Algebra 461 The slides for this text are organized into chapters. This lecture covers relational algebra, from Chapter 4. The relational calculus.
CS 405G: Introduction to Database Systems
CS405G: Introduction to Database Systems
CS 405G: Introduction to Database Systems
CS 405G: Introduction to Database Systems
Relational Algebra Chapter 4 1.
The Relational Algebra and Relational Calculus
Relational Algebra Chapter 4 - part I.
Chapter 6: Formal Relational Query Languages
Instructor: Mohamed Eltabakh
Relational Algebra Chapter 4, Sections 4.1 – 4.2
Chapter 2: Intro to Relational Model
Lecture 33: The Relational Model 2
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
CS639: Data Management for Data Science
Example of a Relation attributes (or columns) tuples (or rows)
Chapter 2: Intro to Relational Model
CS 505: Intermediate Topics to Database Systems
Relational Algebra & Calculus
CS 405G: Introduction to Database Systems
Lecture 2 Relational Database
Recap – Relational languages
CS 405G: Introduction to Database Systems
Presentation transcript:

CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2017

Review: Summary of core operators Selection: Projection: Cross product: Union: Difference: Renaming: Does not really add “processing” power σp R πL R R X S R S R - S ρ S(A1, A2, …) R 2/24/2019 Jinze Liu @ University of Kentucky

Review: Summary of derived operators Join: Natural join: Intersection: R p S R  S R S Many more Outer join, Division, Semijoin, anti-semijoin, … 2/24/2019 Jinze Liu @ University of Kentucky

Jinze Liu @ University of Kentucky Using Join Which classes is Lisa taking? Student(sid: string, name: string, gpa: float) Course(cid: string, department: string) Enrolled(sid: string, cid: string, grade: character) An Answer: Student_Lisa  σname = “Lisa”Student Lisa_Enrolled  Student_Lisa Enrolled Lisa’s classes  πCID Lisa_Enrolled Or: Student_Enrolled  Student Enrolled Lisa_Enrolled  σname = “Lisa” Student_Enrolled 2/24/2019 Jinze Liu @ University of Kentucky

Jinze Liu @ University of Kentucky Join Example sid name age gpa 1234 John 21 3.5 1123 Mary 22 3.8 1012 Lisa 2.6 sid name age gpa 1012 Lisa 22 2.6 σname = “Lisa”  sid cid grade 1123 108 A 1012 647 B sid name age gpa cid grade 1012 Lisa 22 2.6 647 A 108 B cid 647 108 πcid 2/24/2019 Jinze Liu @ University of Kentucky

Jinze Liu @ University of Kentucky Lisa’s Class π CID( (σname = “Lisa”Student) Enrolled) Enroll  πCID Lisa’s classes Student σname = “Lisa” Who’s Lisa? 2/24/2019 Jinze Liu @ University of Kentucky

Students in Lisa’s Classes SID of Students in Lisa’s classes Student_Lisa  σname = “Lisa”Student Lisa_Enrolled  Student_Lisa Enrolled Lisa’s classes  πCID Lisa_Enrolled Enrollment in Lisa’s classes  Lisa’s classes Enrolled Students in Lisa’s class  πSID Enrollment in Lisa’s classes Students in Lisa’s classes Enroll  πSID Enroll  πCID Lisa’s classes Student σname = “Lisa” Who’s Lisa? 2/24/2019 Jinze Liu @ University of Kentucky

Tips in Relational Algebra Use temporary variables Use foreign keys to join tables 2/24/2019 Jinze Liu @ University of Kentucky

An exercise πname  πSID Student  πCID Enroll  σname = “Lisa” Enroll Names of students in Lisa’s classes Student  πname Their names Students in Lisa’s classes Enroll  πSID Enroll  πCID Lisa’s classes Student σname = “Lisa” Who’s Lisa? 2/24/2019 Jinze Liu @ University of Kentucky

Set Minus Operation - πCID Course Enroll Student  σname = “Lisa” πCID CID’s of the courses that Lisa is NOT taking CID’s of the courses that Lisa IS taking All CID’s - πCID Course Enroll Student  σname = “Lisa” πCID 2/24/2019 Jinze Liu @ University of Kentucky

Renaming Operation Enrolled1(SID1, CID1,Grade1) Enrolled 1234 647 A 1123 108 sid1 cid1 grade1 1234 647 A 1123 108 Enroll1(SID1, CID1,Grade1) 2/24/2019 Jinze Liu @ University of Kentucky

Jinze Liu @ University of Kentucky Example We have the following relational schemas Student(sid: string, name: string, gpa: float) Course(cid: string, department: string) Enrolled(sid: string, cid: string, grade: character) SID’s of students who take at least two courses Enrolled Enrolled πSID (Enrolled Enrolled.SID = Enrolled.SID & Enrolled.CID ¹ Enrolled.CID Enrolled) 2/24/2019 Jinze Liu @ University of Kentucky

Jinze Liu @ University of Kentucky Example (cont.) Enroll1(SID1, CID1,Grade1) Enrolled Enroll2(SID2, CID2,Grade2) Enrolled πSID (Enroll1 SID1 = SID2 & CID1 ¹ CID2Enroll2) SID1 = SID2 & CID1 ¹ CID2 πSID1 Expression tree syntax: ρEnroll1(SID1, CID1,Grade1) ρEnroll2(SID2, CID2, Grade2) Enroll 2/24/2019 Jinze Liu @ University of Kentucky

Tips in Relational Algebra A comparison is to identify a relationship 2/24/2019 Jinze Liu @ University of Kentucky

Jinze Liu @ University of Kentucky A trickier exercise Who has the highest GPA? Who has a GPA? Who does NOT have the highest GPA? Whose GPA is lower than somebody else’s? - πSID Student Student ρStudent1 ρStudent2 Student1.GPA < Student2.GPA πStudent1.SID A deeper question: When (and why) is “-” needed? 2/24/2019 Jinze Liu @ University of Kentucky

Jinze Liu @ University of Kentucky Monotone operators RelOp What happens to the output? Add more rows to the input... If some old output rows may need to be removed Then the operator is non-monotone Otherwise the operator is monotone That is, old output rows always remain “correct” when more rows are added to the input Formally, for a monotone operator op: R µ R’ implies op( R ) µ op( R’ ) 2/24/2019 Jinze Liu @ University of Kentucky

Classification of relational operators Selection: σp R Projection: πL R Cross product: R X S Join: R p S Natural join: R  S Union: R U S Difference: R - S Intersection: R ∩ S Monotone Monotone w.r.t. R; non-monotone w.r.t S 2/24/2019 Jinze Liu @ University of Kentucky

Why is “-” needed for highest GPA? Composition of monotone operators produces a monotone query Old output rows remain “correct” when more rows are added to the input Highest-GPA query is non-monotone Current highest GPA is 4.1 Add another GPA 4.2 Old answer is invalidated So it must use difference! 2/24/2019 Jinze Liu @ University of Kentucky

Why do we need core operator X? Cross product The only operator that adds columns Difference The only non-monotone operator Union The only operator that allows you to add rows? Selection? Projection? 2/24/2019 Jinze Liu @ University of Kentucky

Jinze Liu @ University of Kentucky Additional Operators Outer join Division 2/24/2019 Jinze Liu @ University of Kentucky

Jinze Liu @ University of Kentucky (Left) Outer Join Input: two tables R and S Notation: R P S Purpose: pairs rows from two tables Output: for each row r in R and each row s in S, if p satisfies, output a row rs (concatenation of r and s) Otherwise, output a row r with NULLs Right outer join and full outer join are defined similarly 2/24/2019 Jinze Liu @ University of Kentucky

Left Outer Join Example Employee Eid = Mid Department Did Mid Dname 4 1234 Research 5 1123 Finance Eid Name 1234 John Smith 1123 Mary Carter 1011 Bob Lee Eid = Mid Eid Name Did Mid Dname 1234 John Smith 4 Research 1123 Mary Carter 5 Finance 1011 Bob Lee NULL 2/24/2019 Jinze Liu @ University of Kentucky

Jinze Liu @ University of Kentucky Division Operator Input: two tables R and S Notation: R  S Purpose: Find the subset of items in one set R that are related to all items in another set 2/24/2019 Jinze Liu @ University of Kentucky

Jinze Liu @ University of Kentucky Query type: Find the subset of items in one set that are related to all items in another set Example: Find professors who have taught courses in all departments Why does this involve division? ProfId DeptId DeptId Contains row <p,d> if professor p has taught a course in department d All department Ids 24 2/24/2019 Jinze Liu @ University of Kentucky

Why is r.a. a good query language? Simple A small set of core operators who semantics are easy to grasp Declarative? Yes, compared with older languages like CODASYL Though operators do look somewhat “procedural” Complete? With respect to what? 2/24/2019 Jinze Liu @ University of Kentucky

Jinze Liu @ University of Kentucky Turing machine? Relational algebra has no recursion Example of something not expressible in relational algebra: Given relation Parent(parent, child), who are Bart’s ancestors? Why not Turing machine? Optimization becomes undecidable You can always implement it at the application level Recursion is added to SQL nevertheless! 2/24/2019 Jinze Liu @ University of Kentucky

Jinze Liu @ University of Kentucky Review Expression tree Tips in writing R.A. Use temporary variables Use foreign keys to join tables A comparison is to identify a relationship Use set minus in non-monotonic results 2/24/2019 Jinze Liu @ University of Kentucky