Summary of Relational Algebra

Slides:



Advertisements
Similar presentations
1 Relational Algebra* and Tuple Calculus * The slides in this lecture are adapted from slides used in Standford's CS145 course.
Advertisements

Tallahassee, Florida, 2014 COP4710 Database Systems Relational Algebra Fall 2014.
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.
1 Relational Algebra. Motivation Write a Java program Translate it into a program in assembly language Execute the assembly language program As a rough.
Relational Algebra (end) SQL April 19 th, Complex Queries Product ( pid, name, price, category, maker-cid) Purchase (buyer-ssn, seller-ssn, store,
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.
Relational Algebra Maybe -- SQL. Confused by Normal Forms ? 3NF BCNF 4NF If a database doesn’t violate 4NF (BCNF) then it doesn’t violate BCNF (3NF) !
1 Database Systems Relations as Bags Grouping and Aggregation Database Modification.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #3.
1 Lecture 07: Relational Algebra. 2 Outline Relational Algebra (Section 6.1)
1 Relational Algebra Basic operators Relational algebra expression tree.
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
1 Introduction to Database Systems CSE 444 Lecture 20: Query Execution: Relational Algebra May 21, 2008.
Relational Algebra (Chapter 7)
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.
1 Lecture 2 Relational Algebra Based on
Relational Algebra 2. Relational Algebra Formalism for creating new relations from existing ones Its place in the big picture: Declartive query language.
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.
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 Introduction to Database Systems, CS420 Relational Algebra.
1 Database Design: DBS CB, 2 nd Edition Relational Algebra: Basic Operations & Algebra of Bags Ch. 5.
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
Select-From-Where Statements Multirelation Queries Subqueries
Lecture 14: Relational Algebra Projects XML?
Basic Operations Algebra of Bags
Relational Algebra.
CPSC-310 Database Systems
Slides are reused by the approval of Jeffrey Ullman’s
COP4710 Database Systems Relational Algebra.
An Algebraic Query Language
Relational Algebra at a Glance
Relational Algebra Chapter 4 1.
Lecture 8: Relational Algebra
Schedule Today: Next After that Subqueries, Grouping and Aggregation.
Introduction to Database Systems, CS420
06a: SQL-1 The Basics– Select-From-Where
CS 440 Database Management Systems
Chapter 2: Intro to Relational Model
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.
Operators Expression Trees Bag Model of Data
Lecture 10: Relational Algebra (continued), Datalog
Relational Algebra Chapter 4 1.
CPSC-310 Database Systems
Instructor: Mohamed Eltabakh
Basic Operations Algebra of Bags
Lecture 9: Relational Algebra (continued), Datalog
Lecture 33: The Relational Model 2
Where are we? Until now: Modeling databases (ODL, E/R): all about the schema Now: Manipulating the data: queries, updates, SQL Then: looking inside -
Schedule Today: Next And then Relational Algebra.
Relational Algebra Friday, 11/14/2003.
Lecture 3: Relational Algebra and SQL
Syllabus Introduction Website Management Systems
CPSC-608 Database Systems
Lecture 11: Functional Dependencies
Instructor: Zhe He Department of Computer Science
Select-From-Where Statements Multirelation Queries Subqueries
Presentation transcript:

Summary of Relational Algebra Basic primitives: E ::= R | σC(E) | Π A1, A2, ..., An (E) | E1 X E2 | E1 U E2 | E1 - E2 | | ρS(A1, A2, …, An)(E) Abbreviations: | E1 E2 | E1 C E2 | E1 ∩ E2

The Joins and Cross Products Cross Product: R(A, B) X S (B, C) Schema (A, R.B, R.S, C) All pairs from R with all pairs of S Theta Join: R(A, B) Cond S (B, C) All pairs of R with all pairs of S minus those that don’t satisfy Cond Natural Join: R(A, B) S (B, C) Schema (A, B, C) All pairs of R with all pairs of S where R.B = S.B

Exercise #1: Natural Join How do we express using the basic operators? R(A, B) and S (B, C)

Exercise #1: Natural Join How do we express using the basic operators? R(A, B) and S (B, C) At least three solutions: ΠA, B, C (σ B=B1(R x ρB1, C(S))

Relational Algebra Six basic operators, many derived Combine operators in order to construct queries: relational algebra expressions

Building Complex Expressions Algebras allow us to express sequences of operations in a natural way. Example in arithmetic algebra: (x + 4)*(y - 3) Relational algebra allows the same. Three notations: Sequences of assignment statements. Expressions with several operators. Expression trees.

1. Sequences of Assignments Create temporary relation names. Renaming can be implied by giving relations a list of attributes. R3(X, Y) := R1 Example: R3 := R1 C R2 can be written: R4 := R1 x R2 R3 := σC (R4)

2. Expressions with Several Operators Precedence of relational operators: Unary operators --- select, project, rename --- have highest precedence, bind first. Then come products and joins. Then intersection. Finally, union and set difference bind last. But you can always insert parentheses to force the order you desire.

3. Expression Trees Leaves are operands (relations). Interior nodes are operators, applied to their child or children.

Example Given Bars(name, addr), Sells(bar, beer, price), find the names of all the bars that are either on Maple St. or sell Bud for less than $3.

As a Tree: UNION RENAMER(name) PROJECTname PROJECTbar Given Bars(name, addr), Sells(bar, beer, price), find the names of all the bars that are either on Maple St. or sell Bud for less than $3. UNION RENAMER(name) PROJECTname PROJECTbar SELECTaddr = “Maple St.” SELECT price<3 AND beer=“Bud” Bars Sells

Exercise #2 Given Bars(name, addr), Sells(bar, beer, price), find the names of all the bars that are either on Maple St. or sell Bud for less than $3. Start with a of Bars and Sells

Exercise #2 Given Bars(name, addr), 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(σ addr = “Maple St” OR(beer =“bud” AND price < 3) (Bars name=barSells))

Q: How would we do this? Using Sells(bar, beer, price), find the bars that sell two different beers at the same price.

Q: How would we do this? Using Sells(bar, beer, price), find the bars that sell two different beers at the same price. Πbar (sbeer1 != beer (Sells ρSells1(bar,beer1,price)(Sells(bar, beer, price)))

More Queries Product ( pid, name, price, category, maker-cid) Purchase (buyer-ssn, salesperson-ssn, store, pid) Company (cid, name, stock price, country) Person (ssn, name, phone number, city) Find phone numbers of people who bought gizmos from Fred.

Expression Tree Π numbers Π ssn Π pid σname=fred σcategory=gizmo Product ( pid, name, price, category, maker-cid) Purchase (buyer-ssn, salesperson-ssn, store, pid) Company (cid, name, stock price, country) Person (ssn, name, phone number, city) Many right answers!! Π numbers Find phone numbers of people who bought gizmos from Fred. ssn=buyer-ssn pid=pid salesperson-ssn=ssn Π ssn Π pid σname=fred σcategory=gizmo Person Purchase Person Product

Practice!! Hard to get better at coming up with relational algebra expressions without practice Lots of problems in the textbook Feel free to post questions on piazza

Sets vs. Bags So far, we have considered set-oriented relational algebra There’s an equivalent bag-oriented relational algebra Multisets instead of sets Relational databases actually use bags as opposed to sets… Most operations are more efficient

Operations on Bags Selection: preserve the number of occurrences Same speed as sets Projection: preserve the number of occurrences Faster than sets: no duplicate elimination Cartesian product, join Every copy joins with every copy

Operations on Bags 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 Faster than sets, no need to look for duplicates Difference: {a,b,b,b,c,c} – {b,c,c,c,d} = {a,b,b} subtract the number of occurrences Similar speed 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 Read the book for more details: Some non-intuitive behavior

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