CS411 Database Systems. 04: Relational Algebra Ch 2.4, 5.1

Similar documents
Relational Algebra and SQL. Basic Operations Algebra of Bags

Relational Algebra. Algebra of Bags

Relational Algebra BASIC OPERATIONS DATABASE SYSTEMS AND CONCEPTS, CSCI 3030U, UOIT, COURSE INSTRUCTOR: JAREK SZLICHTA

Chapter 2 The relational Model of data. Relational algebra

1 Relational Data Model

Relational Query Languages

CS 317/387. A Relation is a Table. Schemas. Towards SQL - Relational Algebra. name manf Winterbrew Pete s Bud Lite Anheuser-Busch Beers

4/10/2018. Relational Algebra (RA) 1. Selection (σ) 2. Projection (Π) Note that RA Operators are Compositional! 3.

Databases-1 Lecture-01. Introduction, Relational Algebra

L22: The Relational Model (continued) CS3200 Database design (sp18 s2) 4/5/2018

Lecture 16. The Relational Model

CSC 261/461 Database Systems Lecture 13. Fall 2017

Chapter 2: The Relational Algebra

Chapter 2: The Relational Algebra

RELATIONAL ALGEBRA. CS 564- Fall ACKs: Dan Suciu, Jignesh Patel, AnHai Doan

Introduction to Data Management CSE 344. Lectures 8: Relational Algebra

Relational Query Languages: Relational Algebra. Juliana Freire

Introduction to Data Management CSE 344. Lectures 8: Relational Algebra

They like one or more beers and frequent one. Bars have unique names and addresses. They. serve one or more beers and are frequented

Introduction to SQL SELECT-FROM-WHERE STATEMENTS SUBQUERIES DATABASE SYSTEMS AND CONCEPTS, CSCI 3030U, UOIT, COURSE INSTRUCTOR: JAREK SZLICHTA

Introduction to SQL. Select-From-Where Statements Multirelation Queries Subqueries. Slides are reused by the approval of Jeffrey Ullman s

Relational Model, Relational Algebra, and SQL

Chapter 6 The database Language SQL as a tutorial

Relational Algebra. Relational Algebra Overview. Relational Algebra Overview. Unary Relational Operations 8/19/2014. Relational Algebra Overview

CS 377 Database Systems

2.2.2.Relational Database concept

What is an Algebra. Core Relational Algebra. What is Relational Algebra? Operação de Seleção. Álgebra Relacional: Resumo

Chapter 8: The Relational Algebra and The Relational Calculus

Relational Model 2: Relational Algebra

Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Chapter 6 Outline. Unary Relational Operations: SELECT and

Data Models. CS552- Chapter Three

Chapter 5 Relational Algebra. Nguyen Thi Ai Thao

Relational Model and Relational Algebra

Relational Algebra 1

Chapter 2: Intro to Relational Model

Chapter 6 Part I The Relational Algebra and Calculus

Selection and Projection

Lecture Query evaluation. Combining operators. Logical query optimization. By Marina Barsky Winter 2016, University of Toronto

SQL: Data Manipulation Language

Chapter 6 The database Language SQL as a tutorial

Ian Kenny. November 28, 2017

CIS 330: Applied Database Systems. ER to Relational Relational Algebra

Relational Algebra. Procedural language Six basic operators

I Relational Database Modeling how to define

Relational Algebra. Relational Algebra. 7/4/2017 Md. Golam Moazzam, Dept. of CSE, JU

Relational Algebra and SQL

RELATIONAL DATA MODEL: Relational Algebra

Databases Relational algebra Lectures for mathematics students

CS3DB3/SE4DB3/SE6DB3 TUTORIAL

Chapter 6 The Relational Algebra and Calculus

Relational Algebra for sets Introduction to relational algebra for bags

CAS CS 460/660 Introduction to Database Systems. Relational Algebra 1.1

Relational model continued. Understanding how to use the relational model. Summary of board example: with Copies as weak entity

Relational Algebra Part I. CS 377: Database Systems

Relational Query Languages. Preliminaries. Formal Relational Query Languages. Example Schema, with table contents. Relational Algebra

Relational Algebra. Relational Query Languages

CS145 Introduction. About CS145 Relational Model, Schemas, SQL Semistructured Model, XML

Database Technology Introduction. Heiko Paulheim

Informationslogistik Unit 4: The Relational Algebra

The Extended Algebra. Duplicate Elimination. Sorting. Example: Duplicate Elimination

Introduction to SQL. Select-From-Where Statements Multirelation Queries Subqueries

Relational Databases

Relational Model & Algebra. Announcements (Thu. Aug. 27) Relational data model. CPS 116 Introduction to Database Systems

Relational Algebra 1

Introduction to Query Processing and Query Optimization Techniques. Copyright 2011 Ramez Elmasri and Shamkant Navathe

Relational Algebra. ICOM 5016 Database Systems. Roadmap. R.A. Operators. Selection. Example: Selection. Relational Algebra. Fundamental Property

CMP-3440 Database Systems

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #3: SQL and Rela2onal Algebra- - - Part 1

ECE 650 Systems Programming & Engineering. Spring 2018

CPS 216 Spring 2003 Homework #1 Assigned: Wednesday, January 22 Due: Monday, February 10

SQL: csc343, Introduction to Databases Renée J. Miller and Fatemeh Nargesian and Sina Sina Meraji. Winter 2018

Chapter 6 The Relational Algebra and Relational Calculus

COSC344 Database Theory and Applications. σ a= c (P) S. Lecture 4 Relational algebra. π A, P X Q. COSC344 Lecture 4 1

RELATIONAL ALGEBRA. CS121: Relational Databases Fall 2017 Lecture 2

Relational Model and Relational Algebra. Rose-Hulman Institute of Technology Curt Clifton

Relational Model History. COSC 304 Introduction to Database Systems. Relational Model and Algebra. Relational Model Definitions.

CS

I Relational Database Modeling how to define

3. Relational Data Model 3.5 The Tuple Relational Calculus

Databases - 4. Other relational operations and DDL. How to write RA expressions for dummies

Chapter 6 5/2/2008. Chapter Outline. Database State for COMPANY. The Relational Algebra and Calculus

Relational Algebra. Note: Slides are posted on the class website, protected by a password written on the board

CSCI3030U Database Models

CMPT 354: Database System I. Lecture 5. Relational Algebra

CSC 261/461 Database Systems Lecture 14. Spring 2017 MW 3:25 pm 4:40 pm January 18 May 3 Dewey 1101

Relational Algebra. Mr. Prasad Sawant. MACS College. Mr.Prasad Sawant MACS College Pune

WEEK 3. EE562 Slides and Modified Slides from Database Management Systems, R.Ramakrishnan 1

Set theory is a branch of mathematics that studies sets. Sets are a collection of objects.

SQL: Data Manipulation Language. csc343, Introduction to Databases Diane Horton Winter 2017

Relational Database: The Relational Data Model; Operations on Database Relations

Agenda. Discussion. Database/Relation/Tuple. Schema. Instance. CSE 444: Database Internals. Review Relational Model

Why SQL? SQL is a very-high-level language. Database management system figures out best way to execute query

ARTICLE RELATIONAL ALGEBRA

Announcements. Relational Model & Algebra. Example. Relational data model. Example. Schema versus instance. Lecture notes

Fundamentals of Database Systems

Chapter 6: RELATIONAL DATA MODEL AND RELATIONAL ALGEBRA

Relational Algebra. Study Chapter Comp 521 Files and Databases Fall

The Relational Model and Relational Algebra

The Relational Algebra and Calculus. Copyright 2013 Ramez Elmasri and Shamkant B. Navathe

Announcements. Agenda. Database/Relation/Tuple. Discussion. Schema. CSE 444: Database Internals. Room change: Lab 1 part 1 is due on Monday

Transcription:

CS411 Database Systems 04: Relational Algebra Ch 2.4, 5.1 1

Basic RA Operations 2

Set Operations Union, difference Binary operations Remember, a relation is a SET of tuples, so set operations are certainly applicable 3

Set Operations: Union Union: all tuples in R1 or R2 Notation: R1 U R2 R1, R2 must have the same schema Output: R1 U R2 has the same schema as R1, R2 Example: ActiveEmployees(SSN, name) RetiredEmployees(SSN, name) ActiveEmployees U RetiredEmployees 4

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

Selection Returns all tuples which satisfy a condition Notation: σ c (R) c is a condition (uses =, <, >, AND, OR, NOT) Output schema: same as input schema Find all employees with salary more than $40,000: σ Salary > 40000 (Employee) 6

Selection Example Employee SSN Name DepartmentID Salary 999999999 John 1 30,000 777777777 Tony 1 32,000 888888888 Alice 2 45,000 Emp. with salary more than $30,000 and department id = 2 σ Salary > 30000 AND DepartmentID = 2 (Employee) SSN Name DepartmentID Salary 888888888 Alice 2 45,000 7

Projection Unary operation: returns certain columns Eliminates duplicate tuples! Notation: π A1,,An (R) If input schema R(B1,,Bm) then {A1,, An} {B1,, Bm} Output schema S(A1,,An) Example: project social-security number and names from Employee (SSN, Name, Dept, Salary) π SSN, Name (Employee) 8

Projection Example Employee SSN Name DepartmentID Salary 999999999 John 1 30,000 777777777 Tony 1 32,000 888888888 Alice 2 45,000 π SSN, Name (Employee) π DepartmentID (Employee)?? SSN Name 999999999 John 777777777 Tony 888888888 Alice 9

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 If Ai is same as Bj, rename them as R1.Ai, R2.Ai 10

Cartesian Product Example Employee Name SSN John 999999999 Tony 777777777 Dependents EmployeeSSN Dname 999999999 Emily 777777777 Joe Employee x Dependents Name SSN EmployeeSSN Dname John 999999999 999999999 Emily John 999999999 777777777 Joe Tony 777777777 999999999 Emily Tony 777777777 777777777 Joe 11

Renaming Does not change the relational instance Changes the relational schema only Notation: ρ S(B1,,Bn) (R) Input schema: R(A1,, An) Output schema: S(B1,, Bn) Example: rename Employee(Name, SSN) ρ RenamedEmployee(LastName, SocSocNo) (Employee) 12

Renaming Example Employee Name SSN John 999999999 Tony 777777777 ρ RenamedEmployee(LastName, SocSocNo) (Employee) LastName SocSocNo John 999999999 Tony 777777777 13

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

Set Operations: Intersection Difference: all tuples both in R1 and in R2 Notation: R1 R2 R1, R2 must have the same schema Output: R1 R2 has the same schema as R1, R2 Example UnionizedEmployees Intersection is derived: R1 R2 = R1 (R1 R2) RetiredEmployees 15

Joins Theta join Natural join Equi-join etc. 16

Theta Join A cartesian product followed by a selection Notation: R1 θ R2 where θ is a condition Input schemas: R1(A1,,An), R2(B1,,Bm) Output schema: S(A1,,An,B1,,Bm) Derived operator: R1 θ R2 = σ θ (R1 x R2) Note that in output schema, if an attribute of R1 has the same name as an attribute of R2, we need renaming, as in Cartesian Product. 17

Example Sells( bar, beer, price ) Bars( name, addr ) Joe s Bud 2.50 Joe s Maple St. Joe s Miller 2.75 Sue s River Rd. Sue s Bud 2.50 Sue s Coors 3.00 BarInfo := Sells Sells.bar = Bars.name Bars BarInfo( bar, beer, price, name, addr ) Joe s Bud 2.50 Joe s Maple St. Joe s Miller 2.75 Joe s Maple St. Sue s Bud 2.50 Sue s River Rd. Sue s Coors 3.00 Sue s River Rd. 18

Notation: R1 Natural Join 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 19

Natural Join Example Employee Name SSN John 999999999 Tony 777777777 Dependents SSN Dname 999999999 Emily 777777777 Joe Employee Dependents = Π Name, SSN, Dname (σ SSN=SSN2 (Employee x ρ SSN2, Dname (Dependents)) Name SSN Dname John 999999999 Emily Tony 777777777 Joe 20

Natural Join A B R= S= X Y X Z Y Z Z V B Z V Z C U W V R S = A B C X Z U X Z V Y Z U Y Z V Z V W 21

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? 22

Equi-join A generalization of Natural Joins, or special case of theta joins where C = equality predicate R1 Α=Β R2 A lot of research on how to do it efficiently 23

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 Schema (A, R.B, R.S, C) 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 24

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 25

How would we do this: Natural Join How do we express R(A, B) and S (B, C) using the basic operators? 26

How would we do this: 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)) 27

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

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: 1. Sequences of assignment statements. 2. Expressions with several operators. 3. Expression trees. 29

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 R4 := R1 x R2 R3 := σ C (R4) C R2 can be written: 30

2. Expressions with Several Operators 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. But you can always insert parentheses to force the order you desire. 31

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

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. 33

As a Tree: UNION 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. RENAME R(name) PROJECT name PROJECT bar SELECT addr = Maple St. SELECT price<3 AND beer= Bud Bars Sells 34

How would we do this? 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 theta of Bars and Sells 35

How would we do this? 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 Many right answers! name=bar Sells)) 36

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

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

Exercise! 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 all individuals who have made a sale. 39

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. 40

Expression Tree Many right answers!! Π numbers ssn=buyer-ssn 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. pid=pid salesperson-ssn=ssn Π ssn Π pid σ name=fred σ category=gizmo Person Purchase Person Product 41

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 42

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 43

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 Faster than sets: no duplicate elimination 44

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 Similar speed Read the book for more details: Some non-intuitive behavior 45

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 46