Relational Algebra 1. Week 4

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

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

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

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

Relational algebra. Iztok Savnik, FAMNIT. IDB, Algebra

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

Relational Algebra. Study Chapter Comp 521 Files and Databases Fall

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

Relational Algebra. Relational Query Languages

Database Management System. Relational Algebra and operations

Relational Query Languages

CSCC43H: Introduction to Databases. Lecture 3

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

MIS Database Systems Relational Algebra

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

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

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

Relational Algebra 1

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

Database Applications (15-415)

Relational Algebra 1

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

CompSci 516: Database Systems

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

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

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

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

Database Applications (15-415)

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

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

Relational Query Languages: Relational Algebra. Juliana Freire

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

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

Implementation of Relational Operations

Evaluation of Relational Operations

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

Relational Algebra. Lecture 4A Kathleen Durant Northeastern University

Introduction to Data Management. Lecture #13 (Relational Calculus, Continued) It s time for another installment of...

Evaluation of Relational Operations. Relational Operations

Basic form of SQL Queries

Database System Concepts, 5 th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

Relational Algebra. Procedural language Six basic operators

Database Systems. Announcement. December 13/14, 2006 Lecture #10. Assignment #4 is due next week.

CompSci 516 Data Intensive Computing Systems

2.2.2.Relational Database concept

RELATIONAL ALGEBRA AND CALCULUS

This lecture. Step 1

CS3DB3/SE4DB3/SE6DB3 TUTORIAL

Chapter 6 Formal Relational Query Languages

Relational Calculus. Chapter Comp 521 Files and Databases Fall

EECS 647: Introduction to Database Systems

Database Systems. October 12, 2011 Lecture #5. Possessed Hands (U. of Tokyo)

Database Systems ( 資料庫系統 )

CIS 330: Applied Database Systems

Introduction to Data Management. Lecture 14 (SQL: the Saga Continues...)

Chapter 2: Intro to Relational Model

Database Applications (15-415)

SQL: Queries, Programming, Triggers

Relational Calculus. Chapter Comp 521 Files and Databases Fall

Evaluation of Relational Operations

COMP 244 DATABASE CONCEPTS AND APPLICATIONS

Chapter 2: Relational Model

CS 4604: Introduction to Database Management Systems. B. Aditya Prakash Lecture #10: Query Processing

Relational Model and Relational Algebra

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

Final Review. Zaki Malik November 20, 2008

Chapter 6: Formal Relational Query Languages

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

Database Applications (15-415)

Evaluation of relational operations

CMP-3440 Database Systems

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

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

Review: Where have we been?

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

CAS CS 460/660 Introduction to Database Systems. Query Evaluation II 1.1

Textbook: Chapter 6! CS425 Fall 2013 Boris Glavic! Chapter 3: Formal Relational Query. Relational Algebra! Select Operation Example! Select Operation!

Database System Concepts

SQL - Lecture 3 (Aggregation, etc.)

Relational Query Languages

Database Management Systems. Chapter 5

Web Science & Technologies University of Koblenz Landau, Germany. Relational Data Model

15-415/615 Faloutsos 1

Implementation of Relational Operations. Introduction. CS 186, Fall 2002, Lecture 19 R&G - Chapter 12

Lecture 2 SQL. Instructor: Sudeepa Roy. CompSci 516: Data Intensive Computing Systems

SQL Part 2. Kathleen Durant PhD Northeastern University CS3200 Lesson 6

Overview of Query Evaluation. Overview of Query Evaluation

SQL: Queries, Constraints, Triggers

Chapter 3: Relational Model

RELATIONAL ALGEBRA. CS121: Relational Databases Fall 2017 Lecture 2

SQL: Queries, Programming, Triggers. Basic SQL Query. Conceptual Evaluation Strategy. Example of Conceptual Evaluation. A Note on Range Variables

Introduction to Data Management. Lecture #10 (Relational Calculus, Continued)

CS330. Query Processing

CS 377 Database Systems

Data Science 100. Databases Part 2 (The SQL) Slides by: Joseph E. Gonzalez & Joseph Hellerstein,

The Database Language SQL (i)

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

CS34800 Information Systems. The Relational Model Prof. Walid Aref 29 August, 2016

3. Relational Data Model 3.5 The Tuple Relational Calculus

Ian Kenny. November 28, 2017

Transcription:

Relational Algebra 1 Week 4

Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports simple, powerful QLs: Strong formal foundation based on logic. Allows for much optimization. Query Languages!= programming languages! QLs not expected to be Turing complete. QLs not intended to be used for complex calculations. QLs support easy, efficient access to large data sets. 2

Formal Relational Query Languages Two mathematical Query Languages form the basis for real languages (e.g. SQL), and for implementation: Relational Algebra: More operational, very useful for representing execution plans. Relational Calculus: Lets users describe what they want, rather than how to compute it. (Nonoperational, declarative.) Understanding Algebra is key to understanding SQL, and query processing! 3

The Role of Relational Algebra in a DBMS 4

Algebra Preliminaries A query is applied to relation instances, and the result of a query is also a relation instance. Schemas of input relations for a query are fixed (but query will run regardless of instance!) The schema for the result of a given query is also fixed! Determined by definition of query language constructs. 5

Procedural language Five basic operators selection projection Relational Algebra SQL is closely based on relational algebra. select project union (why no intersection?) set difference difference Cross product Cartesian product The are some other operators which are composed of the above operators. These show up so often that we give them special names. The operators take one or two relations as inputs and give a new relation as a result. 6

Select Operation Example Relation r" A! B! C! D! σ A=B ^ D > 5 (r)" lowercase Greek sigma 5! 1 23! 7! 7! 3! A! B! C! D! 23! 7! Intuition: The select operation allows us to retrieve some rows of a relation (by some I mean anywhere from none of them to all of them) Here I have retrieved all the rows of the relation r where the value in field A equals the value in field B, and the value in field D is greater than 5. 7

Select Operation Notation: σ p (r) p is called the selection predicate Defined as: lowercase Greek sigma σ σ p (r) = {t t r and p(t)} Where p is a formula in propositional calculus consisting of terms connected by : (and), (or), (not) Each term is one of: <attribute> op where op is one of: =,, >,, <, Example of selection: σ name= Lee (professor) <attribute> or <constant> 8

Project Operation Example I A! B! C! Relation r: π A,C (r) Greek lower-case pi 20! 30! 40! 7! A! C! 7! Intuition: The project operation allows us to retrieve some columns of a relation (by some I mean anywhere from none of them to all of them) Here I have retrieved columns A and C. 9

Project Operation Example II A! B! C! Relation r: 20! 30! 40! Intuition: The project operation removes duplicate rows, since relations are sets. π A,C (r) A! C! =" A! C! Here there are two rows with A = α and C = 1. So one was discarded. 10

Project Operation Notation: π A1, A2,, Ak (r) Greek lower-case pi where A 1, A 2 are attribute names and r is a relation name. The result is defined as the relation of k columns obtained by erasing the columns that are not listed Duplicate rows removed from result, since relations are sets. 11

Union Operation Example A! B! A! B! Relations r, s: 3! r s: r! A! B! 3! s! Intuition: The union operation concatenates two relations, and removes duplicate rows (since relations are sets). Here there are two rows with A = α and B = 2. So one was discarded. 12

Notation: r s Union Operation Defined as: r s = {t t r or t s} For r s to be valid. Union-compatible 1. r, s must have the same arity (same number of attributes) 2. The attribute domains must be compatible (e.g., 2 nd column of r deals with the same type of values as does the 2nd column of s). Although the field types must be the same, the names can be different. For example I can union professor and lecturer where: professor(pid : string, name : string) lecturer(lid : string, first_name : string) 13

Related Operation: Intersection Relations r, s: A! B! A! B! Similar to Union operation. r! 3! s! But Intersection is NOT one of the five basic operations. r s: A!! B!!!! Intuition: The intersection operation computes the common rows between two relations 14

Set Difference Operation Example A! B! A! B! Relations r, s: 3! r s: r! A! B! s! Intuition: The set difference operation returns all the rows that are in r but not in s. 15

Set Difference Operation Notation r s Defined as: r s = {t t r and t s} Set differences must be taken between compatible relations. Union-compatible r and s must have the same arity attribute domains of r and s must be compatible Note that in general r s s r 16

Cross-Product Operation-Example Relations r, s:" A! B! C! D! E! r x s:" r! A! B! C! D! β! γ! γ! 20! 20! γ! E! a! a! b! b! a! a! b! b! 20! s! a! a! b! b! Intuition: The cross product operation returns all possible combinations of rows in r with rows in s. In other words the result is every possible pairing of the rows of r and s. 17

Cross-Product Operation Notation r x s Defined as: r x s = {t q t r and q s} Assume that attributes of r(r) and s(s) are disjoint. (That is, R S = ). If attributes names of r(r) and s(s) are not disjoint, then renaming must be used. 18

Composition of Operations We can build expressions using multiple operations Example: σ A= C (r x s) A! B! r x s:" C! D! E! A! B! r! C! D! γ! 20! s! E! a! a! b! b! β! γ! γ! 20! 20! a! a! b! b! a! a! b! b! A! B! C! D! E! take the cross product of r and s, then return only the rows where A equals B σ A=C (r x s) 20! 19 a! a! b!

Rename Operation Allows us to name, and therefore to refer to, the results of relationalalgebra expressions. Example: ρ (myrelation, (r s)) Renaming columns (rename A to A2): ρ (myrelation(a->a2), (r s)) A! B! A! B! 3! r! s! Take the set difference of r and s, and call the result myrelation" Renaming in relational algebra is essentiality the same as assignment in a programming language A! B! myrelation! 20

Rename Operation If a relational-algebra expression Y has arity n, then ρ(x(a->a1, B->A2, ), Y) returns the result of expression Y under the name X, and with the attributes renamed to A1, A2,., An. For example, ρ (myrelation(a->e, B->K), (r s)) Take the set difference of r and s, and call the result myrelation, while renaming the first field to E, and the second field to K. A! B! A! B! 3! r! s! E! K! myrelation! 21

Sailors Example Sailors(sid, sname, rating, age) Boats(bid, bname, color) Reserves(sid, bid, day) 22

Example Instances Sailors and Reserves relations for our examples. R1 sid bid day 22 101 10/10/96 58 103 11/12/96 S1 S2 sid sname rating age 22 dustin 7 45.0 31 lubber 8 55.5 58 rusty 10 35.0 sid sname rating age 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0 23

Algebra Operations Look what we want to get from the following table: S2 sid sname rating age 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0 24

Selection Selects rows that satisfy selection condition. No duplicates in result! (Why?) Schema of result identical to schema of (only) input relation. S2 sid sname rating age 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0 σ rating ( 2) = > 8 S sid sname rating age 28 yuppy 9 35.0 58 rusty 10 35.0 25

Projection Deletes attributes that are not in projection list. Schema of result contains exactly the fields in the projection list, with the same names that they had in the (only) input relation. Projection operator has to eliminate duplicates! (Why??) Note: real systems typically don t do duplicate elimination unless the user explicitly asks for it. sname yuppy 9 lubber 8 guppy 5 rusty 10 rating π ( S2) sname, rating age 35.0 55.5 π age ( S2) 26

Composition of Operations Result relation can be the input for another relational algebra operation! (Operator composition) S2 sid sname rating age 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0 π ( sname, rating σ ( S2)) = rating > 8 sname rating yuppy 9 rusty 10 27

What do we want to get from two relations? R1 sid bid day 22 101 10/10/96 58 103 11/12/96 S1 sid sname rating age 22 dustin 7 45.0 31 lubber 8 55.5 58 rusty 10 35.0 What about: Who reserved boat 101? Or: Find the name of the sailor who reserved boat 101. 28

Cross-Product Each row of S1 is paired with each row of R1. Result schema has one field per field of S1 and R1, with field names inherited. sid1 sname rating age sid2 bid day 22 dustin 7 45.0 22 101 10/10/96 22 dustin 7 45.0 58 103 11/12/96 31 lubber 8 55.5 22 101 10/10/96 31 lubber 8 55.5 58 103 11/12/96 58 rusty 10 35.0 22 101 10/10/96 58 rusty 10 35.0 58 103 11/12/96 Renaming operator (because of naming conflict): ρ( sid sid1, S1) ρ( sid sid2, R1) 29

Why does this cross product help Query: Find the name of the sailor who reserved boat 101. 30

Another example Find the name of the sailor having the highest rating. AllR=π ratinga ρ(rating >ratinga, S2) Result?=π Sname (σ rating<ratinga (S2 AllR)) What s in Result?? Does it answer our query? S2 sid sname rating age 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0 32

S2 sid sname rating age 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0 AllR ratinga AllR=π ratinga ρ(rating >ratinga, S2) Result?=π Sname (σ rating<ratinga (S2 AllR)) 9 8 5 10 = sid sname rating age ratinga 28 yuppy 9 35.0 9 28 yuppy 9 35.0 8 28 yuppy 9 35.0 5 28 yuppy 9 35.0 10 31 lubber 8 55.5 9 31 lubber 8 55.5 8 31 lubber 8 55.5 5 31 lubber 8 55.5 10 44 guppy 5 35.0 9 44 guppy 5 35.0 8 44 guppy 5 35.0 5 44 guppy 5 35.0 10 58 rusty 10 35.0 9 58 rusty 10 35.0 8 58 rusty 10 35.0 5 58 rusty 10 35.0 10 33

Union, Intersection, Set-Difference All of these operations take two input relations, which must be unioncompatible: Same number of fields. Corresponding fields have the same type. What is the schema of result? sid sname rating age 22 dustin 7 45.0 S1 S2 sid sname rating age 22 dustin 7 45.0 31 lubber 8 55.5 58 rusty 10 35.0 44 guppy 5 35.0 28 yuppy 9 35.0 S1 S2 sid sname rating age 31 lubber 8 55.5 58 rusty 10 35.0 S1 S2 34

Back to our query Find the name of the sailor having the highest rating. 35

Relational Algebra (Summary) Basic operations: Selection ( σ ) Selects a subset of rows from relation. Projection ( π ) Deletes unwanted columns from relation. Cross-product ( ) Allows us to combine two relations. Set-difference ( - ) Tuples in reln. 1, but not in reln. 2. Union ( ) Tuples in reln. 1 and in reln. 2. Also, Rename ( ρ ) Changes names of the attributes Since each operation returns a relation, operations can be composed! (Algebra is closed.) Use of temporary relations recommended. 37