Relational Algebra Prof. Yin-Fu Huang CSIE, NYUST Chapter 7.

Slides:



Advertisements
Similar presentations
Relational Algebra and Relational Calculus
Advertisements

Database Languages Chapter 7. The Relational Algebra.
Copyright © C. J. Date 2005page 97 S#Y S1DURINGS3DURING [d04:d10][d08:d10] S2DURINGS4DURING [d02:d04][d04:d10] [d08:d10] WITH ( EXTEND T2 ADD ( COLLAPSE.
COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 5A Relational Algebra.
 CS 405G: Introduction to Database Systems Lecture 7: Relational Algebra II Instructor: Chen Qian Spring 2014.
Oct 28, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
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.
Cs3431 Relational Algebra : #I Based on Chapter 2.4 & 5.1.
Domains, Relations & Base RelVars (Ch
Database Systems: A Practical Approach to Design, Implementation and Management International Computer Science S. Carolyn Begg, Thomas Connolly Lecture.
Relational Algebra. Relational algebra Consists of collection of operators –Restrict, project, join, … Takes relations as operands Returns relations as.
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.
Functional Dependencies Prof. Yin-Fu Huang CSIE, NYUST Chapter 11.
Concepts of Database Management, Fifth Edition
Relational Algebra Instructor: Mohamed Eltabakh 1.
Relational Algebra II. Semantics - where A WHERE X  Y –  : an operator like =, >, … Result of restriction A by the condition X  Y is A relation –with.
The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.
Further Normalization II: Higher Normal Forms Prof. Yin-Fu Huang CSIE, NYUST Chapter 13.
Relational Algebra - Chapter (7th ed )
Relational Algebra Chapter 4 CIS 458 Sungchul Hong.
Chapter 10 Views. Topics in this Chapter What are Views For? View Retrievals View Updates Snapshots SQL Facilities.
Chapter 7 Relational Algebra. Topics in this Chapter Closure Revisited The Original Algebra: Syntax and Semantics What is the Algebra For? Further Points.
An Introduction to Relational Databases Prof. Yin-Fu Huang CSIE, NYUST Chapter 3.
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.
Advanced Database Systems
Chapter 7 Relational Algebra. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.7-2 Topics in this Chapter Closure Revisited The Original Algebra:
Algebra1 After this lecture, you should be able to:  Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.
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.
Relational Algebra Operators
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
More Relation Operations 2014, Fall Pusan National University Ki-Joune Li.
1 Pertemuan > > Matakuliah: >/ > Tahun: > Versi: >
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
CSC271 Database Systems Lecture # 7. Summary: Previous Lecture  Relational keys  Integrity constraints  Views.
An Introduction to SQL For CS Overview of SQL  It is the standard language for relational systems, although imperfect  Supports data definition.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Relations Prof. Yin-Fu Huang CSIE, NYUST Chapter 6.
Types Prof. Yin-Fu Huang CSIE, NYUST Chapter 5. Advanced Database SystemYin-Fu Huang Relation, tuple, cardinality, attribute, degree, domain, primary.
Integrity Prof. Yin-Fu Huang CSIE, NYUST Chapter 9.
The Relational Model of Data Prof. Yin-Fu Huang CSIE, NYUST Chapter 2.
Views Prof. Yin-Fu Huang CSIE, NYUST Chapter 10. Advanced Database System Yin-Fu Huang 10.1Introduction Example: Var Good_Supplier View (S Where Status.
Advanced Database System
LECTURE THREE RELATIONAL ALGEBRA 11. Objectives  Meaning of the term relational completeness.  How to form queries in relational algebra. 22Relational.
Structured Query Language IV Asma Ahmad (C. J. Date) Database Systems.
STRUCTURE OF PRESENTATION :
Module 2: Intro to Relational Model
UNIT 11 Query Optimization
Chapter # 6 The Relational Algebra and Calculus
Relational Algebra - Part 1
Chapter 2: Intro to Relational Model
Chapter 8 Relational Calculus.
Relational Algebra Chapter 4 - part I.
Relational Algebra : #I
Instructor: Mohamed Eltabakh
Φροντιστήριο SQL (από το βιβλίο του Date)
Advanced Database System
Lecture 33: The Relational Model 2
Relational Algebra.
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
5.1 Relational Operations on Bags
STRUCTURE OF PRESENTATION :
Database Dr. Roueida Mohammed.
Presentation transcript:

Relational Algebra Prof. Yin-Fu Huang CSIE, NYUST Chapter 7

Advanced Database SystemYin-Fu Huang Eight operators of the relational algebra: 1. The traditional set operators union, intersection, difference, and Cartesian product. 2. The special relational operators restrict, project, join, and divide. (See Fig. 7.1) 7.1Introduction

Advanced Database SystemYin-Fu Huang

Advanced Database SystemYin-Fu Huang 7.2Closure Revisited The output from any given relational operation is another relation. Nested relational expressions

Advanced Database SystemYin-Fu Huang 7.3The Original Algebra: Syntax (See Page ) ::= Relation { } | | ( ) ::= | ::= { [All But] } ::= | | | | | | |

Advanced Database SystemYin-Fu Huang 7.3The Original Algebra: Syntax (Cont.) ::= Rename ( ) ::= Union ::= Intersect ::= Minus ::= Times ::= Where ::= Join ::= Divideby Per ::= | (, ) ::= With : ::= As

Advanced Database SystemYin-Fu Huang Tuple-homogeneous Union, Intersect, and Difference (See Fig. 7.2) Product (See Fig. 7.3) If we need to construct the Cartesian product of two relations that do have any such common attribute names, we must use the Rename operator first to rename attributes appropriately. Restrict (See Fig. 7.4) Project (See Fig. 7.5) 7.4 The Original Algebra: Semantics

Advanced Database SystemYin-Fu Huang Fig. 7.2Union, intersection, and difference

Advanced Database SystemYin-Fu Huang Fig. 7.3Cartesian product example

Advanced Database SystemYin-Fu Huang Fig. 7.4Restriction examples

Advanced Database SystemYin-Fu Huang Fig. 7.5Projection examples

Advanced Database SystemYin-Fu Huang Join natural join (See Fig. 7.6) θ-join (See Fig. 7.7) ((S Rename City As Scity) Times (P Rename City As Pcity)) Where Scity > Pcity Divide (See Fig. 7.8) 7.4 The Original Algebra: Semantics (Cont.)

Advanced Database SystemYin-Fu Huang Fig. 7.6&Fig. 7.7

Advanced Database SystemYin-Fu Huang Fig. 7.8Division examples

Advanced Database SystemYin-Fu Huang 7.5Examples Exam 1: ((Sp Join S) Where P#=P#(‘P2’)) {Sname} Exam 2: (((P Where Color=Color(‘Red’)) Join Sp ) {S#} Join S) {Sname} Exam 3: ((S {S#} Divideby P {P#} Per Sp {S#, P#}) Join S) {Sname} Exam 4: S {S#} Divideby (Sp Where S#=S#(‘S2’)) {P#} Per Sp {S#, P#} Exam 5: (((S Rename S# As Sa) {Sa, City} Join (S Rename S# As Sb) {Sb, City}) Where Sa < Sb) {Sa, Sb} Exam 6: ((S {S#} Minus (Sp Where P#=P#(‘P2’)) {S#}) Join S) {Sname}

Advanced Database SystemYin-Fu Huang 7.6What Is the Algebra For? The operators join, intersect, and divide can be defined in terms of the other five. Of the remaining five, however, none can be defined in terms of the other four, so we can regard those five as constituting a primitive or minimum set. Some possible applications: 1. Defining a scope for retrieval 2. Defining a scope for update 3. Defining integrity constraints 4. Defining derived relvars 5. Defining stability requirements 6. Defining security constraints

Advanced Database SystemYin-Fu Huang 7.6What Is the Algebra For? (Cont.) A high-level, symbolic representation of the user‘s intent  Transformation rules ((Sp Join S) Where P#=P#(‘P2’)) {Sname} ((Sp Where P#=P#(‘P2’)) Join S) {Sname} The algebra thus serves as a convenient basis for optimization. A language is said to be relationally complete if it is at least as powerful as the algebra.

Advanced Database SystemYin-Fu Huang 7.7Further Points Associativity and Commutativity Associative: Union, Intersect, Times, Join e.g. (A Union B) Union C = A Union (B Union C) = A Union B Union C Commutative: Union, Intersect, Times, Join e.g. A Union B = B Union A Some Equivalences e.g. r { } = Table_Dum if r=empty, Table_Dee otherwise (a nullary projection) r Join Table_Dee = Table_Dee Join r = r r Times Table_Dee = Table_Dee Times r = r

Advanced Database SystemYin-Fu Huang 7.7Further Points (Cont.) Some Generalizations If s contains no relations at all, then: The join of all relations in s is defined to be Table_Dee. The union of all relations in s is defined to be the empty relation. The intersection of all relations in s is defined to be the “universal” relation.

Advanced Database SystemYin-Fu Huang (See Page 196) ::= Semijoin ::= Semiminus ::= Extend Add ( ) ::= As ::= Summarize Per Add ( ) ::= [ ( ) ] As ::= Count | Sum | Avg | Max | Min | All | Any | Countd | Sumd | Avgd| … ::= Tclose Additional Operators

Advanced Database SystemYin-Fu Huang Additional Operators (Cont.) Semijoin  (a Join b) {X, Y} e.g. S Semijoin (Sp Where P#=P#(‘P2’)) Semidifference  a Minus (a Semijoin b) e.g. S Semiminus (Sp Where P#=P#(‘P2’)) Extend e.g. Extend P Add (Weight * 454) As Gmwt (See Fig. 7.9)

Advanced Database SystemYin-Fu Huang Additional Operators (Cont.)  Exam 1: Extend S Add ‘Supplier’ As Tag  Exam 2: Extend (P Join Sp) Add (Weight * Qty) As Shipwt  Exam 3: (Extend S Add City As Scity) {All But City} Rename  Exam 4: Extend P Add (Weight * 454 As Gmwt, Weight * 16 As Ozwt)  Exam 5: Extend S Add Count((Sp Rename S# As X) Where X=S#) As Np(See Fig. 7.10)

Advanced Database SystemYin-Fu Huang Additional Operators (Cont.)  Exam 1: Summarize Sp Per P {P#} Add (Sum(Qty) As Totqty, Avg(Qty) As Avgqty)  Exam 2: Summarize Sp Per S {S#} Add Count As Np Summarize is not a primitive operator. Extend  Exam 3: Summarize S Per S {City}Add Avg(Status) As Avg_Status  Exam 4: Summarize Sp Per Sp { }Add Sum(Qty) As Grandtotal Summarize e.g. Summarize Sp Per P {P#} Add Sum(Qty) As Totqty (See Fig. 7.11) Tclose  the transitive closure of a

Advanced Database SystemYin-Fu Huang Group e.g. SP Group (P#, Qty) As PQ(See Fig. 7.12) Ungroup e.g. SPQ Ungroup PQ The reversibility of the Group and Ungroup operations(See Fig. 7.13)  Functionally dependency Grouping and Ungrouping

Advanced Database SystemYin-Fu Huang The End.