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

Similar documents
Relational Algebra. Study Chapter Comp 521 Files and Databases Fall

Relational Algebra. [R&G] Chapter 4, Part A CS4320 1

Relational Algebra. Relational Query Languages

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

Database Management Systems. Chapter 4. Relational Algebra. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

Relational Query Languages. Relational Algebra. Preliminaries. Formal Relational Query Languages. Relational Algebra: 5 Basic Operations

Relational Algebra. Chapter 4, Part A. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

v Conceptual Design: ER model v Logical Design: ER to relational model v Querying and manipulating data

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

Relational Model and Relational Algebra

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

Relational Model, Relational Algebra, and SQL

Relational Query Languages

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

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

Relational algebra. Iztok Savnik, FAMNIT. IDB, Algebra

Relational Algebra & Calculus. CS 377: Database Systems

MIS Database Systems Relational Algebra

Introduction to Data Management. Lecture #11 (Relational Algebra)

Relational Algebra Homework 0 Due Tonight, 5pm! R & G, Chapter 4 Room Swap for Tuesday Discussion Section Homework 1 will be posted Tomorrow

Lecture 16. The Relational Model

Relational Algebra 1

Relational Algebra Part I. CS 377: Database Systems

CSCC43H: Introduction to Databases. Lecture 3

Database Applications (15-415)

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

CMP-3440 Database Systems

Database Management System. Relational Algebra and operations

Today s topics. Null Values. Nulls and Views in SQL. Standard Boolean 2-valued logic 9/5/17. 2-valued logic does not work for nulls

CompSci 516: Database Systems

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

FUNCTIONAL DEPENDENCIES

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

Relational Algebra 1

Relational Algebra 1. Week 4

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

Relational Databases. Relational Databases. Extended Functional view of Information Manager. e.g. Schema & Example instance of student Relation

CSE 544 Principles of Database Management Systems. Magdalena Balazinska Fall 2006 Lecture 3 - Relational Model

Databases - Relational Algebra. (GF Royle, N Spadaccini ) Databases - Relational Algebra 1 / 24

This lecture. Projection. Relational Algebra. Suppose we have a relation

CS 377 Database Systems

Relational Algebra and Calculus. Relational Query Languages. Formal Relational Query Languages. Yanlei Diao UMass Amherst Feb 1, 2007

Chapter 6 The Relational Algebra and Calculus

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

Relational Algebra and SQL

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

Database Applications (15-415)

CSC 261/461 Database Systems Lecture 13. Fall 2017

ARTICLE RELATIONAL ALGEBRA

Keys, SQL, and Views CMPSCI 645

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

Chapter 2: Intro to Relational Model

COMP 244 DATABASE CONCEPTS AND APPLICATIONS

RELATIONAL OPERATORS #1

Relational Calculus. Chapter Comp 521 Files and Databases Fall

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

Relational Query Languages: Relational Algebra. Juliana Freire

Lecture #8 (Still More Relational Theory...!)

Database Systems. Course Administration. 10/13/2010 Lecture #4

Relational Model 2: Relational Algebra

Introduction to Data Management. Lecture #14 (Relational Languages IV)

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

RELATIONAL DATA MODEL: Relational Algebra

HW1 is due tonight HW2 groups are assigned. Outline today: - nested queries and witnesses - We start with a detailed example! - outer joins, nulls?

1 Relational Data Model

Relational Algebra. Lecture 4A Kathleen Durant Northeastern University

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

Chapter 6 Part I The Relational Algebra and Calculus

Relational Algebra. Algebra of Bags

Relational Algebra and SQL. Basic Operations Algebra of Bags

Chapter 5 Relational Algebra. Nguyen Thi Ai Thao

Overview of DB & IR. ICS 624 Spring Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa

Database Applications (15-415)

Relational Query Languages

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

Chapter 2: The Relational Algebra

Chapter 6 - Part II The Relational Algebra and Calculus

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

Relational Algebra. B term 2004: lecture 10, 11

Chapter 2: The Relational Algebra

Selection and Projection

CIS 330: Applied Database Systems

Lecture 3 More SQL. Instructor: Sudeepa Roy. CompSci 516: Database Systems

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

Chapter 8: The Relational Algebra and The Relational Calculus

Introduction to Data Management. Lecture #11 (Relational Languages I)

Relational Algebra. Procedural language Six basic operators

Basic operators: selection, projection, cross product, union, difference,

Lecture 3 SQL - 2. Today s topic. Recap: Lecture 2. Basic SQL Query. Conceptual Evaluation Strategy 9/3/17. Instructor: Sudeepa Roy

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

Why Study the Relational Model? The Relational Model. Relational Database: Definitions. The SQL Query Language. Relational Query Languages

EECS 647: Introduction to Database Systems

Relational Calculus. Chapter Comp 521 Files and Databases Fall

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

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

1. (a) Briefly explain the Database Design process. (b) Define these terms: Entity, Entity set, Attribute, Key. [7+8] FIRSTRANKER

Fundamentals of Database Systems

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

SQL: Queries, Programming, Triggers

Part I: Structured Data

Transcription:

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

RELATIONAL QUERY LANGUAGES allow the manipulation and retrieval of data from a database two types of query languages: Declarative: describe what a user wants, rather than how to compute it Tuple Relational Calculus (TRC) Domain Relational Calculus (DRC) Procedural: operational, useful for representing execution plans Relational Algebra (RA) 2

QUERY VS PROGRAMMING LANGUAGES Relational Query Languages: are not Turing complete, which means they cannot capture any type of computation are not intended to be used for complex calculations support easy, efficient access to large datasets 3

WHAT IS RELATIONAL ALGEBRA? 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 relational algebra: an algebra whose operands are relations or variables that represent relations operators do the most common things that we need to do with relations in a database can be used as a query language for relations 4

RELATIONAL ALGEBRA PRELIM Query: Input: relational instances Output: relational instances specified using the schemas may produce different results for different instances the schema of the result is fixed there are two types of notation for attributes: positional (e.g. 2, 4) named-field (e.g. C.name, Person.SSN) 5

RELATIONAL ALGEBRA PRELIM Basic operations: Selection {σ}: selects a subset of rows Projection {π}: deletes columns Cross-product { }: combines two relations Set-difference{ } Union { } When the relations have named fields: Renaming {ρ} Additional operations: Intersection, join, division 6

BASIC OPERATIONS 7

SELECTION Notation: σ ( (R) C is a condition that refers to the attributes of R outputs the rows of R that satisfy C output schema: same as input schema Example σ,-./01 Person σ,-./01,9:,-.;0< (Person) σ,-./01,9: 9,=.> @,ABC (Person) 8

SELECTION EXAMPLE Person SSN name age phonenumber 934729837 Paris 24 608-374-8422 934729837 Paris 24 603-534-8399 123123645 John 30 608-321-1163 384475687 Arun 25 206-473-8221 σ,-./01 Person SSN name age phonenumber 123123645 John 30 608-321-1163 384475687 Arun 25 206-473-8221 9

PROJECTION Notation: π EF,E H,,E J (R) outputs only the columns A L, A 0,, A 9 removes any duplicate tuples (why?) output schema: R(A L, A 0,, A 9 ) Example π MMN,,-. Person π MMN,OPQ9.NR=S.A,,-. (Person) 10

PROJECTION EXAMPLE Person SSN name age phonenumber 934729837 Paris 24 608-374-8422 934729837 Paris 24 603-534-8399 123123645 John 30 608-321-1163 384475687 Arun 20 206-473-8221 SSN name 934729837 Paris 123123645 John 384475687 Arun π MMN,9,=. Person 11

UNION Notation: R L R 0 outputs all tuples in R L or R 0 both relations must have the same schema! output schema: same as input A B A B a 1 b 1 a 2 b 1 a 2 b 2 A B a 1 b 1 a 3 b 1 = a 4 b 4 a 1 b 1 a 2 b 1 a 2 b 2 a 3 b 1 a 4 b 4 12

DIFFERENCE Notation: R L R 0 outputs all tuples in R L and not in R 0 both relations must have the same schema! output schema: same as input A B a 1 b 1 a 2 b 1 a 2 b 2 A B a 1 b 1 a 3 b 1 = a 4 b 4 A B a 2 b 1 a 2 b 2 13

CROSS-PRODUCT Notation: R L R 0 matches each tuples in R L with each tuple in R 0 input schema: R L A L, A 0,, A 9, R 0 B L, B 0,, B = output schema: R A L,, A 9, B L,, B = 14

CROSS-PRODUCT EXAMPLE Person SSN name 934729837 Paris 123123645 John Dependent depssn depname 934729837 Helen 934729837 Bob Person Dependent SSN name depssn depname 934729837 Paris 934729837 Helen 123123645 John 934729837 Bob 934729837 Paris 934729837 Bob 123123645 John 934729837 Helen 15

RENAMING Notation: ρ EF,E H,,E J (R) does not change the instance, only the schema! input schema: R B L, B 0,, B 9 output schema: R A L,, A 9 Why is it necessary? 16

RENAMING EXAMPLE Person SSN name 934729837 Paris 123123645 John Dependent SSN name 934729837 Helen 934729837 Bob Person ρ :.OMMN,:.O9,=. (Dependent) SSN name depssn depname 934729837 Paris 934729837 Helen 123123645 John 934729837 Bob 934729837 Paris 934729837 Bob 123123645 John 934729837 Helen 17

DERIVED OPERATIONS 18

INTERSECTION Notation: R L R 0 outputs all tuples in R L and R 0 output schema: same as input can be expressed as: R L (R L R 0 ) A B a 1 b 1 a 2 b 1 a 2 b 2 A B a 1 b 1 a 3 b 1 = A B a 1 b 1 a 4 b 4 19

JOIN (THETA JOIN) Notation: R L ( R 0 = σ ( (R L R 0 ) cross-product followed by a selection C can be any boolean-valued condition might have less tuples than the cross-product! 20

THETA JOIN EXAMPLE Person SSN name age 934729837 Paris 26 123123645 John 22 Dependent dssn dname dage 934729837 Helen 23 934729837 Bob 28 Person @.ACQ9.,-./^.O.9:.9_.:,-. Dependent SSN name age dssn dname dage 934729837 Paris 26 934729837 Helen 23 21

EQUI-JOIN Notation: R L ( R 0 special case of join where the condition C contains only equalities between attributes! output schema: same as the cross-product Example for R A, B, S(C, D) R b>( S output schema: T(A, B, C, D) 22

NATURAL JOIN Notation: R L R 0 equi-join on all the common fields the output schema has one copy of each common attribute Person SSN name age 934729837 Paris 26 123123645 John 22 Dependent SSN dname 934729837 Helen 934729837 Bob Person Dependent SSN name age dname 934729837 Paris 26 Helen 934729837 Paris 26 Bob 23

NATURAL JOIN Natural Join R S Input schema: R A, B, C, D, S A, C, E Output schema: T(A, B, C, D, E) Input schema: R A, B, C, S D, E Output schema: T(A, B, C, D, E) Input schema: R A, B, C, S A, B, C Output schema? T(A, B, C,) 24

SEMI-JOIN Notation: R L R 0 natural join followed by projection on the attributes of R 1 Example: R A, B, C, S(B, D) R S = π E,b,( R S output schema: T(A, B, C) 25

DIVISION Notation: R L /R 0 suppose R L (A, B) and R 0 B the output contains all values a such that for every tuple (b) in R 2, tuple (a, b) is in R 1 output schema: R(A) 26

DIVISION EXAMPLE A A B a 1 b 1 a 1 b 2 a 1 b 3 a 2 b 1 B 1 B b 2 b 3 b 1 B 2 B b 1 A /B 1 A A /B 2 A a 1 a 1 a 2 27

EXTENDING RELATIONAL ALGEBRA 28

GROUP BY AGGREGATE is part of the so-called extended RA helps us to compute counts, sums, min, max, Examples What is the average age of the customers? How many people bought an ipad? CS 564 [Fall 2015] - Paris Koutris 29

GROUP BY AGGREGATE Notation: γ l,e-- m (R) group by the attributes in X aggregatethe attribute in Y SUM, COUNT, AVG (average), MIN, MAX Output schema: X + an extra (numerical) attribute CS 564 [Fall 2015] - Paris Koutris 30

EXAMPLE Person SSN name age 934729837 Paris 24 123123645 John 30 384475687 Arun 21 γ Eno(,-.) Person AVG(age) 25 CS 564 [Fall 2015] - Paris Koutris 31

EXAMPLE Person SSN name age phonenumber 934729837 Paris 24 608-374-8422 934729837 Paris 24 603-534-8399 123123645 John 30 608-321-1163 384475687 Arun 21 206-473-8221 γ MMN,(pqNr(OPQ9.NR=S.A) Person SSN COUNT(phoneNumber) 934729837 2 123123645 1 384475687 1 CS 564 [Fall 2015] - Paris Koutris 32

CONSTRUCTING RA QUERIES 33

COMBINING RA OPERATORS We can build more complex queries by combining RA operators together e.g. standard algebra: x + 1 y z 0 There are 3 different notations: sequence of assignment statements expressions with operators expression trees 34

COMBINING RA OPERATORS Input schema: R B, C, S(A, B) expressions with operators π E (σ (>L R S) sequence of assignment statements R y = σ (>L R R yy = R S R yyy = π E (R yy ) expression trees S π E σ (>L R 35

EXPRESSIVE POWER RA cannot express transitive closure! Edges From a b a c To b c d d Transitive closure computes all pairs of nodes connected by a directed path 36

EXAMPLES Sailors (sid, sname, rating, age) Reserves (sid, bid, day) Boats (bid, bname, color) Q1: What are the names of the sailors who have reserved boat with name 100? 37

EXAMPLES Sailors (sid, sname, rating, age) Reserves (sid, bid, day) Boats (bid, bname, color) Q2: What are the names of the sailors who have reserved a red boat? 38

EXAMPLES Sailors (sid, sname, rating, age) Reserves (sid, bid, day) Boats (bid, bname, color) Q3: What are the names of the sailors who have reserved a green or red boat? 39

EXAMPLES Sailors (sid, sname, rating, age) Reserves (sid, bid, day) Boats (bid, bname, color) Q4: What are the names of the sailors who have reserved a green and a red boat? 40

EXAMPLES Sailors (sid, sname, rating, age) Reserves (sid, bid, day) Boats (bid, bname, color) Q5: Find the names of the sailors who have reserved all boats with name 470. 41

EXAMPLES Product ( pid, name, price, category, maker-cid) Purchase (buyer-ssn, seller-ssn, store, pid) Company (cid, name, country) Person (ssn, name, phone, city) Q6: Find the phone numbers of the people who have bought ipads from Fred (the salesman). 42

EXAMPLES Product ( pid, name, price, category, maker-cid) Purchase (buyer-ssn, seller-ssn, store, pid) Company (cid, name, country) Person (ssn, name, phone, city) Q7: Find the names and phone numbers of the people who have bought products from the USA. 43

EXAMPLES Product ( pid, name, price, category, maker-cid) Purchase (buyer-ssn, seller-ssn, store, pid) Company (cid, name, country) Person (ssn, name, phone, city) Q8: Find the names of the people who have bought products only from the USA. 44

EXAMPLES Product ( pid, name, price, category, maker-cid) Purchase (buyer-ssn, seller-ssn, store, pid) Company (cid, name, country) Person (ssn, name, phone, city) Q9: Find the names of the people who have bought products from the USA but not from Greece. 45

EXAMPLES Product ( pid, name, price, category, maker-cid) Purchase (buyer-ssn, seller-ssn, store, pid) Company (cid, name, country) Person (ssn, name, phone, city) Q10: Find the names of the people who have bought products from the USA and live in Madison. 46

EXAMPLES City (cid, name, population) Flight (fid, length, start-city, end-city, aid) Airline (aid, name, profit) Q11: Find the flight ids for flights that start in a city with id MSN and end in a city with id LON. 47

EXAMPLES City (cid, name, population) Flight (fid, length, start-city, end-city, aid) Airline (aid, name, profit) Q12: Find the names of the cities that have a flight for every airline with profit more than 0. 48

RECAP Relational Algebra query language for relations Basic Operations selection, projection difference, union cross-product, renaming Derived Operations join, natural join, equi-join, division, etc 49