Relational Algebra CMSC 461 Michael Wilson. Relational algebra  Before we get into SQL, want to take a look at what exactly SQL is really modeling 

Slides:



Advertisements
Similar presentations
COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 5A Relational Algebra.
Advertisements

Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Relational Algebra Dashiell Fryer. What is Relational Algebra? Relational algebra is a procedural query language. Relational algebra is a procedural query.
Relational Algebra Ch. 7.4 – 7.6 John Ortiz. Lecture 4Relational Algebra2 Relational Query Languages  Query languages: allow manipulation and retrieval.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A Modified by Donghui Zhang.
1 Relational Algebra & Calculus. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
Relational Model CMSC 461 Michael Wilson. What is a relation?  A relation is a set of tuples  (d 1, d 2, d 3, …, d n )  Where each element d i is a.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
Relational Algebra - Basic Operations CS263 Lecture 11.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
--The SQL Query Language DML--1 The SQL Query Language DML The SQL Query Language DML (SELECT)
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
Introduction to Database Systems 1 Relational Algebra Relational Model: Topic 3.
Database Systems: A Practical Approach to Design, Implementation and Management International Computer Science S. Carolyn Begg, Thomas Connolly Lecture.
1 Relational Algebra and Calculus Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Rutgers University Relational Algebra 198:541 Rutgers University.
Relational Algebra Chapter 4 - part I. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
CSCD343- Introduction to databases- A. Vaisman1 Relational Algebra.
Relational Algebra.
RELATIONAL ALGEBRA Objectives
Relational Model & Relational Algebra. 2 Relational Model u Terminology of relational model. u How tables are used to represent data. u Connection between.
Chapter 3 Section 3.4 Relational Database Operators
1 Relational Algebra and Calculus Chapter 4. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.
Copyright © Curt Hill The Relational Algebra What operations can be done?
Bayu Adhi Tama, ST., MTI. Introduction Relational algebra and relational calculus are formal languages associated with the relational.
Advanced Database Systems
SQL continued CMSC 461 Michael Wilson. Right back into it  Recap:  Learned how to log in to PostgreSQL  Learned about PostgreSQL data types  Learned.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra.
SQL CMSC 461 Michael Wilson. Finally, some code  This is where the theory and practice actually come together  Basically taking the relational algebra.
Relational Algebra  Souhad M. Daraghma. Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational.
Chapter 7 Relational Algebra. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.7-2 Topics in this Chapter Closure Revisited The Original Algebra:
1 Relational Algebra & Calculus Chapter 4, Part A (Relational Algebra)
1 Relational Algebra and Calculas Chapter 4, Part A.
1.1 CAS CS 460/660 Introduction to Database Systems Relational Algebra.
Relational Algebra.
1 Relational Algebra Chapter 4, Sections 4.1 – 4.2.
Chapter 5 Relational Algebra and Relational Calculus Pearson Education © 2009.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
Database Management Systems 1 Raghu Ramakrishnan Relational Algebra Chpt 4 Xin Zhang.
Chapter 2: Relational Model II Relational Algebra basics Relational Algebra basics.
Chapter 5 Relational Algebra Pearson Education © 2014.
SCUHolliday - coen 1785–1 Schedule Today: u Relational Algebra. u Read Chapter 5 to page 199. Next u SQL Queries. u Read Sections And then u Subqueries,
1 Geog 357 – Introduction to GIS The Relational Language.
Relational Algebra MBAD 613 R. Nakatsu. Relational Data Manipulation Language Query-by-Example; Query-by-Form Transform-Oriented Languages Relational.
Chapter 2: Intro to Relational Model. 2.2 Example of a Relation attributes (or columns) tuples (or rows)
CSCD34-Data Management Systems - A. Vaisman1 Relational Algebra.
CS 338Relational Algebra4-1 Relational Algebra Lecture Topics Basic concepts and operations of relational algebra Reference, selection, projection Cross-product.
Database Management Systems, R. Ramakrishnan1 Relational Algebra Module 3, Lecture 1.
1 Pertemuan > > Matakuliah: >/ > Tahun: > Versi: >
CMPT 258 Database Systems Relational Algebra (Chapter 4)
CSC271 Database Systems Lecture # 7. Summary: Previous Lecture  Relational keys  Integrity constraints  Views.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Functional dependencies CMSC 461 Michael Wilson. Designing tables  Now we have all the tools to build our databases  How should we actually go about.
Section 1.2 – 1.3 Outline Intersection  Disjoint Sets (A  B=  ) AND Union  OR Universe The set of items that are possible for membership Venn Diagrams.
Relational Algebra Continued CMSC 461 Michael Wilson.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
1 CS122A: Introduction to Data Management Lecture #7 Relational Algebra I Instructor: Chen Li.
LECTURE THREE RELATIONAL ALGEBRA 11. Objectives  Meaning of the term relational completeness.  How to form queries in relational algebra. 22Relational.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
Query Languages Language in which user requests information from the database. Categories of languages Procedural Non-procedural, or declarative “Pure”
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 6 th Edition Chapter 8: Relational Algebra.
COMP3017 Advanced Databases
Schedule Today: Next And then Relational Algebra.
Chapter 2: Intro to Relational Model
The Relational Algebra
Chapter 6: Formal Relational Query Languages
Chapter 2: Intro to Relational Model
Presentation transcript:

Relational Algebra CMSC 461 Michael Wilson

Relational algebra  Before we get into SQL, want to take a look at what exactly SQL is really modeling  Foundation for SQL and other query languages

Base operations  Union  Difference  Cartesian Product  Projection  Selection  Rename

Base operations  With these operations you can accomplish everything you need in relational algebra  There are more operations, but they are for simplification

Union  Say you have two relations with tuples {a, b, c} and {d, e, f}  The union of the two is {a, b, c, d, e, f}  One requirement: the two relations must have the same attributes  This is called being “ union-compatible ”  Notation: A ∪ B

Difference  Also known as the relative complement  Say you have two relations  Relation A: {a, b, c}  Relation B: {a, b, f}  Notation: B \ A  The relative complement of A in B  B \ A results in the set of elements in B that are not in A  B \ A = {f}  The two relations must be union-compatible

Cartesian product  This one can get kind of ugly  Requirement: the two relations involved must have completely disjoint attributes  A Cartesian product of two relations A and B is the set of every tuple in A paired with every tuple in B  Notation: A × B

Cartesian product  Two relations A and B with disjoint attributes  A has tuples {a, b, c}  B has tuples {d, e, f}  Each tuple has attribute values  a has a 1, a 2, …, a n  b has b 1, b 2, …, b n  Etc.

Cartesian product  Resulting product by tuples:  (a, d)  (a, e)  (a, f)  (b, d)  (b, e)  (b, f)  (c, d)  (c, e)  (c, f)

Cartesian product  Listing out the attribute values:  (a 1, a 2, …, a n, d 1, d 2, …, d n )  (a 1, a 2, …, a n, e 1, e 2, …, e n )  (a 1, a 2, …, a n, f 1, f 2, …, f n )  (b 1, b 2, …, b n, d 1, d 2, …, d n )  (b 1, b 2, …, b n, e 1, e 2, …, e n )  (b 1, b 2, …, b n, f 1, f 2, …, f n )  (c 1, c 2, …, c n, d 1, d 2, …, d n )  (c 1, c 2, …, c n, e 1, e 2, …, e n )  (c 1, c 2, …, c n, f 1, f 2, …, f n )

Projection  This is an operation that is used on one relation  Essentially restricts the attributes of a relation to the ones you’re interested about  Notation: π a1,a2,…an (R)  a i = attribute I  R = relation to project

Projection  Example (from Wikipedia):  π contactName,contactPhoneNumber (addressBook)

Projection addresslastContacted contactPhoneNumber numberTypecontactName 111 Great Street 1 day ago CellPhil 123 Not So Great Street Last month LandlineHenry 8 Get Out of Here Way Last week WorkBob 7 RUN! Drive 2 years ago CellOctavio

Projection contactNamecontactPhoneNumber Phil Henry Bob Octavio

Selection  Lists a set of tuples that match the specified criteria  Notation: σ p (R)  p is a selection predicate  This consists of terms that are connected by logic operators  ^ (and)  v (or)  ¬ (not)

Selection  Terms have the following format:   is =, ≠, >, ≥, <, ≤  Examples:  phoneNumber=  GPA>2.0

Selection UMBC IDAgeFNameLNameGPA SM Mario 4.0 SM LuigiMario3.9 MT SamusAran4.0 FF SnowVilliers0.2 SW SnowWhite3.5

Selection  σ GPA<4.0 (R)  Select all from our relation where the attribute value for GPA is less than 4.0

Selection UMBC IDAgeFNameLNameGPA SM LuigiMario3.9 FF SnowVilliers0.2 SW SnowWhite3.5

Selection  σ age>20^fName=‘Snow’ (R)  Select all from our relation where the attribute value for age is greater than 20 and the attribute value for fName is equal to ‘Snow’

Selection UMBC IDAgeFNameLNameGPA FF SnowVilliers0.2 SW SnowWhite3.5

Rename  The result of a rename is literally the same as the original relation, but with an attribute renamed  ρ a/b (R)  The b attribute in the resulting tuples is renamed to a

Rename UMBC IDAgeFNameLNameGPA SM Mario 4.0 SM LuigiMario3.9 MT SamusAran4.0 FF SnowVilliers0.2 SW SnowWhite3.5

Rename  ρ MIT ID/UMBC ID (R)  Rename UMBC ID to MIT ID

Rename MIT IDAgeFNameLNameGPA SM Mario 4.0 SM LuigiMario3.9 MT SamusAran4.0 FF SnowVilliers0.2 SW SnowWhite3.5