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

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

This lecture. Step 1

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

Database Applications (15-415)

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

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

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

CSCC43H: Introduction to Databases. Lecture 3

Relational Algebra. Relational Query Languages

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

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

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

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

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

Basic form of SQL Queries

Relational Calculus. Chapter Comp 521 Files and Databases Fall

Relational Calculus. Chapter Comp 521 Files and Databases Fall

Relational algebra. Iztok Savnik, FAMNIT. IDB, Algebra

Relational Algebra 1

Relational Query Languages

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

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

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

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

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

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

Database Management System. Relational Algebra and operations

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)

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

Relational Algebra. Study Chapter Comp 521 Files and Databases Fall

Relational Algebra 1. Week 4

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

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

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

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

CIS 330: Applied Database Systems

MIS Database Systems Relational Algebra

Overview of the Class and Introduction to DB schemas and queries. Lois Delcambre

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

Keys, SQL, and Views CMPSCI 645

Relational Algebra 1

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

SQL: Queries, Programming, Triggers

SQL: Queries, Constraints, Triggers

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

Database Systems ( 資料庫系統 )

Database Management Systems. Chapter 5

2.2.2.Relational Database concept

The Database Language SQL (i)

SQL. Chapter 5 FROM WHERE

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

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

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

Evaluation of Relational Operations. Relational Operations

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

Implementation of Relational Operations

QUERIES, PROGRAMMING, TRIGGERS

CS 377 Database Systems

SQL: The Query Language Part 1. Relational Query Languages

Relational terminology. Databases - Sets & Relations. Sets. Membership

RELATIONAL ALGEBRA AND CALCULUS

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

SQL - Lecture 3 (Aggregation, etc.)

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

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

Database Applications (15-415)

Evaluation of Relational Operations

CompSci 516: Database Systems

Review: Where have we been?

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

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

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

Evaluation of relational operations

CompSci 516 Data Intensive Computing Systems

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

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

Database Applications (15-415)

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

Advanced Database Management Systems

Advanced Database Management Systems

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

Principles of Data Management. Lecture #12 (Query Optimization I)

Evaluation of Relational Operations

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

Overview of Query Evaluation. Overview of Query Evaluation

Relational Query Optimization. Highlights of System R Optimizer

More on SQL Nested Queries Aggregate operators and Nulls

Relational Query Languages: Relational Algebra. Juliana Freire

CMP-3440 Database Systems

Chapter 6 The Relational Algebra and Calculus

COMP 244 DATABASE CONCEPTS AND APPLICATIONS

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

Chapter 6 Part I The Relational Algebra and Calculus

Ian Kenny. November 28, 2017

Overview of Query Evaluation

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

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

Database Applications (15-415)

Relational Model and Relational Algebra

Transcription:

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

This lecture This lecture covers relational algebra which is the formal language underlying the manipulation of relations. We follow the notation from Chapter 4 of Ramakrishnan & Gehrke. (GF Royle, N Spadaccini 2006-2010) Databases - Relational Algebra 2 / 24

Projection and Selection Relational Algebra Relational algebra is a procedural language that allows us to describe operations on relations in a formal and mathematically precise. An expression in relational algebra describes a sequence of operations that can be applied to a relation and which produces a relation as a result. The primary operations of the relational algebra are projection, selection and joins. (GF Royle, N Spadaccini 2006-2010) Databases - Relational Algebra 3 / 24

Projection and Selection Projection Suppose we have a relation R C 1 C 2 C n, where C 1, C 2,..., C n are the columns of the relation. If S is a subset of the columns then π S (R) is the relation obtained from R by deleting all the columns not in S, and it is called the projection onto S of the relation R. (GF Royle, N Spadaccini 2006-2010) Databases - Relational Algebra 4 / 24

Projection and Selection Example Relation Suppose R is the following relation customerid name address accountmgr 1121 Bunnings Subiaco 137 1122 Bunnings Claremont 137 1211 Mitre 10 Myaree 186 1244 Mitre 10 Joondalup 186 1345 Joe s Hardware Nedlands 204 1399 NailsRUs Jolimont 361 (GF Royle, N Spadaccini 2006-2010) Databases - Relational Algebra 5 / 24

Projection and Selection Example Projections Then π customerid, name (R) is customerid name 1121 Bunnings 1122 Bunnings 1211 Mitre 10 1244 Mitre 10 1345 Joe s Hardware 1399 NailsRUs We have projected the relation onto the two named columns, thus obtaining a smaller relation. (GF Royle, N Spadaccini 2006-2010) Databases - Relational Algebra 6 / 24

Projection and Selection Another example projection If R is the relation given above, then π name (R) is the relation name Bunnings Mitre 10 Joe s Hardware NailsRUs As the result of a projection is a relation which is defined to be a set, the output cannot contain any duplicate rows. (GF Royle, N Spadaccini 2006-2010) Databases - Relational Algebra 7 / 24

Projection and Selection In SQL It should be clear that there is a direct relationship between the projection operator π, and the SELECT columns FROM statement In practical terms one difference is that SQL normally does not automatically remove duplicate rows, because this is a relatively expensive operation. (GF Royle, N Spadaccini 2006-2010) Databases - Relational Algebra 8 / 24

Projection and Selection Selection Projection is an operation that extracts columns from a relation, while selection is the operation that extracts rows from a relation. If R is a relation and B is a boolean function on the columns of R then σ B (R) is the relation with the same columns as R and whose rows are the rows of R for which the boolean function evaluates to true. Equivalently we say that σ B selects the rows from R that satisfy the boolean condition B. (GF Royle, N Spadaccini 2006-2010) Databases - Relational Algebra 9 / 24

Projection and Selection Example Selection If R is the relation defined above, then is the relation σ customerid<1300 (R) customerid name address accountmgr 1121 Bunnings Subiaco 137 1122 Bunnings Claremont 137 1211 Mitre 10 Myaree 186 1244 Mitre 10 Joondalup 186 (GF Royle, N Spadaccini 2006-2010) Databases - Relational Algebra 10 / 24

Projection and Selection Combining selection and projection As the result of a selection or projection is a relation, we can use it as the input for another selection or projection and thus build up complex relational algebra expressions. π accountmgr (σ name= Bunnings (R)) This compound operation first selects the rows that correspond to Bunnings and then projects onto the single column accountmgr in other words, this relational algebra expression answers the query What is the staff id of the account manager for Bunnings". (GF Royle, N Spadaccini 2006-2010) Databases - Relational Algebra 11 / 24

Projection and Selection In SQL The selection operator in relational algebra corresponds directly to the WHERE clause of SQL. Thus we can directly translate the relational algebra expression of the previous slide into an SQL query SELECT accountmgr FROM customer WHERE name = Bunnings ; (GF Royle, N Spadaccini 2006-2010) Databases - Relational Algebra 12 / 24

Projection and Selection Boolean functions The boolean functions that can be used as the selection condition are combinations using (for AND) and (for OR) of terms of the form or attribute op constant attribute1 op attribute2 where op is a comparison operator in the set {<,, =,,, >}. (GF Royle, N Spadaccini 2006-2010) Databases - Relational Algebra 13 / 24

Projection and Selection Relational Algebra Expressions A relational algebra expression is recursively defined to be A relation, or A unary operator applied to a single expression A binary operator applied to two expressions Selection and projection are unary operators because they operate on a single relation. (GF Royle, N Spadaccini 2006-2010) Databases - Relational Algebra 14 / 24

Joins Binary Operators Relational algebra permits the use of the standard set operations: Union ( ) If R and S are union-compatible, then R S is the set of tuples in either R or S. Intersection ( ) If R and S are union-compatible, then R S is the set of tuples in both R and S. Set Difference ( ) If R and S are union-compatible then R S is the set of tuples in R that are not in S Cartesian Product ( ) If R has arity r and S has arity s, then R S has arity r + s and has all tuples whose projection onto the first r columns is in R and whose projection onto the last s columns is in S. (GF Royle, N Spadaccini 2006-2010) Databases - Relational Algebra 15 / 24

Joins Union in MySQL Of the first three set operations, MySQL 5.0 only supports UNION as a direct command. This can be useful when two subsets of data are being pulled from different sets of tables. The UNION can simply be placed between two quite independent SELECT statements as long as both return the same number of columns. SELECT birth FROM president UNION SELECT death FROM president; (GF Royle, N Spadaccini 2006-2010) Databases - Relational Algebra 16 / 24

Joins Joins The join of two relations R and S is one of the most important operations in real databases and is defined as follows. Suppose that c is a boolean function that may involve the attributes of both R and S. Then is defined to be R c S σ c (R S). In other words a join is the result of selecting certain rows from the Cartesian product. (GF Royle, N Spadaccini 2006-2010) Databases - Relational Algebra 17 / 24

Joins Equijoin In almost all the examples we have seen so far, the join condition has actually consisted of equalities between attributes of R and S often an equality between a key and a foreign key. In such a join, the resulting relation will have a number of duplicated columns in particular any attributes used in the join condition will appear twice. An equijoin of two tables is a join where the join condition is a conjunction of equalities of the form R.attribute1 = S.attribute2 with the columns of S that appear in the join condition projected out. (GF Royle, N Spadaccini 2006-2010) Databases - Relational Algebra 18 / 24

Joins Natural Join The natural join of two relations R and S is the equijoin whose join condition involves every column having the same name in R and S. As the join condition can be determined by context, it can simply be omitted and R S denotes the natural join of R and S. (GF Royle, N Spadaccini 2006-2010) Databases - Relational Algebra 19 / 24

Relational Queries Examples from the text Consider the following conceptual schema taken almost directly from Ramakrishnan & Gehrke that is related to a boat-rental operation. name bname sid age bid colour Sailor date Boat Reserves (GF Royle, N Spadaccini 2006-2010) Databases - Relational Algebra 20 / 24

Relational Queries Sample entity sets sid sname age 22 Dustin 45.0 29 Brutus 33.0 31 Lubber 55.5 32 Andy 25.5 58 Rusty 35.0 64 Horatio 35.0 71 Zorba 16.0 74 Horatio 35.0 85 Art 25.5 95 Bob 63.5 sailor sid bid date 22 101 10-Aug-06 22 102 10-Aug-06 22 103 11-Aug-06 22 104 12-Aug-06 31 102 02-Aug-06 31 103 03-Aug-06 31 104 17-Aug-06 64 101 18-Aug-06 64 102 05-Aug-06 74 103 05-Aug-06 reserves bid name colour 101 Interlake blue 102 Interlake red 103 Clipper green 104 Marine red boat (GF Royle, N Spadaccini 2006-2010) Databases - Relational Algebra 21 / 24

Relational Queries Queries in relational algebra Suppose we wish to answer the question Which sailors have reserved boat 103? One expression that answers this query is π sname (σ bid=103 (reserves) sailor) An equivalent expression that also answers this query is π sname (σ bid=103 (reserves sailor)) (GF Royle, N Spadaccini 2006-2010) Databases - Relational Algebra 22 / 24

Relational Queries Unpacking the expression The selection σ bid=103 (reserves) extracts from reserves the entries relating to boat 103. sid bid date 22 103 11-Aug-06 31 103 03-Aug-06 74 103 05-Aug-06 We then compute the natural join of this relation with sailor getting sid bid date sid sname age 22 103 11-Aug-06 22 Dustin 45.0 31 103 03-Aug-06 31 Lubber 55.5 74 103 05-Aug-06 74 Horatio 35.0 (GF Royle, N Spadaccini 2006-2010) Databases - Relational Algebra 23 / 24

Relational Queries Unpacking this expression cont. The final stage is the projection onto the single field sname resulting in the final relation sname Dustin Lubber Horatio The query π sname (σ bid=103 (reserves sailor)) produces the same answer, but generates much larger intermediate relations. One task of the query optimizer is to take an SQL query and to determine an evaluation strategy by converting it into an equivalent, but more efficient relational expression. (GF Royle, N Spadaccini 2006-2010) Databases - Relational Algebra 24 / 24