Relational Algebra & Calculus

Similar documents
Relational Algebra and Calculus

Introduction to Data Management. Lecture #12 (Relational Algebra II)

Relational Algebra 2. Week 5

Database Applications (15-415)

Database Systems Relational Algebra. A.R. Hurson 323 CS Building

CS5300 Database Systems

Database Systems SQL. A.R. Hurson 323 CS Building

Databases 2011 The Relational Algebra

Schema Refinement and Normal Forms. The Evils of Redundancy. Schema Refinement. Yanlei Diao UMass Amherst April 10, 2007

Schema Refinement. Yanlei Diao UMass Amherst. Slides Courtesy of R. Ramakrishnan and J. Gehrke

Schema Refinement and Normal Forms

Schema Refinement and Normal Forms. Case Study: The Internet Shop. Redundant Storage! Yanlei Diao UMass Amherst November 1 & 6, 2007

Plan of the lecture. G53RDB: Theory of Relational Databases Lecture 2. More operations: renaming. Previous lecture. Renaming.

Schema Refinement and Normal Forms

Database Applications (15-415)

CAS CS 460/660 Introduction to Database Systems. Functional Dependencies and Normal Forms 1.1

Chapter 3 Relational Model

Query Processing. 3 steps: Parsing & Translation Optimization Evaluation

7 RC Simulates RA. Lemma: For every RA expression E(A 1... A k ) there exists a DRC formula F with F V (F ) = {A 1,..., A k } and

INTRODUCTION TO RELATIONAL DATABASE SYSTEMS

General Overview - rel. model. Carnegie Mellon Univ. Dept. of Computer Science /615 DB Applications. Motivation. Overview - detailed

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

Relational Algebra on Bags. Why Bags? Operations on Bags. Example: Bag Selection. σ A+B < 5 (R) = A B

Relational Algebra Part 1. Definitions.

CS632 Notes on Relational Query Languages I

Introduction to Data Management. Lecture #6 (Relational Design Theory)

CSE 562 Database Systems

Computational Logic. Relational Query Languages with Negation. Free University of Bozen-Bolzano, Werner Nutt

Introduction to Data Management. Lecture #6 (Relational DB Design Theory)

CS54100: Database Systems

3515ICT: Theory of Computation. Regular languages

Schema Refinement and Normal Forms

Sets are one of the basic building blocks for the types of objects considered in discrete mathematics.

The Evils of Redundancy. Schema Refinement and Normal Forms. Example: Constraints on Entity Set. Functional Dependencies (FDs) Example (Contd.

The Evils of Redundancy. Schema Refinement and Normal Forms. Example: Constraints on Entity Set. Functional Dependencies (FDs) Refining an ER Diagram

Query answering using views

GAV-sound with conjunctive queries

Proving simple set properties...

Schedule. Today: Jan. 17 (TH) Jan. 24 (TH) Jan. 29 (T) Jan. 22 (T) Read Sections Assignment 2 due. Read Sections Assignment 3 due.

Equational Logic. Chapter Syntax Terms and Term Algebras

First-Order Theorem Proving and Vampire. Laura Kovács (Chalmers University of Technology) Andrei Voronkov (The University of Manchester)

Languages. Theory I: Database Foundations. Relational Algebra. Paradigms. Projection. Basic Operators. Jan-Georg Smaus (Georg Lausen)

Background for Discrete Mathematics

Equivalence Relations and Partitions, Normal Subgroups, Quotient Groups, and Homomorphisms

Foundations of Mathematics MATH 220 FALL 2017 Lecture Notes

CAIM: Cerca i Anàlisi d Informació Massiva

Mathematics Review for Business PhD Students Lecture Notes

CS2742 midterm test 2 study sheet. Boolean circuits: Predicate logic:

Handout 2 (Correction of Handout 1 plus continued discussion/hw) Comments and Homework in Chapter 1

CS 186, Fall 2002, Lecture 6 R&G Chapter 15

Database Design and Normalization

CIS 375 Intro to Discrete Mathematics Exam 3 (Section M004: Blue) 6 December Points Possible

Sri vidya college of engineering and technology

Relational Calculus. Dr Paolo Guagliardo. University of Edinburgh. Fall 2016

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska

Relational completeness of query languages for annotated databases

Structural Induction

Mathematics Review for Business PhD Students

Notes on Sets for Math 10850, fall 2017

2.2 Lowenheim-Skolem-Tarski theorems

CIS 375 Intro to Discrete Mathematics Exam 3 (Section M001: Green) 6 December Points Possible

Packet #2: Set Theory & Predicate Calculus. Applied Discrete Mathematics

Learning Goals. Relational Query Languages. Formal Relational Query Languages. Formal Query Languages: Relational Algebra and Relational Calculus

Discrete Mathematics. W. Ethan Duckworth. Fall 2017, Loyola University Maryland

Database Normaliza/on. Debapriyo Majumdar DBMS Fall 2016 Indian Statistical Institute Kolkata

Math 541 Fall 2008 Connectivity Transition from Math 453/503 to Math 541 Ross E. Staffeldt-August 2008

Lecture 2: Syntax. January 24, 2018

Introduction to Metalogic

CHAPTER 1: Functions

6c Lecture 14: May 14, 2014

Incomplete Information in RDF

Study sheet for Final CS1100 by Matt in Wed night class

With Question/Answer Animations. Chapter 2

The non-logical symbols determine a specific F OL language and consists of the following sets. Σ = {Σ n } n<ω

About the relationship between formal logic and complexity classes

BOOLEAN ALGEBRA INTRODUCTION SUBSETS

2.1 Sets. Definition 1 A set is an unordered collection of objects. Important sets: N, Z, Z +, Q, R.

Today s Topics. Methods of proof Relationships to logical equivalences. Important definitions Relationships to sets, relations Special functions

Lecture 11: Measuring the Complexity of Proofs

2 Metric Spaces Definitions Exotic Examples... 3

Discrete Mathematics. (c) Marcin Sydow. Sets. Set operations. Sets. Set identities Number sets. Pair. Power Set. Venn diagrams

Propositional Logic. What is discrete math? Tautology, equivalence, and inference. Applications

Discrete Basic Structure: Sets

arxiv: v1 [cs.pl] 19 May 2016

Logic and Databases. Phokion G. Kolaitis. UC Santa Cruz & IBM Research Almaden. Lecture 4 Part 1

Hyperreal Numbers: An Elementary Inquiry-Based Introduction. Handouts for a course from Canada/USA Mathcamp Don Laackman

Constraints: Functional Dependencies

ALTER TABLE Employee ADD ( Mname VARCHAR2(20), Birthday DATE );

Sets. Slides by Christopher M. Bourke Instructor: Berthe Y. Choueiry. Fall 2007

Great Theoretical Ideas in Computer Science. Lecture 4: Deterministic Finite Automaton (DFA), Part 2

CSE 2001: Introduction to Theory of Computation Fall Suprakash Datta

From Constructibility and Absoluteness to Computability and Domain Independence

Introduction. Foundations of Computing Science. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Constraints: Functional Dependencies

CHAPTER 1. Preliminaries. 1 Set Theory

Relations. Relations of Sets N-ary Relations Relational Databases Binary Relation Properties Equivalence Relations. Reading (Epp s textbook)

Exercises for Unit VI (Infinite constructions in set theory)

Schema Refinement and Normal Forms. The Evils of Redundancy. Functional Dependencies (FDs) [R&G] Chapter 19

Part II. Logic and Set Theory. Year

Chapter 0 Introduction. Fourth Academic Year/ Elective Course Electrical Engineering Department College of Engineering University of Salahaddin

Transcription:

Relational Algebra & Calculus Yanlei Diao UMass Amherst Slides Courtesy of R. Ramakrishnan and J. Gehrke 1

Outline v Conceptual Design: ER model v Logical Design: ER to relational model v Querying and manipulating data Practical language: SQL Declarative: say what you want, not how to get it Formal languages: Relational Algebra and Calculus Mathematical foundation for query processing 2

What is an Algebra? v A 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. 3

What is Relational Algebra v Relational Algebra: Operands are relations. Each operator takes 1 or 2 relations and produces a relation. v Closure property: relational algebra is closed under the relational model. Relational operators can be arbitrarily composed! 4

Relational Algebra v Basic operations: Selection ( σ ) Selects a subset of rows from a relation. Projection ( π ) Deletes unwanted columns from a 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 or in reln. 2. v Additional operations: Join ( ), Intersection ( ), Division ( / ), Renaming ( ρ ) Can be derived from basic operators. Not essential, but useful for writing simpler queries. 5

Example Instances Sailors S1 S2 22 dustin 7 45.0 31 lubber 8 55.5 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 R1 Reserves sid bid day 22 101 10/10/96 58 103 11/12/96 6

Projection v Retain only attributes in the projection list; delete others. v Schema of result contains exactly the fields in projection list. S2 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 sname yuppy 9 lubber 8 guppy 5 rusty 10 rating π ( S2) sname, rating 7

Projection (contd.) S2 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 age 35.0 55.5 π age ( S2) v Projection operator has to eliminate duplicates! Real systems typically don t do duplicate elimination unless the user explicitly asks for it. (Why not?) 8

Selection v Select rows that satisfy the selection condition; discard others. v Schema of result identical to schema of input. S2 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 28 yuppy 9 35.0 σ rating S >8 ( 2) 9

Selection (contd.) S2 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 σ (rating > 5 rating < 5) age < 50 (S2) 10

A Simple Example of Composition v Composition: result relation of an operator can be the input to another operator. 28 yuppy 9 35.0 σ rating S >8 ( 2) sname rating yuppy 9 rusty 10 π σ ( ( S )) sname, rating rating>8 2 11

Union, Intersection, Set-Difference v v v Set operations: Union ( ) Intersection ( ) Set difference ( - ) Take two input relations, which must be union-compatible: Same number of fields. Corresponding fields have the the same type. What is the schema of result? S1 22 dustin 7 45.0 31 lubber 8 55.5 S2 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 12

Example Set Operations S1 22 dustin 7 45.0 31 lubber 8 55.5 S2 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 22 dustin 7 45.0 31 lubber 8 55.5 44 guppy 5 35.0 28 yuppy 9 35.0 S1 S2 Duplicate elimination: remove tuples that have same values in all attributes. 13

Example Set Operations S1 22 dustin 7 45.0 31 lubber 8 55.5 S2 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 22 dustin 7 45.0 S1 S2 31 lubber 8 55.5 S1 S2 Duplicates? 14

Cross (Cartesian) Product v S1 R1: Each row of S1 is paired with each row of R1. S1 22 dustin 7 45.0 31 lubber 8 55.5 R1 sid bid day 22 101 10/10/96 58 103 11/12/96 S1 R1 (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 22 101 10/10/96 58 103 11/12/96 15

Cross-Product (contd.) (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 22 101 10/10/96 58 103 11/12/96 v Result schema inherits all fields of S1 and R1. Conflict: Both S1 and R1 have a field called sid. Renaming operator: ρ ( C( 1 sid1, 5 sid2), S1 R1) What if we want to find pairs s.t. S1.sid = S2.sid? 16

Join v Condition (theta) Join: R θ S = σ θ (R S) (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 S 1 R 1 S1. sid < R1. sid Result schema same as that of cross-product. But often fewer tuples, more efficient for computation. 17

Join v Equi-Join: A special case of condition join where the condition θ contains only equalities. bid day 22 dustin 7 45.0 101 10/10/96 103 11/12/96 S1 R1 sid Result schema contains only one copy of fields for which equality is specified. v Natural Join ( R w v S ): equi-join on all common fields. 18

Self-Join S1 22 dustin 7 45.0 31 lubber 8 55.5 S2 22 dustin 7 45.0 31 lubber 8 55.5 ρ(s1,s) ρ(s2,s) S1.rating>S2.rating S1.age<S2.age 19

Example Schema v Sailors(sid: integer, sname: string, rating: integer, age: integer) v Boats(bid: integer, color: string) v Reserves(sid: integer, bid: integer, day: date) 20

Find names of sailors who ve reserved boat #103 v Sailors(sid: integer, sname: string, rating: integer, age: integer) v Boats(bid: integer, color: string) v Reserves(sid: integer, bid: integer, day: date) v How many relations do we need to access? If more than 1, use cross-products or joins. v Filtering condition? Use selection. v Attributes to return? Use projection. 21

Find names of sailors who ve reserved boat #103 v Solution 1: π (( σ Re serves ) Sailors ) sname bid =103 v Solution 2: ρ ( Temp1, σ Re serves) bid =103 ρ ( Temp2, Temp1 Sailors) π sname ( Temp2) v Solution 3: π sname( σ (Re serves Sailors)) bid =103 Algebraic equivalence! 22

Find names of sailors who ve reserved a red boat v Sailors(sid: integer, sname: string, rating: integer, age: integer) v Boats(bid: integer, color: string) v Reserves(sid: integer, bid: integer, day: date) v Boat color is only available in Boats; so need an extra join: π sname (( σ color = ' red ' Boats) Re serves Sailors) 23

Find names of sailors who ve reserved a red or a green boat v Sailors(sid: integer, sname: string, rating: integer, age: integer) v Boats(bid: integer, color: string) v Reserves(sid: integer, bid: integer, day: date) 24

Find names of sailors who ve reserved a red or a green boat v 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) v Can also define Tempboats using union. How? 25

Find names of sailors who ve reserved a red boat and a green boat v A single selection won t work. Why? v Instead, intersect sailors who ve reserved red boats and sailors who ve reserved green boats. sid is a key for Sailors ρ ( Tempred, π (( σ sid color = ' red ' ρ ( Tempgreen, π (( σ sid color = ' green' Boats) Re serves)) Boats) Re serves)) π sname (( Tempred Tempgreen) Sailors) Can we project onto name and then do the intersection? 26

Find the names of sailors who ve reserved all boats 27

Relational Calculus v Query has the form: ' ) ( ) *! # x1, x2,..., xn p x1, x2,..., xn # " $ + & ), & % ) - domain variables, or constants formula v Answer includes all tuples x1, x2,..., xn that make the formula! # p x1, x2,..., xn # " $ & & % be true. 28

Formulas v Formula is recursively defined: Atomic formulas: retrieving tuples from relations, or making comparisons of values Logical connectives:,, Quantifiers:, (optional) o x F(x) is equivalent to x F(x) 29

Find names of sailors rated > 7 who have reserved boat #103 Relational Algebra: π (( σ Reserves) ( σ sname bid = 103 rating> 7 Sailors)) Relational Calculus: {X sname X sid, X rating, X age Sailors(X sid, X sname, X rating, X age ) X rating > 7 X bid, X day Reserves(X sid, X bid, X day ) X bid =103 } v Where is the join? Use to find a tuple in Reserves that `joins with the Sailors tuple under consideration. 30

Find names of sailors who ve reserved all boats Sailors(sid: integer, sname: string, rating: integer, age: integer) Boats(bid: integer, color: string) Reserves(sid: integer, bid: integer, day: date) {X sname X sid, X rating, X age X sid, X sname, X rating, X age Sailors X bid, X color Boats ( X day X sid, X bid, X day Reserves) } v To find sailors who ve reserved all red boats: {X sname X sid, X rating, X age X sid, X sname, X rating, X age Sailors X bid, X color Boats (X color ʹ redʹ X day X sid, X bid, X day Reserves) } 31

Unsafe Queries, Expressive Power v Some syntactically correct calculus queries can have an infinite number of answers! Such queries are called unsafe. ( " %, * e.g., S $ S Sailors' * ) - * + $ # v Equivalence result: every query that can be expressed in relational algebra can be expressed as a safe query in relational calculus; the converse is also true. ' & *. v Query language (e.g., SQL) can express every query that is expressible in relational algebra/calculus. 32

Find names of sailors who ve reserved all boats {X sname X sid, X rating, X age X sid, X sname, X rating, X age Sailors X bid, X color Boats ( X day X sid, X bid, X day Reserves) } {X sname X sid, X rating, X age X sid, X sname, X rating, X age Sailors X bid, X color Boats ( X day X sid, X bid, X day Reserves) } 33

Find the names of sailors who ve reserved all boats v Step 1: for each sailor, check if there exists a boat that he has not reserved (called formula F). ρ(s _ neg, π sid ( (π sid Re serves) (π bid Boats) (π sid,bid Re serves))) v Step 2: find sailors for which F is not true and retrieve their names π sname ( (π sid Re serves S _ neg) Sailors ) - : the only way to express negation in relational algebra! 34