1 Relational Algebra. Motivation Write a Java program Translate it into a program in assembly language Execute the assembly language program As a rough.

Slides:



Advertisements
Similar presentations
1 Relational Algebra Basic Operations Algebra of Bags.
Advertisements

IS698: Database Management Min Song IS NJIT. The Relational Data Model.
1 Relational Algebra 1 Basic Operations. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which new values.
1 Relational Algebra* and Tuple Calculus * The slides in this lecture are adapted from slides used in Standford's CS145 course.
Tallahassee, Florida, 2014 COP4710 Database Systems Relational Algebra Fall 2014.
SQL Queries Principal form: SELECT desired attributes FROM tuple variables –– range over relations WHERE condition about tuple variables; Running example.
Lecture 07: Relational Algebra
Relational Algebra Indra Budi Fakultas Ilmu Komputer UI 2 n Basic Relational Operations: l Unary Operations  SELECT   PROJECT 
1 Relational Algebra Lecture #9. 2 Querying the Database Goal: specify what we want from our database Find all the employees who earn more than $50,000.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
1 Lecture 07: Relational Algebra. 2 Outline Relational Algebra (Section 6.1)
1 Relational Algebra Basic operators Relational algebra expression tree.
1 Relational Algebra Operators Expression Trees Bag Model of Data.
Relational Schema Design (end) Relational Algebra Finally, querying the database!
One More Normal Form Consider the dependencies: Product Company Company, State Product Is it in BCNF?
Relation Decomposition A, A, … A 12n Given a relation R with attributes Create two relations R1 and R2 with attributes B, B, … B 12m C, C, … C 12l Such.
Relational Algebra Basic Operations Algebra of Bags.
Relational Algebra CIS 4301 Lecture Notes Lecture /28/2006.
Databases 1 First lecture. Informations Lecture: Monday 12:15-13:45 (3.716) Practice: Thursday 10:15-11:45 (2-519) Website of the course:
Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
RELATIONAL DATA MODEL 1. 2 What is a Data Model? 1.Mathematical representation of data. wExamples: relational model = tables; semistructured model = trees/graphs.
1 Introduction to Database Systems CSE 444 Lecture 20: Query Execution: Relational Algebra May 21, 2008.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
Relational Algebra (Chapter 7)
The Relational Algebra and Calculus
1 Relational Algebra Basic Operations Algebra of Bags.
Databases : Relational Algebra 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes.
From Professor Ullman, Relational Algebra.
Database Management Systems Chapter 5 The Relational Algebra Instructor: Li Ma Department of Computer Science Texas Southern University, Houston October,
1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which.
1 Lecture 2 Relational Algebra Based on
Transactions, Relational Algebra, XML February 11 th, 2004.
CSE 544: Relational Operators, Sorting Wednesday, 5/12/2004.
1 Lecture 7: Normal Forms, Relational Algebra Monday, 10/15/2001.
Relational Algebra 2. Relational Algebra Formalism for creating new relations from existing ones Its place in the big picture: Declartive query language.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Advanced Relational Algebra & SQL (Part1 )
Databases : Relational Algebra - Complex Expression 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof.
Lecture 13: Relational Decomposition and Relational Algebra February 5 th, 2003.
The Relational Model Lecture 16. Today’s Lecture 1.The Relational Model & Relational Algebra 2.Relational Algebra Pt. II [Optional: may skip] 2 Lecture.
1 Lecture 10: Database Design and Relational Algebra Monday, October 20, 2003.
Relational Algebra BASIC OPERATIONS 1 DATABASE SYSTEMS AND CONCEPTS, CSCI 3030U, UOIT, COURSE INSTRUCTOR: JAREK SZLICHTA.
1 Relational Algebra Basic Operations Algebra of Bags.
1 CSE544: Lecture 7 XQuery, Relational Algebra Monday, 4/22/02.
1 Introduction to Database Systems, CS420 Relational Algebra.
1 Database Design: DBS CB, 2 nd Edition Relational Algebra: Basic Operations & Algebra of Bags Ch. 5.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
1. Chapter 2: The relational Database Modeling Section 2.4: An algebraic Query Language Chapter 5: Algebraic and logical Query Languages Section 5.1:
Ritu CHaturvedi Some figures are adapted from T. COnnolly
Summary of Relational Algebra
Basic Operations Algebra of Bags
Relational Algebra.
CPSC-310 Database Systems
Slides are reused by the approval of Jeffrey Ullman’s
COMP3017 Advanced Databases
COP4710 Database Systems Relational Algebra.
Fundamental of Database Systems
Relational Algebra at a Glance
Lecture 8: Relational Algebra
Operators Expression Trees Bag Model of Data
The Relational Algebra and Relational Calculus
Basic Operations Algebra of Bags
Lecture 33: The Relational Model 2
Relational Algebra Friday, 11/14/2003.
CS639: Data Management for Data Science
Lecture 3: Relational Algebra and SQL
Lecture 11: Functional Dependencies
Select-From-Where Statements Multirelation Queries Subqueries
Presentation transcript:

1 Relational Algebra

Motivation Write a Java program Translate it into a program in assembly language Execute the assembly language program As a rough analogy, here we write a SQL query Translate it into a program in relational algebra language Execute the program 2

3 Example Sells(bar,beer,price )Bars(bar,addr ) Joe’sBud2.50Joe’sMaple St. Joe’sMiller2.75Sue’sRiver Rd. Sue’sBud2.50 Sue’sCoors3.00 Query: Find all locations that sell beer for less than Select addr From Sells, Bars Where (Sells.bar = Bars.bar) and (Sells.price < 2.75)

4 Translating into a program in RA Sells Bars JOIN Sells.bar = Bars.bar PROJECT Bars.addr SELECT Sells.price < 2.75

5 Relational Algebra at a Glance Operators: relations as input, new relation as output Five basic RA operations: –Basic Set Operations union, difference (no intersection, no complement) –Selection:  –Projection:  –Cartesian Product: X When our relations have attribute names: –Renaming:  Derived operations: –Intersection, complement –Joins (natural,equi-join, theta join, semi-join)

6 Five Basic RA Operations

7 Set Operations Union, difference Binary operations

8 Set Operations: Union Union: all tuples in R1 or R2 Notation: R1 U R2 R1, R2 must have the same schema R1 U R2 has the same schema as R1, R2 Example: –ActiveEmployees U RetiredEmployees

9 Set Operations: Difference Difference: all tuples in R1 and not in R2 Notation: R1 – R2 R1, R2 must have the same schema R1 - R2 has the same schema as R1, R2 Example –AllEmployees - RetiredEmployees

10 Selection Returns all tuples which satisfy a condition Notation:  c (R) c is a condition: =,, and, or, not Output schema: same as input schema Find all employees with salary more than $40,000: –  Salary > (Employee)

11 Find all employees with salary more than $40,000.  Salary > (Employee)

12 Ullman: Selection R1 := SELECT C (R2) –C is a condition (as in “if” statements) that refers to attributes of R2. –R1 is all those tuples of R2 that satisfy C.

13 Example Relation Sells: barbeerprice Joe’sBud2.50 Joe’sMiller2.75 Sue’sBud2.50 Sue’sMiller3.00 JoeMenu := SELECT bar=“Joe’s” (Sells): barbeerprice Joe’sBud2.50 Joe’sMiller2.75

14 Projection Unary operation: returns certain columns Eliminates duplicate tuples ! Notation:  A1,…,An (R) Input schema R(B1,…,Bm) Condition: {A1, …, An} {B1, …, Bm} Output schema S(A1,…,An) Example: project social-security number and names: –  SSN, Name (Employee)

15  SSN, Name (Employee)

16 Projection R1 := PROJ L (R2) –L is a list of attributes from the schema of R2. –R1 is constructed by looking at each tuple of R2, extracting the attributes on list L, in the order specified, and creating from those components a tuple for R1. –Eliminate duplicate tuples, if any.

17 Example Relation Sells: barbeerprice Joe’sBud2.50 Joe’sMiller2.75 Sue’sBud2.50 Sue’sMiller3.00 Prices := PROJ beer,price (Sells): beerprice Bud2.50 Miller2.75 Miller3.00

18 Cartesian Product Each tuple in R1 with each tuple in R2 Notation: R1 x R2 Input schemas R1(A1,…,An), R2(B1,…,Bm) Condition: {A1,…,An} {B1,…Bm} =  Output schema is S(A1, …, An, B1, …, Bm) Notation: R1 x R2 Example: Employee x Dependents Very rare in practice; but joins are very common

19

20 Product R3 := R1 * R2 –Pair each tuple t1 of R1 with each tuple t2 of R2. –Concatenation t1t2 is a tuple of R3. –Schema of R3 is the attributes of R1 and R2, in order. –But beware attribute A of the same name in R1 and R2: use R1.A and R2.A.

21 Example: R3 := R1 * R2 R1(A,B ) R2(B,C ) R3(A,R1.B,R2.B,C )

22 Renaming Does not change the relational instance Changes the relational schema only Notation:  B1,…,Bn (R) Input schema: R(A1, …, An) Output schema: S(B1, …, Bn) Example:  LastName, SocSocNo (Employee)

23 Renaming Example Employee NameSSN John Tony LastNameSocSocNo John Tony  LastName, SocSocNo (Employee)

24 Renaming The RENAME operator gives a new schema to a relation. R1 := RENAME R1(A1,…,An) (R2) makes R1 be a relation with attributes A1,…,An and the same tuples as R2. Simplified notation: R1(A1,…,An) := R2.

25 Example Bars(name, addr ) Joe’sMaple St. Sue’sRiver Rd. R(bar, addr ) Joe’sMaple St. Sue’sRiver Rd. R(bar, addr) := Bars

26 Derived RA Operations 1) Intersection 2) Most importantly: Join

27 Set Operations: Intersection All tuples both in R1 and in R2 Notation: R1 R2 R1, R2 must have the same schema R1 R2 has the same schema as R1, R2 Example –UnionizedEmployees RetiredEmployees Intersection is derived: –R1 R2 = R1 – (R1 – R2) why ?

28 Joins Theta join Natural join Equi-join Semi-join Inner join Outer join etc.

29 Theta Join A join that involves a predicate Notation: R1  R2 where  is a condition Input schemas: R1(A1,…,An), R2(B1,…,Bm) {A1,…An} {B1,…,Bm} =  Output schema: S(A1,…,An,B1,…,Bm) Derived operator: R1  R2 =   (R1 x R2)

30 Theta-Join R3 := R1 JOIN C R2 –Take the product R1 * R2. –Then apply SELECT C to the result. As for SELECT, C can be any boolean-valued condition. –Historic versions of this operator allowed only A theta B, where theta was =, <, etc.; hence the name “theta- join.”

31 Example Sells(bar,beer,price )Bars(name,addr ) Joe’sBud2.50Joe’sMaple St. Joe’sMiller2.75Sue’sRiver Rd. Sue’sBud2.50 Sue’sCoors3.00 BarInfo := Sells JOIN Sells.bar = Bars.name Bars BarInfo(bar,beer,price,name,addr ) Joe’sBud2.50Joe’sMaple St. Joe’sMiller2.75Joe’sMaple St. Sue’sBud2.50Sue’sRiver Rd. Sue’sCoors3.00Sue’sRiver Rd.

32 Natural Join Notation: R1 R2 Input Schema: R1(A1, …, An), R2(B1, …, Bm) Output Schema: S(C1,…,Cp) –Where {C1, …, Cp} = {A1, …, An} U {B1, …, Bm} Meaning: combine all pairs of tuples in R1 and R2 that agree on the attributes: –{A1,…,An} {B1,…, Bm} (called the join attributes) Equivalent to a cross product followed by selection Example Employee Dependents

33 Natural Join Example Employee NameSSN John Tony Dependents SSNDname Emily Joe NameSSNDname John Emily Tony Joe Employee Dependents =  Name, SSN, Dname (  SSN=SSN2 (Employee x  SSN2, Dname (Dependents))

34 Natural Join R= S= R S = AB XY XZ YZ ZV BC ZU VW ZV ABC XZU XZV YZU YZV ZVW

35 Natural Join Given the schemas R(A, B, C, D), S(A, C, E), what is the schema of R S ? Given R(A, B, C), S(D, E), what is R S ? Given R(A, B), S(A, B), what is R S ?

36 Natural Join A frequent type of join connects two relations by: –Equating attributes of the same name, and –Projecting out one copy of each pair of equated attributes. Called natural join. Denoted R3 := R1 JOIN R2.

37 Example Sells(bar,beer,price )Bars(bar,addr ) Joe’sBud2.50Joe’sMaple St. Joe’sMiller2.75Sue’sRiver Rd. Sue’sBud2.50 Sue’sCoors3.00 BarInfo := Sells JOIN Bars Note Bars.name has become Bars.bar to make the natural join “work.” BarInfo(bar,beer,price,addr ) Joe’sBud2.50Maple St. Joe’sMilller2.75Maple St. Sue’sBud2.50River Rd. Sue’sCoors3.00River Rd.

38 Equi-join Most frequently used in practice: R1  R2 Natural join is a particular case of equi-join A lot of research on how to do it efficiently

39 Semijoin R S =  A1,…,An (R S) Where the schemas are: –Input: R(A1,…An), S(B1,…,Bm) –Output: T(A1,…,An)

40 Semijoin Applications in distributed databases: Product(pid, cid, pname,...) at site 1 Company(cid, cname,...) at site 2 Query:  price>1000 (Product) cid=cid Company Compute as follows: T1 =  price>1000 (Product) site 1 T2 = P cid (T1) site 1 send T2 to site 2 (T2 smaller than T1) T3 = T2 Company site 2 (semijoin) send T3 to site 1 (T3 smaller than Company) Answer = T3 T1 site 1 (semijoin)

41 Joins Theta join Natural join Equi-join Semi-join Inner join Outer join etc.

42 Relational Algebra at a Glance Operators: relations as input, new relation as output Five basic RA operations: –Basic Set Operations union, difference (no intersection, no complement) –Selection:  –Projection:  –Cartesian Product: X When our relations have attribute names: –Renaming:  Derived operations: –Intersection, complement –Joins (natural,equi-join, theta join, semi-join)

43 Building Complex Expressions Example –in arithmetic algebra: (x + 4)*(y - 3) Relational algebra allows the same. That is, we can build complex expressions using relational algebra operators Three notations, just as in arithmetic: 1.Sequences of assignment statements. 2.Expressions with several operators. 3.Expression trees.

44 Sequences of Assignments Create temporary relation names. Renaming can be implied by giving relations a list of attributes. Example: R3 := R1 JOIN C R2 can be written: R4 := R1 * R2 R3 := SELECT C (R4)

45 Expressions with Several Operators Example: the theta-join R3 := R1 JOIN C R2 can be written: R3 := SELECT C (R1 * R2) Precedence of relational operators: 1.Unary operators --- select, project, rename --- have highest precedence, bind first. 2.Then come products and joins. 3.Then intersection. 4.Finally, union and set difference bind last. wBut you can always insert parentheses to force the order you desire.

46 Expression Trees Leaves are operands --- either variables standing for relations or particular, constant relations. Interior nodes are operators, applied to their child or children.

47 Example Using the relations Bars(name, addr) and Sells(bar, beer, price), find the names of all the bars that are either on Maple St. or sell Bud for less than $3. Sells(bar,beer,price )Bars(name,addr ) Joe’sBud2.50Joe’sMaple St. Joe’sMiller2.75Sue’sRiver Rd. Sue’sBud2.50 Sue’sCoors3.00

48 As a Tree: BarsSells SELECT addr = “Maple St.” SELECT price<3 AND beer=“Bud” PROJECT name RENAME R(name) PROJECT bar UNION Using the relations Bars(name, addr) and Sells(bar, beer, price), find the names of all the bars that are either on Maple St. or sell Bud for less than $3. name Joe’s nameaddress Joe’sMaple St. name Joe’s Sue’s barbeerprice Joe’sBud2.5 Sue’sBud2.5 bar Joe’s Sue’s

49 Example Using Sells(bar, beer, price), find the bars that sell two different beers at the same price. Sells(bar,beer,price ) Joe’sBud2.50 Joe’sMiller2.75 Sue’sBud2.50 Joe’sCoors2.50

50 Strategy Sells natural-join S = (bar, beer, beer1, price) –need a selection condition though. What is it? Sells(bar,beer,price ) Joe’sBud2.50 Joe’sMiller2.75 Sue’sBud2.50 Joe’sCoors2.50 S(bar,beer1,price ) Joe’sBud2.50 Joe’sMiller2.75 Sue’sBud2.50 Joe’sCoors2.50 Using Sells(bar, beer, price), find the bars that sell two different beers at the same price.

51 Strategy Summary By renaming, define a copy of Sells, called S(bar, beer1, price). The natural join of Sells and S consists of quadruples (bar, beer, beer1, price) such that the bar sells both beers at this price.

52 The Tree Sells RENAME S(bar, beer1, price) JOIN PROJECT bar SELECT beer != beer1

53 Complex Queries Product ( pid, name, price, category, maker-cid) Purchase (buyer-ssn, seller-ssn, store, pid) Company (cid, name, stock price, country) Person(ssn, name, phone, city) Find phone numbers of people who bought gizmos from Fred. Find telephony products that somebody bought

54 Expression Tree Person Purchase Person Product  name=fred  name=gizmo  pid  ssn seller-ssn=ssnpid=pidbuyer-ssn=ssn  phone

55 Exercises Product ( pid, name, price, category, maker-cid) Purchase (buyer-ssn, seller-ssn, store, pid) Company (cid, name, stock price, country) Person(ssn, name, phone number, city) Ex #1: Find people who bought telephony products. Ex #2: Find names of people who bought American products

56 Exercises Product ( pid, name, price, category, maker-cid) Purchase (buyer-ssn, seller-ssn, store, pid) Company (cid, name, stock price, country) Person(ssn, name, phone number, city) Ex #3: Find names of people who bought American products and did not buy French products Ex #4: Find names of people who bought American products and they live in Madison.

57 Exercises Product ( pid, name, price, category, maker-cid) Purchase (buyer-ssn, seller-ssn, store, pid) Company (cid, name, stock price, country) Person(ssn, name, phone number, city) Ex #5: Find people who bought stuff from Joe or bought products from a company whose stock prices is more than $50.

58 Operations on Bags (and why we care) Union: {a,b,b,c} U {a,b,b,b,e,f,f} = {a,a,b,b,b,b,b,c,e,f,f} –add the number of occurrences Difference: {a,b,b,b,c,c} – {b,c,c,c,d} = {a,b,b,d} –subtract the number of occurrences Intersection: {a,b,b,b,c,c} {b,b,c,c,c,c,d} = {b,b,c,c} –minimum of the two numbers of occurrences Selection: preserve the number of occurrences Projection: preserve the number of occurrences (no duplicate elimination) Cartesian product, join: no duplicate elimination

59 Summary of Relational Algebra Why bother ? Can write any RA expression directly in C++/Java, seems easy. Two reasons: –Each operator admits sophisticated implementations (think of,  C ) –Expressions in relational algebra can be rewritten: optimized

60 Example Sells(bar,beer,price )Bars(bar,addr ) Joe’sBud2.50Joe’sMaple St. Joe’sMiller2.75Sue’sRiver Rd. Sue’sBud2.50 Sue’sCoors3.00 Query: Find all locations that sell beer for less than Select addr From Sells, Bars Where (Sells.bar = Bars.bar) and (Sells.price < 2.75)

61 Example Sells Bars JOIN Sells.bar = Bars.bar PROJECT Bars.addr SELECT Sells.price < 2.75

62 What is an “Algebra” Mathematical system consisting of: –Operands --- variables or values from which new values can be constructed. –Operators --- symbols denoting procedures that construct new values from given values.

63 What is Relational Algebra? An algebra whose operands are relations or variables that represent relations. Operators are designed to do the most common things that we need to do with relations in a database. –The result is an algebra that can be used as a query language for relations.

64 Glimpse Ahead: Efficient Implementations of Operators  (age >= 30 AND age <= 35) (Employees) –Method 1: scan the file, test each employee –Method 2: use an index on age –Which one is better ? Depends a lot… Employees Relatives –Iterate over Employees, then over Relatives –Iterate over Relatives, then over Employees –Sort Employees, Relatives, do “merge-join” –“hash-join” –etc

65 Glimpse Ahead: Optimizations Product ( pid, name, price, category, maker-cid) Purchase (buyer-ssn, seller-ssn, store, pid) Person(ssn, name, phone number, city) Which is better:  price>100 (Product) (Purchase  city=sea Person)  price>100 (Product) Purchase)  city=sea Person Depends ! This is the optimizer’s job…

66 Finally: RA has Limitations ! Cannot compute “transitive closure” Find all direct and indirect relatives of Fred Cannot express in RA !!! Need to write C program Name1Name2Relationship FredMaryFather MaryJoeCousin MaryBillSpouse NancyLouSister

Apply the Lessons You Have Learned Suppose you work at Facebook Now in charge of helping FB applications manage the FB friendship graph What should you do? 67