Relational Algebra (CB Chapter 5.1) CPSC 356 Database Ellen Walker Hiram College.

Slides:



Advertisements
Similar presentations
Relational Database Operators
Advertisements

พีชคณิตแบบสัมพันธ์ (Relational Algebra) บทที่ 3 อ. ดร. ชุรี เตชะวุฒิ CS (204)321 ระบบฐานข้อมูล 1 (Database System I)
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Relational Model (CB Chapter 4) CPSC 356 Database Ellen Walker Hiram College.
Relational Algebra Ch. 7.4 – 7.6 John Ortiz. Lecture 4Relational Algebra2 Relational Query Languages  Query languages: allow manipulation and retrieval.
CS4432: Database Systems II Query Operator & Algebraic Expressions 1.
Algebraic and Logical Query Languages Spring 2011 Instructor: Hassan Khosravi.
Concepts of Database Management Sixth Edition
Relational Algebra 1 Chapter 5.1 V3.0 Napier University Dr Gordon Russell.
Oct 28, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Nov 18, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
Chapter 11.1 and 11.2 Data Manipulation: Relational Algebra and SQL Brian Cobarrubia Introduction to Database Management Systems October 4, 2007.
Murali Mani Relational Algebra. Murali Mani What is Relational Algebra? Defines operations (data retrieval) for relational model SQL’s DML (Data Manipulation.
Concepts of Database Management, Fifth Edition
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Chapter 3 Section 3.4 Relational Database Operators
Copyright © Curt Hill The Relational Algebra What operations can be done?
Relational Model Concepts. The relational model represents the database as a collection of relations. Each relation resembles a table of values. A table.
Chapter 2 Adapted from Silberschatz, et al. CHECK SLIDE 16.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
Relational Algebra - Chapter (7th ed )
Relational Algebra Chapter 4 CIS 458 Sungchul Hong.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
Bayu Adhi Tama, ST., MTI. Introduction Relational algebra and relational calculus are formal languages associated with the relational.
Chapter 5 Relational Algebra and Relational Calculus Pearson Education © 2009.
Chapter 5 Relational Algebra Pearson Education © 2014.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Advanced Relational Algebra & SQL (Part1 )
Chapter 2: Intro to Relational Model. 2.2 Example of a Relation attributes (or columns) tuples (or rows)
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Introduction.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
CMPT 258 Database Systems Relational Algebra (Chapter 4)
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
April 20022CS3X1 Database Design Relational algebra John Wordsworth Department of Computer Science The University of Reading Room.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 6 th Edition Chapter 8: Relational Algebra.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 6: Formal Relational.
Ritu CHaturvedi Some figures are adapted from T. COnnolly
Database Systems Chapter 6
Chapter (6) The Relational Algebra and Relational Calculus Objectives
Relational Algebra Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
Module 2: Intro to Relational Model
Chapter # 6 The Relational Algebra and Calculus
Relational Algebra - Part 1
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
The Relational Algebra and Relational Calculus
376a. Database Design Dept. of Computer Science Vassar College
Relational Algebra Chapter 4 - part I.
More Relational Algebra
CS 3630 Database Design and Implementation
Instructor: Mohamed Eltabakh
Data Manipulation using Relational Algebra
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
The Relational Algebra
Chapter 2: Intro to Relational Model
Example of a Relation attributes (or columns) tuples (or rows)
Chapter 2: Intro to Relational Model
Chapter 4 Relational Algebra
Relational Database Operators
Presentation transcript:

Relational Algebra (CB Chapter 5.1) CPSC 356 Database Ellen Walker Hiram College

Relational Algebra Mathematical language for operating on relations Each operator takes one or more relations as input, and produces one relation as output Relational algebra operators can be implemented as functions in a programming language A relational algebra expression indicates which operators to use, in which order

Example Relation (PhoneBook) FirstLastDept PhoneTitle LouisOliphantCPSCLouis5275Asst EllenWalkerCPSCWalkerEL5250Prof LauraVanWormerPHYSVanwormerLA5249Prof CathyMansorWECMansorCN5957Dean BradGoodnerBIOLGoodnerBW5260Prof

Another Relation (Dept) NameBuilding CPSCColton WECHinsdale PHYSGerstacker BIOLGerstacker MGMTHinsdale

Selection (  condition ) The output relation has all rows of the input relation that satisfy the condition  (Title=“prof”) Phonebook FirstLastDept PhoneTitle LauraVanWormerPHYSVanwormerLA5249Prof EllenWalkerCPSCWalkerEL5250Prof BradGoodnerBIOLGoodnerBW5260Prof

Valid Conditions Basic condition –Dept = ‘CS’attribute compare to const –First < Lastattribute compare to attribute Combination of conditions using AND, OR, or NOT Note: All attributes must come from the relation in the selection

Pseudocode for Selection Select (input relation, condition, &output rel) Do for every tuple in the input relation If the tuple satisfies the condition Copy the tuple to the output relation Time = O(number of tuples in input relation)

Projection (  attributes ) Create a new relation with only the listed attributes in it.  last, phone PhoneBook LastPhone Oliphant5275 Walker5250 VanWormer5249 Mansor5957 Goodner5260

Relations Have No Duplicates  dept PhoneBook Only 4 rows, even though PhoneBook had 5! Dept CPSC PHYS WEC BIOL

Pseudocode for Projection Project (input relation, attribute-list, &output rel) Do for every tuple in the input relation Do for every attribute in the input relation If the attribute is in the attribute-list copy the value to the output relation Remove duplicates in the output relation Time = O(number of tuples in relation + time for duplicate-removal)

Combining Select & Project  first, last (  title=prof PhoneBook ) FirstLast LauraVanWormer EllenWalker BradGoodner

Remember: A Relation is a Set A set is an unordered collection of unique elements Set S = {1,2,3} “1 is an element of S” {a,b,c} = {a,c,b} A subset of a set is another set whose elements all come from the original set. {a,b} is a subset of {a,c,b} {1,2,3} is a subset of {1,2,3} {1,2,4} is not a subset of {1,2,3} {} (the empty set) is a subset of every set!

Basic Set Operations Union: the set of all elements in either or both original sets –{1,2} union {2,3} = {1,2,3} Intersection: the set of all elements in both original sets (only) –{1,2} intersect {2,3} = {2} Set Difference: the set of all elements in the first but not the second set –{1,2} – {2,3} = {1}

Applying to Relations Relations must be “comparable” –Same set of attributes in each relation! Union = all tuples Intersection = all matching tuples Set Difference = all tuples from first but not second

Basic Operation Examples R1 =  dept PhoneBook R2 =  name as “dept” Dept –Rename attribute to be same R1  R2 = R2 (in this case) R1  R2 = R1 (in this case) R1 – R2 = { } (empty set) R2 – R1 = Dept CPSC PHYS WEC BIOL Dept CPSC PHYS WEC BIOL MGMT R1R2 Dept MGMT

Another Set Operation Cartesian product: a set of ordered pairs, where each contains one element from each original set {1,2,3} x {a, b} = {(1,a), (1,b), (2,a), (2,b), (3,a), (3,b)} For Relations: create a new relation with every combination of tuples

Cartesian Product (X)  dept, last PhoneBook X Dept LastDeptNameBldg OliphantCPSC Colton WalkerCPSC Colton VanWorme r PHYS CPSCColton MansorWEC CPSCColton GoodnerBIOL CPSCColton OliphantCPSC WECHinsdale WalkerCPSC WECHinsdale VanWorme r PHYS WECHinsdale MansorWEC Hinsdale GoodnerBIOL WECHinsdale OliphantCPSC PHYSColton WalkerCPSC PHYSColton Etc….

Pseudocode for Cartesian Product Product (relation1, relation2, &output rel) Do for every tuple in relation1 Do for every tuple in relation2 Build a row with all attributes from both relations Add it to the output relation Time = O(number of tuples in relation1 * number of tuples in relation 2) This is the most expensive operation in relational algebra!

Join Combines X and Select Theta Join: –  any condition R1 X R2 Equijoin: –  equality condition R1 X R2 Natural Join: –  equality condition R1 X R2 –Project to remove one copy of each equal attribute Left or Right Outer Join: –Include all tuples from (left or right) side, even if they don’t have a match

Naïve Pseudocode for Join Join (rel1, rel2, condition, output rel) product (rel1, rel2, tmp) select (tmp, condition, output rel) Time: Same as Cartesian Product To keep time down, keep the size of the relations down -- we’ll look at this later!

Let’s Try Some Examples: What are the first and last names of all professors who don’t work in Hinsdale? What are the telephone extensions of people who work in Hinsdale? Which buildings contain people whose phone numbers are between 5000 and 5200? List the Dept. Name, Building Name, and phone numbers for All departments (even those without phone numbers).

First and last names of all professors who don’t work in Hinsdale Select “all professors” –  Title=“Prof” (Phonebook) Select “Departments not in Hinsdale” –  Bldg != “Hinsdale” (Dept) Connect these relations where depts match –(  Title=“Prof” (Phonebook)) |X| Dept=Name (  Bldg != “Hinsdale” (Dept)) One project to get the final result –  first,last ((  Title=“Prof” (Phonebook)) |X| Dept=Name (  Bldg != “Hinsdale” (Dept)))

Telephone extensions of people who work in Hinsdale Select “Departments in Hinsdale” and project to just Dept to make the table smaller –  Name (  Bldg = “Hinsdale” (Dept)) Join with PhoneBook to get only those in the right departments –  Name (  Bldg = “Hinsdale” (Dept)) |X| Dept=Name PhoneBook) Project to get just the extensions –  Phone (  Name (  Bldg = “Hinsdale” (Dept)) |X| Dept=Name PhoneBook))

Buildings with phone numbers between 5000 and 5300 Select to get phone numbers from 5000 to 5300 and Project to have only the dept attribute –  dept (  5000 =Phone (PhoneBooks)) Join with Dept to associate department names with buildings –(  dept (  5000 =Phone (PhoneBooks)) |X| dept=name (Dept)) Project to get just the building names –  bldg ((  dept (  5000 =Phone (PhoneBooks)) |X| dept=name (Dept)))

Dept. Name, Building, and phone numbers for all departments Join to include info from *ALL* departments. This requires an outer join –Dept X| name=dept PhoneBook Project to get the right attributes –  name, bldg,phone Dept X| name=dept PhoneBook

Aggregation Operations that allow you to combine all the values in a table (column) in some way: COUNT SUM AVG MIN MAX –Examples: How many CPSC majors are there? What is the average GPA of CPSC majors?

Grouping Aggregate all elements in one column based on values in another column Aggregation operators (COUNT, SUM, AVG, MIN, MAX) Format: – group-by-attribute, F OPER attribute (table) Note: F is a backward cursive F in the book.

Grouping Examples List average GPAs by major major F AVG GPA (Student) What is the average GPA of CPSC students? σ major=CPSC ( major F AVG GPA (Student)) List number of faculty in each department dept F COUNT id (Faculty)