# 1 Problem Session 1 Products and Joins. 2 Outline uAnnouncements uSynopsis of Last Week uProducts and Joins – formal expressions, examples uProducts and.

## Presentation on theme: "1 Problem Session 1 Products and Joins. 2 Outline uAnnouncements uSynopsis of Last Week uProducts and Joins – formal expressions, examples uProducts and."— Presentation transcript:

1 Problem Session 1 Products and Joins

2 Outline uAnnouncements uSynopsis of Last Week uProducts and Joins – formal expressions, examples uProducts and Joins – sample problems uQ&A

3 Announcements uMake sure you’re on the CS145 Coursework site by today uGradiance - one due Mon 10/8, two due Wed 10/10 uChallenge Problems #1 due Mon 10/8 wCorrection: #2 – can use union, intersection, difference as well wSubmit directly through Coursework (text in Assignments preferred, though.doc or.pdf in Drop Box acceptable) uAlways email your questions to the staff list!

4 Synopsis of Last Week uData model: general conceptual way of structuring data wRelational model = tables (e.g., SQL) wSemistructured model = trees/graphs (e.g., XML) uSchema: structure of a particular relation or database under a certain data model uXML: language for semistructured model. DTD describes the structure. uRelational Algebra: algebra operating on relations. Prelude to SQL. uSQL: select-from-where

5 Products and Joins uProduct: R1 X R2 = {(t1, t2) : t1 in R1 and t2 in R2}  Theta Join: R1 ⋈ C R2 = σ C (R1 X R2)  Natural Join: R1 ⋈ R2 = π schema(R1) SETUNION schema(R2) (R1 ⋈ R1.A=R2.A and R1.B=R2.B and… R2)

6 Example: Product R1(A,B ) 12 34 R2(B,C ) 56 48 210 R3(A,R1.B,R2.B,C ) 1256 1248 12210 3456 3448 34210 R3 := R1 X R2

7 Example: Theta Join R1(A,B ) 12 34 R2(B,C ) 56 48 210 R3 := R1 ⋈ R1.B=R2.B R2 R3(A,R1.B,R2.B,C ) 1256 1248 12210 3456 3448 34210

8 Example: Natural Join R1(A,B ) 12 34 R2(B,C ) 56 48 210 R3 := R1 ⋈ R2 R3(A,R1.B,R2.B,C ) 1256 1248 12210 3456 3448 34210

9 Sample Problem #1 Drinkers(name, addr, phone) Likes(drinker, beer) Find names and addresses of all drinkers who like Bud.

10 Sample Problem #1 Drinkers(name, addr, phone) Likes(drinker, beer) Find names and addresses of all drinkers who like Bud. Drinkers Likes σ name=drinker σ beer=‘Bud’ π name, addr X uMethod 1: filter, then concatenate uQuestion: how would you write this in SQL? uOne Possible Answer: SELECT name, addr FROM Drinkers, ( SELECT * FROM Likes WHERE beer = ‘Bud’) WHERE name = drinker;

11 Sample Problem #1 Drinkers(name, addr, phone) Likes(drinker, beer) Find names and addresses of all drinkers who like Bud. Drinkers Likes σ name=drinker π name, addr X uMethod 2: concatenate, then filter uQuestion: how would you write this in SQL? uOne Possible Answer: SELECT name, addr FROM Drinkers, Likes WHERE name = drinker AND beer = ‘Bud’; AND beer=‘Bud’

12 Sample Problem #2 Drinkers(name, addr, phone) Find names of all pairs of drinkers who live at the same address.

13 Sample Problem #2 Drinkers(name, addr, phone) Find names of all pairs of drinkers who live at the same address. Drinkers σ name { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/11/3336991/slides/slide_13.jpg", "name": "13 Sample Problem #2 Drinkers(name, addr, phone) Find names of all pairs of drinkers who live at the same address.", "description": "Drinkers σ name

14 Q&A uWhat’s the difference between the relational and semistructured models? wSee first lecture. Basically, relational = rigid tables and semistructured = flexible graphs. uThe semistructured model is supposed to be flexible, but can’t you represent everything in relational algebra if you plan far enough ahead? wWell, in reality there are a lot of things that you can’t possibly plan ahead for, so you need the semistructured model for that.

Download ppt "1 Problem Session 1 Products and Joins. 2 Outline uAnnouncements uSynopsis of Last Week uProducts and Joins – formal expressions, examples uProducts and."

Similar presentations