Relational Algebra 1

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

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

Database Management System. Relational Algebra and operations

Relational Algebra. Chapter 4, Part A. 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. Relational Query Languages

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

Relational algebra. Iztok Savnik, FAMNIT. IDB, Algebra

MIS Database Systems Relational Algebra

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

Relational Query Languages

Relational Algebra 1

CSCC43H: Introduction to Databases. Lecture 3

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

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

Database Applications (15-415)

Relational Algebra. Study Chapter Comp 521 Files and Databases Fall

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

CIS 330: Applied Database Systems. ER to Relational 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

Relational Algebra 1. Week 4

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

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

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

CompSci 516: Database Systems

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

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

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

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

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

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

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

Basic form of SQL Queries

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

This lecture. Step 1

Implementation of Relational Operations

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

Evaluation of Relational Operations. Relational Operations

SQL: Queries, Programming, Triggers

Relational Calculus. Chapter Comp 521 Files and Databases Fall

Database Applications (15-415)

RELATIONAL ALGEBRA AND CALCULUS

CIS 330: Applied Database Systems

SQL: Queries, Constraints, Triggers

Evaluation of Relational Operations

CompSci 516 Data Intensive Computing Systems

Review: Where have we been?

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

Evaluation of relational operations

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

Relational Calculus. Chapter Comp 521 Files and Databases Fall

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

The Database Language SQL (i)

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

SQL. Chapter 5 FROM WHERE

Evaluation of Relational Operations

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

SQL: The Query Language Part 1. Relational Query Languages

Experimenting with bags (tables and query answers with duplicate rows):

Relational Query Languages: Relational Algebra. Juliana Freire

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

Keys, SQL, and Views CMPSCI 645

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

Implementing Joins 1

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

Database Systems ( 資料庫系統 )

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

Database Applications (15-415)

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

Database Management Systems. Chapter 5

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

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

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

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

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

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

Overview of Query Evaluation. Overview of Query Evaluation

Database Applications (15-415)

SQL - Lecture 3 (Aggregation, etc.)

Administriva. CS 133: Databases. General Themes. Goals for Today. Fall 2018 Lec 11 10/11 Query Evaluation Prof. Beth Trushkowsky

CS330. Query Processing

CS3DB3/SE4DB3/SE6DB3 TUTORIAL

The SQL Query Language. Creating Relations in SQL. Adding and Deleting Tuples. Destroying and Alterating Relations. Conceptual Evaluation Strategy

Database Management Systems. Chapter 5

2.2.2.Relational Database concept

Overview of Query Evaluation

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

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

Enterprise Database Systems

SQL: Queries, Constraints, Triggers (iii)

Data Science 100 Databases Part 2 (The SQL) Previously. How do you interact with a database? 2/22/18. Database Management Systems

Overview of Query Processing

CSIT5300: Advanced Database Systems

Announcement. Reading Material. Overview of Query Evaluation. Overview of Query Evaluation. Overview of Query Evaluation 9/26/17

EECS 647: Introduction to Database Systems

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

Database Applications (15-415)

15-415/615 Faloutsos 1

Evaluation of Relational Operations

Database Management Systems. Chapter 5

Transcription:

Relational Algebra 1

Relational Algebra Last time: started on Relational Algebra What your SQL queries are translated to for evaluation A formal query language based on operators Rel Rel Op Rel Op Rel Rel Unary operator Binary operator 2

Selection operator Input: a relation Output: a relation containing a subset of the tuples from the input relation That satisfy a certain condition bid name color 101 Misty red 102 Pearl blue 103 Speedy blue selection bid = 101 bid name color 101 Misty red 3

Projection operator Input: a relation Output: a relation containing a subset of the columns from the input relation bid name color 101 Misty red 102 Pearl blue 103 Speedy blue projection color color red blue Relations are sets, so duplicates removed!! 4

Cross Product bid name color 101 Misty red 102 Pearl blue cross product??? sid bid day 22 101 9/10/13 58 102 9/11/13 5

In mathematical notation SELECT FROM WHERE S.sname Sailors S, Reserves R S.sid=R.sid AND R.bid='101'; 6

What does this buy us? Explicit workflow ( where the tuples go ) Can start thinking about implementation: Need an implementation for each of the boxes Maybe can reorder and/or combine some of them for better performance? 7

Now let's make it more formal So far: intuitive "boxes and arrows" presentation Now: more formal mathematical specification Useful because: If you implement these operators, have to know precisely what you're implementing If we want to reorder some of them, need to know (prove) that it's safe 8

Preliminaries A query is applied to relation instances, and the result of a query is also a relation instance A query is a sequence of operators Operators need to compose (can feed output of one as input to the next one) So both input and output relations need to be sets Set relational algebra Next time: bag relational algebra (duplicates allowed) 9

Preliminaries Schemas of operator outputs determined by schemas of inputs E.g. selection -> same schema Projection -> subset of columns Positional vs. named-field notation: Positional notation (arguably) easier for formal definitions, named-field notation more readable. We mostly use named-field notation in this course 10

Relational Algebra The "core" operators: Selection ( ) Selects a subset of rows from relation. Projection ( Cross-product ( ) Deletes unwanted columns from relation. ) Allows us to combine two relations. Join ( ) Technically redundant, but very handy Set operators: 11

Example Instances R1 sid bid day 22 101 10/10/96 58 103 11/12/96 Sailors and Reserves relations for our examples. 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 12

Selection Selects rows that satisfy selection condition. Schema of result identical to schema of input relation. sid sname rating age 28 yuppy 9 35.0 58 rusty 10 35.0 13

Selection formal definition Formally, let c be a selection condition i.e. a Boolean combination (using AND and OR) of terms of the form "att op const" or "att1 op att2", where att, att1, att2 are attribute names op is =,!=, <, >, <=, >= const is a value from the domain of the attribute in question Then 14

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 input relation. Eliminates duplicates (relations are sets) sname yuppy 9 lubber 8 guppy 5 rusty 10 age 35.0 55.5 rating 15

Projection formal definition (1) For a tuple, let denote the restriction of tuple t to exactly the attributes in Suppose Suppose denotes value of tuple t for attribute a Then The dot represents concatenation 16

Projection formal definition (2) Let be a relation and a subset of the attributes of Then is defined as 17

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 if possible. Conflict: Both S1 and R1 have a field called sid. (sid) sname rating age (sid) 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: 18

Cross product formal definition Not that hard! 19

Join operator Very common case: cross product followed by selection that "connects" attributes from both relations Handy to define a shorthand operator for it But technically don't need to 20

Joins Condition Join: (sid) sname rating age (sid) bid day 22 dustin 7 45.0 58 103 11/12/96 31 lubber 8 55.5 58 103 11/12/96 Result schema same as that of cross-product. Fewer tuples than cross-product, might be able to compute more efficiently 21

Joins Equi-Join: A special case of condition join where the join condition contains only equalities. 22

A handy shortcut If the join condition is equality, unwieldy to carry two copies of column in result Add a projection to remove one duplicate column Shortcut (per your textbook): assume the projection without specifying it i.e. retain only one copy of each column where join condition has equality sid sname rating age bid day 22 dustin 7 45.0 101 10/10/96 58 rusty 10 35.0 103 11/12/96 23

Natural joins symbol without condition Equality join on all fields with common name in both tables And retain only one copy of each such field If want to join on only some of the common attributes, need to specify condition explicitly 24

Union, Intersection, Set-Difference All of these operators take two input relations, which must be union-compatible: Same number of fields. `Corresponding fields have the same type. 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 25

Formal definitions Easier and easier! 26

Confusion alert! Union vs. Join These are NOT the same thing Let's make sure we understand the difference! 27

A few examples A few more examples to get the hang of it 28

Find names of sailors who ve reserved boat #103 Solution 1: Solution 2: 29

Find names of sailors who ve reserved a red boat Information about boat color only available in Boats; so need an extra join: A more efficient solution: A query optimizer can find this, given the first solution! 30

Find sailors who ve reserved a red or a green boat Can identify all red or green boats, then find sailors who ve reserved one of these boats: ρ ( Tempboats, ( σ )) color = ' red ' color = ' green' Boats π sname ( Tempboats Re serves Sailors) Can also define Tempboats using union! (How?) 31

Find sailors who ve reserved a red and a green boat Previous approach won t work! Must identify sailors who ve reserved red boats, sailors who ve reserved green boats, then find the intersection (note that sid is a key for Sailors): ρ ( Tempred, π (( σ sid color = ' red ' ρ ( Tempgreen, π (( σ sid color = ' green' Boats) Re serves)) Boats) Re serves)) π sname (( Tempred Tempgreen) Sailors) 32

Additional operators The basic operator toolkit is the one you've seen But can be handy to introduce extra operators that are technically redundant Joins are an example Intersection is actually redundant too (try to express it using the others) 33