Oct 28, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.

Slides:



Advertisements
Similar presentations
พีชคณิตแบบสัมพันธ์ (Relational Algebra) บทที่ 3 อ. ดร. ชุรี เตชะวุฒิ CS (204)321 ระบบฐานข้อมูล 1 (Database System I)
Advertisements

CS4432: Database Systems II Query Operator & Algebraic Expressions 1.
Algebraic and Logical Query Languages Spring 2011 Instructor: Hassan Khosravi.
1 Lecture 12: Further relational algebra, further SQL
Relational Operations on Bags Extended Operators of Relational Algebra.
Relational Algebra on Bags A bag is like a set, but an element may appear more than once. –Multiset is another name for “bag.” Example: {1,2,1,3} is a.
Nov 24, 2003Murali Mani SQL B term 2004: lecture 12.
Murali Mani SQL. Murali Mani SELECT-FROM-WHERE SELECT * FROM Student WHERE sName=“Greg” AND address=“320 FL”  (sName=“Greg” AND address=“320 FL”) (Student)
Relational Operations on Bags Extended Operators of Relational Algebra.
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.
Instructor: Mohamed Eltabakh
Nov 18, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
Murali Mani Relational Algebra. Murali Mani What is Relational Algebra? Defines operations (data retrieval) for relational model SQL’s DML (Data Manipulation.
The Relational Algebra and Calculus. Relational Algebra Overview Relational algebra is the basic set of operations for the relational model These operations.
Relational Algebra Basic Operations Algebra of Bags.
Relational Algebra Instructor: Mohamed Eltabakh 1.
Relational Algebra Example Database Application (COMPANY) Relational Algebra –Unary Relational Operations –Relational Algebra Operations From Set Theory.
Chapter 6: Formal Relational Query Languages. 6.2 Chapter 6: Formal Relational Query Languages Relational Algebra Tuple Relational Calculus Domain Relational.
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 )
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
Relational Algebra (Chapter 7)
From Relational Algebra to SQL CS 157B Enrique Tang.
Chapter 6 The Relational Algebra Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
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.
Relational Algebra MBAD 613 R. Nakatsu. Relational Data Manipulation Language Query-by-Example; Query-by-Form Transform-Oriented Languages Relational.
Advanced Relational Algebra & SQL (Part1 )
Databases : Relational Algebra - Complex Expression 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof.
More Relation Operations 2015, Fall Pusan National University Ki-Joune Li.
More Relation Operations 2014, Fall Pusan National University Ki-Joune Li.
Relational Algebra Instructor: Mohamed Eltabakh 1 Part II.
1 Algebra of Queries Classical Relational Algebra It is a collection of operations on relations. Each operation takes one or two relations as its operand(s)
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
Relational Algebra Instructor: Mohamed Eltabakh 1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
More SQL (and Relational Algebra). More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
Relational Algebra Instructor: Mohamed Eltabakh 1.
Relational Algebra National University of Computer and Emerging Sciences Lecture # 6 June 30,2012.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 6: Formal Relational.
Database Systems Chapter 6
Chapter (6) The Relational Algebra and Relational Calculus Objectives
Module 2: Intro to Relational Model
Chapter # 6 The Relational Algebra and Calculus
Relational Algebra - Part 1
Chapter 2: Intro to Relational Model
Operators Expression Trees Bag Model of Data
SQL : Query Language CS3431.
The Relational Algebra and Relational Calculus
376a. Database Design Dept. of Computer Science Vassar College
Relational Algebra Chapter 4 - part I.
Relational Algebra : #I
More Relational Algebra
CS 3630 Database Design and Implementation
Instructor: Mohamed Eltabakh
More Relation Operations
More Complex Operators
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
5.1 Relational Operations on Bags
SQL: Structured Query Language
Relational Algebra Chapter 4 - part I.
Presentation transcript:

Oct 28, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11

Oct 28, 2003Murali Mani Basics Relational Algebra is defined on bags, rather than relations. Bag or multiset allows duplicate values; but order is not significant. We can write an expression using relational algebra operators with parentheses: we need closure – an operator on bag returns a bag. Relational algebra includes set operators, and other operators specific to relational model.

Oct 28, 2003Murali Mani Set Operators Union, Intersection, Difference, cross product Union, Intersection and Difference are defined only for union compatible relations. Two relations are union compatible if they have the same set of attributes and the types (domains) of the attributes are the same. Eg of two relations that are not union compatible: Student (sNumber, sName) Course (cNumber, cName)

Oct 28, 2003Murali Mani Union: ∪ Consider two bags R 1 and R 2 that are union- compatible. Suppose a tuple t appears in R 1 m times, and in R 2 n times. Then in the union, t appears m + n times. AB R1R1 AB R2R2 AB R 1 ∪ R 2

Oct 28, 2003Murali Mani Intersection: ∩ Consider two bags R 1 and R 2 that are union- compatible. Suppose a tuple t appears in R 1 m times, and in R 2 n times. Then in the intersection, t appears min (m, n) times. AB R1R1 AB R2R2 AB R 1 ∩ R 2

Oct 28, 2003Murali Mani Difference: - Consider two bags R 1 and R 2 that are union- compatible. Suppose a tuple t appears in R 1 m times, and in R 2 n times. Then in R 1 – R 2, t appears max (0, m - n) times. AB R1R1 AB R2R2 AB 12 R 1 – R 2

Oct 28, 2003Murali Mani Bag semantics vs Set semantics Union is idempotent for sets – (R1 ∪ R2) ∪ R2 = R1 ∪ R2 Union is not idempotent for bags. Intersection and difference are idempotent for sets and bags. For sets and bags, R 1  R 2 = R 1 – (R 1 – R 2 ).

Oct 28, 2003Murali Mani Cross Product (Cartesian Product): Ⅹ Consider two bags R 1 and R 2. Suppose a tuple t 1 appears in R 1 m times, and a tuple t 2 appears in R 2 n times. Then in R 1 X R 2, t 1 t 2 appears mn times. AB R1R1 BC R2R2 AR 1.BR 2.BC R 1 X R 2

Oct 28, 2003Murali Mani Basic Relational Operations Select, Project, Join Select: denoted σ C (R): selects the subset of tuples of R that satisfies selection condition C. C can be any boolean expression, its clauses can be combined with AND, OR, NOT. ABC R σ (C ≥ 6) (R) ABC

Oct 28, 2003Murali Mani Select Select is commutative: σ C2 (σ C1 (R)) = σ C1 (σ C2 (R)) Select is idempotent: σ C (σ C (R)) = σ C (R) We can combine multiple select conditions into one condition. σ C1 (σ C2 (… σ Cn (R)…)) = σ C1 AND C2 AND … Cn (R)

Oct 28, 2003Murali Mani Project: π A1, A2, …, An (R) Consider relation (bag) R with set of attributes A R. π A1, A2, …, An (R), where A1, A2, …, An  A R returns the tuples in R, but only with columns A1, A2, …, An. ABC R π A, B (R) AB

Oct 28, 2003Murali Mani Project: Bag Semantics vs Set Semantics For bags, the cardinality of R = cardinality of π A1, A2, …, An (R). For sets, cardinality of R ≥ cardinality of π A1,A2, …, An (R). For sets and bags project is not commutative project is idempotent

Oct 28, 2003Murali Mani Natural Join: R ⋈ S Consider relations (bags) R with attributes A R, and S with attributes A S. Let A = A R ∩ A S. R ⋈ S can be defined as π A R – A, A, A S - A (σ R.A1 = S.A1 AND R.A2 =S.A2 AND … R.An=S.An (R X S)) where A = {A1, A2, …, An} The above expression says: select those tuples in R X S that agree in values for each of the A attributes, and project the resulting tuples such that we have only one value for each A attribute.

Oct 28, 2003Murali Mani Natural Join example AB R1R1 BC R2R2 ABC R 1 ⋈ R 2

Oct 28, 2003Murali Mani Theta Join: R ⋈ C S Theta Join is similar to natural join, except that we can specify any condition C. It is defined as R ⋈ C S = (σ C (R X S)) AB R1R1 BC R2R2 R 1 ⋈ R1.B<R2.B R 2 AR 1.BR 2.BC

Oct 28, 2003Murali Mani Outer Join: R ⋈ o S Similar to natural join, however, if there is a tuple in R, that has no “matching” tuple in S, or a tuple in S that has no matching tuple in R, then that tuple also appears, with null values for attributes in S (or R). ABC R1R1 BCD R2R2 R 1 ⋈ o R 2 ABCD null

Oct 28, 2003Murali Mani Left Outer Join: R ⋈ o L S Similar to natural join, however, if there is a tuple in R, that has no “matching” tuple in S, then that tuple also appears, with null values for attributes in S (note: a tuple in S that has no matching tuple in R does not appear). ABC R1R1 BCD R2R2 R 1 ⋈ o L R 2 ABCD null 789

Oct 28, 2003Murali Mani Right Outer Join: R ⋈ o R S Similar to natural join, however, if there is a tuple in S, that has no “matching” tuple in R, then that tuple also appears, with null values for attributes in R (note: a tuple in R that has no matching tuple in S does not appear). ABC R1R1 BCD R2R2 R 1 ⋈ o R R 2 ABCD null6712

Oct 28, 2003Murali Mani Renaming: ρ S(A1, A2, …, An) (R) Rename relation R to S, attributes of R are renamed to A1, A2, …, An BCD R2R2 XCD ρ S(X, C, D) (R 2 ) S

Oct 28, 2003Murali Mani Duplicate Elimination: δ (R) Convert a bag to a set. R AB δ (R) AB 12 34

Oct 28, 2003Murali Mani Aggregation operators MIN, MAX, COUNT, SUM, AVG The aggregate operators aggregate the values in one column of a relation. R AB MIN (B) = 2 MAX (B) = 4 COUNT (B) = 4 SUM (B) = 10 AVG (B) = 2.5

Oct 28, 2003Murali Mani Grouping Operators

Oct 28, 2003Murali Mani Sorting Operator

Oct 28, 2003Murali Mani Extended Projection