Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.

Slides:



Advertisements
Similar presentations
Chapter 6 The Relational Algebra
Advertisements

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
พีชคณิตแบบสัมพันธ์ (Relational Algebra) บทที่ 3 อ. ดร. ชุรี เตชะวุฒิ CS (204)321 ระบบฐานข้อมูล 1 (Database System I)
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus.
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
Copyright © 2004 Pearson Education, Inc.. Chapter 6 The Relational Algebra and Relational Calculus.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Cs3431 Relational Algebra : #I Based on Chapter 2.4 & 5.1.
Database Systems: A Practical Approach to Design, Implementation and Management International Computer Science S. Carolyn Begg, Thomas Connolly Lecture.
The Relational Algebra and Calculus. Relational Algebra Overview Relational algebra is the basic set of operations for the relational model These operations.
1 The Relational Data Model, Relational Constraints, and The Relational Algebra.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra and Calculus.
Relational Algebra Example Database Application (COMPANY) Relational Algebra –Unary Relational Operations –Relational Algebra Operations From Set Theory.
Relational Model Concepts. The relational model represents the database as a collection of relations. Each relation resembles a table of values. A table.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
Relational Algebra - Chapter (7th ed )
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra and Calculus.
CS 380 Introduction to Database Systems Chapter 7: The Relational Algebra and Relational Calculus.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
Lecture 15: Relational Algebra
METU Department of Computer Eng Ceng 302 Introduction to DBMS The Relational Algebra by Pinar Senkul resources: mostly froom Elmasri, Navathe and other.
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.
The Relational Algebra and Calculus
Relational Algebra. 2 Outline  Relational Algebra Unary Relational Operations Relational Algebra Operations from Set Theory Binary Relational Operations.
Chapter 6 The Relational Algebra Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Chapter 5 Relational Algebra and Relational Calculus Pearson Education © 2009.
Chapter 5 Relational Algebra Pearson Education © 2014.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
DatabaseIM ISU1 Fundamentals of Database Systems Chapter 6 The Relational Algebra.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 Part a The Relational Algebra and Relational Calculus Hours 1,2.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Al-Maarefa College for Science and Technology INFO 232: Database systems Chapter 3 “part 2” The Relational Algebra and Calculus Instructor Ms. Arwa Binsaleh.
Advanced Relational Algebra & SQL (Part1 )
The Relational Algebra. Slide 6- 2 Outline Relational Algebra Unary Relational Operations Relational Algebra Operations From Set Theory Binary Relational.
Slide 6- 1 Additional Relational Operations Aggregate Functions and Grouping A type of request that cannot be expressed in the basic relational algebra.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
CSC271 Database Systems Lecture # 7. Summary: Previous Lecture  Relational keys  Integrity constraints  Views.
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.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Chapter 6 The Relational Algebra Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
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
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus تنبيه : شرائح العرض.
Relational Algebra National University of Computer and Emerging Sciences Lecture # 6 June 30,2012.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 6 th Edition Chapter 8: Relational Algebra.
Database Systems Chapter 6
The Relational Algebra and Relational Calculus
The Relational Algebra and Calculus
COMP3017 Advanced Databases
Module 2: Intro to Relational Model
Chapter # 6 The Relational Algebra and Calculus
Fundamental of Database Systems
Relational Algebra - Part 1
Chapter 2: Intro to Relational Model
Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition
Chapter 4 The Relational Algebra and Calculus
The Relational Algebra and Calculus
The Relational Algebra and Relational Calculus
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
Presentation transcript:

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Relational Algebra What is relational algebra A formal language with operations defined on relations A sequence of operations forms an expression A relational algebra expression generates a new relation f(R1, R2, …, Rn)  R’ What’s the difference between SQL and relational algebra SQL specifies what you want, but not how to get it Relational algebra defines the order of operations Why is it important Used for query processing and optimization

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Relational Algebra Relational algebra and SQL Relational algebra was designed before SQL Most DBMS still use it in internal module Classification of operations Set-based operations: Union, Intersection, Difference, Cartesian product Relational model based: Project, Select (restrict), Join Additional : Aggregate functions, Outer join, etc. Another classification Input is 1 relation: Project, Select Input is 2 or more relation: Union, Intersection, Difference, Cartesian product, Join

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6.1. Unary Operation: SELECT Select Symbol: σ Syntax: σ (conditions) (input relation) Given ONE input relation, return ROWS that satisfy the conditions Conditions: a number of logical expressions (and / or/ not) Comparison:, <>, >=, <=, etc Not the SELECT in SQL !! (also called restrict)

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Q1: what is the result Q2: what is the equivalent SQL? SELECT * FROM EMPLOYEE WHERE (Dno=4 AND Salary>25000) OR (Dno=5 AND Salary>30000)

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6.1. Unary Operation: SELECT Some further questions: Multiple levels of “select” σ (Sex=‘M’) (σ (dno=4 and salary>30000) (EMPLOYEE)) Q: Will the orders of select affect the result? Commutative property

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6.1. Unary Operation: Project Project Symbol: π Syntax: π (attribute names) (input relation) Given ONE input relation, returns COLUMNS listed Columns : comma separated list This is the SELECT in SQL !!

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Q3: what is the result Q4: what is the equivalent SQL? SELECT Lname, Fname, Salary FROM EMPLOYEE ?

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

6.1.2 Unary Operation: Project Some further questions: Orders of the attributes in the new relation Duplicate elimination Elements in a set should be unique Any duplicate rows in the result of a Project will be removed SQL equivalent to project: SELECT DISTINCT Sex, Salary FROM EMPLOYEE SQL allows duplicated rows while relational algebra doesn’t! The list of attributes (column names) should appear in input relation

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Orders of operations/rename Two ways to write expressions with multiple operators Save the output of an expression as a new relation Write expressions nested operations TEMP  σ (dno=4 and salary>30000) (EMPLOYEE) R(Last_name, Salary)  π (L_name, salary) TEMP Rename operator (ρ) ρ S(B1, B2, …, Bn) (R) : S is the new name of R, B1~Bn are new column names of R.

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Q5: What is the corresponding SQL?

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus Summary

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6.2 Set Theory based operations Union (U) Intersection (∩) Set difference (-) Input: TWO relations Output: ONE relation

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Given two relations, Union: AUB return a new relation with all the rows in either relation Intersection: A∩B return a new relations with all the rows in both relations Minus: A-B returns a new relation with all the rows in A but not in B 6.2 Set Theory based operations A UNION B A INTERSECT B A MINUS B

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Further question: Can we UNION any two relations? Union Compatibility Same number of attributes Same data type for each pair of attributes Duplicate elimination SQL: UNION, INTERSECT, EXCEPT 6.2 Set Theory based operations

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Generate all combinations of rows from two relations Symbol: X Relation A and B A has m attributes, B has n attributes A X B has m+n attributes (columns) |A|*|B| rows FEMALE_EMPS X DEPENDENT SQL: SELECT * FROM A, B 6.3 Cartesian Product (Cross product)

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Continued next page…

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Cardinality of the result relation What is the min/max number of rows and columns of the result σ (Condition1 and Condition 2) (R) π (column1,column2, … column N) (R) R U S R ∩ S R – S R X S Some questions to discuss

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Aggregate functions: Compute statistics (e.g,. SUM, COUNT, MIN, MAX) on collections of values Not defined in original relational algebra. Group by Aggregate some attributes (e.g., salary) of rows in the same GROUP (e.g., department) F (R) How many people are there in EACH Department? F (EMPLOYEE) Aggregate functions and group by

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Write relational algebra expressions for the following questions Text book 6.16 Exercises