Relational Query Languages: Relational Algebra. Juliana Freire

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

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

Relational Algebra and SQL. Basic Operations Algebra of Bags

Relational Query Languages

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

Chapter 2 The relational Model of data. Relational algebra

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

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

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

Relational Algebra. Algebra of Bags

Relational algebra. Iztok Savnik, FAMNIT. IDB, Algebra

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

Relational Algebra. Relational Query Languages

Relational Algebra for sets Introduction to relational algebra for bags

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

MIS Database Systems Relational Algebra

CSCC43H: Introduction to Databases. Lecture 3

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

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

1 Relational Data Model

Relational Algebra 1. Week 4

Relational Algebra. Lecture 4A Kathleen Durant Northeastern University

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

Relational Query Languages

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

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

Databases-1 Lecture-01. Introduction, Relational Algebra

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

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

Relational Model and Relational Algebra

CMP-3440 Database Systems

Database Applications (15-415)

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

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

Comp 5311 Database Management Systems. 2. Relational Model and Algebra

Relational Algebra 1

Chapter 2: Intro to Relational Model

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

Ian Kenny. November 28, 2017

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

Chapter 2: Relational Model

Introduction Relational Algebra Operations

2.2.2.Relational Database concept

CS 377 Database Systems

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

Database Management System. Relational Algebra and operations

Relational Algebra. Relational Query Languages

Relational Algebra Part I. CS 377: Database Systems

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

Relational Model, Relational Algebra, and SQL

Chapter 2: Intro to Relational Model

COMP 244 DATABASE CONCEPTS AND APPLICATIONS

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

Chapter 3: Relational Model

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

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

EECS 647: Introduction to Database Systems

CS2300: File Structures and Introduction to Database Systems

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

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

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

Chapter 6 Part I The Relational Algebra and Calculus

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

Faloutsos - Pavlo CMU SCS /615

Overview. Carnegie Mellon Univ. School of Computer Science /615 - DB Applications. Concepts - reminder. History

Chapter 8: Relational Algebra

Relational Algebra. Procedural language Six basic operators

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

Database System Concepts

Selection and Projection

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

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

Chapter 6 The Relational Algebra and Calculus

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

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

Modeling access. Relational query languages. Queries. Selection σ P (R) Relational model: Relational algebra

CSC 261/461 Database Systems Lecture 13. Fall 2017

Lecture 16. The Relational Model

A l Ain University Of Science and Technology

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

RELATIONAL DATA MODEL: Relational Algebra

Databases Relational algebra Lectures for mathematics students

3. Relational Data Model 3.5 The Tuple Relational Calculus

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

Chapter 6: Formal Relational Query Languages

CS 4604: Introduction to Database Management Systems. B. Aditya Prakash Lecture #2: The Relational Model and Relational Algebra

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

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

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

CompSci 516: Database Systems

Database Applications (15-415)

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

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

Rela+onal Algebra. Rela+onal Query Languages. CISC437/637, Lecture #6 Ben Cartere?e

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

Relational Algebra and SQL

Unit- III (Functional dependencies and Normalization, Relational Data Model and 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

Chapter 6 The Relational Algebra and Relational Calculus

Transcription:

Relational Query Languages: Relational Algebra

Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports simple, powerful QLs: Simple data structure sets! Easy to understand, easy to manipulate 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.

Formal Relational Query Languages Two mathematical Query Languages form the basis for real relational 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.)

What is an 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 Expressions can be constructed by applying operators to atomic operands and/or other expressions Operations can be composed -- algebra is closed Parentheses are needed to group operators

Basics of Relational Algebra Algebra of arithmetic: operands are variables and constants, and operators are the usual arithmetic operators E.g., (x+y)*2 or ((x+7)/(y-3)) + x Relational algebra: operands are variables that stand for relations and relations (sets of tuples), and operators are designed to do the most common things we need to do with relations in databases, e.g., union, intersection, selection, projection, Cartesian product, etc E.g., (π c-owner Checking-account) (π s-owner Savings-account) The result is an algebra that can be used as a query language for relations.

Basics of Relational Algebra (cont.) A query is applied to relation instances, and the result of a query is also a relation instance Algebra is closed à operations can be composed 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. Operators refer to relation attributes by position or name: E.g., Account(number, owner, balance, type) Positionalß Account.$1 = Account.number à Named field Positionalß Account.$3 = Account.balance à Named field Positional notation easier for formal definitions, named-field notation more readable.

Relational Algebra: Operations The usual set operations: union, intersection, difference Both operands must have the same relation schema Operations that remove parts of relations: Selection: pick certain rows Projection: pick certain columns Operations that combine tuples from two relations: Cartesian product, join Renaming of relations and attributes

Removing Parts of Relations

Selection: Example σ (sigma) σ c R= select -- produces a new relation with the subset of the tuples in R that match the condition C Sample query: σ Type = savings Account Account Number Owner Balance Type 101 J. Smith 1000.00 checking 102 W. Wei 2000.00 checking 103 J. Smith 5000.00 savings 104 M. Jones 1000.00 checking 105 H. Martin 10,000.00 checking Number Owner Balance Type 103 J. Smith 5000.00 savings

Selection: Another Example σ Balance < 4000 Account Account Selects rows that satisfy selection condition Number Owner Balance Type 101 J. Smith 1000.00 checking 102 W. Wei 2000.00 checking 103 J. Smith 5000.00 savings 104 M. Jones 1000.00 checking 105 H. Martin 10,000.00 checking Number Owner Balance Type 101 J. Smith 1000.00 checking 102 W. Wei 2000.00 checking 104 M. Jones 1000.00 checking Schema of result identical to schema of input relation

Projection: Example π (pi) π AttributeList R = project -- deletes attributes that are not in projection list. Sample query: π Number, Owner, Type Account Account Number Owner Balance Type 101 J. Smith 1000.00 checking 102 W. Wei 2000.00 checking 103 J. Smith 5000.00 savings 104 M. Jones 1000.00 checking 105 H. Martin 10,000.00 checking

π = project Projection: Example Sample query: π Number, Owner, Type Account Account Number Owner Balance Type 101 J. Smith 1000.00 checking 102 W. Wei 2000.00 checking 103 J. Smith 5000.00 savings 104 M. Jones 1000.00 checking 105 H. Martin 10,000.00 checking Number Owner Type 101 J. Smith checking 102 W. Wei checking 103 J. Smith savings 104 M. Jones checking 105 H. Martin checking

π = project Projection: Example Sample query: π Number, Owner, Type Account Account Number Owner Balance Type 101 J. Smith 1000.00 checking 102 W. Wei 2000.00 checking 103 J. Smith 5000.00 savings 104 M. Jones 1000.00 checking 105 H. Martin 10,000.00 checking Number Owner Type 101 J. Smith checking 102 W. Wei checking 103 J. Smith savings 104 M. Jones checking 105 H. Martin checking Schema of result is a subset of the schema of input relation

Projection: Another Example π Owner Account Account Number Owner Balance Type 101 J. Smith 1000.00 checking 102 W. Wei 2000.00 checking 103 J. Smith 5000.00 savings 104 M. Jones 1000.00 checking 105 H. Martin 10,000.00 checking Owner J. Smith W. Wei M. Jones H. Martin Note: Projection operator eliminates duplicates, Why??? In a DBMS products, do you think duplicates should be eliminated for every query? Are they?

Extended (Generalized) Projection Allows arithmetic functions and duplicate occurrences of the same attribute to be used in the projection list F1, F2,, Fn (E) E is any relational-algebra expression F 1, F 2,, F n are arithmetic expressions involving constants and attributes in the schema of E. Given relation credit-info(customer-name, limit, creditbalance), find how much more each person can spend: customer-name, limit credit-balance (credit-info) Can use rename to give a name to the column! customer-name, (limit credit-balance) à credit-available (credit-info)

Extended Projection: Another Example R = ( A B ) 1 2 3 4 π A+B->C,A,A (R) = C A1 A2 3 1 1 7 3 3 17

Set Operations

Union: Example = union Checking-account Savings-account Checking-account Savings-account c-num c-owner c-balance 101 J. Smith 1000.00 102 W. Wei 2000.00 104 M. Jones 1000.00 105 H. Martin 10,000.00 s-num s-owner s-balance 103 J. Smith 5000.00 c-num c-owner c-balance 101 J. Smith 1000.00 102 W. Wei 2000.00 104 M. Jones 1000.00 105 H. Martin 10,000.00 103 J. Smith 5000.00

Union Compatible Two relations are union-compatible if they have the same degree (i.e., the same number of attributes) and the corresponding attributes are defined on the same domains. Suppose we have these tables: Checking-Account (c-num:str, c-owner:str, c-balance:real) Savings-Account (s-num:str, s-owner:str, s-balance:real) These are union-compatible tables. Union, intersection, & difference require unioncompatible tables

Intersection = intersection Checking-account Savings-account What s the answer to this query? Checking-account c-num c-owner c-balance 101 J. Smith 1000.00 102 W. Wei 2000.00 104 M. Jones 1000.00 105 H. Martin 10,000.00 Savings-account s-num s-owner s-balance 103 J. Smith 5000.00

Intersection (cont.) Checking-account Savings-account What s the answer to this query? Checking-account c-num c-owner c-balance 101 J. Smith 1000.00 102 W. Wei 2000.00 104 M. Jones 1000.00 105 H. Martin 10,000.00 Savings-account s-num s-owner s-balance 103 J. Smith 5000.00 It s empty. There are no tuples that are in both tables. (π c-owner Checking-account) (π s-owner Savings-account) What s the answer to this new query?

Intersection (cont.) Checking-account Savings-account What s the answer to this query? It s empty. There are no tuples that are in both tables. Checking-account c-num c-owner c-balance 101 J. Smith 1000.00 102 W. Wei 2000.00 104 M. Jones 1000.00 105 H. Martin 10,000.00 (π c-owner Checking-account) (π s-owner Savings-account) Savings-account s-num s-owner s-balance 103 J. Smith 5000.00 What s the answer to this new query? c-owner J. Smith

= difference Difference Checking-account c-num c-owner c-balance 101 J. Smith 1000.00 102 W. Wei 2000.00 104 M. Jones 1000.00 105 H. Martin 10,000.00 Savings-account s-num s-owner s-balance 103 J. Smith 5000.00 Find all the customers that own a Checking-account and do not own a Savings-account. (π c-owner Checking-account) (π s-owner Savings-account) What is the schema of result? c-owner W. Wei M. Jones H. Martin

Challenge Question How could you express the intersection operation if you didn t have an Intersection operator in relational algebra? [Hint: Can you express Intersection using only the Difference operator?] A B =???

Combining Tuples of Two Relations

X cross product Cross Product: Example Teacher X Course Teacher t-num t-name Course c-num c-name 101 Smith 514 Intro to DB 105 Jones 513 Intro to OS 110 Fong Cross product: combine information from 2 tables produces: every possible combination of a teacher and a course t-num t-name c-num c-name 101 Smith 514 Intro to DB 105 Jones 514 Intro to DB 110 Fong 514 Intro to DB 101 Smith 513 Intro to OS 105 Jones 513 Intro to OS 110 Fong 513 Intro to OS

Cross Product R1 X R2 Each row of R1 is paired with each row of R2. Result schema has one field per field of R1 and R2, with field names `inherited if possible. What about R1 X R1? Teacher X Teacher t-num t-name t-num t-name Conflict! Renaming operator: Teacher X ρ T (Teacher) = Teacher X T ρ T(t-num1, t-name1) (Teacher) X Teacher No conflict!

Renaming The ρ operator gives a new name to a relation. R1 := ρ R1(A1,,An) (R2) makes R1 be a relation with attributes A1,,An and the same tuples as R2. Simplified notation: R1(A1,,An) := R2. 32

Example: Renaming Bookstore( name, addr ) Joe s Maple St. Sue s River Rd. R(bs, addr) := Bookstore R( bs, addr ) Joe s Maple St. Sue s River Rd. 33

Join: Example = join Account Number=Account Deposit Account Number Owner Balance Type 101 J. Smith 1000.00 checking 102 W. Wei 2000.00 checking 103 J. Smith 5000.00 savings 104 M. Jones 1000.00 checking 105 H. Martin 10,000.00 checking Deposit Account Transaction-id Date Amount 102 1 10/22/00 500.00 102 2 10/29/00 200.00 104 3 10/29/00 1000.00 105 4 11/2/00 10,000.00 Number Owner Balance Type Account Transaction-id Date Amount 102 W. Wei 2000.00 checking 102 1 10/22/00 500.00 102 W. Wei 2000.00 checking 102 2 10/29/00 200.00 104 M. Jones 1000.00 checking 104 3 10/29/00 1000.00 105 H. Martin 10,000.00 checking 105 4 11/2/00 10000.00

Join: Example join Account Number=Account Deposit Note that when the join is based on equality, then we have two identical attributes (columns) in the answer. Number Owner Balance Type Account Trans-id Date Amount 102 W. Wei 2000.00 checking 102 1 10/22/00 500.00 102 W. Wei 2000.00 checking 102 2 10/29/00 200.00 104 M. Jones 1000.00 checking 104 3 10/29/00 1000.00 105 H. Martin 10,000.00 checking 105 4 11/2/00 10000.00

Join: Example join Account (Number=Account and Amount>700) Deposit Account Deposit Number Owner Balance Type 101 J. Smith 1000.00 checking 102 W. Wei 2000.00 checking 103 J. Smith 5000.00 savings 104 M. Jones 1000.00 checking 105 H. Martin 10,000.00 checking Account T-id Date Amount 102 1 10/22/00 500.00 102 2 10/29/00 200.00 104 3 10/29/00 1000.00 105 4 11/2/00 10,000.00 Number Owner Balance Type Account T-id Date Amount 104 M. Jones 1000.00 checking 104 3 10/29/00 1000.00 105 H. Martin 10,000.00 checking 105 4 11/2/00 10000.00

Challenge Question How could you express the join operation if you didn t have a join operator in relational algebra? [Hint: are there other operators that you could use, in combination?]

Joins Condition Join: R c S = σ c (R X S) Sometimes called a theta-join Result schema same as that of cross-product Fewer tuples than cross-product, might be able to compute more efficiently

Joins Equi-Join: A special case of condition join where the condition c contains only equalities. Student sid Takes Result schema similar to cross-product, but only one copy of fields for which equality is specified. Natural Join: Equijoin on all common fields. r s Joined tuple

Challenge Question How could you express the natural join operation if you didn t have a natural join operator in relational algebra? Consider you have two relations R(A,B,C) and S(B,C,D).????