 CS 405G: Introduction to Database Systems Lecture 7: Relational Algebra II Instructor: Chen Qian Spring 2014.

Slides:



Advertisements
Similar presentations
1 Advanced SQL Queries. 2 Example Tables Used Reserves sidbidday /10/04 11/12/04 Sailors sidsnameratingage Dustin Lubber Rusty.
Advertisements

Relational Algebra Rohit Khokher. Relational Algebra Set Oriented Operations UnionIntersectionDifference Cartesian Product Relation Oriented Operations.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
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.
Ver 1,12/09/2012Kode :CCs 111,sistem basisdataFASILKOM Chapter 2: Relational Model Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A Modified by Donghui Zhang.
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
1 Today’s Class  Relational Model  SQL CS F212 Database Systems.
Relational Algebra – Basis for Relational Query Languages Based on presentation by Juliana Freire.
CMSC424, Spring 2005 CMSC424: Database Design Lecture 5.
Chapter 2 Relational Model (part II) Hankz Hankui Zhuo
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Extended.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Tuple Relational.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 90 Database Systems I SQL Queries.
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Outer Join Aggregate Functions.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
CS405G: Introduction to Database Systems Final Review.
CSCD343- Introduction to databases- A. Vaisman1 Relational Algebra.
Relational algebra SHIRAJ MOHAMED M | MIS 1. Relational algebra Notation SHIRAJ MOHAMED M | MIS 2.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
CSc-340 2b1 Introduction to SQL Chapter 3 [2 of 2] Null Values Aggregate Functions Nested Subqueries Modification of the Database.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 5 th Edition, Oct 5, 2006 Outer Join n An extension of the join operation that avoids loss.
Chapter 6: Formal Relational Query Languages. 6.2 Chapter 6: Formal Relational Query Languages Relational Algebra Tuple Relational Calculus Domain Relational.
Instructor: Jinze Liu Fall Basic Components (2) Relational Database Web-Interface Done before mid-term Must-Have Components (2) Security: access.
SQL: Queries, Programming, Triggers. Example Instances We will use these instances of the Sailors and Reserves relations in our examples. If the key for.
Computing & Information Sciences Kansas State University Monday, 08 Sep 2008CIS 560: Database System Concepts Lecture 5 of 42 Monday, 08 September 2008.
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany) DIRECTOR ARUNAI ENGINEERING COLLEGE TIRUVANNAMALAI.
Yufis Azhar – Teknik Informatika – UMM.  Aggregation function takes a collection of values (of a single attribute) and returns a single value as a result.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 5 th Edition, Oct 5, 2006 Example.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Aggregate Functions Outer Join.
 CS 405G: Introduction to Database Systems Lecture 6: Relational Algebra Instructor: Chen Qian.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
CMPT 258 Database Systems Relational Algebra (Chapter 4)
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 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
IS 230Lecture 7Slide 1 Relational Algebra Lecture 8 Text Book – Chapter.
Chapter 3: Relational Model III Additional Relational Algebra Operations Additional Relational Algebra Operations Views Views.
Database System Concepts, 6 th Ed. Chapter 6: Formal Relational Query Languages.
Relational Algebra Instructor: Mohamed Eltabakh 1.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Basic SQL Queries.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 6: Formal Relational.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 6: Formal Relational.
01/31/11 SQL Examples Edited by John Shieh CS3754 Classnote #10.
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany)
Relational Algebra - Part 1
Chapter 3 Introduction to SQL(3)
Chapter 3: Relational Model III
SQL The Query Language R & G - Chapter 5
Chapter 2: Intro to Relational Model
Relational Algebra Chapter 4, Part A
CS 405G: Introduction to Database Systems
Chapter 2: Relational Model
CS 405G: Introduction to Database Systems
Lecture 4 of 42 Relational Joins Wednesday, 30 January 2008
CS 405G: Introduction to Database Systems
Session 3 Welcome: To session 3-the sixth learning sequence
Instructor: Mohamed Eltabakh
SQL: Structured Query Language
CS 405G: Introduction to Database Systems
Relational Algebra Chapter 4 - part I.
Recap – Relational languages
CS 405G: Introduction to Database Systems
Presentation transcript:

 CS 405G: Introduction to Database Systems Lecture 7: Relational Algebra II Instructor: Chen Qian Spring 2014

(1) 7 points. 0.5 for each mistake 6/9/20152

(2) 3 points. No. If starting and ending dates are recorded in multi- valued attributes, we cannot determine which starting date is corresponding to which ending data. 6/9/20153

Review: Summary of core operators Selection: Projection: Cross product: Union: Difference: Renaming: 6/9/2015Chen University of Kentucky σp Rσp R πL RπL R R X SR X S R SR S R - S ρ S(A 1, A 2, …) R 4

Review Summary of derived operators Join: Natural join: Intersection: 6/9/2015Chen University of Kentucky R p S R S Many more Outer join, Division, Semijoin, anti-semijoin, … 5

6/9/2015Chen University of Kentucky Using Join What are the ids of Lisa’s classes? 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 Lisa’s classes  π CID Lisa_Enrolled 6

6/9/2015Chen University of Kentucky Join Example sidnameagegpa 1234John Mary Lisa222.6 sidnameagegpa 1012Lisa222.6 sidcidgrade A A B sidnameagegpacidgrade 1012Lisa A 1012Lisa B σ name=“Lisa” π cid cid

6/9/2015Chen University of Kentucky IDs of Lisa’s Classes π CID ( ( σ name = “Lisa” Student) Enrolled) Enroll π CID IDs of Lisa’s classes Student σ name = “Lisa” Who’s Lisa? 8

6/9/2015Chen 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? 9

6/9/2015Chen University of Kentucky Tips in Relational Algebra Use temporary variables Use foreign keys to join tables 10

6/9/2015Chen University of Kentucky An exercise Names of students in Lisa’s classes Students in Lisa’s classes Student π name Their names Enroll π SID Enroll π CID Lisa’s classes Student σ name = “Lisa” Who’s Lisa? 11

6/9/2015Chen University of Kentucky Set Minus (difference) Operation 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 12

6/9/2015Chen University of Kentucky Renaming Operation  Enrolled1(SID1, CID1,Grade1) Enrolled sidcidgrade A A sid1cid1grade A A  Enroll1(SID1, CID1,Grade1) 13

6/9/2015Chen 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 π SID (Enrolled Enrolled.SID = Enrolled.SID & Enrolled.CID  Enrolled.CID Enrolled) 14

6/9/2015Chen University of Kentucky Example (cont.)  Enroll1(SID1, CID1,Grade1) Enrolled  Enroll2(SID2, CID2,Grade2) Enrolled π SID (Enroll1 SID1 = SID2 & CID1  CID2 Enroll2) ρ Enroll1(SID1, CID1,Grade1) ρ Enroll2(SID2, CID2, Grade2) Enroll SID1 = SID2 & CID1  CID2 π SID1 Expression tree syntax: 15

How does it work? 6/9/2015Chen University of Kentucky sidcidgrade A A B sid2cid2grade A A B Enroll1 SID1 = SID2 Enroll2 sidcidgradesid2cid2grade A A A A A B B A B B 16

6/9/2015Chen University of Kentucky sidcidgrade A A B sid2cid2grade A A B Enroll1 SID1 = SID2 & CID1  CID2 Enroll2 sidcidgradesid2cid2grade A A A A A B B A B B 17

6/9/2015Chen University of Kentucky Tips in Relational Algebra A comparison is to identify a relationship 18

6/9/2015Chen 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 - ρ Student1 ρ Student2 Student1.GPA < Student2.GPA π Student1.SID A deeper question: When (and why) is “-” needed? 19

6/9/2015Chen University of Kentucky Review: Summary of core operators Selection: Projection: Cross product: Union: Difference: Renaming: σp Rσp R πL RπL R R X SR X S R SR S R - S ρ S(A 1, A 2, …) R 20

6/9/2015Chen University of Kentucky Review: Summary of derived operators Join: Natural join: Intersection: R p S R S 21

6/9/2015Chen University of Kentucky Review Relational algebra Use temporary variable Use foreign key to join relations A comparison is to identify a relationship 22

Exercises of R. A. Reserves Sailors Boats Chen University of Kentucky6/9/201523

Problem 1 Find names of sailors who’ve reserved boat #103 Solution: Sailors π sname Who reserved boat #103? Reserves σ bid = “103” Boat #103 Chen University of Kentucky6/9/201524

Information about boat color only available in Boats; so need an extra join: Problem 2: Find names of sailors who’ve reserved a red boat Names of sailors who reserved red boat Sailors π sname Reserve π SID Who reserved red boats? Boat σ color = “red” Red boats Chen University of Kentucky6/9/201525

Problem 3: Find names of sailors who’ve reserved a red boat or a green boat Can identify all red or green boats, then find sailors who’ve reserved one of these boats: Names of sailors who reserved red boat Sailors π sname Reserve π SID Who reserved red boats? Boat σ color = “red”  color = “green” Red boats Chen University of Kentucky6/9/201526

Problem 4: Find names of sailors who’ve reserved only one boat Chen University of Kentucky6/9/201527

6/9/2015Chen University of Kentucky Monotone operators 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’ ) RelOp Add more rows to the input... What happens to the output? 28

6/9/2015Chen 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! 29

6/9/2015Chen 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 Monotone 30

6/9/2015Chen 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? 31

Aggregate Functions and Operations Aggregation function takes a collection of values and returns a single value as a result. avg: average value min: minimum value max: maximum value sum: sum of values count: number of values Aggregate operation in relational algebra G1, G2, …, Gn g F1( A1), F2( A2),…, Fn( An) (E) E is any relational-algebra expression G 1, G 2 …, G n is a list of attributes on which to group (can be empty) Each F i is an aggregate function Each A i is an attribute name 6/9/2015Chen University of Kentucky32

Aggregate Operation – Example Relation r: AB   C g sum(c) (r) sum-C 27 6/9/2015Chen University of Kentucky33

Aggregate Operation – Example Relation account grouped by branch-name: branch-name g sum(balance) (account) branch-nameaccount-numberbalance Perryridge Brighton Redwood A-102 A-201 A-217 A-215 A branch-namebalance Perryridge Brighton Redwood /9/2015Chen University of Kentucky34

Null Values It is possible for tuples to have a null value, denoted by null, for some of their attributes null signifies an unknown value or that a value does not exist. The result of any arithmetic expression involving null is null. Aggregate functions simply ignore null values For duplicate elimination and grouping, null is treated like any other value, and two nulls are assumed to be the same 6/9/2015Chen University of Kentucky35

Null Values Comparisons with null values return the special truth value unknown If false was used instead of unknown, then not (A = 5 Three-valued logic using the truth value unknown: OR: (unknown or true) = true, (unknown or false) = unknown (unknown or unknown) = unknown AND: (true and unknown) = unknown, (false and unknown) = false, (unknown and unknown) = unknown NOT: (not unknown) = unknown Result of select predicate is treated as false if it evaluates to unknown 6/9/2015Chen University of Kentucky36

6/9/2015Chen 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 difference in non-monotonic results 37

How to write answers to a R.A. problem Go ahead to write down a single expression as long as you think it is correct. However, the followings are recommended: Draw an expression tree Write down any intermediate expressions temporary variables renaming operations Because then you can get partial credits! 6/9/2015Chen University of Kentucky38