Lecture #4: Relational Algebra

Slides:



Advertisements
Similar presentations
CMU SCS Carnegie Mellon Univ. School of Computer Science /615 - DB Applications C. Faloutsos & A. Pavlo Lecture #4: Relational Algebra.
Advertisements

CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos & A. Pavlo Lecture#6: Rel. model - SQL part1 (R&G, chapter.
Relational Algebra Tim Kaddoura CS157A. Introduction  Relational query languages are languages for describing queries on a relational database  Three.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications Lecture #16: Schema Refinement & Normalization - Functional Dependencies.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Lecture#7 (cont’d): Rel. model - SQL part3.
Relational Algebra Chapter 4 - part I. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos & A. Pavlo Lecture#7 : Rel. model - SQL part2 (R&G, chapters.
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Rel. model - SQL part1.
Temple University – CIS Dept. CIS331– Principles of Database Systems V. Megalooikonomou Relational Model – SQL Part I (based on notes by Silberchatz,Korth,
Temple University – CIS Dept. CIS331– Principles of Database Systems V. Megalooikonomou Relational Model III (based on notes by Silberchatz,Korth, and.
DBSQL 3-1 Copyright © Genetic Computer School 2009 Chapter 3 Relational Database Model.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science Database Applications Lecture#6: Relational calculus.
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Rel. model - SQL part3.
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Relational model.
Temple University – CIS Dept. CIS331– Principles of Database Systems V. Megalooikonomou Relational Model I (based on notes by Silberchatz,Korth, and Sudarshan.
Chapter 2: Intro to Relational Model. 2.2 Example of a Relation attributes (or columns) tuples (or rows)
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Relational domain calculus.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 – DB Applications C. Faloutsos & A. Pavlo Lecture#5: Relational calculus.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications Lecture #16: Schema Refinement & Normalization - Functional Dependencies.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Rel. model - SQL part2.
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Database design and normalization.
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Relational tuple calculus.
Temple University – CIS Dept. CIS616– Principles of Database Systems V. Megalooikonomou Relational Model (based on notes by Silberchatz,Korth, and Sudarshan.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
Chapter (6) The Relational Algebra and Relational Calculus Objectives
COMP3017 Advanced Databases
Module 2: Intro to Relational Model
CS4432: Database Systems II
Temple University – CIS Dept. CIS661– Principles of Database Systems
Faloutsos - Pavlo C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams
Introduction to Relational Model
Chapter 2: Intro to Relational Model
Relational Algebra Chapter 4 1.
Chapter 3: Intro to Relational Model
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Relational Algebra Chapter 4, Part A
Chapter 2: Intro to Relational Model
Relational Algebra 461 The slides for this text are organized into chapters. This lecture covers relational algebra, from Chapter 4. The relational calculus.
Relational Algebra.
Database Applications (15-415) Relational Calculus Lecture 6, September 6, 2016 Mohammad Hammoud.
The Relational Model Relational Data Model
Relational Algebra 1.
Faloutsos & Pavlo SCS /615 Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications Lecture #16: Schema Refinement & Normalization.
LECTURE 3: Relational Algebra
Relational Algebra Chapter 4 1.
The Relational Algebra and Relational Calculus
Lecture#5: Relational calculus
Relational Algebra Chapter 4 - part I.
Announcements Project 2’s due date is moved to Tuesday 8/3/04
Lecture #17: Schema Refinement & Normalization - Normal Forms
Temple University – CIS Dept. CIS331– Principles of Database Systems
Relational Algebra Chapter 4, Sections 4.1 – 4.2
Computer Science 317 Database Management
02 - The Relational Database Model
Faloutsos - Pavlo C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Example of a Relation attributes (or columns) tuples (or rows)
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
CENG 351 File Structures and Data Managemnet
Chapter 4 Relational Algebra
04 SQL & Relational Algebra
Lecture 2 Relational Database
Presentation transcript:

Lecture #4: Relational Algebra Faloutsos - Pavlo CMU SCS 15-415/615 Carnegie Mellon Univ. School of Computer Science 15-415/615 - DB Applications C. Faloutsos & A. Pavlo Lecture #4: Relational Algebra

Overview history concepts Formal query languages relational algebra Faloutsos - Pavlo CMU SCS 15-415/615 Overview history concepts Formal query languages relational algebra rel. tuple calculus rel. domain calculus Faloutsos - Pavlo CMU SCS 15-415/615

History before: records, pointers, sets etc Faloutsos - Pavlo CMU SCS 15-415/615 History before: records, pointers, sets etc introduced by E.F. Codd in 1970 revolutionary! first systems: 1977-8 (System R; Ingres) Turing award in 1981 Faloutsos - Pavlo CMU SCS 15-415/615

Concepts - reminder Database: a set of relations (= tables) Faloutsos - Pavlo CMU SCS 15-415/615 Concepts - reminder Database: a set of relations (= tables) rows: tuples columns: attributes (or keys) superkey, candidate key, primary key Faloutsos - Pavlo CMU SCS 15-415/615

Example Database: Faloutsos - Pavlo CMU SCS 15-415/615

Example: cont’d Database: k-th attribute (Dk domain) Faloutsos - Pavlo CMU SCS 15-415/615 Example: cont’d k-th attribute (Dk domain) Database: rel. schema (attr+domains) tuple Faloutsos - Pavlo CMU SCS 15-415/615

Example: cont’d rel. schema (attr+domains) instance Faloutsos - Pavlo CMU SCS 15-415/615 Example: cont’d rel. schema (attr+domains) instance Faloutsos - Pavlo CMU SCS 15-415/615

Example: cont’d Di: the domain of the i-th attribute (eg., char(10) Faloutsos - Pavlo CMU SCS 15-415/615 Example: cont’d Di: the domain of the i-th attribute (eg., char(10) rel. schema (attr+domains) instance Faloutsos - Pavlo CMU SCS 15-415/615

Overview history concepts Formal query languages relational algebra Faloutsos - Pavlo CMU SCS 15-415/615 Overview history concepts Formal query languages relational algebra rel. tuple calculus rel. domain calculus Faloutsos - Pavlo CMU SCS 15-415/615

Formal query languages Faloutsos - Pavlo CMU SCS 15-415/615 Formal query languages How do we collect information? Eg., find ssn’s of people in 415 (recall: everything is a set!) One solution: Rel. algebra, ie., set operators Q1: Which ones?? Q2: what is a minimal set of operators? Faloutsos - Pavlo CMU SCS 15-415/615

Relational operators . set union U set difference ‘-’ Faloutsos - Pavlo CMU SCS 15-415/615 Relational operators . set union U set difference ‘-’ Faloutsos - Pavlo CMU SCS 15-415/615

Example: Q: find all students (part or full time) Faloutsos - Pavlo CMU SCS 15-415/615 Example: Q: find all students (part or full time) A: PT-STUDENT union FT-STUDENT Faloutsos - Pavlo CMU SCS 15-415/615

Faloutsos - Pavlo CMU SCS 15-415/615 Observations: two tables are ‘union compatible’ if they have the same attributes (‘domains’) Q: how about intersection U Faloutsos - Pavlo CMU SCS 15-415/615

Observations: A: redundant: STUDENT intersection STAFF = STAFF STUDENT Faloutsos - Pavlo CMU SCS 15-415/615 Observations: A: redundant: STUDENT intersection STAFF = STAFF STUDENT Faloutsos - Pavlo CMU SCS 15-415/615

Observations: A: redundant: STUDENT intersection STAFF = STAFF STUDENT Faloutsos - Pavlo CMU SCS 15-415/615 Observations: A: redundant: STUDENT intersection STAFF = STAFF STUDENT Faloutsos - Pavlo CMU SCS 15-415/615

Observations: A: redundant: STUDENT intersection STAFF = Faloutsos - Pavlo CMU SCS 15-415/615 Observations: A: redundant: STUDENT intersection STAFF = STUDENT - (STUDENT - STAFF) STAFF STUDENT Faloutsos - Pavlo CMU SCS 15-415/615

Observations: A: redundant: STUDENT intersection STAFF = Faloutsos - Pavlo CMU SCS 15-415/615 Observations: A: redundant: STUDENT intersection STAFF = STUDENT - (STUDENT - STAFF) Double negation: We’ll see it again, later… Faloutsos - Pavlo CMU SCS 15-415/615

Relational operators . set union set difference ‘-’ U Faloutsos - Pavlo CMU SCS 15-415/615 Relational operators . set union set difference ‘-’ U Faloutsos - Pavlo CMU SCS 15-415/615

Other operators? eg, find all students on ‘Main street’ A: ‘selection’ Faloutsos - Pavlo CMU SCS 15-415/615 Other operators? eg, find all students on ‘Main street’ A: ‘selection’ Faloutsos - Pavlo CMU SCS 15-415/615

Faloutsos - Pavlo CMU SCS 15-415/615 Other operators? Notice: selection (and rest of operators) expect tables, and produce tables (-> can be cascaded!!) For selection, in general: Faloutsos - Pavlo CMU SCS 15-415/615

Selection - examples Find all ‘Smiths’ on ‘Forbes Ave’ Faloutsos - Pavlo CMU SCS 15-415/615 Selection - examples Find all ‘Smiths’ on ‘Forbes Ave’ ‘condition’ can be any boolean combination of ‘=‘, ‘>’, ‘>=‘, ... Faloutsos - Pavlo CMU SCS 15-415/615

Relational operators selection . set union set difference R - S R U S Faloutsos - Pavlo CMU SCS 15-415/615 Relational operators selection . set union set difference R - S R U S Faloutsos - Pavlo CMU SCS 15-415/615

Relational operators selection picks rows - how about columns? Faloutsos - Pavlo CMU SCS 15-415/615 Relational operators selection picks rows - how about columns? A: ‘projection’ - eg.: finds all the ‘ssn’ - removing duplicates Faloutsos - Pavlo CMU SCS 15-415/615

Relational operators Cascading: ‘find ssn of students on ‘forbes ave’ Faloutsos - Pavlo CMU SCS 15-415/615 Relational operators Cascading: ‘find ssn of students on ‘forbes ave’ Faloutsos - Pavlo CMU SCS 15-415/615

Relational operators selection projection . set union Faloutsos - Pavlo CMU SCS 15-415/615 Relational operators selection projection . set union set difference R - S R U S Faloutsos - Pavlo CMU SCS 15-415/615

Relational operators Are we done yet? Faloutsos - Pavlo CMU SCS 15-415/615 Relational operators Are we done yet? Q: Give a query we can not answer yet! Faloutsos - Pavlo CMU SCS 15-415/615

Relational operators A: any query across two or more tables, Faloutsos - Pavlo CMU SCS 15-415/615 Relational operators A: any query across two or more tables, eg., ‘find names of students in 15-415’ Q: what extra operator do we need?? Faloutsos - Pavlo CMU SCS 15-415/615

Relational operators A: any query across two or more tables, Faloutsos - Pavlo CMU SCS 15-415/615 Relational operators A: any query across two or more tables, eg., ‘find names of students in 15-415’ Q: what extra operator do we need?? A: surprisingly, cartesian product is enough! Faloutsos - Pavlo CMU SCS 15-415/615

Cartesian product eg., dog-breeding: MALE x FEMALE Faloutsos - Pavlo CMU SCS 15-415/615 Cartesian product eg., dog-breeding: MALE x FEMALE gives all possible couples = x Faloutsos - Pavlo CMU SCS 15-415/615

so what? Eg., how do we find names of students taking 415? Faloutsos - Pavlo CMU SCS 15-415/615 so what? Eg., how do we find names of students taking 415? Faloutsos - Pavlo CMU SCS 15-415/615

Cartesian product A: Faloutsos - Pavlo CMU SCS 15-415/615

Cartesian product Faloutsos - Pavlo CMU SCS 15-415/615

Faloutsos - Pavlo CMU SCS 15-415/615 Faloutsos - Pavlo CMU SCS 15-415/615

FUNDAMENTAL Relational operators Faloutsos - Pavlo CMU SCS 15-415/615 FUNDAMENTAL Relational operators selection projection cartesian product MALE x FEMALE set union set difference R - S R U S Faloutsos - Pavlo CMU SCS 15-415/615

Faloutsos - Pavlo CMU SCS 15-415/615 Relational ops Surprisingly, they are enough, to help us answer almost any query we want!! derived/convenience operators: set intersection join (theta join, equi-join, natural join) ‘rename’ operator division Faloutsos - Pavlo CMU SCS 15-415/615

Joins Equijoin: Faloutsos - Pavlo CMU SCS 15-415/615 Faloutsos - Pavlo

Cartesian product A: Faloutsos - Pavlo CMU SCS 15-415/615

Joins Equijoin: theta-joins: Faloutsos - Pavlo CMU SCS 15-415/615 Joins Equijoin: theta-joins: generalization of equi-join - any condition Faloutsos - Pavlo CMU SCS 15-415/615

Joins very popular: natural join: R S Faloutsos - Pavlo CMU SCS 15-415/615 Joins very popular: natural join: R S like equi-join, but it drops duplicate columns: STUDENT (ssn, name, address) TAKES (ssn, cid, grade) Faloutsos - Pavlo CMU SCS 15-415/615

Joins nat. join has 5 attributes equi-join: 6 Faloutsos - Pavlo CMU SCS 15-415/615 Joins nat. join has 5 attributes equi-join: 6 Faloutsos - Pavlo CMU SCS 15-415/615

Natural Joins - nit-picking Faloutsos - Pavlo CMU SCS 15-415/615 Natural Joins - nit-picking if no attributes in common between R, S: nat. join -> cartesian product Faloutsos - Pavlo CMU SCS 15-415/615

Overview - rel. algebra fundamental operators derived operators Faloutsos - Pavlo CMU SCS 15-415/615 Overview - rel. algebra fundamental operators derived operators joins etc rename division examples Faloutsos - Pavlo CMU SCS 15-415/615

Rename op. Q: why? A: shorthand; self-joins; … Faloutsos - Pavlo CMU SCS 15-415/615 Rename op. Q: why? A: shorthand; self-joins; … for example, find the grand-parents of ‘Tom’, given PC (parent-id, child-id) Faloutsos - Pavlo CMU SCS 15-415/615

Rename op. PC (parent-id, child-id) Faloutsos - Pavlo CMU SCS 15-415/615 Rename op. PC (parent-id, child-id) Faloutsos - Pavlo CMU SCS 15-415/615

Rename op. first, WRONG attempt: (why? how many columns?) Faloutsos - Pavlo CMU SCS 15-415/615 Rename op. first, WRONG attempt: (why? how many columns?) Second WRONG attempt: Faloutsos - Pavlo CMU SCS 15-415/615

Faloutsos - Pavlo CMU SCS 15-415/615 Rename op. we clearly need two different names for the same table - hence, the ‘rename’ op. Faloutsos - Pavlo CMU SCS 15-415/615

Overview - rel. algebra fundamental operators derived operators Faloutsos - Pavlo CMU SCS 15-415/615 Overview - rel. algebra fundamental operators derived operators joins etc rename division examples Faloutsos - Pavlo CMU SCS 15-415/615

Division Rarely used, but powerful. Faloutsos - Pavlo CMU SCS 15-415/615 Division Rarely used, but powerful. Example: find suspicious suppliers, ie., suppliers that supplied all the parts in A_BOMB Faloutsos - Pavlo CMU SCS 15-415/615

Division Faloutsos - Pavlo CMU SCS 15-415/615 Faloutsos - Pavlo

Division Observations: ~reverse of cartesian product Faloutsos - Pavlo CMU SCS 15-415/615 Division Observations: ~reverse of cartesian product It can be derived from the 5 fundamental operators (!!) How? Faloutsos - Pavlo CMU SCS 15-415/615

Faloutsos - Pavlo CMU SCS 15-415/615 Division Answer: Observation: find ‘good’ suppliers, and subtract! (double negation) Faloutsos - Pavlo CMU SCS 15-415/615

Faloutsos - Pavlo CMU SCS 15-415/615 Division Answer: Observation: find ‘good’ suppliers, and subtract! (double negation) Faloutsos - Pavlo CMU SCS 15-415/615

Division Answer: All suppliers All bad parts Faloutsos - Pavlo CMU SCS 15-415/615 Division Answer: All suppliers All bad parts Faloutsos - Pavlo CMU SCS 15-415/615

Division Answer: all possible suspicious shipments Faloutsos - Pavlo CMU SCS 15-415/615 Division Answer: all possible suspicious shipments Faloutsos - Pavlo CMU SCS 15-415/615

Division Answer: all possible suspicious shipments that didn’t happen Faloutsos - Pavlo CMU SCS 15-415/615 Division Answer: all possible suspicious shipments that didn’t happen Faloutsos - Pavlo CMU SCS 15-415/615

all suppliers who missed at least one suspicious shipment, Faloutsos - Pavlo CMU SCS 15-415/615 Division Answer: all suppliers who missed at least one suspicious shipment, i.e.: ‘good’ suppliers Faloutsos - Pavlo CMU SCS 15-415/615

Overview - rel. algebra fundamental operators derived operators Faloutsos - Pavlo CMU SCS 15-415/615 Overview - rel. algebra fundamental operators derived operators joins etc rename division examples Faloutsos - Pavlo CMU SCS 15-415/615

Sample schema find names of students that take 15-415 Faloutsos - Pavlo CMU SCS 15-415/615 Sample schema find names of students that take 15-415 Faloutsos - Pavlo CMU SCS 15-415/615

Examples find names of students that take 15-415 Faloutsos - Pavlo CMU SCS 15-415/615 Examples find names of students that take 15-415 Faloutsos - Pavlo CMU SCS 15-415/615

Examples find names of students that take 15-415 Faloutsos - Pavlo CMU SCS 15-415/615 Examples find names of students that take 15-415 Faloutsos - Pavlo CMU SCS 15-415/615

Sample schema find course names of ‘smith’ Faloutsos - Pavlo CMU SCS 15-415/615 Sample schema find course names of ‘smith’ Faloutsos - Pavlo CMU SCS 15-415/615

Examples find course names of ‘smith’ Faloutsos - Pavlo CMU SCS 15-415/615 Examples find course names of ‘smith’ Faloutsos - Pavlo CMU SCS 15-415/615

Faloutsos - Pavlo CMU SCS 15-415/615 Examples find ssn of ‘overworked’ students, ie., that take 412, 413, 415 Faloutsos - Pavlo CMU SCS 15-415/615

Faloutsos - Pavlo CMU SCS 15-415/615 Examples find ssn of ‘overworked’ students, ie., that take 412, 413, 415: almost correct answer: Faloutsos - Pavlo CMU SCS 15-415/615

Faloutsos - Pavlo CMU SCS 15-415/615 Examples find ssn of ‘overworked’ students, ie., that take 412, 413, 415 - Correct answer: c-name=413 c-name=415 Faloutsos - Pavlo CMU SCS 15-415/615

Faloutsos - Pavlo CMU SCS 15-415/615 Examples find ssn of students that work at least as hard as ssn=123, ie., they take all the courses of ssn=123, and maybe more Faloutsos - Pavlo CMU SCS 15-415/615

Sample schema Faloutsos - Pavlo CMU SCS 15-415/615 Faloutsos - Pavlo

Faloutsos - Pavlo CMU SCS 15-415/615 Examples find ssn of students that work at least as hard as ssn=123 (ie., they take all the courses of ssn=123, and maybe more Faloutsos - Pavlo CMU SCS 15-415/615

Conclusions Relational model: only tables (‘relations’) Faloutsos - Pavlo CMU SCS 15-415/615 Conclusions Relational model: only tables (‘relations’) relational algebra: powerful, minimal: 5 operators can handle almost any query! Faloutsos - Pavlo CMU SCS 15-415/615